量化交易之One Piece篇 - Forex single_period_renko_strategy1

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 SinglePeriodRenkoStrategy1(TemplateStrategy):

    author: str = 'Erwin'

    def __init__(self, fast_windows: int, slow_windows: int, renko_ticks: int):
        super().__init__()

        self.fast_windows: int = fast_windows
        self.slow_windows: int = 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.his_renko_blocks: [RenkoBlockModel] = []
        self.his_renko_waves: [RenkoWaveModel] = []
        self.start_renko_price: float = None

        self.renko_size: float = None
        self.stop_loss_price: 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.lots = self.symbol_info.volume_min
        self.renko_size = self.symbol_info.trade_tick_size * self.renko_ticks


    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

        # - 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()

        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 fast_ma_value > 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))
                self.stop_loss_price = None

            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)
                        self.stop_loss_price = bar.close - atr_value

            elif self.pos > 0:
                if bar.low <= self.stop_loss_price:  # 扫到止损
                    self.sell(symbol=self.symbol_info.name, date_time=bar.time, price=self.stop_loss_price, lots=self.pos)
                    self.stop_loss_price = None
                elif 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)
                        self.stop_loss_price = None

        elif fast_ma_value < slow_ma_value:
            if self.pos > 0:
                self.sell(symbol=self.symbol_info.name, date_time=bar.time, price=bar.close, lots=self.pos)
                self.stop_loss_price = None

            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)
                        self.stop_loss_price = bar.close + atr_value

            elif self.pos < 0:
                if bar.high >= self.stop_loss_price:
                    self.buy(symbol=self.symbol_info.name, date_time=bar.time, price=self.stop_loss_price, lots=abs(self.pos))
                    self.stop_loss_price = None
                elif 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))
                        self.stop_loss_price = None


    def on_daily_bar(self, daily_bars_df: pandas.DataFrame = None):
        pass

    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
                        )
                    )

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值