Topics

Forum Topics not found

Replies

william@hallsdesign.co.uk
04 Apr 2022, 18:09

RE: RE: Code to make this work?

prenven570 said:

amusleh said:

Hi,

You have to use ProtoOASpotEvent, you will receive that event after Spot subscription on each tick, not ProtoOAGetTickDataRes.

 

Thanks

Hi,

 

I have been going round in circles with this, and all I want is the current price of a pair I have. I have everything, all my code is placing a trade etc.. But only if I manually add the price in for my pair and run my code.

I have tried all the subscribing and ProtoOASpotEvent stuff. If you wouldnt mind helping me out with the exact code you did, that would be greatly appreciated. I just wish they would allow you to place a market order with a stopLoss and takeProfit without having to faff with all of this!!

Thanks in advance

 

William


@william@hallsdesign.co.uk

william@hallsdesign.co.uk
10 Mar 2022, 18:28

RE: RE: RE: RE:

Thanks for the help with this, think I have solved it too now. :) 

 

prenven570 said:

william@hallsdesign.co.uk said:

prenven570 said:

 

Edit:

i solved  the problem of the unauthorized trading account, but the second question remains: How to open a position? 

 

Hi, how did you solve the unauthorised trading account bit? I have this same issue and I am unsure how to solve. ( just changed my accountID to xxxxxxxx ).

 

Message received: 
 ctidTraderAccountId: xxxxxxxx
errorCode: "ACCOUNT_NOT_AUTHORIZED"
description: "Account is not authorized"

Any help is greatly appreciated.

I solved by using this code:


 
from ctrader_open_api import Client, Protobuf, TcpProtocol, Auth, EndPoints
from ctrader_open_api.messages.OpenApiCommonMessages_pb2 import *
from ctrader_open_api.messages.OpenApiCommonMessages_pb2 import *
from ctrader_open_api.messages.OpenApiMessages_pb2 import *
from ctrader_open_api.messages.OpenApiModelMessages_pb2 import *
from twisted.internet import reactor
import json
import datetime

#you create a .json file with credentials ther's a sample on the github repository (jupyter sample)


credentialsFile = open("credentials-dev.json") credentials = json.load(credentialsFile)

host = EndPoints.PROTOBUF_LIVE_HOST if credentials["HostType"].lower() == "live" else EndPoints.PROTOBUF_DEMO_HOST
client = Client(host, EndPoints.PROTOBUF_PORT, TcpProtocol)

 

def accountAuthResponseCallback(result):
    print("\nAccount authenticated")
    request = ProtoOASymbolsListReq()
    request.ctidTraderAccountId = credentials["AccountId"]
    request.includeArchivedSymbols = False
    deferred = client.send(request)
    
def applicationAuthResponseCallback(result):
    print("\nApplication authenticated")
    request = ProtoOAAccountAuthReq()
    request.ctidTraderAccountId = credentials["AccountId"]
    request.accessToken = credentials["AccessToken"]
    deferred = client.send(request)
    deferred.addCallbacks(accountAuthResponseCallback, onError)

def onError(client, failure): # Call back for errors
    print("\nMessage Error: ", failure)

def disconnected(client, reason): # Callback for client disconnection
    print("\nDisconnected: ", reason)

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))
    
def connected(client): # Callback for client connection
    print("\nConnected")
    request = ProtoOAApplicationAuthReq()
    request.clientId = credentials["ClientId"]
    request.clientSecret = credentials["Secret"]
    deferred = client.send(request)
    deferred.addCallbacks(applicationAuthResponseCallback, onError)
    
# Setting optional client callbacks
client.setConnectedCallback(connected)
client.setDisconnectedCallback(disconnected)
client.setMessageReceivedCallback(onMessageReceived)

# Starting the client service
client.startService()
# Run Twisted reactor, we imported it earlier
reactor.run()

 


@william@hallsdesign.co.uk

william@hallsdesign.co.uk
03 Mar 2022, 18:12

RE: RE:

prenven570 said:

 

Edit:

i solved  the problem of the unauthorized trading account, but the second question remains: How to open a position? 

 

Hi, how did you solve the unauthorised trading account bit? I have this same issue and I am unsure how to solve. ( just changed my accountID to xxxxxxxx ).

 

Message received: 
 ctidTraderAccountId: xxxxxxxx
errorCode: "ACCOUNT_NOT_AUTHORIZED"
description: "Account is not authorized"

Any help is greatly appreciated.


@william@hallsdesign.co.uk