Replies

daytrayding.andreas
03 Jul 2023, 11:27

RE:
Hi, I am struggling to fix my problem, do you have time to assist with email further? it sounds like you have done something similar with connecting the ctrader API to AWS serverless net server that receives json bracket data from tradingview with webhook. Or if you have other contacts that i can contact for getting the bot up and running, that would also be very helpful. Thanks for any replies
@daytrayding.andreas

daytrayding.andreas
23 Jun 2023, 08:23

RE:

Thank you very much! i appreciate all the effort you have put in to help me, i will try to implement your suggestions, and post if it worked. 

 


@daytrayding.andreas

daytrayding.andreas
22 Jun 2023, 07:53

RE:

Thanks! i appreciate your reply. I have tried to solve it with the code below, but i have very little experience in coding so i have maybe all i need in my code, but the structure is maybe totally off, i appreciate if you have time to take a look at what i have made and guide me further if possible.

For an understanding of what i am trying to accomplish, i have followed this youtube video for making an AWS chalice server and then used alpaca as broker, but know i am trying to implement ctrader into the app.py script. Link: https://www.youtube.com/watch?v=TKAo_Z-hzQs&t=1872s  

 

 

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 = "xxxxxxxx"

    request.clientSecret = "xxxxxxxxx"

    # 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.addErrback(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))

 

# 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

    }

 

Thanks for any replies

 

 

 

manelfx9530 said:

Follow their code in their github and you will find your answers. I say this as a guy that understand little of coding this type of stuff and was able to do it.

 

 

 


@daytrayding.andreas