GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — ctpbee/examples/strategy/atr_strategy.py at master · ctpbee/ctpbee Expand file tree/ Copy pathatr_strategy.pyMore file actions 172 lines (157 loc) · 7.68 KB / Copy pathatr_strategy.pyFile metadata and controls172 lines (157 loc) · 7.68 KB 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 from ctpbee import CtpbeeApi from ctpbee.constant import BarData, ContractData, TickData from ctpbee.indicator.indicator import atr, sma from ctpbee.log import VLogger class ATRStrategy(CtpbeeApi): """基于平均真实波动幅度(ATR)的策略,结合均线和ATR止损""" atr_period = 14 sma_period = 20 stop_loss_multiplier = 2 # 止损倍数 take_profit_multiplier = 3 # 止盈倍数 logger = VLogger def __init__(self, name: str, code): super().__init__( name, ) self.instrument_set = set([code]) self.length = max(self.atr_period, self.sma_period) self.close = [] self.high = [] self.low = [] self.pos = 0 self.entry_price = 0 self.stop_loss = 0 self.take_profit = 0 self.init = False self.name = name self.logger.info( f"ATR策略初始化 - 合约: {code}, ATR周期: {self.atr_period}, SMA周期: {self.sma_period}, 止损倍数: {self.stop_loss_multiplier}, 止盈倍数: {self.take_profit_multiplier}", owner=self.name, ) def code(self): return list(self.instrument_set)[0] def on_contract(self, contract: ContractData) -> None: """合约回报: 把 local_symbol 形式补进 instrument_set。 INSTRUMENT_INDEPEND 过滤比较的是 event.data.local_symbol (如 "ag2612.SHFE"), 只存裸合约名会在开启该配置后漏掉全部行情。 """ if contract.symbol == self.code(): self.instrument_set.add(contract.local_symbol) def on_tick(self, tick: TickData) -> None: pass def on_bar(self, bar: BarData): if bar is None or not self.init: return self.logger.debug( f"收到K线数据 - {bar.local_symbol} {bar.datetime.strftime('%Y-%m-%d %H:%M:%S')} 最高价: {bar.high_price}, 最低价: {bar.low_price}, 收盘价: {bar.close_price}", owner=self.name, ) self.close.append(bar.close_price) self.high.append(bar.high_price) self.low.append(bar.low_price) if len(self.close) <= self.length * 2: self.logger.debug( f"数据不足,需要至少 {self.length * 2} 根K线,当前 {len(self.close)} 根", owner=self.name, ) return # 计算SMA和ATR current_sma = sma(self.close, self.sma_period)[-1] current_atr = atr(self.high, self.low, self.close, self.atr_period)[-1] recent_atr = atr(self.high, self.low, self.close, self.atr_period)[-10:] max_recent_atr = max(recent_atr) # 趋势判断:价格在SMA上方为上涨趋势,下方为下跌趋势 trend_up = bar.close_price > current_sma trend_down = bar.close_price < current_sma self.logger.debug( f"指标计算 - SMA: {current_sma:.2f}, ATR: {current_atr:.2f}, 最近10日最大ATR: {max_recent_atr:.2f}, 趋势: {'上涨' if trend_up else '下跌' if trend_down else '横盘'}", owner=self.name, ) # 交易逻辑 if self.pos == 0: # 买入信号:上涨趋势且ATR较小(波动性低) if trend_up and current_atr < max_recent_atr: self.logger.info( f"买入信号 - 上涨趋势且波动性降低: SMA {current_sma:.2f}, 价格 {bar.close_price:.2f}, ATR {current_atr:.2f} < 最近10日最大ATR {max_recent_atr:.2f}", owner=self.name, ) self.pos = 1 self.entry_price = bar.close_price self.stop_loss = ( self.entry_price - current_atr * self.stop_loss_multiplier ) self.take_profit = ( self.entry_price + current_atr * self.take_profit_multiplier ) self.logger.success( f"开仓买入 - 合约: {bar.local_symbol}, 价格: {bar.close_price}, 止损: {self.stop_loss:.2f}, 止盈: {self.take_profit:.2f}, 当前仓位: {self.pos}", owner=self.name, ) self.action.buy_open(bar.close_price, 1, bar) # 卖出信号:下跌趋势且ATR较小(波动性低) elif trend_down and current_atr < max_recent_atr: self.logger.info( f"卖出信号 - 下跌趋势且波动性降低: SMA {current_sma:.2f}, 价格 {bar.close_price:.2f}, ATR {current_atr:.2f} < 最近10日最大ATR {max_recent_atr:.2f}", owner=self.name, ) self.pos = -1 self.entry_price = bar.close_price self.stop_loss = ( self.entry_price + current_atr * self.stop_loss_multiplier ) self.take_profit = ( self.entry_price - current_atr * self.take_profit_multiplier ) self.logger.success( f"开仓卖出 - 合约: {bar.local_symbol}, 价格: {bar.close_price}, 止损: {self.stop_loss:.2f}, 止盈: {self.take_profit:.2f}, 当前仓位: {self.pos}", owner=self.name, ) self.action.sell_open(bar.close_price, 1, bar) elif self.pos == 1: # 多头止损和止盈 if bar.low_price <= self.stop_loss: self.logger.info( f"多头止损触发 - 合约: {bar.local_symbol}, 当前最低价: {bar.low_price:.2f} <= 止损价: {self.stop_loss:.2f}", owner=self.name, ) self.logger.success( f"平仓卖出 - 合约: {bar.local_symbol}, 价格: {bar.close_price}, 当前仓位: {self.pos} -> 0", owner=self.name, ) self.action.buy_close(bar.close_price, 1, bar) self.pos = 0 elif bar.high_price >= self.take_profit: self.logger.info( f"多头止盈触发 - 合约: {bar.local_symbol}, 当前最高价: {bar.high_price:.2f} >= 止盈价: {self.take_profit:.2f}", owner=self.name, ) self.logger.success( f"平仓卖出 - 合约: {bar.local_symbol}, 价格: {bar.close_price}, 当前仓位: {self.pos} -> 0", owner=self.name, ) self.action.buy_close(bar.close_price, 1, bar) self.pos = 0 elif self.pos == -1: # 空头止损和止盈 if bar.high_price >= self.stop_loss: self.logger.info( f"空头止损触发 - 合约: {bar.local_symbol}, 当前最高价: {bar.high_price:.2f} >= 止损价: {self.stop_loss:.2f}", owner=self.name, ) self.logger.success( f"平仓买入 - 合约: {bar.local_symbol}, 价格: {bar.close_price}, 当前仓位: {self.pos} -> 0", owner=self.name, ) self.action.sell_close(bar.close_price, 1, bar) self.pos = 0 elif bar.low_price <= self.take_profit: self.logger.info( f"空头止盈触发 - 合约: {bar.local_symbol}, 当前最低价: {bar.low_price:.2f} <= 止盈价: {self.take_profit:.2f}", owner=self.name, ) self.logger.success( f"平仓买入 - 合约: {bar.local_symbol}, 价格: {bar.close_price}, 当前仓位: {self.pos} -> 0", owner=self.name, ) self.action.sell_close(bar.close_price, 1, bar) self.pos = 0 |
| You are visitor 01402792 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |