BTCUSD 調整可能SLTP戦略
この戦略はSMA(10)とSMA(25)のクロスオーバーにEMA(150)フィルターを組み合わせてBTCUSDを取引します。ロングエントリーはプルバックを待ちます:クロスオーバー後に戻り率が追跡され、価格がそのレベルを再度上抜けるとロングポジションが建てられます。ショートエントリーは価格がEMAを下回っている間に弱気クロスオーバーが発生した時点で即座に実行されます。
エグジットは調整可能なテイクプロフィット、ストップロス、ブレイクイーブン距離を使用します。EMA(150)の下でSMA(10)がSMA(25)を下抜ける場合もロングポジションは決済されます。
詳細
- エントリー条件:
- ロング:SMA(10)がSMA(25)を上抜け、その後価格が設定パーセンテージ分戻り、戻りレベルを再上抜け。
- ショート:EMA(150)の下でSMA(10)がSMA(25)を下抜け。
- ロング/ショート: ロングとショート。
- エグジット条件:
- 設定可能なテイクプロフィット、ストップロス、ブレイクイーブン距離。
- EMA(150)の下でSMA(10)がSMA(25)を下抜けた際にロング決済。
- ストップ: あり(ポイント単位で調整可能)。
- デフォルト値:
FastSmaLength= 10SlowSmaLength= 25EmaFilterLength= 150TakeProfitDistance= 1000StopLossDistance= 250BreakEvenTrigger= 500RetracementPercentage= 0.01
- フィルター:
- カテゴリ: トレンドフォロー
- 方向: ロング & ショート
- インジケーター: SMA, EMA
- ストップ: あり
- 複雑さ: 中程度
- 時間軸: 任意
- 季節性: いいえ
- ニューラルネットワーク: いいえ
- ダイバージェンス: いいえ
- リスクレベル: 中
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>
/// BTCUSD strategy with adjustable SL/TP using SMA crossover.
/// Enters long on golden cross above EMA filter, short on death cross below EMA filter.
/// </summary>
public class BtcusdAdjustableSltpStrategy : Strategy
{
private readonly StrategyParam<int> _fastSmaLength;
private readonly StrategyParam<int> _slowSmaLength;
private readonly StrategyParam<DataType> _candleType;
private decimal _prevFast;
private decimal _prevSlow;
public int FastSmaLength { get => _fastSmaLength.Value; set => _fastSmaLength.Value = value; }
public int SlowSmaLength { get => _slowSmaLength.Value; set => _slowSmaLength.Value = value; }
public DataType CandleType { get => _candleType.Value; set => _candleType.Value = value; }
public BtcusdAdjustableSltpStrategy()
{
_fastSmaLength = Param(nameof(FastSmaLength), 120)
.SetGreaterThanZero()
.SetDisplay("Fast SMA", "Length of fast SMA", "Indicators");
_slowSmaLength = Param(nameof(SlowSmaLength), 450)
.SetGreaterThanZero()
.SetDisplay("Slow SMA", "Length of slow SMA", "Indicators");
_candleType = Param(nameof(CandleType), TimeSpan.FromMinutes(1).TimeFrame())
.SetDisplay("Candle Type", "Type of candles", "General");
}
/// <inheritdoc />
public override IEnumerable<(Security sec, DataType dt)> GetWorkingSecurities()
{
return [(Security, CandleType)];
}
/// <inheritdoc />
protected override void OnReseted()
{
base.OnReseted();
_prevFast = 0m;
_prevSlow = 0m;
}
/// <inheritdoc />
protected override void OnStarted2(DateTime time)
{
base.OnStarted2(time);
var fastSma = new SimpleMovingAverage { Length = FastSmaLength };
var slowSma = new SimpleMovingAverage { Length = SlowSmaLength };
var subscription = SubscribeCandles(CandleType);
subscription
.Bind(fastSma, slowSma, ProcessCandle)
.Start();
var area = CreateChartArea();
if (area != null)
{
DrawCandles(area, subscription);
DrawIndicator(area, fastSma);
DrawIndicator(area, slowSma);
DrawOwnTrades(area);
}
}
private void ProcessCandle(ICandleMessage candle, decimal fast, decimal slow)
{
if (candle.State != CandleStates.Finished)
return;
if (_prevFast == 0m || _prevSlow == 0m)
{
_prevFast = fast;
_prevSlow = slow;
return;
}
if (_prevFast <= _prevSlow && fast > slow && Position <= 0)
{
BuyMarket();
}
else if (_prevFast >= _prevSlow && fast < slow && Position >= 0)
{
SellMarket();
}
_prevFast = fast;
_prevSlow = slow;
}
}
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 SimpleMovingAverage
from StockSharp.Algo.Strategies import Strategy
class btcusd_adjustable_sltp_strategy(Strategy):
def __init__(self):
super(btcusd_adjustable_sltp_strategy, self).__init__()
self._fast_sma_length = self.Param("FastSmaLength", 120) \
.SetGreaterThanZero() \
.SetDisplay("Fast SMA", "Length of fast SMA", "Indicators")
self._slow_sma_length = self.Param("SlowSmaLength", 450) \
.SetGreaterThanZero() \
.SetDisplay("Slow SMA", "Length of slow SMA", "Indicators")
self._candle_type = self.Param("CandleType", DataType.TimeFrame(TimeSpan.FromMinutes(1))) \
.SetDisplay("Candle Type", "Type of candles", "General")
self._prev_fast = 0.0
self._prev_slow = 0.0
@property
def candle_type(self):
return self._candle_type.Value
@candle_type.setter
def candle_type(self, value):
self._candle_type.Value = value
def OnReseted(self):
super(btcusd_adjustable_sltp_strategy, self).OnReseted()
self._prev_fast = 0.0
self._prev_slow = 0.0
def OnStarted2(self, time):
super(btcusd_adjustable_sltp_strategy, self).OnStarted2(time)
fast_sma = SimpleMovingAverage()
fast_sma.Length = self._fast_sma_length.Value
slow_sma = SimpleMovingAverage()
slow_sma.Length = self._slow_sma_length.Value
subscription = self.SubscribeCandles(self.candle_type)
subscription.Bind(fast_sma, slow_sma, self.OnProcess).Start()
area = self.CreateChartArea()
if area is not None:
self.DrawCandles(area, subscription)
self.DrawIndicator(area, fast_sma)
self.DrawIndicator(area, slow_sma)
self.DrawOwnTrades(area)
def OnProcess(self, candle, fast_val, slow_val):
if candle.State != CandleStates.Finished:
return
fast_v = float(fast_val)
slow_v = float(slow_val)
if self._prev_fast == 0 or self._prev_slow == 0:
self._prev_fast = fast_v
self._prev_slow = slow_v
return
if self._prev_fast <= self._prev_slow and fast_v > slow_v and self.Position <= 0:
self.BuyMarket()
elif self._prev_fast >= self._prev_slow and fast_v < slow_v and self.Position >= 0:
self.SellMarket()
self._prev_fast = fast_v
self._prev_slow = slow_v
def CreateClone(self):
return btcusd_adjustable_sltp_strategy()