import pandas
from op_forex.model.bar_model import BarModel
from op_forex.model.symbol_info import SymbolInfo
from op_forex.strategy.template_strategy import TemplateStrategy
from tqz_extern.tqz_constant import StrategyDirect
from op_forex.model.renko_block_model import RenkoBlockModel, RenkoColor
from op_forex.model.renko_wave_model import RenkoWaveModel
class MultiPeriodRenkoStrategy1(TemplateStrategy):
author: str = 'Erwin'
def __init__(self, fast_windows: int, slow_windows: int, daily_fast_windows: int, daily_slow_windows: int, renko_ticks: int):
super().__init__()
self.fast_windows: int = fast_windows
self.slow_windows: int = slow_windows
self.daily_fast_windows: int = daily_fast_windows
self.daily_slow_windows: int = daily_slow_windows
self.renko_ticks: int = renko_ticks
# self.atr_range_multi: int = 3
self.hour_bars_df = pandas.DataFrame(columns=['time', 'high', 'open', 'low', 'close', 'volume'])
self.daily_bars_df = None
self.his_renko_blocks: [RenkoBlockModel] = []
self.his_renko_waves: [RenkoWaveModel] = []
self.start_renko_price: float = None
self.renko_size: float = None
self.lots: float = 0
def on_init(self, symbol_info: SymbolInfo, strategy_direct: StrategyDirect):
self.strategy_direct = strategy_direct
self.symbol_info = symbol_info
self.renko_size = self.symbol_info.trade_tick_size * self.renko_ticks
self.lots = self.symbol_info.volume_min
def on_bar(self, bar: BarModel = None):
self.hour_bars_df.loc[len(self.hour_bars_df)] = [bar.time, bar.high, bar.open, bar.low, bar.close, bar.volume]
if len(self.hour_bars_df) < self.slow_windows:
return
if len(self.daily_bars_df) < self.daily_slow_windows:
return
# - calculate part.
fast_ma_value = self.hour_bars_df[-self.fast_windows:].close.mean()
slow_ma_value = self.hour_bars_df[-self.slow_windows:].close.mean()
daily_fast_ma_value = self.daily_bars_df[-self.daily_fast_windows:].close.mean()
daily_slow_ma_value = self.daily_bars_df[-self.daily_slow_windows:].close.mean()
# atr_value = (self.hour_bars_df[-self.slow_windows:].high - self.hour_bars_df[-self.slow_windows:].low).mean() * self.atr_range_multi
# - update renko_block, renko_wave.
self.update_renko_block_renko_wave(cur_bar=bar)
if len(self.his_renko_blocks) < 2:
return
if len(self.his_renko_waves) < 3: # 如果是空间背驰, 则 self.his_renko_waves 至少有四个元素;
return
cur_renko, pre_renko = self.his_renko_blocks[-1], self.his_renko_blocks[-2]
cur_wave, pre_wave = self.his_renko_waves[-1], self.his_renko_waves[-3]
if daily_fast_ma_value > daily_slow_ma_value: # 日线多头
if self.pos < 0:
self.buy(symbol=self.symbol_info.name, date_time=bar.time, price=bar.close, lots=abs(self.pos))
if fast_ma_value > slow_ma_value:
if 0 == self.pos:
if RenkoColor.GREEN == pre_renko.renko_color and RenkoColor.RED == cur_renko.renko_color: # 低点产生
if pre_wave.renko_wave_price < cur_wave.renko_wave_price: # 下降段 价格背驰
self.buy(symbol=self.symbol_info.name, date_time=bar.time, price=bar.close, lots=self.lots)
elif self.pos > 0:
if RenkoColor.RED == pre_renko.renko_color and RenkoColor.GREEN == cur_renko.renko_color: # 高点产生
if pre_wave.renko_wave_price > cur_wave.renko_wave_price: # 上涨段 价格背驰
self.sell(symbol=self.symbol_info.name, date_time=bar.time, price=bar.close, lots=self.pos)
elif daily_fast_ma_value < daily_slow_ma_value:
if self.pos > 0:
self.sell(symbol=self.symbol_info.name, date_time=bar.time, price=bar.close, lots=self.pos)
if fast_ma_value < slow_ma_value:
if 0 == self.pos:
if RenkoColor.RED == pre_renko.renko_color and RenkoColor.GREEN == cur_renko.renko_color: # 高点
if pre_wave.renko_wave_price > cur_wave.renko_wave_price: # 上涨段 价格背驰
self.sell(symbol=self.symbol_info.name, date_time=bar.time, price=bar.close, lots=self.lots)
elif self.pos < 0:
if RenkoColor.GREEN == pre_renko.renko_color and RenkoColor.RED == cur_renko.renko_color: # 低点
if pre_wave.renko_wave_price < cur_wave.renko_wave_price: # 下降段价格背驰
self.buy(symbol=self.symbol_info.name, date_time=bar.time, price=bar.close, lots=abs(self.pos))
def on_daily_bar(self, daily_bars_df: pandas.DataFrame = None):
self.daily_bars_df = daily_bars_df
def on_stop(self):
pass
def update_renko_block_renko_wave(self, cur_bar: BarModel):
if 0 == len(self.his_renko_blocks):
if self.start_renko_price is None:
self.start_renko_price = cur_bar.close
else:
if (self.start_renko_price + self.renko_size) <= cur_bar.close: # new red renko
self.his_renko_blocks.append(
RenkoBlockModel(
renko_price=self.start_renko_price + ((cur_bar.close - self.start_renko_price) / self.renko_size) * self.renko_size,
renko_color=RenkoColor.RED,
renko_size=self.renko_size
)
)
elif (self.start_renko_price - self.renko_size) >= cur_bar.close: # new green renko
self.his_renko_blocks.append(
RenkoBlockModel(
renko_price=self.start_renko_price - ((self.start_renko_price - cur_bar.close) / self.renko_size) * self.renko_size,
renko_color=RenkoColor.GREEN,
renko_size=self.renko_size
)
)
else:
last_renko: RenkoBlockModel = self.his_renko_blocks[-1]
if RenkoColor.RED == last_renko.renko_color:
if (last_renko.renko_price + self.renko_size) <= cur_bar.close: # new red renko
self.his_renko_blocks.append(
RenkoBlockModel(
renko_price=last_renko.renko_price + ((cur_bar.close - last_renko.renko_price) / self.renko_size) * self.renko_size,
renko_color=RenkoColor.RED,
renko_size=self.renko_size
)
)
elif (last_renko.renko_price - 2 * self.renko_size) >= cur_bar.close: # new green renko
self.his_renko_waves.append(
RenkoWaveModel(
renko_wave_price=last_renko.renko_price,
renko_color=RenkoColor.RED,
renko_size=self.renko_size
)
)
self.his_renko_blocks.append(
RenkoBlockModel(
renko_price=last_renko.renko_price - ((last_renko.renko_price - cur_bar.close) / self.renko_size) * self.renko_size,
renko_color=RenkoColor.GREEN,
renko_size=self.renko_size
)
)
elif RenkoColor.GREEN == last_renko.renko_color:
if (last_renko.renko_price + 2 * self.renko_size) <= cur_bar.close: # new red renko
self.his_renko_waves.append(
RenkoWaveModel(
renko_wave_price=last_renko.renko_price,
renko_color=RenkoColor.GREEN,
renko_size=self.renko_size
)
)
self.his_renko_blocks.append(
RenkoBlockModel(
renko_price=last_renko.renko_price + ((cur_bar.close - last_renko.renko_price) / self.renko_size) * self.renko_size,
renko_color=RenkoColor.RED,
renko_size=self.renko_size
)
)
elif (last_renko.renko_price - self.renko_size) >= cur_bar.close: # new green renko
self.his_renko_blocks.append(
RenkoBlockModel(
renko_price=last_renko.renko_price - ((last_renko.renko_price - cur_bar.close) / self.renko_size) * self.renko_size,
renko_color=RenkoColor.GREEN,
renko_size=self.renko_size
)
)
量化交易之One Piece篇 - Forex multi_period_renko_strategy1
最新推荐文章于 2024-08-26 08:01:26 发布