Início
/
Exemplos de estratégias
Ver no GitHub
Lorenzo SuperScalp Strategy
This scalping strategy combines RSI, Bollinger Bands and MACD. It buys when RSI is below 45, price is near the lower band and MACD crosses up. It sells when RSI is above 55, price is near the upper band and MACD crosses down. A minimum number of bars between trades prevents rapid re-entry.
Details
Entry Criteria :
Long : RSI < 45 && Close < LowerBand * 1.02 && MACD crosses above signal.
Short : RSI > 55 && Close > UpperBand * 0.98 && MACD crosses below signal.
Long/Short : Both.
Exit Criteria : Opposite signal.
Stops : None.
Default Values :
RSI Length = 14
Bollinger Length = 20
Bollinger Multiplier = 2
MACD Fast = 12
MACD Slow = 26
MACD Signal = 9
Min Bars = 15
Filters :
Category: Trend following
Direction: Both
Indicators: Multiple
Stops: No
Complexity: Medium
Timeframe: Short-term
Seasonality: No
Neural networks: No
Divergence: No
Risk level: Medium
using System;
using Ecng.Common;
using StockSharp.Algo.Indicators;
using StockSharp.Algo.Strategies;
using StockSharp.Messages;
namespace StockSharp.Samples.Strategies;
public class LorenzoSuperScalpStrategy : Strategy
{
private readonly StrategyParam<int> _rsiLength;
private readonly StrategyParam<int> _bbLength;
private readonly StrategyParam<DataType> _candleType;
public int RsiLength { get => _rsiLength.Value; set => _rsiLength.Value = value; }
public int BbLength { get => _bbLength.Value; set => _bbLength.Value = value; }
public DataType CandleType { get => _candleType.Value; set => _candleType.Value = value; }
public LorenzoSuperScalpStrategy()
{
_rsiLength = Param(nameof(RsiLength), 14).SetGreaterThanZero();
_bbLength = Param(nameof(BbLength), 20).SetGreaterThanZero();
_candleType = Param(nameof(CandleType), TimeSpan.FromMinutes(5).TimeFrame());
}
/// <inheritdoc />
protected override void OnStarted2(DateTime time)
{
base.OnStarted2(time);
var rsi = new RelativeStrengthIndex { Length = RsiLength };
var bb = new BollingerBands { Length = BbLength, Width = 2m };
var lastSignal = DateTimeOffset.MinValue;
var cooldown = TimeSpan.FromMinutes(360);
var subscription = SubscribeCandles(CandleType);
subscription
.BindEx(rsi, bb, (candle, rsiVal, bbVal) =>
{
if (candle.State != CandleStates.Finished)
return;
if (!rsiVal.IsFormed || !bbVal.IsFormed)
return;
var r = rsiVal.ToDecimal();
var bbTyped = (BollingerBandsValue)bbVal;
if (bbTyped.UpBand is not decimal upper || bbTyped.LowBand is not decimal lower)
return;
if (candle.OpenTime - lastSignal < cooldown)
return;
if (r < 45m && candle.ClosePrice <= lower && Position <= 0)
{
BuyMarket();
lastSignal = candle.OpenTime;
}
else if (r > 55m && candle.ClosePrice >= upper && Position >= 0)
{
SellMarket();
lastSignal = candle.OpenTime;
}
})
.Start();
var area = CreateChartArea();
if (area != null)
{
DrawCandles(area, subscription);
DrawIndicator(area, bb);
DrawOwnTrades(area);
}
}
}
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, BollingerBands
from StockSharp.Algo.Strategies import Strategy
class lorenzo_super_scalp_strategy(Strategy):
def __init__(self):
super(lorenzo_super_scalp_strategy, self).__init__()
self._rsi_length = self.Param("RsiLength", 14) \
.SetGreaterThanZero()
self._bb_length = self.Param("BbLength", 20) \
.SetGreaterThanZero()
self._candle_type = self.Param("CandleType", DataType.TimeFrame(TimeSpan.FromMinutes(5)))
self._last_signal_ticks = 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(lorenzo_super_scalp_strategy, self).OnReseted()
self._last_signal_ticks = 0
def OnStarted2(self, time):
super(lorenzo_super_scalp_strategy, self).OnStarted2(time)
self._last_signal_ticks = 0
self._rsi = RelativeStrengthIndex()
self._rsi.Length = self._rsi_length.Value
self._bb = BollingerBands()
self._bb.Length = self._bb_length.Value
self._bb.Width = 2
subscription = self.SubscribeCandles(self.candle_type)
subscription.BindEx(self._rsi, self._bb, self.OnProcess).Start()
def OnProcess(self, candle, rsi_val, bb_val):
if candle.State != CandleStates.Finished:
return
if not rsi_val.IsFormed or not bb_val.IsFormed:
return
r = float(rsi_val)
bb_upper = bb_val.UpBand
bb_lower = bb_val.LowBand
if bb_upper is None or bb_lower is None:
return
upper = float(bb_upper)
lower = float(bb_lower)
close = float(candle.ClosePrice)
cooldown_ticks = TimeSpan.FromMinutes(360).Ticks
current_ticks = candle.OpenTime.Ticks
if current_ticks - self._last_signal_ticks < cooldown_ticks:
return
if r < 45.0 and close <= lower and self.Position <= 0:
self.BuyMarket()
self._last_signal_ticks = current_ticks
elif r > 55.0 and close >= upper and self.Position >= 0:
self.SellMarket()
self._last_signal_ticks = current_ticks
def CreateClone(self):
return lorenzo_super_scalp_strategy()