自動トレンドライン戦略
直近のピボット高値と安値を結ぶことで動的なサポートとレジスタンスのトレンドラインを構築します。価格がレジスタンスラインを上回って終値をつけたときにロングシグナルが発生し、価格がサポートラインを下回ったときにショートシグナルが発火します。
詳細
- エントリー条件:
- ロング: 終値がレジスタンストレンドラインを上抜け。
- ショート: 終値がサポートトレンドラインを下抜け。
- エグジット条件:
- 反対シグナルまたはポジション転換。
- インジケーター:
- ピボットベースのトレンドライン。
- ストップ: なし。
- デフォルト値:
LeftBars= 100RightBars= 15
- フィルター:
- トレンドフォロー
- 単一時間軸
- インジケーター: ピボットトレンドライン
- ストップ: なし
- 複雑さ: 低
using System;
using System.Collections.Generic;
using Ecng.Common;
using StockSharp.Algo.Strategies;
using StockSharp.BusinessEntities;
using StockSharp.Messages;
namespace StockSharp.Samples.Strategies;
/// <summary>
/// Automatic trendlines strategy.
/// A pivot high is a high above the LeftBars candles before it and the RightBars candles after it, and a pivot low is the
/// mirror. The resistance line connects the last two pivot highs and the support line the last two pivot lows, both extended
/// to the current candle. A close crossing above resistance goes long and a close crossing below support goes short,
/// reversing an opposite position.
/// </summary>
public class AutomaticTrendlinesStrategy : Strategy
{
private readonly StrategyParam<int> _leftBars;
private readonly StrategyParam<int> _rightBars;
private readonly StrategyParam<DataType> _candleType;
private readonly List<(decimal high, decimal low)> _window = [];
private int _barIndex;
private (int index, decimal price)? _lastHigh;
private (int index, decimal price)? _prevHigh;
private (int index, decimal price)? _lastLow;
private (int index, decimal price)? _prevLow;
private decimal? _prevClose;
private decimal? _prevResistance;
private decimal? _prevSupport;
/// <summary>
/// Candles before a pivot it must exceed.
/// </summary>
public int LeftBars
{
get => _leftBars.Value;
set => _leftBars.Value = value;
}
/// <summary>
/// Candles after a pivot it must exceed.
/// </summary>
public int RightBars
{
get => _rightBars.Value;
set => _rightBars.Value = value;
}
/// <summary>
/// Candle type.
/// </summary>
public DataType CandleType
{
get => _candleType.Value;
set => _candleType.Value = value;
}
/// <summary>
/// Constructor.
/// </summary>
public AutomaticTrendlinesStrategy()
{
_leftBars = Param(nameof(LeftBars), 100)
.SetGreaterThanZero()
.SetDisplay("Left Bars", "Candles before a pivot it must exceed", "Pivots");
_rightBars = Param(nameof(RightBars), 15)
.SetGreaterThanZero()
.SetDisplay("Right Bars", "Candles after a pivot it must exceed", "Pivots");
_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();
ResetState();
}
private void ResetState()
{
_window.Clear();
_barIndex = -1;
_lastHigh = null;
_prevHigh = null;
_lastLow = null;
_prevLow = null;
_prevClose = null;
_prevResistance = null;
_prevSupport = null;
}
/// <inheritdoc />
protected override void OnStarted2(DateTime time)
{
base.OnStarted2(time);
ResetState();
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;
_barIndex++;
_window.Add((candle.HighPrice, candle.LowPrice));
var size = LeftBars + RightBars + 1;
if (_window.Count > size)
_window.RemoveAt(0);
if (_window.Count == size)
DetectPivots();
var close = candle.ClosePrice;
var resistance = LineValue(_prevHigh, _lastHigh);
var support = LineValue(_prevLow, _lastLow);
var prevClose = _prevClose;
var prevResistance = _prevResistance;
var prevSupport = _prevSupport;
_prevClose = close;
_prevResistance = resistance;
_prevSupport = support;
if (prevClose is not decimal pc)
return;
if (!IsFormedAndOnlineAndAllowTrading())
return;
var crossUp = resistance is decimal res && prevResistance is decimal prevRes && pc <= prevRes && close > res;
var crossDown = support is decimal sup && prevSupport is decimal prevSup && pc >= prevSup && close < sup;
if (crossUp && Position <= 0)
BuyMarket(Volume + Math.Abs(Position));
else if (crossDown && Position >= 0)
SellMarket(Volume + Math.Abs(Position));
}
private void DetectPivots()
{
var (pivotHigh, pivotLow) = _window[LeftBars];
var isHigh = true;
var isLow = true;
for (var i = 0; i < _window.Count; i++)
{
if (i == LeftBars)
continue;
if (_window[i].high >= pivotHigh)
isHigh = false;
if (_window[i].low <= pivotLow)
isLow = false;
}
var pivotIndex = _barIndex - RightBars;
if (isHigh)
{
_prevHigh = _lastHigh;
_lastHigh = (pivotIndex, pivotHigh);
}
if (isLow)
{
_prevLow = _lastLow;
_lastLow = (pivotIndex, pivotLow);
}
}
private decimal? LineValue((int index, decimal price)? first, (int index, decimal price)? second)
{
if (first is not (int i1, decimal p1) || second is not (int i2, decimal p2) || i2 == i1)
return null;
var slope = (p2 - p1) / (i2 - i1);
return p2 + slope * (_barIndex - i2);
}
}
import clr
clr.AddReference("StockSharp.Messages")
clr.AddReference("StockSharp.Algo")
clr.AddReference("StockSharp.Algo.Strategies")
from System import TimeSpan
from StockSharp.Messages import DataType, CandleStates
from StockSharp.Algo.Strategies import Strategy
class automatic_trendlines_strategy(Strategy):
"""
Automatic trendlines strategy.
A pivot high is a high above the LeftBars candles before it and the RightBars candles after it, and a pivot low is the
mirror. The resistance line connects the last two pivot highs and the support line the last two pivot lows, both extended
to the current candle. A close crossing above resistance goes long and a close crossing below support goes short,
reversing an opposite position.
"""
def __init__(self):
super(automatic_trendlines_strategy, self).__init__()
self._left_bars = self.Param("LeftBars", 100) \
.SetGreaterThanZero() \
.SetDisplay("Left Bars", "Candles before a pivot it must exceed", "Pivots")
self._right_bars = self.Param("RightBars", 15) \
.SetGreaterThanZero() \
.SetDisplay("Right Bars", "Candles after a pivot it must exceed", "Pivots")
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 CandleType(self):
return self._candle_type.Value
def GetWorkingSecurities(self):
return [(self.Security, self.CandleType)]
def _reset_state(self):
self._window = []
self._bar_index = -1
self._last_high = None
self._prev_high = None
self._last_low = None
self._prev_low = None
self._prev_close = None
self._prev_resistance = None
self._prev_support = None
def OnReseted(self):
super(automatic_trendlines_strategy, self).OnReseted()
self._reset_state()
def OnStarted2(self, time):
super(automatic_trendlines_strategy, self).OnStarted2(time)
self._reset_state()
subscription = self.SubscribeCandles(self.CandleType)
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
self._bar_index += 1
self._window.append((float(candle.HighPrice), float(candle.LowPrice)))
size = self._left_bars.Value + self._right_bars.Value + 1
if len(self._window) > size:
self._window.pop(0)
if len(self._window) == size:
self._detect_pivots()
close = float(candle.ClosePrice)
resistance = self._line_value(self._prev_high, self._last_high)
support = self._line_value(self._prev_low, self._last_low)
prev_close = self._prev_close
prev_resistance = self._prev_resistance
prev_support = self._prev_support
self._prev_close = close
self._prev_resistance = resistance
self._prev_support = support
if prev_close is None:
return
if not self.IsFormedAndOnlineAndAllowTrading():
return
cross_up = resistance is not None and prev_resistance is not None and prev_close <= prev_resistance and close > resistance
cross_down = support is not None and prev_support is not None and prev_close >= prev_support and close < support
if cross_up and self.Position <= 0:
self.BuyMarket(self.Volume + abs(self.Position))
elif cross_down and self.Position >= 0:
self.SellMarket(self.Volume + abs(self.Position))
def _detect_pivots(self):
left = self._left_bars.Value
pivot_high, pivot_low = self._window[left]
is_high = True
is_low = True
for i in range(len(self._window)):
if i == left:
continue
if self._window[i][0] >= pivot_high:
is_high = False
if self._window[i][1] <= pivot_low:
is_low = False
pivot_index = self._bar_index - self._right_bars.Value
if is_high:
self._prev_high = self._last_high
self._last_high = (pivot_index, pivot_high)
if is_low:
self._prev_low = self._last_low
self._last_low = (pivot_index, pivot_low)
def _line_value(self, first, second):
if first is None or second is None or first[0] == second[0]:
return None
slope = (second[1] - first[1]) / (second[0] - first[0])
return second[1] + slope * (self._bar_index - second[0])
def CreateClone(self):
return automatic_trendlines_strategy()