Skip to content

Repository files navigation

这是用于服务器调试交易代码的repo

开仓逻辑

"""target挂单价格的倒推计算逻辑,固定年化收益倒推""" target_annual_return = 0.24 fee = 0.0015 days = remaining_days F = future_bid_price K = strike_price S = index_price

long open 的情况:买入合成多头,卖出交割合约,价差= 交割合约 - 合成多头

x = call_bid_price leverage = index_price/(x - put_bid1_price) synthetic_long_price = x - put_bid1_price + K

求解x的表达式

( F / synthetic_long_price - 1) * leverage = target_annual_return/365*days target_bid_price = x - fee

short open 的情况:卖出合成空头,买入交割合约,价差= 交割合约 - 合成多头

F = future_ask_price x = put_bid_price leverage = index_price/(x - call_bid1_price) synthetic_short_price = call_bid1_price - x + K (synthetic_short_price / F - 1) * leverage = target_annual_return/365*days target_bid_price = x - fee

仓位管理逻辑

要确保记录同一对冲组合 数据结构 self.position_hedge_mgr = "option_maker_cid": { "option_maker_cid": option_maker_cid, datetime "trade_direction": trade_direction, "status": status, "expiration_date": expiration_date, strike, btc_sopt_price "option_maker":{"symbol":symbol, "filled_qty":filled_qty, "filled_avg_price":filled_avg_price, "side":side}, "option_hedge":{"symbol":symbol, "filled_qty":filled_qty, "filled_avg_price":filled_avg_price, "side":side}, "future_hedge":{"symbol":symbol, "filled_qty":filled_qty, "filled_avg_price":filled_avg_price, "side":side}, "open_spread":open_spread, "open_cost"= open_cost, "open_annual_return":open_annual_return,

}

更新逻辑 on_order -- option_maker filled/partially_filled -- 初始化 position_mgr/更新option_maker,status = need_hedge -- option_hedge filled -- 更新option_hedge -- future_hedge filled -- 更新future_hedge,status = fully_hedged

其他计算方法 status = need_hedge / fully_hedged trade_direction = long_open / short_open

实际开仓spread价差收益(btc计价) if status == fully_hedged if long_open open_spread_price = long_open_spread = future_filled_avg_price / btc_sopt_price - option_maker_filled_avg_price - option_hedge_filled_avg_price - strike / btc_sopt_price else open_spread_price = short_open_spread = option_hedge_filled_avg_price - option_maker_filled_avg_price - future_filled_avg_price / btc_sopt_price + strike / btc_sopt_price

之后风控监控current_spread_price 和 open_spread_price 有没有扩大

open_spread = (open_spread_price - fee) * filled_qty

open_cost = option_maker 花出去的权利金 - option_hedge 得到的权利金 open_annual_return = open_spread/open_cost /remain days *365

on stop要保存self.position_hedge_mgr到本地,下次重启策略加载回来

order type

option_maker, option_hedge, future_hedge, reverse_close, option_close_maker, option_close_hedge, future_close_hedge

订单管理器

        self.order_mgr[cid] = {
                'id': "",
                'cid': cid,
                'symbol': signal['symbol'],
                'side': 'Buy',
                'price': price,
                'quantity': signal['order_quantity'],
                'filled': 0.0,  # 初始化已成交数量
                'filled_avg_price': -1.0, # 初始化成交价格
                'status': 'pend', # 初始化状态
                'order_type': 'option_maker', # 初始化类型
                'trade_direction': signal['trade_direction'], # 初始化交易方向,这些字段只有option_maker需要
                'strike_price': signal['strike_price'], # 初始化行权价,这些字段只有option_maker需要
                'expiration': signal['expiration'] # 初始化到期日,这些字段只有option_maker需要
            }

仓位管理器(交易所api获取的)

self.positions[symbol] = { 'symbol' = symbol, 'instrument' = future/option, 'amount' = amount, 'mark_price' = mark_price, 'usd_amount' = usd_amount, (只有future有 usd_amount = amount * mark_price,去和 position_hedge_mgr的usd amount去做对比) 'entry_price' = entry_price 'margin' = margin 'unrealized_pnl' = unrealized_pnl 'side' = side (long/short) }

QmRB7ftQ4qhMp7b6RMip8LlW8fp4j_rNtfNsNCljspY

break even point的合成combo的usd敞口

盈亏平衡点的期权平均开仓价格*做多/空btc个数 = usd敞口,用usd敞口去对冲等量usd amount的交割合约,实操上不完全相等没事,因为交割合约10usd四舍五入误差,感觉误差100usd以内没关系 盈亏平衡点bep F = X + C - P (X是strike price) 这个F就是期权平均开仓价格, 合成多头pnl = F - X - C + P 合成空头pnl = X - F + C - P pnl = 0 时,F = X + C - P

如果 C P 为btc计价: F = X + F(C - P) F = X / (1 - C + P)

新:目标挂单价计算公式,盈利计算公式

期权组合的usd价格,也就是平均开仓价格 就等于bep (X是strike price) bep = X / (1 - C + P) 相当于以F usd这个价格买入/卖出价值F *合约数量usd的btc 交割合约那边得以交割合约价格卖出/买入相同usd价值的btc bep和交割合约的价差就是我们套利的价差,为什么不直接用bep?

leverage = 1/abs(C - P) synthetic long目标挂单价满足,求解C (F / bep - 1) * leverage = target_annual_return/365*days (F(1 - C + P) / X - 1) / (C - P) = R

synthetic short目标挂单价满足,求解P (bep / F - 1) * leverage = target_annual_return/365*days (X / F(1 - C + P) - 1) / (P - C) = R

交易所request 统计

交易所限频每秒最多5个请求 positions_update_check_interval =60s 60秒获取一次仓位信息 account_summary_check_interval = 2 # 账户摘要检查间隔2秒 order_state_check 10秒获取一次交易所订单信息,之后可能会增加一两次查询单个订单 最低大约1秒钟 37/60个 请求

开单限频 1秒1个单,撤单不受限

About

backup code testing on my own aws server

Resources

Stars

Watchers

Forks

Releases

Packages

Contributors

Languages