Open only one position (openApiPy)

Created at 30 Mar 2022, 08:41
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!
PR

prenven570

Joined 24.12.2021

Open only one position (openApiPy)
30 Mar 2022, 08:41


I open the position inside "on message received" function, but this way it will open multiple position because of the loop, my goal is to open only one position, how can i do?

 

this is my code:

 

def onMessageReceived(client, message): # Callback for receiving all messages
    if message.payloadType in [ProtoHeartbeatEvent().payloadType, ProtoOAAccountAuthRes().payloadType, ProtoOAApplicationAuthRes().payloadType, ProtoOASymbolsListRes().payloadType, ProtoOAGetTrendbarsRes().payloadType]:
        return
    print("\nMessage received: \n", Protobuf.extract(message))
    
    
    if message.payloadType == ProtoOAExecutionEvent().payloadType:
        print("Execution event received")
        executionEvent = Protobuf.extract(message)
        print(executionEvent.position.positionId)
        positionId = executionEvent.position.positionId
        
      
    if #some conditions:
        symbolId = 1106
        clientMsgId = None
        volume = 2

        request = ProtoOANewOrderReq()
        request.ctidTraderAccountId = currentAccountId

        request.symbolId = int(symbolId)
        request.orderType = ProtoOAOrderType.MARKET
        request.tradeSide = ProtoOATradeSide.SELL
        request.volume = int(volume) * 100 


        deferred = client.send(request, clientMsgId = clientMsgId)
        deferred.addErrback(onError)


@prenven570
Replies

amusleh
30 Mar 2022, 09:05

Hi,

You can use a global variable to check if the position is already opened or not, ex:

isPositionsRequestSent = False
def onMessageReceived(client, message): # Callback for receiving all messages
    if message.payloadType in [ProtoHeartbeatEvent().payloadType, ProtoOAAccountAuthRes().payloadType, ProtoOAApplicationAuthRes().payloadType, ProtoOASymbolsListRes().payloadType, ProtoOAGetTrendbarsRes().payloadType]:
        return
    print("\nMessage received: \n", Protobuf.extract(message))
    
    
    if message.payloadType == ProtoOAExecutionEvent().payloadType:
        print("Execution event received")
        executionEvent = Protobuf.extract(message)
        print(executionEvent.position.positionId)
        positionId = executionEvent.position.positionId
        
      
    if isPositionsRequestSent is False:
        global isPositionsRequestSent
        isPositionsRequestSent = True
        symbolId = 1106
        clientMsgId = None
        volume = 2

        request = ProtoOANewOrderReq()
        request.ctidTraderAccountId = currentAccountId

        request.symbolId = int(symbolId)
        request.orderType = ProtoOAOrderType.MARKET
        request.tradeSide = ProtoOATradeSide.SELL
        request.volume = int(volume) * 100 


        deferred = client.send(request, clientMsgId = clientMsgId)
        deferred.addErrback(onError)

There are other solutions too, but this one is simplest one, it all depends on what you want to do and your design and architecture.

It's not something related to Open API or our package, these are basic programming stuff that you should know.


@amusleh

prenven570
30 Mar 2022, 09:36

RE:

amusleh said:

Hi,

You can use a global variable to check if the position is already opened or not, ex:

isPositionsRequestSent = False
def onMessageReceived(client, message): # Callback for receiving all messages
    if message.payloadType in [ProtoHeartbeatEvent().payloadType, ProtoOAAccountAuthRes().payloadType, ProtoOAApplicationAuthRes().payloadType, ProtoOASymbolsListRes().payloadType, ProtoOAGetTrendbarsRes().payloadType]:
        return
    print("\nMessage received: \n", Protobuf.extract(message))
    
    
    if message.payloadType == ProtoOAExecutionEvent().payloadType:
        print("Execution event received")
        executionEvent = Protobuf.extract(message)
        print(executionEvent.position.positionId)
        positionId = executionEvent.position.positionId
        
      
    if isPositionsRequestSent is False:
        global isPositionsRequestSent
        isPositionsRequestSent = True
        symbolId = 1106
        clientMsgId = None
        volume = 2

        request = ProtoOANewOrderReq()
        request.ctidTraderAccountId = currentAccountId

        request.symbolId = int(symbolId)
        request.orderType = ProtoOAOrderType.MARKET
        request.tradeSide = ProtoOATradeSide.SELL
        request.volume = int(volume) * 100 


        deferred = client.send(request, clientMsgId = clientMsgId)
        deferred.addErrback(onError)

There are other solutions too, but this one is simplest one, it all depends on what you want to do and your design and architecture.

It's not something related to Open API or our package, these are basic programming stuff that you should know.

Perfect, thanks, this method worked:

    global isPositionsRequestSent
    if isPositionsRequestSent is False:
        
        isPositionsRequestSent = True


@prenven570