def __init__(self): """ 初始化 """ self.strategy = config.strategy self.platform = config.accounts[0]["platform"] self.account = config.accounts[0]["account"] self.access_key = config.accounts[0]["access_key"] self.secret_key = config.accounts[0]["secret_key"] self.host = config.accounts[0]["host"] self.wss = config.accounts[0]["wss"] self.symbol = config.symbol self.contract_type = config.contract_type self.channels = config.markets[0]["channels"] self.orderbook_length = config.markets[0]["orderbook_length"] self.orderbooks_length = config.markets[0]["orderbooks_length"] self.klines_length = config.markets[0]["klines_length"] self.trades_length = config.markets[0]["trades_length"] self.market_wss = config.markets[0]["wss"] self.orderbook_invalid_seconds = 5 self.last_bid_price = 0 # 上次的买入价格 self.last_ask_price = 0 # 上次的卖出价格 self.last_orderbook_timestamp = 0 # 上次的orderbook时间戳 self.raw_symbol = self.symbol.split( '_')[0] if self.contract_type != 'SWAP' else self.symbol.split( '-')[0] self.ask1_price = 0 self.bid1_price = 0 self.ask1_volume = 0 self.bid1_volume = 0 if self.contract_type == "this_week": self.future_contract_code = self.symbol + "_CW" elif self.contract_type == "next_week": self.future_contract_code = self.symbol + "_NW" elif self.contract_type == "quarter": self.future_contract_code = self.symbol + "_CQ" # 交易模块 cc = { "strategy": self.strategy, "platform": self.platform, "symbol": self.symbol, "contract_type": self.contract_type, "account": self.account, "access_key": self.access_key, "secret_key": self.secret_key, "host": self.host, "wss": self.wss, "order_update_callback": self.on_event_order_update, "asset_update_callback": self.on_event_asset_update, "position_update_callback": self.on_event_position_update, "init_success_callback": self.on_event_init_success_callback, } self.trader = Trade(**cc) # 行情模块 cc = { "platform": self.platform, "symbols": [self.future_contract_code], "channels": self.channels, "orderbook_length": self.orderbook_length, "orderbooks_length": self.orderbooks_length, "klines_length": self.klines_length, "trades_length": self.trades_length, "wss": self.market_wss, "orderbook_update_callback": self.on_event_orderbook_update, "kline_update_callback": self.on_event_kline_update, "trade_update_callback": self.on_event_trade_update } self.market = Market(**cc) # 60秒执行1次 LoopRunTask.register(self.on_ticker, 60)
def __init__(self): """ 初始化 """ self.strategy = config.strategy self.platform = config.accounts[0]["platform"] self.account = config.accounts[0]["account"] self.access_key = config.accounts[0]["access_key"] self.secret_key = config.accounts[0]["secret_key"] self.host = config.accounts[0]["host"] self.wss = config.accounts[0]["wss"] self.future_platform = config.accounts[1]["platform"] self.future_account = config.accounts[1]["account"] self.future_access_key = config.accounts[1]["access_key"] self.future_secret_key = config.accounts[1]["secret_key"] self.future_host = config.accounts[1]["host"] self.future_wss = config.accounts[1]["wss"] self.symbol = config.markets[0]["symbol"] self.channels = config.markets[0]["channels"] self.orderbook_length = config.markets[0]["orderbook_length"] self.orderbooks_length = config.markets[0]["orderbooks_length"] self.klines_length = config.markets[0]["klines_length"] self.trades_length = config.markets[0]["trades_length"] self.market_wss = config.markets[0]["wss"] self.future_symbol = config.markets[1]["symbol"] self.future_contract_type = config.markets[1]["contract_type"] self.future_channels = config.markets[1]["channels"] self.future_orderbook_length = config.markets[1]["orderbook_length"] self.future_orderbooks_length = config.markets[1]["orderbooks_length"] self.future_klines_length = config.markets[1]["klines_length"] self.future_trades_length = config.markets[1]["trades_length"] self.future_market_wss = config.markets[1]["wss"] self.orderbook_invalid_seconds = 100 # orderbook无效时间 self.spread = 1 # 价差设定 self.volume = 1 # 每次开仓数量 self.max_quantity = 10 # 最大仓位数量(多仓的最大仓位数量和空仓的最大数量) self.delta_limit = 1 # delta超过多少进行对冲 self.future_volume_usd = 100 # 交割合约面值 self.last_bid_price = 0 # 上次的买入价格 self.last_ask_price = 0 # 上次的卖出价格 self.last_orderbook_timestamp = 0 # 上次的orderbook时间戳 self.last_mark_price = 0 # 上次的标记价格 self.raw_symbol = self.symbol.split('-')[0] self.partition_symbol = self.symbol.split('-')[1] self.ask1_price = 0 self.bid1_price = 0 self.ask1_volume = 0 self.bid1_volume = 0 self.mark_price = 0 # 标记价格 # 交易模块 cc = { "strategy": self.strategy, "platform": self.platform, "symbol": self.symbol, "account": self.account, "access_key": self.access_key, "secret_key": self.secret_key, "host": self.host, "wss": self.wss, "order_update_callback": self.on_event_order_update, "asset_update_callback": self.on_event_asset_update, "position_update_callback": self.on_event_position_update, "init_success_callback": self.on_event_init_success_callback, } self.trader = Trade(**cc) future_cc = { "strategy": self.strategy, "platform": self.future_platform, "symbol": self.future_symbol, "contract_type": self.future_contract_type, "account": self.future_account, "access_key": self.future_access_key, "secret_key": self.future_secret_key, "host": self.future_host, "wss": self.future_wss, "order_update_callback": self.on_event_order_update_future, "asset_update_callback": self.on_event_asset_update_future, "position_update_callback": self.on_event_position_update_future, "init_success_callback": self.on_event_init_success_callback_future, } self.future_trader = Trade(**future_cc) # 行情模块 cc = { "platform": self.platform, "symbols": [self.symbol], "channels": self.channels, "orderbook_length": self.orderbook_length, "orderbooks_length": self.orderbooks_length, "klines_length": self.klines_length, "trades_length": self.trades_length, "wss": self.market_wss, "orderbook_update_callback": self.on_event_orderbook_update, "kline_update_callback": self.on_event_kline_update, "trade_update_callback": self.on_event_trade_update } self.market = Market(**cc) if self.future_contract_type == "this_week": self.future_contract_code = self.future_symbol + "_CW" elif self.future_contract_type == "next_week": self.future_contract_code = self.future_symbol + "_NW" elif self.future_contract_type == "quarter": self.future_contract_code = self.future_symbol + "_CQ" # 行情模块 market_cc = { "platform": self.future_platform, "symbols": [self.future_contract_code], "channels": self.future_channels, "orderbook_length": self.future_orderbook_length, "orderbooks_length": self.future_orderbooks_length, "klines_length": self.future_klines_length, "trades_length": self.future_trades_length, "wss": self.future_market_wss, "orderbook_update_callback": self.on_event_orderbook_update_future, "kline_update_callback": self.on_event_kline_update_future, "trade_update_callback": self.on_event_trade_update_future } self.future_market = Market(**market_cc) # 10秒执行1次 LoopRunTask.register(self.on_ticker, 10) # delta对冲 LoopRunTask.register(self.delta_hedging, 10)
def __init__(self): """ 初始化 """ self.strategy = config.strategy self.platform = config.accounts[0]["platform"] self.account = config.accounts[0]["account"] self.access_key = config.accounts[0]["access_key"] self.secret_key = config.accounts[0]["secret_key"] self.host = config.accounts[0]["host"] self.wss = config.accounts[0]["wss"] self.symbol = config.symbol self.contract_type = config.contract_type self.channels = config.markets[0]["channels"] self.orderbook_length = config.markets[0]["orderbook_length"] self.orderbooks_length = config.markets[0]["orderbooks_length"] self.klines_length = config.markets[0]["klines_length"] self.trades_length = config.markets[0]["trades_length"] self.market_wss = config.markets[0]["wss"] self.contract_size = 0.01 self.orderbook_invalid_seconds = 10 self.last_bid_price = 0 # 上次的买入价格 self.last_ask_price = 0 # 上次的卖出价格 self.last_orderbook_timestamp = 0 # 上次的orderbook时间戳 # self.raw_symbol = self.symbol.split('-')[0] self.ask1_price = 0 self.bid1_price = 0 self.ask1_volume = 0 self.bid1_volume = 0 self.tradeema = TradeEMA() # # 交易模块 # cc = { # "strategy": self.strategy, # "platform": self.platform, # "symbol": self.symbol, # "contract_type": self.contract_type, # "account": self.account, # "access_key": self.access_key, # "secret_key": self.secret_key, # "host": self.host, # "wss": self.wss, # "order_update_callback": self.on_event_order_update, # "asset_update_callback": self.on_event_asset_update, # "position_update_callback": self.on_event_position_update, # "init_success_callback": self.on_event_init_success_callback, # } # self.trader = Trade(**cc) # 行情模块 for s in self.symbol: logger.info(s) cc = { "platform": self.platform, "symbols": [s], "channels": self.channels, "orderbook_length": self.orderbook_length, "orderbooks_length": self.orderbooks_length, "klines_length": self.klines_length, "trades_length": self.trades_length, "wss": self.market_wss, "orderbook_update_callback": self.on_event_orderbook_update, "kline_update_callback": self.on_event_kline_update, "trade_update_callback": self.on_event_trade_update } market = Market(**cc) # 60秒执行1次 LoopRunTask.register(self.on_ticker, 10)
def __init__(self, **kwargs): """ 初始化 """ self.strategy = config.strategy self.platform = config.accounts[0]["platform"] self.account = config.accounts[0]["account"] self.access_key = config.accounts[0]["access_key"] self.secret_key = config.accounts[0]["secret_key"] self.host = config.accounts[0]["host"] self.wss = config.accounts[0]["wss"] self.contract_type = config.contract_type self.channels = config.markets[0]["channels"] self.orderbook_length = config.markets[0]["orderbook_length"] self.orderbooks_length = config.markets[0]["orderbooks_length"] self.klines_length = config.markets[0]["klines_length"] self.trades_length = config.markets[0]["trades_length"] self.market_wss = config.markets[0]["wss"] self.symbol = kwargs.get("symbol") self.contract_size = kwargs.get("contract_size") self.orderbook_invalid_seconds = 10 self.last_bid_price = 0 # 上次的买入价格 self.last_ask_price = 0 # 上次的卖出价格 self.last_orderbook_timestamp = 0 # 上次的orderbook时间戳 self.raw_symbol = self.symbol.split('-')[1].upper() self.ask1_price = 0 self.bid1_price = 0 self.ask1_volume = 0 self.bid1_volume = 0 self.total_count = 0 self.exceed_1s_count = 0 self.exceed_100ms_count = 0 self.exceed_50ms_count = 0 self.orderids = {} # 交易模块 cc = { "strategy": self.strategy, "platform": self.platform, "symbol": self.symbol, "contract_type": self.contract_type, "account": self.account, "access_key": self.access_key, "secret_key": self.secret_key, "host": self.host, "wss": self.wss, "order_update_callback": self.on_event_order_update, "asset_update_callback": self.on_event_asset_update, "position_update_callback": self.on_event_position_update, "init_success_callback": self.on_event_init_success_callback, } self.trader = Trade(**cc) # 行情模块 cc = { "platform": self.platform, "symbols": [self.symbol], "channels": self.channels, "orderbook_length": self.orderbook_length, "orderbooks_length": self.orderbooks_length, "klines_length": self.klines_length, "trades_length": self.trades_length, "wss": self.market_wss, "orderbook_update_callback": self.on_event_orderbook_update, "kline_update_callback": self.on_event_kline_update, "trade_update_callback": self.on_event_trade_update } self.market = Market(**cc) # 60秒执行1次 LoopRunTask.register(self.on_ticker, 2) LoopRunTask.register(self.on_showresult, 10)
def __init__(self): """ 初始化 """ self.strategy = config.strategy self.platform = config.accounts[0]["platform"] self.account = config.accounts[0]["account"] self.access_key = config.accounts[0]["access_key"] self.secret_key = config.accounts[0]["secret_key"] self.host = config.accounts[0]["host"] self.wss = config.accounts[0]["wss"] self.swap_platform = config.accounts[1]["platform"] self.swap_account = config.accounts[1]["account"] self.swap_access_key = config.accounts[1]["access_key"] self.swap_secret_key = config.accounts[1]["secret_key"] self.swap_host = config.accounts[1]["host"] self.swap_wss = config.accounts[1]["wss"] self.symbol = config.markets[0]["symbol"] self.channels = config.markets[0]["channels"] self.orderbook_length = config.markets[0]["orderbook_length"] self.orderbooks_length = config.markets[0]["orderbooks_length"] self.klines_length = config.markets[0]["klines_length"] self.trades_length = config.markets[0]["trades_length"] self.market_wss = config.markets[0]["wss"] self.swap_symbol = config.markets[1]["symbol"] self.swap_contract_type = config.markets[1]["contract_type"] self.swap_channels = config.markets[1]["channels"] self.swap_orderbook_length = config.markets[1]["orderbook_length"] self.swap_orderbooks_length = config.markets[1]["orderbooks_length"] self.swap_klines_length = config.markets[1]["klines_length"] self.swap_trades_length = config.markets[1]["trades_length"] self.swap_market_wss = config.markets[1]["wss"] self.orderbook_invalid_seconds = config.orderbook_invalid_seconds self.spread = config.spread self.volume = config.quantity self.max_quantity = config.max_quantity self.delta_limit = config.delta_limit self.swap_volume_usd = config.swap_volume_usd self.last_bid_price = 0 # 上次的买入价格 self.last_ask_price = 0 # 上次的卖出价格 self.last_orderbook_timestamp = 0 # 上次的orderbook时间戳 self.last_mark_price = 0 # 上次的标记价格 self.raw_symbol = self.symbol.split('-')[0] self.partition_symbol = self.symbol.split('-')[1] self.ask1_price = 0 self.bid1_price = 0 self.ask1_volume = 0 self.bid1_volume = 0 self.mark_price = 0 # 标记价格 # 交易模块 cc = { "strategy": self.strategy, "platform": self.platform, "symbol": self.symbol, "account": self.account, "access_key": self.access_key, "secret_key": self.secret_key, "host": self.host, "wss": self.wss, "order_update_callback": self.on_event_order_update, "asset_update_callback": self.on_event_asset_update, "position_update_callback": self.on_event_position_update, "init_success_callback": self.on_event_init_success_callback, } self.trader = Trade(**cc) swap_cc = { "strategy": self.strategy, "platform": self.swap_platform, "symbol": self.swap_symbol, "contract_type": self.swap_contract_type, "account": self.swap_account, "access_key": self.swap_access_key, "secret_key": self.swap_secret_key, "host": self.swap_host, "wss": self.swap_wss, "order_update_callback": self.on_event_order_update_swap, "asset_update_callback": self.on_event_asset_update_swap, "position_update_callback": self.on_event_position_update_swap, "init_success_callback": self.on_event_init_success_callback_swap, } self.swap_trader = Trade(**swap_cc) # 行情模块 cc = { "platform": self.platform, "symbols": [self.symbol], "channels": self.channels, "orderbook_length": self.orderbook_length, "orderbooks_length": self.orderbooks_length, "klines_length": self.klines_length, "trades_length": self.trades_length, "wss": self.market_wss, "orderbook_update_callback": self.on_event_orderbook_update, "kline_update_callback": self.on_event_kline_update, "trade_update_callback": self.on_event_trade_update } self.market = Market(**cc) # 行情模块 market_cc = { "platform": self.swap_platform, "symbols": [self.swap_symbol], "channels": self.swap_channels, "orderbook_length": self.swap_orderbook_length, "orderbooks_length": self.swap_orderbooks_length, "klines_length": self.swap_klines_length, "trades_length": self.swap_trades_length, "wss": self.swap_market_wss, "orderbook_update_callback": self.on_event_orderbook_update_swap, "kline_update_callback": self.on_event_kline_update_swap, "trade_update_callback": self.on_event_trade_update_swap } self.swap_market = Market(**market_cc) # 10秒执行1次 LoopRunTask.register(self.on_ticker, 10) # delta对冲 LoopRunTask.register(self.delta_hedging, 10)
from alpha import starter from alpha.utils import logger from alpha.utils.tools import ts_to_datetime_str import time from alpha.tasks import LoopRunTask, SingleTask import pdb def d(): pdb.set_trace() async def cur_ts(**kwargs): logger.info(ts_to_datetime_str()) if __name__ == '__main__': LoopRunTask.register(cur_ts, 1) starter.start()
def __init__(self): """ 初始化 """ print(config) self.strategy = config.strategy self.platform = config.accounts[0]["platform"] self.account = config.accounts[0]["account"] self.access_key = config.accounts[0]["access_key"] self.secret_key = config.accounts[0]["secret_key"] self.host = config.accounts[0]["host"] self.wss = config.accounts[0]["wss"] self.symbol = config.symbol self.lever_rate = config.lever_rate self.place_num = config.place_num self.contract_type = config.contract_type self.channels = config.markets[0]["channels"] self.orderbook_length = config.markets[0]["orderbook_length"] self.orderbook_step = config.markets[0]["orderbook_step"] self.orderbooks_length = config.markets[0]["orderbooks_length"] self.klines_length = config.markets[0]["klines_length"] self.klines_period = config.markets[0]["klines_period"] self.trades_length = config.markets[0]["trades_length"] self.market_wss = config.markets[0]["wss"] self.orderbook_invalid_seconds = 0.5 self.last_bid_price = 0 # 上次的买入价格 self.last_ask_price = 0 # 上次的卖出价格 self.last_orderbook_timestamp = 0 # 上次的orderbook时间戳 self.last_open_order_time = 0 #上次开单时间 self.raw_symbol = self.symbol.split('-')[0] self.ask1_price = 0 self.bid1_price = 0 self.ask1_volume = 0 self.bid1_volume = 0 self.periods = [5, 10] self.trade_init_result = False # rest api if self.platform == HUOBI_SWAP: from alpha.platforms.swap.huobi_swap_api import HuobiSwapRestAPI self._rest_api = HuobiSwapRestAPI(self.host, self.access_key, self.secret_key) elif self.platform == HUOBI_DELIVERY: from alpha.platforms.delivery.huobi_delivery_api import HuobiDeliveryRestAPI self._rest_api = HuobiDeliveryRestAPI(self.host, self.access_key, self.secret_key) # 交易模块 cc = { "strategy": self.strategy, "platform": self.platform, "symbol": self.symbol, "contract_type": self.contract_type, "account": self.account, "access_key": self.access_key, "secret_key": self.secret_key, "host": self.host, "wss": self.wss, "rest_api": self._rest_api, "order_update_callback": self.on_event_order_update, "asset_update_callback": self.on_event_asset_update, "position_update_callback": self.on_event_position_update, "init_success_callback": self.on_event_init_trade_success_callback, } self.trader = Trade(**cc) # 行情模块 cc = { "platform": self.platform, "symbol": self.symbol, "contract_type": self.contract_type, "channels": self.channels, "orderbook_length": self.orderbook_length, "orderbook_step": self.orderbook_step, "orderbooks_length": self.orderbooks_length, "klines_length": self.klines_length, "klines_period": self.klines_period, "trades_length": self.trades_length, "wss": self.market_wss, "rest_api": self._rest_api, "orderbook_update_callback": self.on_event_orderbook_update, "kline_update_callback": self.on_event_kline_update, "trade_update_callback": self.on_event_trade_update, } self.market = Market(**cc) # 1秒执行1次 LoopRunTask.register(self.on_ticker, 5)
def __init__(self): """ 初始化 """ self.strategy = config.strategy #just the name self.platform = config.accounts[0]["platform"] self.account = config.accounts[0]["account"] self.access_key = config.accounts[0]["access_key"] self.secret_key = config.accounts[0]["secret_key"] self.host = config.accounts[0]["host"] self.wss = config.accounts[0]["wss"] self.symbol = config.symbol self.contract_type = config.contract_type self.channels = config.markets[0]["channels"] self.orderbook_length = config.markets[0]["orderbook_length"] self.orderbooks_length = config.markets[0]["orderbooks_length"] self.klines_length = config.markets[0]["klines_length"] self.trades_length = config.markets[0]["trades_length"] self.market_wss = config.markets[0]["wss"] self.contract_size = 0.01 self.orderbook_invalid_seconds = 10 self.last_bid_price = 0 # 上次的买入价格 self.last_ask_price = 0 # 上次的卖出价格 self.last_orderbook_timestamp = 0 # 上次的orderbook时间戳 self.raw_symbol = self.symbol.split('-')[0] self.ask1_price = 0 self.bid1_price = 0 self.ask1_volume = 0 self.bid1_volume = 0 self.client_orderid = 0 # Trade module 交易模块 cc = { "strategy": self.strategy, "platform": self.platform, "symbol": self.symbol, "contract_type": self.contract_type, "account": self.account, "access_key": self.access_key, "secret_key": self.secret_key, "host": self.host, "wss": self.wss, "order_update_callback": self.on_event_order_update, "asset_update_callback": self.on_event_asset_update, "position_update_callback": self.on_event_position_update, "init_success_callback": self.on_event_init_success_callback, } self.trader = Trade(**cc) # quotes module 行情模块 cc = { "platform": self.platform, "symbols": [self.symbol], "channels": self.channels, "orderbook_length": self.orderbook_length, "orderbooks_length": self.orderbooks_length, "klines_length": self.klines_length, "trades_length": self.trades_length, "wss": self.market_wss, "orderbook_update_callback": self.on_event_orderbook_update, "kline_update_callback": self.on_event_kline_update, "trade_update_callback": self.on_event_trade_update } self.market = Market(**cc) # 60秒执行1次 LoopRunTask.register(self.on_ticker, 1)