下落時ナンピン買い戦略
下落時ナンピン買い戦略は、相対力指数(RSI)が定義された閾値を下回ったときに購入します。各シグナルが既存のロングポジションに追加され、エントリー価格を平均化します。終値が前のバーの高値を上回って突破したときに戦略は退出します。
詳細
- エントリー条件:
- RSI が
RsiBuyThresholdを下回る。
- RSI が
- ロング/ショート: ロングのみ。
- エグジット条件:
- 終値が前のバーの高値を超える。
- ストップ: なし。
- デフォルト値:
RsiLength= 10RsiBuyThreshold= 33
- フィルター:
- カテゴリ: 平均回帰
- 方向: ロング
- インジケーター: RSI
- ストップ: いいえ
- 複雑さ: 低
- 時間軸: 任意
- 季節性: いいえ
- ニューラルネットワーク: いいえ
- ダイバージェンス: いいえ
- リスクレベル: 低
using System;
using System.Collections.Generic;
using Ecng.Common;
using StockSharp.Algo.Indicators;
using StockSharp.Algo.Strategies;
using StockSharp.BusinessEntities;
using StockSharp.Messages;
namespace StockSharp.Samples.Strategies;
/// <summary>
/// Averaging down strategy.
/// Every candle that closes with RSI below RsiBuyThreshold buys another Volume, averaging the entry price of the long. The whole
/// long closes when a close exceeds the previous candle's high. Long only.
/// </summary>
public class AveragingDown2Strategy : Strategy
{
private readonly StrategyParam<int> _rsiLength;
private readonly StrategyParam<decimal> _rsiBuyThreshold;
private readonly StrategyParam<DataType> _candleType;
private decimal? _prevHigh;
/// <summary>
/// RSI period.
/// </summary>
public int RsiLength
{
get => _rsiLength.Value;
set => _rsiLength.Value = value;
}
/// <summary>
/// RSI level below which the strategy buys.
/// </summary>
public decimal RsiBuyThreshold
{
get => _rsiBuyThreshold.Value;
set => _rsiBuyThreshold.Value = value;
}
/// <summary>
/// Candle type.
/// </summary>
public DataType CandleType
{
get => _candleType.Value;
set => _candleType.Value = value;
}
/// <summary>
/// Constructor.
/// </summary>
public AveragingDown2Strategy()
{
_rsiLength = Param(nameof(RsiLength), 10)
.SetGreaterThanZero()
.SetDisplay("RSI Length", "RSI period", "Indicators");
_rsiBuyThreshold = Param(nameof(RsiBuyThreshold), 33m)
.SetDisplay("RSI Buy Threshold", "RSI level below which the strategy buys", "Signals");
_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();
_prevHigh = null;
}
/// <inheritdoc />
protected override void OnStarted2(DateTime time)
{
base.OnStarted2(time);
_prevHigh = null;
var rsi = new RelativeStrengthIndex { Length = RsiLength };
var subscription = SubscribeCandles(CandleType);
subscription
.Bind(rsi, ProcessCandle)
.Start();
var area = CreateChartArea();
if (area != null)
{
DrawCandles(area, subscription);
DrawOwnTrades(area);
var oscillators = CreateChartArea();
if (oscillators != null)
DrawIndicator(oscillators, rsi);
}
}
private void ProcessCandle(ICandleMessage candle, decimal rsi)
{
if (candle.State != CandleStates.Finished)
return;
var prevHigh = _prevHigh;
_prevHigh = candle.HighPrice;
if (prevHigh is not decimal ph)
return;
if (!IsFormedAndOnlineAndAllowTrading())
return;
if (Position > 0 && candle.ClosePrice > ph)
SellMarket(Position);
else if (rsi < RsiBuyThreshold)
BuyMarket(Volume);
}
}
import clr
clr.AddReference("StockSharp.Messages")
clr.AddReference("StockSharp.Algo")
clr.AddReference("StockSharp.Algo.Indicators")
clr.AddReference("StockSharp.Algo.Strategies")
from System import TimeSpan
from StockSharp.Messages import DataType, CandleStates
from StockSharp.Algo.Indicators import RelativeStrengthIndex
from StockSharp.Algo.Strategies import Strategy
class averaging_down2_strategy(Strategy):
"""
Averaging down strategy.
Every candle that closes with RSI below RsiBuyThreshold buys another Volume, averaging the entry price of the long. The whole
long closes when a close exceeds the previous candle's high. Long only.
"""
def __init__(self):
super(averaging_down2_strategy, self).__init__()
self._rsi_length = self.Param("RsiLength", 10) \
.SetGreaterThanZero() \
.SetDisplay("RSI Length", "RSI period", "Indicators")
self._rsi_buy_threshold = self.Param("RsiBuyThreshold", 33.0) \
.SetDisplay("RSI Buy Threshold", "RSI level below which the strategy buys", "Signals")
self._candle_type = self.Param("CandleType", DataType.TimeFrame(TimeSpan.FromMinutes(1))) \
.SetDisplay("Candle Type", "Type of candles to use", "General")
self._prev_high = None
@property
def CandleType(self):
return self._candle_type.Value
def GetWorkingSecurities(self):
return [(self.Security, self.CandleType)]
def OnReseted(self):
super(averaging_down2_strategy, self).OnReseted()
self._prev_high = None
def OnStarted2(self, time):
super(averaging_down2_strategy, self).OnStarted2(time)
self._prev_high = None
rsi = RelativeStrengthIndex()
rsi.Length = self._rsi_length.Value
subscription = self.SubscribeCandles(self.CandleType)
subscription.Bind(rsi, self._process_candle).Start()
area = self.CreateChartArea()
if area is not None:
self.DrawCandles(area, subscription)
self.DrawOwnTrades(area)
oscillators = self.CreateChartArea()
if oscillators is not None:
self.DrawIndicator(oscillators, rsi)
def _process_candle(self, candle, rsi_value):
if candle.State != CandleStates.Finished:
return
prev_high = self._prev_high
self._prev_high = float(candle.HighPrice)
if prev_high is None:
return
if not self.IsFormedAndOnlineAndAllowTrading():
return
if self.Position > 0 and float(candle.ClosePrice) > prev_high:
self.SellMarket(self.Position)
elif float(rsi_value) < float(self._rsi_buy_threshold.Value):
self.BuyMarket(self.Volume)
def CreateClone(self):
return averaging_down2_strategy()