再描画なし Renko エミュレーション戦略
終値を使ってRenkoブロックをエミュレートし、再描画なしでパターン転換に基づいてトレードする。
詳細
- エントリー条件:
- 新しいブロックが形成された後、前ブロックの方向と価格シーケンスが上昇継続を示す場合にロング。
- 逆のシーケンスでショート。
- ロング/ショート: 両方。
- エグジット条件: ブロックの方向が反転した際にポジションを決済。
- ストップ: いいえ。
- デフォルト値:
BrickSize= 3CandleType= TimeSpan.FromMinutes(1)
- フィルター:
- カテゴリ: パターン
- 方向: 両方
- インジケーター: なし
- ストップ: いいえ
- 複雑さ: 中級
- 時間軸: 任意
- 季節性: いいえ
- ニューラルネットワーク: いいえ
- ダイバージェンス: いいえ
- リスクレベル: 中
using System;
using System.Collections.Generic;
using Ecng.Common;
using StockSharp.Algo.Strategies;
using StockSharp.BusinessEntities;
using StockSharp.Messages;
namespace StockSharp.Samples.Strategies;
/// <summary>
/// Non-repainting Renko emulation strategy.
/// Renko bricks of BrickSize price units are built from finished candle closes only, so a brick never changes once formed.
/// When a new brick continues the direction of the previous brick the strategy enters in that direction (reversing an
/// opposite position); when a new brick reverses the direction, the open position is closed.
/// </summary>
public class NonRepaintingRenkoEmulationStrategy : Strategy
{
private readonly StrategyParam<decimal> _brickSize;
private readonly StrategyParam<DataType> _candleType;
private decimal? _brickLevel;
private int _prevDirection;
/// <summary>
/// Brick size in price units.
/// </summary>
public decimal BrickSize
{
get => _brickSize.Value;
set => _brickSize.Value = value;
}
/// <summary>
/// Candle type.
/// </summary>
public DataType CandleType
{
get => _candleType.Value;
set => _candleType.Value = value;
}
/// <summary>
/// Constructor.
/// </summary>
public NonRepaintingRenkoEmulationStrategy()
{
_brickSize = Param(nameof(BrickSize), 3m)
.SetGreaterThanZero()
.SetDisplay("Brick Size", "Brick size in price units", "Renko");
_candleType = Param(nameof(CandleType), TimeSpan.FromMinutes(1).TimeFrame())
.SetDisplay("Candle Type", "Type of candles to use", "General");
}
/// <inheritdoc />
public override IEnumerable<(Security sec, DataType dt)> GetWorkingSecurities()
{
return [(Security, CandleType)];
}
/// <inheritdoc />
protected override void OnReseted()
{
base.OnReseted();
_brickLevel = null;
_prevDirection = 0;
}
/// <inheritdoc />
protected override void OnStarted2(DateTime time)
{
base.OnStarted2(time);
_brickLevel = null;
_prevDirection = 0;
var subscription = SubscribeCandles(CandleType);
subscription
.Bind(ProcessCandle)
.Start();
var area = CreateChartArea();
if (area != null)
{
DrawCandles(area, subscription);
DrawOwnTrades(area);
}
}
private void ProcessCandle(ICandleMessage candle)
{
if (candle.State != CandleStates.Finished)
return;
var close = candle.ClosePrice;
if (_brickLevel is not decimal level)
{
_brickLevel = close;
return;
}
var bricks = (int)Math.Floor(Math.Abs(close - level) / BrickSize);
if (bricks == 0)
return;
var direction = close > level ? 1 : -1;
_brickLevel = level + direction * bricks * BrickSize;
// With several bricks in one candle the previous brick has the same direction.
var prevDirection = bricks > 1 ? direction : _prevDirection;
_prevDirection = direction;
if (prevDirection == 0 || !IsFormedAndOnlineAndAllowTrading())
return;
if (direction == prevDirection)
{
if (direction > 0 && Position <= 0)
BuyMarket(Volume + Math.Abs(Position));
else if (direction < 0 && Position >= 0)
SellMarket(Volume + Math.Abs(Position));
}
else if (direction < 0 && Position > 0)
SellMarket(Position);
else if (direction > 0 && Position < 0)
BuyMarket(-Position);
}
}
import clr
clr.AddReference("StockSharp.Messages")
clr.AddReference("StockSharp.Algo")
clr.AddReference("StockSharp.Algo.Strategies")
import math
from System import TimeSpan, Decimal
from StockSharp.Messages import DataType, CandleStates
from StockSharp.Algo.Strategies import Strategy
class non_repainting_renko_emulation_strategy(Strategy):
"""
Non-repainting Renko emulation strategy.
Renko bricks of BrickSize price units are built from finished candle closes only, so a brick never changes once formed.
When a new brick continues the direction of the previous brick the strategy enters in that direction (reversing an
opposite position); when a new brick reverses the direction, the open position is closed.
"""
def __init__(self):
super(non_repainting_renko_emulation_strategy, self).__init__()
self._brick_size = self.Param("BrickSize", 3.0).SetGreaterThanZero().SetDisplay("Brick Size", "Brick size in price units", "Renko")
self._candle_type = self.Param("CandleType", DataType.TimeFrame(TimeSpan.FromMinutes(1))).SetDisplay("Candle Type", "Type of candles to use", "General")
self._reset_state()
@property
def candle_type(self):
return self._candle_type.Value
def _reset_state(self):
self._brick_level = None
self._prev_direction = 0
def OnReseted(self):
super(non_repainting_renko_emulation_strategy, self).OnReseted()
self._reset_state()
def OnStarted2(self, time):
super(non_repainting_renko_emulation_strategy, self).OnStarted2(time)
self._reset_state()
subscription = self.SubscribeCandles(self.candle_type)
subscription.Bind(self._process_candle).Start()
area = self.CreateChartArea()
if area is not None:
self.DrawCandles(area, subscription)
self.DrawOwnTrades(area)
def _process_candle(self, candle):
if candle.State != CandleStates.Finished:
return
close = candle.ClosePrice
if self._brick_level is None:
self._brick_level = close
return
level = self._brick_level
brick = Decimal(self._brick_size.Value)
bricks = int(math.floor(float(abs(close - level) / brick)))
if bricks == 0:
return
direction = 1 if close > level else -1
self._brick_level = level + Decimal(direction * bricks) * brick
# With several bricks in one candle the previous brick has the same direction.
prev_direction = direction if bricks > 1 else self._prev_direction
self._prev_direction = direction
if prev_direction == 0 or not self.IsFormedAndOnlineAndAllowTrading():
return
if direction == prev_direction:
if direction > 0 and self.Position <= 0:
self.BuyMarket(self.Volume + abs(self.Position))
elif direction < 0 and self.Position >= 0:
self.SellMarket(self.Volume + abs(self.Position))
elif direction < 0 and self.Position > 0:
self.SellMarket(self.Position)
elif direction > 0 and self.Position < 0:
self.BuyMarket(-self.Position)
def CreateClone(self):
return non_repainting_renko_emulation_strategy()