Automating Your Strategy: How to Access Raw Market Data via API

avatar
· 阅读量 963

The Foundation of Algo-Trading

Before you can automate a strategy on FollowMe or any other platform, you need a reliable data feed. Visual charts are great for humans, but your algorithms need raw numbers—JSON data delivered via HTTP.

The "Query" Challenge

Advanced APIs often require you to send specific parameters to filter data (e.g., requesting only the last 2 candles of Gold). This is often done by embedding a JSON object into the URL. If you look at the code below, the test_url1 contains a long string starting with %7B. This is simply the machine-readable version of the configuration comments above it.

Python Implementation

Here is a functional script to pull the latest price action for USDJPY.

Python

import time

import requests # pip3 install requestsimport json

# Extra headers

test_headers = {

    'Content-Type' : 'application/json'

}

'''

github: https://github.com/alltick/rea...

Register for token: https://alltick.co/register

Website: https://alltick.co

 

code: Please check the code list to select the code you want to query

kline_type: kline type, 1 for 1-min K, 2 for 5-min K, 3 for 15-min K, 4 for 30-min K, 5 for 1-hour K, 6 for 2-hour K, 7 for 4-hour K, 8 for daily K, 9 for weekly K, 10 for monthly K

query_kline_num: number of klines to query, max 1000

 

URL encode the following JSON and paste it into the query field of the http query string

{"trace" : "python_http_test1","data" : {"code" : "USDJPY","kline_type" : 1,"kline_timestamp_end" : 0,"query_kline_num" : 2,"adjust_type": 0}}

{"trace" : "python_http_test2","data" : {"symbol_list": [{"code": "GOLD"}]}}

{"trace" : "python_http_test3","data" : {"symbol_list": [{"code": "GOLD"}]}}

'''

test_url1 = 'https://quote.aatest.online/qu...

 

resp1 = requests.get(url=test_url1, headers=test_headers)

# Decoded text returned by the request

text1 = resp1.text

print(text1)

From Code to Signal

Once you receive text1, you parse it to get the latest Close price. If Close > Open, your bot triggers a buy; otherwise, it sells. This simple script is the cornerstone of automated execution.


Automating Your Strategy: How to Access Raw Market Data via API


风险提示:本文所述仅代表作者个人观点,不代表 Followme 的官方立场。Followme 不对内容的准确性、完整性或可靠性作出任何保证,对于基于该内容所采取的任何行为,不承担任何责任,除非另有书面明确说明。

喜欢的话,赞赏支持一下
回复 0

暂无评论,立马抢沙发

  • tradingContest