Trying to close an open trade with FIX API

Created at 09 Mar 2023, 11:13
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!
DA

davidp13

Joined 06.05.2014

Trying to close an open trade with FIX API
09 Mar 2023, 11:13


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()

 


@davidp13
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

davidp13
13 May 2023, 11:08

RE: RE:

Unfortunately not. 


@davidp13