教程:智能执行器策略
我们将在本教程中创建一个交易应用程序,它接收来自 python API 的简单命令来调整账户风险。 使用 Python 生成信号并使用 Smart Executor 执行订单。
创建智能执行器策略的步骤
要创建灵活的策略,您需要执行以下操作:
- 决定交易哪个交易所。
- 添加您计划交易的品种或收听市场数据。 3.增加价格模型。
- 添加变量以生成信号。
- 添加模型。
- 添加策略。
- 创建python脚本来生成信号和发送命令。 查看更多详细信息 -> smart_executor 参考
Configure
我们使用 json 文件来配置交易应用程序。 该文件包含有关 api 密钥、日志路径和策略组件的信息。 请更新以下示例配置中的 API 密钥信息:
{
"instance": {
"license_id":"apifiny001",
"license_key":"apifiny123456",
"log_path": "./logs/strategy_demo",
"name": "strategy_demo"
},
"ib_gateway": {
"ip": "127.0.0.1",
"port": 4002
},
"servers": {
"redis_server": "127.0.0.1"
},
"apikeys": {
"BINANCE": {
"key": "",
"secret": ""
},
"BINANCEUS": {
"key": "",
"secret": ""
},
"OKEX": {
"key": "",
"secret": "",
"pass": ""
}
},
"fees": {
"BINANCE": {
"make": 0.0,
"take": 0.00045
},
"BINANCEUS": {
"make": 0.0,
"take": 0.000462
},
"OKEX": {
"make": 0.0,
"take": 0.0002
}
},
"exchanges":
[
{"exchange":"BINANCEUS","trade_type":"Direct","market_data_type":"Direct"}
],
"risk_formulas": [
["Port_Risk", ["RiskFormula", {"components": [[["BTCUSDT", "BINANCEUS"], 1.0]]}]]
],
"accounts": [
[10001, ["Account", {"risk_formulas": ["Port_Risk"], "id": 10001}]]
],
"symbols": [
{"port": ["BTCUSDT", "BINANCEUS"], "cid": 10000}
],
"symbol_info": {
"BTCUSDT.BINANCEUS": {"ticksize": 0.01, "lotsize": 0.0001, "price_precision": 2.0, "min_order_size": 0.0001, "qty_precision": 6.0, "multiple": 1.0}
},
"players": [
["BTCUSDT.HUOBI_Player", ["CobJsonPlayer", {"port": ["BTCUSDT", "BINANCEUS"], "path": "/data/cc/prod/ccc_record_2"}]]
],
"samplers": [
["ts_basis", ["TimeSampler", {"halflife": 1, "msecs": 100}]]
],
"pricing_models": [
["BTCUSDT.HUOBI_midpx", ["MidPx", {"port": ["BTCUSDT", "BINANCEUS"]}]],
["BTCUSDT.HUOBI_Vwap", ["Vwap", {"port": ["BTCUSDT", "BINANCEUS"], "sampler": "ts_basis"}]]
],
"variables": [
["Zero", ["Const", {"value": 0.0}]]
],
"models": [
["Zero_m", ["LinearModel", {"variable": "Zero"}]]
],
"strategies": [
["BTCUSDT.BINANCEUS", ["CCEventMakerStrategy", {"symbol": "BTCUSDT", "trade_market": "BINANCEUS", "account": 10001, "dep_pm": "BTCUSDT.HUOBI_midpx", "model": "Zero_m", "VWap": "BTCUSDT.HUOBI_Vwap", "max_spread_bps": 5, "max_quote_frombbo_bps": 0.001, "update_quote_bps": 2, "quote_bps": 3, "allowed_bps": 5, "order_notional": 500, "max_notional": 2500, "max_risk": 2500, "cooloff": 100, "start_time": "00:30:00", "end_time": "23:59:59", "use_separate_logs": true, "close_mode": "none"}]]
]
}
Python Script
要创建功能性 Python 脚本,您将需要以下部分:
- 使用您的算法获取数据和处理。 我们在示例中提供 CCxtMarketData 作为获取原始数据的简单方法。
- 使用我们的 ExecutorApi 向我们的智能执行器发送命令。
CCxtMarketData
Field | Description | Mandatory |
---|---|---|
exchange | The exchange you need data from. | Yes |
symbol_coin | The symbol coin you need data. | Yes |
symbol_basecoin | The basecoin of the symbol you need data. | Yes |
Function | Parameters | Mandatory |
---|---|---|
get_price | NA | Yes |
get_ema | sample time interval, halflife | Yes |
get_candle_data | sample time interval | Yes |
ExecutorApi
Field | Description | Mandatory |
---|---|---|
exchange | The exchange you need data from. | Yes |
symbol_coin | The symbol coin you need data. | Yes |
symbol_basecoin | The basecoin of the symbol you need data. | Yes |
Commands
Function | Description | Parameters |
---|---|---|
send_cmd_maker | Send position change you want, accumulate on smart executor side, and execute by making order. | instance name, trading direction, target position level |
send_cmd_taker | Send the position level you want, calculate order quantity on smart executor side, and execute by taking order. | instance name, target position level |
您只需通过 send_cmd_maker 发送仓位增减指令,Smart Executor 会自动汇总您的所有指令并执行;
Example
from apifiny.smart.ExecutorApi import ExecutorApi as ea
from apifiny.smart.examples.CcxtMarketData import CcxtMarketData as md
def strategy_live_demo(exchange, symbol_coin,symbol_basecoin,instance,targetLevel,interval,thresh,totalLevel):
commander = ea(exchange,symbol_coin,symbol_basecoin)
data= md(exchange, symbol_coin,symbol_basecoin)
count = 0
while True:
try:
data.get_price()
#test.get_candle_data("1m") #1m, 5m, 1h, 1d #By default you will have 500 metrics (if available)
data_long = data.get_ema("1m",5)
data_short = data.get_ema("1m",1)
ema_long = data_long['Close'][data_long['Close'].size-1]
ema_short = data_short['Close'][data_short['Close'].size-1]
except:
print("lost connection from ccxt")
time.sleep(interval)
continue
if ema_long < ema_short and totalLevel < 1:
if totalLevel < 0 and (abs(ema_long - ema_short)/data.mid) > thresh*0.0001:
print("thresh check: ",{(ema_long - ema_short)/data.mid}, " total level: ", totalLevel)
print("buy back: ema_long: ",{ema_long}, " ema_short: ", {ema_short}, "thresh", {thresh})
commander.send_cmd_maker(instance, "buy", targetLevel)
count += 1
totalLevel += targetLevel
elif totalLevel >= 0 and (abs(ema_long - ema_short)/data.mid) > thresh*(1+totalLevel/targetLevel)*0.0001:
print("thresh check: ",{(ema_long - ema_short)/data.mid}, " total level: ", totalLevel)
print("buy more: ema_long: ",{ema_long}, " ema_short: ", {ema_short}, "thresh", {thresh*(1+abs(totalLevel)/targetLevel)*0.0001})
commander.send_cmd_maker(instance, "buy", targetLevel)
count += 1
totalLevel += targetLevel
elif ema_long > ema_short and totalLevel > -1:
if totalLevel <= 0 and (abs(ema_long - ema_short)/data.mid) > thresh*(1+abs(totalLevel)/targetLevel)*0.0001:
print("thresh check: ",{(ema_long - ema_short)/data.mid}, " total level: ", totalLevel)
print("sell more: ema_long: ",{ema_long}, " ema_short: ", {ema_short}, "thresh", {thresh*(1+abs(totalLevel)/targetLevel)*0.0001})
commander.send_cmd_maker(instance, "sell", targetLevel)
count += 1
totalLevel -= targetLevel
elif totalLevel > 0 and (abs(ema_long - ema_short)/data.mid) > thresh*0.0001:
print("thresh check: ",{(ema_long - ema_short)/data.mid}, " total level: ", totalLevel)
print("sell back: ema_long: ",{ema_long}, " ema_short: ", {ema_short}, "thresh", {thresh})
commander.send_cmd_maker(instance, "sell", targetLevel)
count += 1
totalLevel -= targetLevel
time.sleep(interval)