new_row = { "dt": kline_start['end_dt'], "date_str": DataMatrixAPI.datetime_to_str(DataMatrixAPI.milli_timestamp_to_datetime(kline_start['end_dt'])), "next_1min_ret": lead_rets[0], "next_2min_ret": lead_rets[1], "next_5min_ret": lead_rets[2], "next_10min_ret": lead_rets[3], "next_30min_ret": lead_rets[4], "next_60min_ret": lead_rets[5], "arc30": arc30, "arc60": arc60, "arc120": arc120, "arc240": arc240, "vrc30": vrc30, "vrc60": vrc60, "vrc120": vrc120, "vrc240": vrc240 } await self.add_row(new_row) async def on_orderbook_update_callback(self, orderbook: Orderbook): ... async def on_trade_update_callback(self, trade: Trade): ... async def on_ticker_update_callback(self, ticker: Ticker): ... async def on_order_update_callback(self, order: Order): ... async def on_fill_update_callback(self, fill: Fill): ... async def on_position_update_callback(self, position: Position): ... async def on_asset_update_callback(self, asset: Asset): ... if __name__ == '__main__': default_main(DataMatrixDemo)
# -*- coding:utf-8 -*- """ huobi_future行情采集模块 Project: alphahunter Author: HJQuant Description: Asynchronous driven quantitative trading framework """ from quant.collect import Collect from quant.startup import default_main if __name__ == '__main__': default_main(Collect)
logger.info("trade:", trade, caller=self) async def on_ticker_update_callback(self, ticker: Ticker): """ 市场行情tick更新 """ logger.info("ticker:", ticker, caller=self) async def on_order_update_callback(self, order: Order): """ 订单状态更新 """ logger.info("order:", order, caller=self) async def on_fill_update_callback(self, fill: Fill): """ 订单成交通知 """ logger.info("fill:", fill, caller=self) async def on_position_update_callback(self, position: Position): """ 持仓更新 """ logger.info("position:", position, caller=self) async def on_asset_update_callback(self, asset: Asset): """ 账户资产更新 """ logger.info("asset:", asset, caller=self) if __name__ == '__main__': default_main(DemoStrategy)
prev_kline = None return new_kline, prev_kline def handle_documents(self, documents): sum_amount = 0 sum_volume = 0 for document in documents: sum_amount += document["volume"]*document["tradeprice"] sum_volume += document["volume"] return { "avg_price": sum_amount / sum_volume, "sum_volume": sum_volume, "sum_amount": sum_amount, } async def on_state_update_callback(self, state: State, **kwargs): ... async def on_kline_update_callback(self, kline: Kline): ... async def on_orderbook_update_callback(self, orderbook: Orderbook): ... async def on_trade_update_callback(self, trade: Trade): ... async def on_ticker_update_callback(self, ticker: Ticker): ... async def on_order_update_callback(self, order: Order): ... async def on_fill_update_callback(self, fill: Fill): ... async def on_position_update_callback(self, position: Position): ... async def on_asset_update_callback(self, asset: Asset): ... if __name__ == '__main__': default_main(klinesrv)
async def on_position_update_callback(self, position: Position): ... async def on_order_update_callback(self, order: Order): """ 订单状态更新 """ logger.info("order:", order, caller=self) async def on_fill_update_callback(self, fill: Fill): """ 订单成交通知 """ logger.info("fill:", fill, caller=self) async def submit_orders(self, delta_position): """ 根据当前最新的delta_position来执行下单操作 """ delta = tools.nearest(delta_position['BTC'], self.syminfo.size_tick) #下单量 if abs(delta) < 0.01: #原因是现货买入交易手续费从'货'中扣,极为恶心的设计 return if delta > 0: #做多 s, e = await self.create_order(self.gw, self.symbols[0], ORDER_ACTION_BUY, self.last_kline.close+100, abs(delta)) #限价单模拟市价单 if e: logger.error("error:", e, caller=self) elif delta < 0: #做空 s, e = await self.create_order(self.gw, self.symbols[0], ORDER_ACTION_SELL, self.last_kline.close-100, abs(delta)) #限价单模拟市价单 if e: logger.error("error:", e, caller=self) if __name__ == '__main__': default_main(CTAMultiTimeframeStrategy)
... #策略进程运行过程中如果收到某些错误通知,可以根据实际情况判断,比如可以做一些策略善后工作,然后结束本策略进程 return async def on_kline_update_callback(self, kline: Kline): ... async def on_orderbook_update_callback(self, orderbook: Orderbook): ... async def on_trade_update_callback(self, trade: Trade): ... async def on_ticker_update_callback(self, ticker: Ticker): ... async def on_order_update_callback(self, order: Order): ... async def on_fill_update_callback(self, fill: Fill): ... async def on_position_update_callback(self, position: Position): ... async def on_asset_update_callback(self, asset: Asset): ... if __name__ == '__main__': default_main(SymInfoWriter)
async def on_fill_update_callback(self, fill: Fill): """ 订单成交通知 """ logger.info("fill:", fill, caller=self) async def submit_orders(self, delta_position): """ 根据当前最新的delta_position来执行下单操作 """ delta = tools.nearest(delta_position['BTC'], self.syminfo.size_tick) #下单量 if abs(delta) < 0.01: #原因是现货买入交易手续费从'货'中扣,极为恶心的设计 return if delta > 0: #做多 s, e = await self.create_order(self.gw, self.symbols[0], ORDER_ACTION_BUY, self.last_kline.close + 100, abs(delta)) #限价单模拟市价单 if e: logger.error("error:", e, caller=self) elif delta < 0: #做空 s, e = await self.create_order(self.gw, self.symbols[0], ORDER_ACTION_SELL, self.last_kline.close - 100, abs(delta)) #限价单模拟市价单 if e: logger.error("error:", e, caller=self) if __name__ == '__main__': default_main(CTAMultiSignalStrategy)
""" logger.info("fill:", fill, caller=self) async def on_asset_update_callback(self, asset: Asset): """ 账户资产更新 """ logger.info("asset:", asset, caller=self) balance = asset.assets #e.g. {"BTC": {"free": 1.1, "locked": 2.2, "total": 3.3}, ... } if asset.platform == self.platform_main: #获取[主交易所]的资产信息 self.eth_available_main = balance["ETH"]["free"] self.usdt_available_main = balance["USDT"]["free"] if not self.eth_initial_main: self.eth_initial_main = self.eth_available_main #策略开始时的初始资产 if not self.usdt_initial_main: self.usdt_initial_main = self.usdt_available_main #策略开始时的初始资产 elif asset.platform == self.platform_reference: #获取[从交易所]的资产信息 self.eth_available_reference = balance["ETH"]["free"] self.usdt_available_reference = balance["USDT"]["free"] if not self.eth_initial_reference: self.eth_initial_reference = self.eth_available_reference #策略开始时的初始资产 if not self.usdt_initial_reference: self.usdt_initial_reference = self.usdt_available_reference #策略开始时的初始资产 async def on_position_update_callback(self, position: Position): ... if __name__ == '__main__': default_main(CarryBrickStrategy)
async def on_fill_update_callback(self, fill: Fill): """ 订单成交通知 """ logger.info("fill:", fill, caller=self) async def submit_orders(self, delta_position): """ 根据当前最新的delta_position来执行下单操作 """ delta = tools.nearest(delta_position['BTC'], self.syminfo.size_tick) #下单量 if abs(delta) < 0.01: #原因是现货买入交易手续费从'货'中扣,极为恶心的设计 return if delta > 0: #做多 s, e = await self.create_order(self.gw, self.symbols[0], ORDER_ACTION_BUY, self.last_kline.close + 100, abs(delta)) #限价单模拟市价单 if e: logger.error("error:", e, caller=self) elif delta < 0: #做空 s, e = await self.create_order(self.gw, self.symbols[0], ORDER_ACTION_SELL, self.last_kline.close - 100, abs(delta)) #限价单模拟市价单 if e: logger.error("error:", e, caller=self) if __name__ == '__main__': default_main(CTAStrategy)
await asyncio.sleep(1) for sym in self.symbols: #开始建立索引 t_depth = self.t_depth_map[sym] s, e = await t_depth.create_index({'dt':1}) if e: logger.error("create_index depth:", e, caller=self) t_trade = self.t_trade_map[sym] s, e = await t_trade.create_index({'dt':1}) if e: logger.error("create_index trade:", e, caller=self) t_kline = self.t_kline_map[sym] s, e = await t_kline.create_index({'begin_dt':1}) if e: logger.error("create_index kline:", e, caller=self) #结束进程 self.stop() async def on_state_update_callback(self, state: State, **kwargs): ... async def on_kline_update_callback(self, kline: Kline): ... async def on_orderbook_update_callback(self, orderbook: Orderbook): ... async def on_trade_update_callback(self, trade: Trade): ... async def on_ticker_update_callback(self, ticker: Ticker): ... async def on_order_update_callback(self, order: Order): ... async def on_fill_update_callback(self, fill: Fill): ... async def on_position_update_callback(self, position: Position): ... async def on_asset_update_callback(self, asset: Asset): ... if __name__ == '__main__': default_main(CreateIndex)