ejtraderLabs / ejtraderCT

The best Python Ctrader FIX API Perfect for HFT
MIT License
61 stars 21 forks source link

How to Subscribe to a Symbol and Receive Quotes in the Absence of Open Positions? #31

Open FxMathSolution opened 10 months ago

FxMathSolution commented 10 months ago

In the context of algorithmic trading, I've noticed that I receive a 'Symbol not Subscribed' alert when attempting to get quotes for a specific symbol (e.g., EURUSD) when there are no open positions for that pair. Is there a way to subscribe to a symbol and receive quotes even in the absence of open positions? What approach should be taken to ensure the availability of quotes for any given symbol?

github-actions[bot] commented 10 months ago

We're glad you've opened your first issue. Please provide all the necessary details and any relevant code or screenshots to help us understand the problem better. Our team will review your issue and provide assistance as soon as possible. Thank you for contributing!

FxMathSolution commented 10 months ago

I just testing this example:

api = Ctrader(server,account,password)
time.sleep(3)

checkConnection = api.isconnected()
print("Is Connected?: ", checkConnection)
time.sleep(1)

api.subscribe("EURUSD", "GBPUSD")

quote = api.quote()
print(quote)

# Buy position
price = api.quote()
price = price['EURUSD']['bid'] 

symbol = "EURUSD"
volume = 0.01 # position size:
stoploss =  round(price - 0.00010,6)
takeprofit = round(price + 0.00010,6)

id = api.buy(symbol, volume, stoploss, takeprofit)
print(f"Position: {id}")

# sell position 
price = api.quote()
price = price['EURUSD']['bid'] 

symbol = "EURUSD"
volume = 0.01 # position size
stoploss =  round(price + 0.00010,6)
takeprofit = round(price - 0.00010,6)

id = api.sell(symbol, volume, stoploss, takeprofit)
print(f"Position: {id}")
FxMathSolution commented 10 months ago

I fixed it. Add a sleep here:

api.subscribe("EURUSD", "GBPUSD")
time.sleep(1)