Read Market Data

Created at 09 Oct 2024, 06:43
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!
VI

viktor.rebeja

Joined 09.10.2024

Read Market Data
09 Oct 2024, 06:43


Hello  !

i use this code:

import requests

import json


 

# Define the endpoint URL for market data

BASE_URL = "https://demo.ctraderapi.com:5036"


 

# Set your access token here

ACCESS_TOKEN = "L6w2VRqhs2bwigCmISM_OHK8Do2i5rOxVp_3tCGYTCI"


 

# Set headers with the access token

headers = {

    "Authorization": f"Bearer {ACCESS_TOKEN}",

    "Content-Type": "application/json"

}


 

# Define the instruments you want to query

instruments = ["EURUSD", "GBPUSD", "AUDUSD"]  # Add more instruments as needed


 

# Function to fetch market data

import requests


 

def fetch_market_data(access_token):

    url = "https://demo.ctraderapi.com:5036/marketdata/EURUSD/quotes"

    headers = {

        "Authorization": f"Bearer {access_token}",

        "Content-Type": "application/json"

    }


 

    try:

        response = requests.get(url, headers=headers)

        response.raise_for_status()  # Raises an error for 4xx/5xx responses

        data = response.json()

        print("Market Data:", json.dumps(data, indent=4))

    except requests.exceptions.RequestException as e:

        print(f"An error occurred: {e}")


 

fetch_market_data("YOUR_ACCESS_TOKEN")

if __name__ == "__main__":

    fetch_market_data(instruments)

 

 

 but get errors like this:

sharpvinvest@cloudshell:~/ctraderAPI (sharpinvest-428908)$ python ctraderapi.py 
An error occurred: ('Connection aborted.', RemoteDisconnected('Remote end closed connection without response'))
An error occurred: ('Connection aborted.', RemoteDisconnected('Remote end closed connection without response'))
sharpvinvest@cloudshell:~/ctraderAPI (sharpinvest-428908)$ python ctraderapi.py 
An error occurred: ('Connection aborted.', RemoteDisconnected('Remote end closed connection without response'))
An error occurred: ('Connection aborted.', RemoteDisconnected('Remote end closed connection without response'))
sharpvinvest@cloudshell:~/ctraderAPI (sharpinvest-428908)$ python ctraderapi.py 
An error occurred: ('Connection aborted.', RemoteDisconnected('Remote end closed connection without response'))
An error occurred: ('Connection aborted.', RemoteDisconnected('Remote end closed connection without response'))
sharpvinvest@cloudshell:~/ctraderAPI (sharpinvest-428908)$ 


@viktor.rebeja
Replies

PanagiotisCharalampous
09 Oct 2024, 11:51

Hi there,

Where did you find this code and why do you think it should work? Why are you sending http requests to the server?

Best regards,

Panagiotis


@PanagiotisCharalampous

viktor.rebeja
09 Oct 2024, 14:00 ( Updated at: 10 Oct 2024, 06:52 )

RE: Read Market Data

PanagiotisCharalampous said: 

Hi there,

Where did you find this code and why do you think it should work? Why are you sending http requests to the server?

Best regards,

Panagiotis

Chat GPT write it.

I wonder if the end points: are working:

LiveDemolive.ctraderapi.com:5035 (for operating with Protobufs)demo.ctraderapi.com:5035 (for operating with Protobufs)live.ctraderapi.com:5036 (for operating with JSON)demo.ctraderapi.com:5036 (for operating with JSON)

I have many attemts to read some market data but I fail !
 


@viktor.rebeja

PanagiotisCharalampous
10 Oct 2024, 06:56

RE: RE: Read Market Data

Chat GPT write it.

:)

If you don't know how to code, it's better to assign the job to a professional developer. This code is nonsense.


@PanagiotisCharalampous