Replies

karakayafaruk6@gmail.com
12 May 2023, 21:11

RE:

davidp13 said:

HELLO did you solve your problem? I have the same issue :) Can you help me?  

Hi. I have been trying to close a trade that I opened from reading a CSV file using the Python module. I can open the trade/s fine, but when I try and run the script again it never closes the trade. What am I missing here? Thanks Below is the code that I use:

from twisted.internet import reactor
import json
from ctrader_fix import *
import datetime
import csv

with open("config-trade.json") as configFile:
    config = json.load(configFile)

client = Client(config["Host"], config["Port"], ssl=config["SSL"])


def send(request):
    deferred = client.send(request)
    deferred.addCallback(lambda _: print("\nSent: ", request.getMessage(client.getMessageSequenceNumber()).replace("", "")))


def onMessageReceived(client, responseMessage):
    print("\nReceived: ", responseMessage.getMessage().replace("", ""))
    messageType = responseMessage.getFieldValue(35)

    if messageType == "A":
        securityListRequest = SecurityListRequest(config)
        securityListRequest.SecurityReqID = "A"
        securityListRequest.SecurityListRequestType = 0
        send(securityListRequest)

    elif messageType == "y":
        symboldIds = responseMessage.getFieldValue(55)
        if config["TargetSubID"] == "TRADE":
            with open("openclose.csv") as csvfile:
                reader = csv.DictReader(csvfile)
                for row in reader:
                    if row["SELL"] == "1":
                        order = OrderCancelRequest(config)
                        order.ClOrdID = "C"
                        order.OrigClOrdID = "B"
                        order.Symbol = symboldIds[int(row["SYMBOL_ID"])]
                        order.Side = 1
                        order.OrderQty = 1
                        order.OrdType = 1
                        order.Designation = "Close From Jupyter"
                        send(order)
                    else:
                        newOrderSingle = NewOrderSingle(config)
                        newOrderSingle.ClOrdID = "B"
                
                        newOrderSingle.Symbol = symboldIds[int(row["SYMBOL_ID"])]
                        newOrderSingle.Side = 1
                        newOrderSingle.OrderQty = 1
                        newOrderSingle.OrdType = 1
                        newOrderSingle.Designation = "From Jupyter"
                        send(newOrderSingle)

    elif messageType == "8" or messageType == "j":
        print("We are done, stopping the reactor")
        reactor.stop()


def disconnected(client, reason):
    print("\nDisconnected, reason: ", reason)


def connected(client):
    print("Connected")
    logonRequest = LogonRequest(config)
    send(logonRequest)


client.setConnectedCallback(connected)
client.setDisconnectedCallback(disconnected)
client.setMessageReceivedCallback(onMessageReceived)

client.startService()

reactor.run()

 

 


@karakayafaruk6@gmail.com

karakayafaruk6@gmail.com
09 May 2023, 01:20

RE: RE: RE: RE:

ctid1980098 said:

Thank you for the document. I try to close my current position 25% or 50%. or close all LONG  positions at the same time

Yes i think you can close partially by 25 or 50%. For closing a position, the opposite order has to be sent with the same amount or an amount that reduces position by certain percentage.

So tag 38= will contain an amount 50% less than your original position. then it should work. 

To close all long positions you would need to open opposite sell positions with tag 721 containing the ids of the original buy positions and it should all close. 

Thanks for the info. I am still struggling :) still couldn't open a position :) :)


@karakayafaruk6@gmail.com

karakayafaruk6@gmail.com
07 May 2023, 23:49 ( Updated at: 21 Dec 2023, 09:23 )

RE: RE:

ctid1980098 said:

Thank you for the document. I try to close my current position 25% or 50%. or close all LONG  positions at the same time

Hi,

the only documentation i'm aware of is on the ctrader site and specification here https://help.ctrader.com/fix/specs/cTraderFixApi_v2.18.1.pdf 

Buy and sell orders examples can be found in the above link as a new order single 35=D message. 

Closing positions: To close a buy position you need to open a corresponding sell position that includes the buy position id in the tag 721=.... so as to link the buy with the closing sell position. there is one example of this given below. see tag 721 under "market order to existing position". same is for the reverse case. 

What kind of complex cases are you referring to? 

 

 


@karakayafaruk6@gmail.com

karakayafaruk6@gmail.com
04 May 2023, 02:27

RE: RE:

PanagiotisChar you are right !. I reconfigured heartbeat code again. and that fixed the problem. THANK YOU <3

 


@karakayafaruk6@gmail.com

karakayafaruk6@gmail.com
04 May 2023, 01:30

RE:

PanagiotisChar said:

Hi there,

Is your application sending heartbeats to the server?

Aieden Technologies

Need help? Join us on Telegram

Need premium support? Trade with us

 

Sent:  8=FIX.4.4|9=92|35=1|49=demo.icmarkets.8709889|56=cServer|57=TRADE|50=TRADE|34=3|52=20230503-22:29:54|112=4|10=101|

Disconnected, reason:  [Failure instance: Traceback (failure with no frames): <class 'twisted.internet.error.ConnectionDone'>: Connection was closed cleanly.

Failure: twisted.internet.error.TimeoutError: User timeout caused connection failure.

I am sending heartbeats. 35=1 means heartbeat right? but I get disconnect immediately after I send the heartbeat......  what could it be? I am using the exact python code.


@karakayafaruk6@gmail.com

karakayafaruk6@gmail.com
03 May 2023, 19:50

RE: RE:

it is me again. I have the same problem today also. I can only stay online 2 minutes. after the host kicks me, I can reconnect but I get kicked again in 2 minutes.

If it continues like that for a while, I get kicked for several hours.  I feel like my API access is blocked for a few hours. I think the host doesn't like one or some of my settings. What do you think this could be?    Thanks for any help .  I posted below my hearthbeat message and disconnection Reason...

 

Received:  8=FIX.4.4|9=99|35=1|34=3|49=cServer|50=TRADE|52=20230503-16:44:53.796|56=demo.icmarkets.8709889|57=TRADE|112=TEST|10=075|
Disconnected, reason:  [Failure instance: Traceback (failure with no frames): <class 'twisted.internet.error.ConnectionDone'>: Connection was closed cleanly.
]
Connected
Received:  8=FIX.4.4|9=102|35=A|34=1|49=cServer|50=TRADE|52=20230503-16:45:40.528|56=demo.icmarkets.8709889|57=TRADE|98=0|108=50|10=121|
We are logged in

 


@karakayafaruk6@gmail.com

karakayafaruk6@gmail.com
03 May 2023, 11:50

RE:

PanagiotisChar said:

Hi there,

Is your application sending heartbeats to the server?

Aieden Technologies

Need help? Join us on Telegram

Need premium support? Trade with us

 

Hello PanagiotisChar thank you for reply.

At my first fix API demo trial,

Yes my application was sending heartbeats 30 secons and 50 seconds I tried both. It was working at the first and still was disconnected every 2 minutes and reconnecting and became online instantly but again, heartbeats was working after 2 minutes disconnected again and reconnected.

At my second demo fix api trial I was connected but instantly disconnected. Again connected and instantly disconnected. Like kicked from the server... I did not change any settings only changed heartbeat 30seconds to 50 seconds. It looks like I at least I was connecting in a short time period at my first trial. At my second trial I get instant connection and instant disconnection....

Every help is appreciated 


@karakayafaruk6@gmail.com