Hi, i have tried to implement the lines you sent, but keep getting the errors below on VSC and CMD after i tried to run and post a webhook from Insomnia local host, any idea what causing these errors?
Appreciate any suggestions.
VSC 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 requests, json
from chalice import Chalice
app = Chalice(app_name='test')
hostType = input("Host (Live/Demo): ")
host = EndPoints.PROTOBUF_LIVE_HOST if hostType.lower() == "live" else EndPoints.PROTOBUF_DEMO_HOST
r = requests.post(ORDERS_URL, json=data, headers=HEADERS)
response = json.loads(r.content)
print(response)
print(response.keys())
return {
'message': ' I bought the stock!',
'webhook_message': webhook_message
}
VSC ERROR:
Connected
Unhandled Error
Traceback (most recent call last):
File "C:\Users\roslaan\AppData\Local\Programs\Python\Python310\lib\site-packages\twisted\python\log.py", line 101, in callWithLogger
return callWithContext({"system": lp}, func, *args, **kw)
File "C:\Users\roslaan\AppData\Local\Programs\Python\Python310\lib\site-packages\twisted\python\log.py", line 85, in callWithContext
return context.call({ILogContext: newCtx}, func, *args, **kw)
File "C:\Users\roslaan\AppData\Local\Programs\Python\Python310\lib\site-packages\twisted\python\context.py", line 118, in callWithContext
return self.currentContext().callWithContext(ctx, func, *args, **kw)
File "C:\Users\roslaan\AppData\Local\Programs\Python\Python310\lib\site-packages\twisted\python\context.py", line 83, in callWithContext
return func(*args, **kw)
--- <exception caught here> ---
File "C:\Users\roslaan\AppData\Local\Programs\Python\Python310\lib\site-packages\twisted\internet\selectreactor.py", line 149, in _doReadOrWrite
why = getattr(selectable, method)()
File "C:\Users\roslaan\AppData\Local\Programs\Python\Python310\lib\site-packages\twisted\internet\tcp.py", line 614, in doConnect
self._connectDone()
File "C:\Users\roslaan\AppData\Local\Programs\Python\Python310\lib\site-packages\twisted\internet\tcp.py", line 642, in _connectDone
self.protocol.makeConnection(self)
File "C:\Users\roslaan\AppData\Local\Programs\Python\Python310\lib\site-packages\twisted\protocols\tls.py", line 211, in makeConnection
ProtocolWrapper.makeConnection(self, transport)
File "C:\Users\roslaan\AppData\Local\Programs\Python\Python310\lib\site-packages\twisted\protocols\policies.py", line 74, in
makeConnection
self.wrappedProtocol.makeConnection(self)
File "C:\Users\roslaan\AppData\Local\Programs\Python\Python310\lib\site-packages\twisted\internet\protocol.py", line 508, in
makeConnection
self.connectionMade()
File "C:\Users\roslaan\AppData\Local\Programs\Python\Python310\lib\site-packages\twisted\internet\endpoints.py", line 142, in connectionMade
self._wrappedProtocol.makeConnection(self.transport)
File "C:\Users\roslaan\AppData\Local\Programs\Python\Python310\lib\site-packages\twisted\internet\protocol.py", line 508, in
makeConnection
self.connectionMade()
File "C:\Users\roslaan\AppData\Local\Programs\Python\Python310\lib\site-packages\ctrader_open_api\tcpProtocol.py", line 21, in connectionMade
self.factory.connected(self)
File "C:\Users\roslaan\AppData\Local\Programs\Python\Python310\lib\site-packages\ctrader_open_api\factory.py", line 11, in connected
self.client._connected(protocol)
File "C:\Users\roslaan\AppData\Local\Programs\Python\Python310\lib\site-packages\ctrader_open_api\client.py", line 32, in _connected
self._connectedCallback(self)
File "c:\Users\roslaan\Documents\Privat\Bot\VSC\API\test\app.py", line 30, in connected
deferred.addCallbacks(onProtoOAApplicationAuthRes, onError)
builtins.NameError: name 'onProtoOAApplicationAuthRes' is not defined
Disconnected: [Failure instance: Traceback (failure with no frames): <class 'twisted.internet.error.ConnectionLost'>: Connection to the other side was lost in a non-clean fashion: Connection lost.
]
Unhandled Error
Traceback (most recent call last):
File "c:\Users\roslaan\Documents\Privat\Bot\VSC\API\test\app.py", line 66, in <module>
reactor.run()
File "C:\Users\roslaan\AppData\Local\Programs\Python\Python310\lib\site-packages\twisted\internet\base.py", line 1318, in run self.mainLoop()
File "C:\Users\roslaan\AppData\Local\Programs\Python\Python310\lib\site-packages\twisted\internet\base.py", line 1331, in mainLoop
reactorBaseSelf.doIteration(t)
File "C:\Users\roslaan\AppData\Local\Programs\Python\Python310\lib\site-packages\twisted\internet\selectreactor.py", line 143, in doSelect
_logrun(selectable, _drdw, selectable, method)
--- <exception caught here> ---
File "C:\Users\roslaan\AppData\Local\Programs\Python\Python310\lib\site-packages\twisted\python\log.py", line 101, in callWithLogger
return callWithContext({"system": lp}, func, *args, **kw)
File "C:\Users\roslaan\AppData\Local\Programs\Python\Python310\lib\site-packages\twisted\python\log.py", line 85, in callWithContext
return context.call({ILogContext: newCtx}, func, *args, **kw)
File "C:\Users\roslaan\AppData\Local\Programs\Python\Python310\lib\site-packages\twisted\python\context.py", line 118, in callWithContext
return self.currentContext().callWithContext(ctx, func, *args, **kw)
File "C:\Users\roslaan\AppData\Local\Programs\Python\Python310\lib\site-packages\twisted\python\context.py", line 83, in callWithContext
return func(*args, **kw)
File "C:\Users\roslaan\AppData\Local\Programs\Python\Python310\lib\site-packages\twisted\internet\selectreactor.py", line 154, in _doReadOrWrite
self._disconnectSelectable(selectable, why, method == "doRead")
File "C:\Users\roslaan\AppData\Local\Programs\Python\Python310\lib\site-packages\twisted\internet\posixbase.py", line 306, in _disconnectSelectable
selectable.connectionLost(failure.Failure(why))
File "C:\Users\roslaan\AppData\Local\Programs\Python\Python310\lib\site-packages\twisted\internet\tcp.py", line 507, in connectionLost
self._commonConnection.connectionLost(self, reason)
File "C:\Users\roslaan\AppData\Local\Programs\Python\Python310\lib\site-packages\twisted\internet\tcp.py", line 325, in connectionLost
protocol.connectionLost(reason)
File "C:\Users\roslaan\AppData\Local\Programs\Python\Python310\lib\site-packages\twisted\protocols\tls.py", line 394, in connectionLost
ProtocolWrapper.connectionLost(self, reason)
File "C:\Users\roslaan\AppData\Local\Programs\Python\Python310\lib\site-packages\twisted\protocols\policies.py", line 113, in connectionLost
self.wrappedProtocol.connectionLost(reason)
File "C:\Users\roslaan\AppData\Local\Programs\Python\Python310\lib\site-packages\twisted\internet\endpoints.py", line 161, in connectionLost
return self._wrappedProtocol.connectionLost(reason)
File "C:\Users\roslaan\AppData\Local\Programs\Python\Python310\lib\site-packages\twisted\application\internet.py", line 467,
in connectionLost
self._lostNotification(reason)
File "C:\Users\roslaan\AppData\Local\Programs\Python\Python310\lib\site-packages\twisted\application\internet.py", line 660,
in <lambda>
self._factory, lambda _: self._clientDisconnected()
File "C:\Users\roslaan\AppData\Local\Programs\Python\Python310\lib\site-packages\automat\_methodical.py", line 235, in _clientDisconnected
(outputs, outTracer) = transitioner.transition(self)
File "C:\Users\roslaan\AppData\Local\Programs\Python\Python310\lib\site-packages\automat\_core.py", line 157, in transition
outState, outputSymbols = self._automaton.outputForInput(self._state,
File "C:\Users\roslaan\AppData\Local\Programs\Python\Python310\lib\site-packages\automat\_core.py", line 137, in outputForInput
raise NoTransition(state=inState, symbol=inputSymbol)
automat._core.NoTransition: no transition for MethodicalInput(method=<function _ClientMachine._clientDisconnected at 0x000001EB74FCD5A0>) in MethodicalState(method=<function _ClientMachine._connecting at 0x000001EB74FCC940>)
rosland.andreas
26 Jun 2023, 13:19
RE:
Hi, i have tried to implement the lines you sent, but keep getting the errors below on VSC and CMD after i tried to run and post a webhook from Insomnia local host, any idea what causing these errors?
Appreciate any suggestions.
VSC 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 requests, json
from chalice import Chalice
app = Chalice(app_name='test')
hostType = input("Host (Live/Demo): ")
host = EndPoints.PROTOBUF_LIVE_HOST if hostType.lower() == "live" else EndPoints.PROTOBUF_DEMO_HOST
client = Client(host, EndPoints.PROTOBUF_PORT, TcpProtocol)
def onError(failure): # Call back for errors
print("Message Error: ", failure)
def connected(client): # Callback for client connection
print("\nConnected")
# Now we send a ProtoOAApplicationAuthReq
request = ProtoOAApplicationAuthReq()
request.clientId = "XXXXXXXXX"
request.clientSecret = "XXXXXXXX"
# Client send method returns a Twisted deferred
deferred = client.send(request)
# You can use the returned Twisted deferred to attach callbacks
# for getting message response or error backs for getting error if something went wrong
# deferred.addCallbacks(onProtoOAApplicationAuthRes, onError)
deferred.addCallbacks(onProtoOAApplicationAuthRes, onError)
def disconnected(client, reason): # Callback for client disconnection
print("\nDisconnected: ", reason)
def onMessageReceived(client, message): # Callback for receiving all messages
print("Message received: \n", Protobuf.extract(message))
def accountAuthResponseCallback(result):
print("\nAccount authenticated")
# From here you build your next step
def applicationAuthResponseCallback(result):
print("\nApplication authenticated")
request = ProtoOAAccountAuthReq()
request.ctidTraderAccountId = credentials["XXXXXXXXXX"]
request.accessToken = credentials["XXXXX"]
deferred = client.send(request)
deferred.addCallbacks(accountAuthResponseCallback, onError)
# Setting optional client callbacks
client.setConnectedCallback(connected)
client.setDisconnectedCallback(disconnected)
client.setMessageReceivedCallback(onMessageReceived)
# Starting the client service
client.startService()
# Run Twisted reactor
reactor.run()
@app.route('/Buy_Stock', methods=['POST'])
def Buy_Stock():
request = app.current_request
webhook_message = request.json_body
data = {
"symbol": webhook_message['ticker'],
"qty": 1,
"side": "buy",
"type": "limit",
"limit_price": webhook_message['close'],
"time_in_force": "gtc",
"order_class": "bracket",
"take_profit": {
"limit_price": webhook_message['close'] * 1.05
},
"stop_loss": {
"stop_price": webhook_message['close'] * 0.98,
}
}
r = requests.post(ORDERS_URL, json=data, headers=HEADERS)
response = json.loads(r.content)
print(response)
print(response.keys())
return {
'message': ' I bought the stock!',
'webhook_message': webhook_message
}
VSC ERROR:
Connected
Unhandled Error
Traceback (most recent call last):
File "C:\Users\roslaan\AppData\Local\Programs\Python\Python310\lib\site-packages\twisted\python\log.py", line 101, in callWithLogger
return callWithContext({"system": lp}, func, *args, **kw)
File "C:\Users\roslaan\AppData\Local\Programs\Python\Python310\lib\site-packages\twisted\python\log.py", line 85, in callWithContext
return context.call({ILogContext: newCtx}, func, *args, **kw)
File "C:\Users\roslaan\AppData\Local\Programs\Python\Python310\lib\site-packages\twisted\python\context.py", line 118, in callWithContext
return self.currentContext().callWithContext(ctx, func, *args, **kw)
File "C:\Users\roslaan\AppData\Local\Programs\Python\Python310\lib\site-packages\twisted\python\context.py", line 83, in callWithContext
return func(*args, **kw)
--- <exception caught here> ---
File "C:\Users\roslaan\AppData\Local\Programs\Python\Python310\lib\site-packages\twisted\internet\selectreactor.py", line 149, in _doReadOrWrite
why = getattr(selectable, method)()
File "C:\Users\roslaan\AppData\Local\Programs\Python\Python310\lib\site-packages\twisted\internet\tcp.py", line 614, in doConnect
self._connectDone()
File "C:\Users\roslaan\AppData\Local\Programs\Python\Python310\lib\site-packages\twisted\internet\tcp.py", line 642, in _connectDone
self.protocol.makeConnection(self)
File "C:\Users\roslaan\AppData\Local\Programs\Python\Python310\lib\site-packages\twisted\protocols\tls.py", line 211, in makeConnection
ProtocolWrapper.makeConnection(self, transport)
File "C:\Users\roslaan\AppData\Local\Programs\Python\Python310\lib\site-packages\twisted\protocols\policies.py", line 74, in
makeConnection
self.wrappedProtocol.makeConnection(self)
File "C:\Users\roslaan\AppData\Local\Programs\Python\Python310\lib\site-packages\twisted\internet\protocol.py", line 508, in
makeConnection
self.connectionMade()
File "C:\Users\roslaan\AppData\Local\Programs\Python\Python310\lib\site-packages\twisted\internet\endpoints.py", line 142, in connectionMade
self._wrappedProtocol.makeConnection(self.transport)
File "C:\Users\roslaan\AppData\Local\Programs\Python\Python310\lib\site-packages\twisted\internet\protocol.py", line 508, in
makeConnection
self.connectionMade()
File "C:\Users\roslaan\AppData\Local\Programs\Python\Python310\lib\site-packages\ctrader_open_api\tcpProtocol.py", line 21, in connectionMade
self.factory.connected(self)
File "C:\Users\roslaan\AppData\Local\Programs\Python\Python310\lib\site-packages\ctrader_open_api\factory.py", line 11, in connected
self.client._connected(protocol)
File "C:\Users\roslaan\AppData\Local\Programs\Python\Python310\lib\site-packages\ctrader_open_api\client.py", line 32, in _connected
self._connectedCallback(self)
File "c:\Users\roslaan\Documents\Privat\Bot\VSC\API\test\app.py", line 30, in connected
deferred.addCallbacks(onProtoOAApplicationAuthRes, onError)
builtins.NameError: name 'onProtoOAApplicationAuthRes' is not defined
Disconnected: [Failure instance: Traceback (failure with no frames): <class 'twisted.internet.error.ConnectionLost'>: Connection to the other side was lost in a non-clean fashion: Connection lost.
]
Unhandled Error
Traceback (most recent call last):
File "c:\Users\roslaan\Documents\Privat\Bot\VSC\API\test\app.py", line 66, in <module>
reactor.run()
File "C:\Users\roslaan\AppData\Local\Programs\Python\Python310\lib\site-packages\twisted\internet\base.py", line 1318, in run self.mainLoop()
File "C:\Users\roslaan\AppData\Local\Programs\Python\Python310\lib\site-packages\twisted\internet\base.py", line 1331, in mainLoop
reactorBaseSelf.doIteration(t)
File "C:\Users\roslaan\AppData\Local\Programs\Python\Python310\lib\site-packages\twisted\internet\selectreactor.py", line 143, in doSelect
_logrun(selectable, _drdw, selectable, method)
--- <exception caught here> ---
File "C:\Users\roslaan\AppData\Local\Programs\Python\Python310\lib\site-packages\twisted\python\log.py", line 101, in callWithLogger
return callWithContext({"system": lp}, func, *args, **kw)
File "C:\Users\roslaan\AppData\Local\Programs\Python\Python310\lib\site-packages\twisted\python\log.py", line 85, in callWithContext
return context.call({ILogContext: newCtx}, func, *args, **kw)
File "C:\Users\roslaan\AppData\Local\Programs\Python\Python310\lib\site-packages\twisted\python\context.py", line 118, in callWithContext
return self.currentContext().callWithContext(ctx, func, *args, **kw)
File "C:\Users\roslaan\AppData\Local\Programs\Python\Python310\lib\site-packages\twisted\python\context.py", line 83, in callWithContext
return func(*args, **kw)
File "C:\Users\roslaan\AppData\Local\Programs\Python\Python310\lib\site-packages\twisted\internet\selectreactor.py", line 154, in _doReadOrWrite
self._disconnectSelectable(selectable, why, method == "doRead")
File "C:\Users\roslaan\AppData\Local\Programs\Python\Python310\lib\site-packages\twisted\internet\posixbase.py", line 306, in _disconnectSelectable
selectable.connectionLost(failure.Failure(why))
File "C:\Users\roslaan\AppData\Local\Programs\Python\Python310\lib\site-packages\twisted\internet\tcp.py", line 507, in connectionLost
self._commonConnection.connectionLost(self, reason)
File "C:\Users\roslaan\AppData\Local\Programs\Python\Python310\lib\site-packages\twisted\internet\tcp.py", line 325, in connectionLost
protocol.connectionLost(reason)
File "C:\Users\roslaan\AppData\Local\Programs\Python\Python310\lib\site-packages\twisted\protocols\tls.py", line 394, in connectionLost
ProtocolWrapper.connectionLost(self, reason)
File "C:\Users\roslaan\AppData\Local\Programs\Python\Python310\lib\site-packages\twisted\protocols\policies.py", line 113, in connectionLost
self.wrappedProtocol.connectionLost(reason)
File "C:\Users\roslaan\AppData\Local\Programs\Python\Python310\lib\site-packages\twisted\internet\endpoints.py", line 161, in connectionLost
return self._wrappedProtocol.connectionLost(reason)
File "C:\Users\roslaan\AppData\Local\Programs\Python\Python310\lib\site-packages\twisted\application\internet.py", line 467,
in connectionLost
self._lostNotification(reason)
File "C:\Users\roslaan\AppData\Local\Programs\Python\Python310\lib\site-packages\twisted\application\internet.py", line 660,
in <lambda>
self._factory, lambda _: self._clientDisconnected()
File "C:\Users\roslaan\AppData\Local\Programs\Python\Python310\lib\site-packages\automat\_methodical.py", line 235, in _clientDisconnected
(outputs, outTracer) = transitioner.transition(self)
File "C:\Users\roslaan\AppData\Local\Programs\Python\Python310\lib\site-packages\automat\_core.py", line 157, in transition
outState, outputSymbols = self._automaton.outputForInput(self._state,
File "C:\Users\roslaan\AppData\Local\Programs\Python\Python310\lib\site-packages\automat\_core.py", line 137, in outputForInput
raise NoTransition(state=inState, symbol=inputSymbol)
automat._core.NoTransition: no transition for MethodicalInput(method=<function _ClientMachine._clientDisconnected at 0x000001EB74FCD5A0>) in MethodicalState(method=<function _ClientMachine._connecting at 0x000001EB74FCC940>)
CMD ERROR:
{'code': 40010001, 'message': 'invalid order type'}
dict_keys(['code', 'message'])
127.0.0.1 - - [26/Jun/2023 09:03:53] "POST /Buy_Stock HTTP/1.1" 200 -
@rosland.andreas