Python Closing Existing Positions Help
Python Closing Existing Positions Help
11 May 2023, 16:43
Hello, Sorry if I'm bothering anyone because I open New Threads these days.
I am using Twisted , pyhton ctrader.fix
I can open LONG or SHORT postions.
but when I try to close those existing positions, I have troubles.
thanks to a member named ctid1980098 tried to help me explained the logic,
but I am a beginner and my attempts fail...
First, I try to position IDs like this ,
class CustomRequestForPositions(RequestForPositions):
def __init__(self, config):
super().__init__(config)
self.PosReqID = generate_unique_id()
position_request = CustomRequestForPositions(config)
position_request.PosReqID = generate_unique_id()
position_request.Symbol = ticker
response_message = yield client.send(position_request)
After I try to extract the numbers next to 721= 123456 from the received RequestForPositions Message.
721 PosMaintRptID is POSITION ID
def extract_position_ids(message):
matches = re.findall(r'721=[0-9]+', message)
position_ids = [match[4:] for match in matches]
print("721 NUMBER:", position_ids)
return position_ids
After I try to make opposite market orders with the same ID . which has extracted from the previous step.
as I told you I am a beginner . I may be completely wrong. I may not be able to do extraction properly.
I would be very hapy if someone share with me a python code
to close the existing positions ... (with position request and extraction included.) Therefore I can try it to apply
to my existing code and try more... ...