How to set account and send order after thereactor was run? cTrader PythonOpenApi

Viewed 11

I'm coding my trading bot with parser and I want it to set account and send new order if the bot found it on a webpage that was just parsed. I see that all the commands before reactor.run() execute sucessfully, but I need my commands to be executed inside the main cycle of onMessageReceived() callback or something like it.

def accountAuthResponseCallback(result):
    print("\nAccount authenticated")
    request = ProtoOASymbolsListReq()
    request.ctidTraderAccountId = profile_id
    request.includeArchivedSymbols = False
    deferred = client.send(request)

def applicationAuthResponseCallback(result):
    print("\nApplication authenticated")
    request = ProtoOAAccountAuthReq()
    request.ctidTraderAccountId = profile_id
    request.accessToken = accessToken
    deferred = client.send(request)
    deferred.addCallbacks(accountAuthResponseCallback, onError)


def connected(client):  # Callback for client connection
    print("\nConnected")
    request = ProtoOAApplicationAuthReq()
    request.clientId = appClientId
    request.clientSecret = appClientSecret
    deferred = client.send(request)
    deferred.addErrback(onError)

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

def onMessageReceived(client, message):  # Callback for receiving all messages
    if message.payloadType in [ProtoOASubscribeSpotsRes().payloadType, ProtoOAAccountLogoutRes().payloadType,
                               ProtoHeartbeatEvent().payloadType]:
        return
    elif message.payloadType == ProtoOAApplicationAuthRes().payloadType:
        print("API Application authorized\n")
        print(
            "Please use setAccount command to set the authorized account before sending any other command, try help for more detail\n")
        print("To get account IDs use ProtoOAGetAccountListByAccessTokenReq command")
        if currentAccountId is not None:
            sendProtoOAAccountAuthReq()
            return
    elif message.payloadType == ProtoOAAccountAuthRes().payloadType:
        protoOAAccountAuthRes = Protobuf.extract(message)
        print(f"Account {protoOAAccountAuthRes.ctidTraderAccountId} has been authorized\n")
        print("This acccount will be used for all future requests\n")
        print("You can change the account by using setAccount command")
    else:
        print("Message received: \n", Protobuf.extract(message))

    all_open_trades_db = sqlalchemy.create_engine(f'sqlite:///opened_trades.db')
    setAccount(24471308)
    get_all_open_trades("https://www.forexfactory.com/***", all_open_trades_db)

    time.sleep(3)

    setAccount(24471314)
    get_all_open_trades("https://www.forexfactory.com/***", all_open_trades_db)

def main():
    client.setConnectedCallback(connected)
    client.setDisconnectedCallback(disconnected)
    client.setMessageReceivedCallback(onMessageReceived)
    client.startService()
    reactor.run()

if __name__ == "__main__":
    main()

It doesn't set accounts and send orders inside the get_all_open_trades() Help me!

0 Answers
Related