Trade execution failed (The server returned a "405 Method Not Allowed, Something is broken. Please let us know what you were doing when this error occurred. We will fix it as soon as possible. Sorry for any inconvenience caused. )

Created at 05 Jul 2023, 11:25
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!
DA

daytrayding.andreas

Joined 19.05.2022

Trade execution failed (The server returned a "405 Method Not Allowed, Something is broken. Please let us know what you were doing when this error occurred. We will fix it as soon as possible. Sorry for any inconvenience caused. )
05 Jul 2023, 11:25


Hi, i think there are something wrong with my @app.route, maybe ctrader api doesn't support POST method.

I am getting this error from the terminal: (Does this error mean that it have an connection with Ctrader API, but there is something wrong with the HTTP request???)

And this error from Insomnia: 

So it gets the error that is set on the code on line 79:

 

from ctrader_open_api import Client, Protobuf, TcpProtocol, Auth, EndPoints

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

from twisted.internet import reactor, ssl

import requests, json

from chalice import Chalice


 

app = Chalice(app_name='ctrader_app')

 

API_ENDPOINT = 'https://openapi.ctrader.com'  # Replace with the correct API endpoint URL

API_TOKEN = '<XXX>'

CLIENT_ID = '<XXX>'

CLIENT_SECRET = '<XXX>'

ACCOUNT_ID = '<XXX>'

 

# Setting up the cTrader client

hostType = "Demo"

host = EndPoints.PROTOBUF_LIVE_HOST if hostType.lower() == "live" else EndPoints.PROTOBUF_DEMO_HOST

client = Client(host, EndPoints.PROTOBUF_PORT, TcpProtocol, ssl.CertificateOptions())


 

def onError(failure): # Callback for errors

    print("Message Error: ", failure)

 

def connected(client): # Callback for client connection

    print("\nConnected")

    # Now we send a ProtoOAApplicationAuthReq

    request = ProtoOAApplicationAuthReq()

    request.clientId = CLIENT_ID

    request.clientSecret = CLIENT_SECRET

    # Client send method returns a Twisted deferred

    deferred = client.send(request)

    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 onProtoOAApplicationAuthRes(result):

    print("\nApplication authenticated")

    request = ProtoOAAccountAuthReq()

    request.ctidTraderAccountId = ACCOUNT_ID

    request.accessToken = result.accessToken

    deferred = client.send(request)

    deferred.addCallbacks(accountAuthResponseCallback, onError)

 

def accountAuthResponseCallback(result):

    print("\nAccount authenticated")

    # From here you can build your next steps

 

def execute_trade(symbol, side, quantity):

    # Set up the request payload for the cTrader API

    payload = {

        'symbol': symbol,

        'side': side,

        'quantity': quantity,

        'client_id': CLIENT_ID,

        'client_secret': CLIENT_SECRET,

        'account_id': ACCOUNT_ID

        # Add other required fields for your trade execution

    }

 

    # Make a POST request to the cTrader API

    response = requests.post(API_ENDPOINT, headers={'Authorization': f'Bearer {API_TOKEN}'}, json=payload)

 

      # Print the response text for detailed information

    print(response.text)

 

    # Process the response and return the result

    if response.status_code == 200:

        # Trade execution successful

        return True

    else:

        # Trade execution failed

        return False

 

@app.route('/webhook', methods=['POST'])

def handle_webhook():

    request = app.current_request

    webhook_message = request.json_body

 

    symbol = webhook_message['ticker']

    side = 'buy'

    quantity = 1

 

    trade_execution_result = execute_trade(symbol, side, quantity)

 

    if trade_execution_result:

        return {'message': 'Trade executed successfully'}

    else:

        return {'message': 'Trade execution failed'}

 

# Setting optional client callbacks

client.setConnectedCallback(connected)

client.setDisconnectedCallback(disconnected)

client.setMessageReceivedCallback(onMessageReceived)

 

# Starting the client service

client.startService()

 

# Run




 


@daytrayding.andreas