如何将实时股市API数据转换为可用格式来计算技术指标?

问题描述 投票:0回答:2

我正在尝试使用 python 自动化交易策略。我已经有了天使经纪股票市场 API,它可以获取股票的开盘价、收盘价和收盘价。

问题是,默认情况下 API 会打印数据。我无法理解如何将此数据转换为可用格式(例如 DataFrames),从而相应地更新价格。

对于历史数据,我可以创建 DataFrame 来使用 Matplotlib、Plotly 绘制各种图表,并在某些情况下使用 TA-lib 或数学公式计算指标。

那么,如何将实时股市API数据转换为可用的格式来计算技术指标呢?

以下是API:

from smartapi import 
SmartWebSocket

# feed_token=092017047
FEED_TOKEN="YOUR_FEED_TOKEN"
CLIENT_CODE="YOUR_CLIENT_CODE"
# token="mcx_fo|224395"
token="EXCHANGE|TOKEN_SYMBOL"    
#SAMPLE: nse_cm|2885&nse_cm|1594&nse_cm|11536&nse_cm|3045
task="mw"   # mw|sfi|dp

ss = SmartWebSocket(FEED_TOKEN, CLIENT_CODE)

def on_message(ws, message):
    print("Ticks: {}".format(message))

def on_open(ws):
    print("on open")
    ss.subscribe(task,token)

def on_error(ws, error):
    print(error)

def on_close(ws):
    print("Close")

# Assign the callbacks.
ss._on_open = on_open
ss._on_message = on_message
ss._on_error = on_error
ss._on_close = on_close

ss.connect()

任何建议都有帮助。感谢您宝贵的时间。

python api stock trading
2个回答
0
投票

因为您有帐户,所以您可以访问该文档。在提问之前,请先阅读它并观看一些教程。由于存在帐户墙,我无法访问该文档。


0
投票

Answer1:您可以在代码中使用“Thread”和全局变量。 答案2:您使用 SmartApi.SmartConnect().ltpData(exchange, stocksymbol, symboltoken) 来获取 ohlc 值...

© www.soinside.com 2019 - 2024. All rights reserved.