[python][fix-api] how to stay in a session?

Created at 29 Jan 2023, 00:03
How’s your experience with the cTrader Platform?
Your feedback is crucial to cTrader's development. Please take a few seconds to share your opinion and help us improve your trading experience. Thanks!
WW

wwweeeeee

Joined 04.07.2022

[python][fix-api] how to stay in a session?
29 Jan 2023, 00:03


I use simplefix and socket python modules. After I send logon and market data request I see market data for around 5 minutes and then I gets an error: 'none'

how to stay in a session?
Here is what I sending and my responce:

sending:
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((HOST, PORT))

s.sendall(logon())
print(parse(s.recv(4096), 50))

s.sendall(market_data())
while True:
    print(s.recv(4096))

 

responce:

8=FIX.4.4|9=106|35=A|34=1|49=CSERVER|50=QUOTE|52=20230128-21:45:28.927|56=demo.roboforex.6183532|57=any|98=0|108=30|141=Y|10=228
b'8=FIX.4.4\x019=135\x0135=W\x0134=2\x0149=CSERVER\x0150=QUOTE\x0152=20230128-21:45:28.988\x0156=demo.roboforex.6183532\x0157=any\x0155=1\x01268=2\x01269=0\x01270=1.08677\x01269=1\x01270=1.08678\x0110=075\x01'
b'8=FIX.4.4\x019=88\x0135=0\x0134=3\x0149=CSERVER\x0150=QUOTE\x0152=20230128-21:45:59.917\x0156=demo.roboforex.6183532\x0157=any\x0110=107\x01'
b'8=FIX.4.4\x019=97\x0135=1\x0134=4\x0149=CSERVER\x0150=QUOTE\x0152=20230128-21:46:14.917\x0156=demo.roboforex.6183532\x0157=any\x01112=TEST\x0110=119\x01'
none
none
none
none
none
none
none
none
none
none
none
none
none
none
none
none
none
none
none
none
none
none

 


@wwweeeeee
Replies

ctid1980098
30 Jan 2023, 11:02

RE:

hi wwweeeeee,

I see in your login message you have set the heartbeat to 30 seconds. Are you responding to this heartbeat message every 30 seconds to keep session alive? 

You also received a test request. Have you responded to this this? 

Also note, I see you selected 4096 bytes to be received. What happens when you have read 4096 bytes. does your software have a loop to continiously read from socket or are you handling this async? 

 


@ctid1980098

PanagiotisChar
30 Jan 2023, 11:49

 Hi there, 

You need to send heartbeats to the server to keep the connection alive.

Aieden Technologies

Need help? Join us on Telegram

Need premium support? Trade with us


@PanagiotisChar

wwweeeeee
30 Jan 2023, 23:40 ( Updated at: 30 Jan 2023, 23:44 )

RE: PanagiotisChar, ctid1980098

Thank you both for a reply, seems like there are a lot I have to fix in my code.

1) How to respond to a heartbeat message every 30 seconds to keep session alive? I have to send some kind of FIX-message right? Can you show a fix message example please? I looked here but couldn't find (https://help.ctrader.com/fix/specs/cTraderFixApi_v2.18.1.pdf#logon-example)

2) To respond to a test request I have to send a fix message send that contains |112=Y|?
something similar to this?

b'8=FIX.4.4\x019=97\x0135=1\x0134=4\x0149=CSERVER\x0150=QUOTE\x0152=20230128-21:46:14.917\x0156=demo.roboforex.6183532\x0157=any\x01112=Y\x0110=119\x01'

Also note, I see you selected 4096 bytes to be received. What happens when you have read 4096 bytes. does your software have a loop to continiously read from socket or are you handling this async?

yes, a loop


@wwweeeeee

ctid1980098
01 Feb 2023, 10:02 ( Updated at: 01 Feb 2023, 14:42 )

RE: RE: PanagiotisChar, ctid1980098

hi,

Cannot comment on language specifics.

1. To respond to a heartbeat message you would need to possibly run a loop writing to the socket every 30 seconds or whatever time you have set in the login message. Yes, you would need to send a fix message. See below from the link you posted. The heartbeat message is constructed with a standard header and standard trailer as detailed in document. A typical example message would look something like this:

8=FIX.4.4|9=58|35=0|34=5|49=demo.*******|56=cServer||52=20190605-11:57:29|10=175|

Remember to increment the message sequence number incrementally by 1 and compute the checksum calculation for the field 10 every time you send a message or receive for that matter. The documentation shows you all the fields that go into the standard header and trailer. 

2.

 from documentation: Forces heartbeat from receiver of request. A response is sent from the receiving system as a Heartbeat message containing the 'TestReqID'.

Normally you would get 112=TEST from server and need to include that in the heartbeat response when you send to server.  

As a side note. When reading message. You would need to read the first few bytes to field "9=58" then you know how many bytes will follow after that and can read exactly that amount. 


@ctid1980098

wwweeeeee
02 Feb 2023, 01:59

RE: RE: RE: PanagiotisChar, ctid1980098

It works!! thank you, it works with heartbeat now.

One more thing I want to ask. I have read in the documentation that if HeartBtInt is set to 0, no heartbeat message is required.

So maybe it's was the better idea? Just to set 108=0 and remove the heartbeat. How important is that heartbeat in general?

 

 

 


@wwweeeeee

ctid1980098
02 Feb 2023, 08:03

RE: RE: RE: RE: PanagiotisChar, ctid1980098

wwweeeeee said:

It works!! thank you, it works with heartbeat now.

One more thing I want to ask. I have read in the documentation that if HeartBtInt is set to 0, no heartbeat message is required.

So maybe it's was the better idea? Just to set 108=0 and remove the heartbeat. How important is that heartbeat in general?

You're welcome. Glad it worked.

During periods of inactivity, FIX clients should generate a heartbeat message at regular time intervals. The Heartbeat (0) message is used by a FIX client to monitor the status of the communication link. 

That is correct. If you set heartbeat to 0 in the login, the server will not send a heartbeat periodically. Better? Not sure on your use case. The reason for the heartbeat is to check if you are connected to the server and you have not lost connection. So pretty important. I set my heartbeat to 5 seconds and respond accordingly every 5 seconds from my application. If there is a break in connection then I will know to reset my socket connection and connect again. So the heartbeat is important to determine drops in connection to server so that a corrective course of action can be implemented. hope that helps. Without a heartbeat it might take longer to determine if something has gone wrong.


@ctid1980098