Kositbablo 10
Multi-timeframe EURUSD strategy using RSI and EMA signals. It checks daily and hourly indicators and opens market orders when both trend filters align.
Parameters
- Take Profit – take profit in points.
- Stop Loss – stop loss in points.
- Turbo Mode – allow new trades even if a position exists.
Rules
- Go long when Daily RSI(11) < 60, Hourly RSI(5) < 48, and EMA20 > EMA2.
- Go short when Daily RSI(22) > 38, Hourly RSI(20) > 60, and EMA23 > EMA12.
- Trades only after the hourly candle is finished.
using System;
using System.Linq;
using System.Collections.Generic;
using Ecng.Common;
using Ecng.Collections;
using Ecng.Serialization;
using StockSharp.Algo.Indicators;
using StockSharp.Algo.Strategies;
using StockSharp.BusinessEntities;
using StockSharp.Messages;
namespace StockSharp.Samples.Strategies;
/// <summary>
/// Strategy based on RSI and EMA signals.
/// Buys when RSI is oversold and EMA cross is bearish (mean-reversion).
/// Sells when RSI is overbought and EMA cross is bearish.
/// </summary>
public class Kositbablo10Strategy : Strategy
{
private readonly StrategyParam<int> _rsiBuyPeriod;
private readonly StrategyParam<int> _rsiSellPeriod;
private readonly StrategyParam<int> _emaLongPeriod;
private readonly StrategyParam<int> _emaShortPeriod;
private readonly StrategyParam<decimal> _stopLossPct;
private readonly StrategyParam<DataType> _candleType;
/// <summary>
/// RSI period for buy signals.
/// </summary>
public int RsiBuyPeriod
{
get => _rsiBuyPeriod.Value;
set => _rsiBuyPeriod.Value = value;
}
/// <summary>
/// RSI period for sell signals.
/// </summary>
public int RsiSellPeriod
{
get => _rsiSellPeriod.Value;
set => _rsiSellPeriod.Value = value;
}
/// <summary>
/// Long EMA period.
/// </summary>
public int EmaLongPeriod
{
get => _emaLongPeriod.Value;
set => _emaLongPeriod.Value = value;
}
/// <summary>
/// Short EMA period.
/// </summary>
public int EmaShortPeriod
{
get => _emaShortPeriod.Value;
set => _emaShortPeriod.Value = value;
}
/// <summary>
/// Stop loss percentage.
/// </summary>
public decimal StopLossPct
{
get => _stopLossPct.Value;
set => _stopLossPct.Value = value;
}
/// <summary>
/// Candle type.
/// </summary>
public DataType CandleType
{
get => _candleType.Value;
set => _candleType.Value = value;
}
public Kositbablo10Strategy()
{
_rsiBuyPeriod = Param(nameof(RsiBuyPeriod), 5)
.SetDisplay("RSI Buy Period", "RSI period for buy signals", "Indicators");
_rsiSellPeriod = Param(nameof(RsiSellPeriod), 20)
.SetDisplay("RSI Sell Period", "RSI period for sell signals", "Indicators");
_emaLongPeriod = Param(nameof(EmaLongPeriod), 20)
.SetDisplay("EMA Long", "Long EMA period", "Indicators");
_emaShortPeriod = Param(nameof(EmaShortPeriod), 5)
.SetDisplay("EMA Short", "Short EMA period", "Indicators");
_stopLossPct = Param(nameof(StopLossPct), 1m)
.SetDisplay("Stop Loss %", "Stop loss percent", "Risk")
.SetGreaterThanZero();
_candleType = Param(nameof(CandleType), TimeSpan.FromHours(4).TimeFrame())
.SetDisplay("Candle Type", "Type of candles", "General");
}
/// <inheritdoc />
public override IEnumerable<(Security sec, DataType dt)> GetWorkingSecurities()
{
return [(Security, CandleType)];
}
/// <inheritdoc />
protected override void OnStarted2(DateTime time)
{
base.OnStarted2(time);
var rsiBuy = new RelativeStrengthIndex { Length = RsiBuyPeriod };
var rsiSell = new RelativeStrengthIndex { Length = RsiSellPeriod };
var emaLong = new ExponentialMovingAverage { Length = EmaLongPeriod };
var emaShort = new ExponentialMovingAverage { Length = EmaShortPeriod };
StartProtection(
takeProfit: new Unit(2m, UnitTypes.Percent),
stopLoss: new Unit(StopLossPct, UnitTypes.Percent));
var subscription = SubscribeCandles(CandleType);
subscription
.Bind(rsiBuy, rsiSell, emaLong, emaShort, ProcessCandle)
.Start();
}
private void ProcessCandle(ICandleMessage candle, decimal rsiBuy, decimal rsiSell, decimal emaLong, decimal emaShort)
{
if (candle.State != CandleStates.Finished)
return;
var buyCond = rsiBuy < 48 && emaLong > emaShort;
var sellCond = rsiSell > 60 && emaLong > emaShort;
if (buyCond && Position <= 0)
{
if (Position < 0)
BuyMarket();
BuyMarket();
}
else if (sellCond && Position >= 0)
{
if (Position > 0)
SellMarket();
SellMarket();
}
}
}
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, Unit, UnitTypes
from StockSharp.Algo.Indicators import RelativeStrengthIndex, ExponentialMovingAverage
from StockSharp.Algo.Strategies import Strategy
class kositbablo_10_strategy(Strategy):
def __init__(self):
super(kositbablo_10_strategy, self).__init__()
self._rsi_buy_period = self.Param("RsiBuyPeriod", 5) \
.SetDisplay("RSI Buy Period", "RSI period for buy signals", "Indicators")
self._rsi_sell_period = self.Param("RsiSellPeriod", 20) \
.SetDisplay("RSI Sell Period", "RSI period for sell signals", "Indicators")
self._ema_long_period = self.Param("EmaLongPeriod", 20) \
.SetDisplay("EMA Long", "Long EMA period", "Indicators")
self._ema_short_period = self.Param("EmaShortPeriod", 5) \
.SetDisplay("EMA Short", "Short EMA period", "Indicators")
self._stop_loss_pct = self.Param("StopLossPct", 1.0) \
.SetDisplay("Stop Loss %", "Stop loss percent", "Risk")
self._candle_type = self.Param("CandleType", DataType.TimeFrame(TimeSpan.FromHours(4))) \
.SetDisplay("Candle Type", "Type of candles", "General")
@property
def rsi_buy_period(self):
return self._rsi_buy_period.Value
@property
def rsi_sell_period(self):
return self._rsi_sell_period.Value
@property
def ema_long_period(self):
return self._ema_long_period.Value
@property
def ema_short_period(self):
return self._ema_short_period.Value
@property
def stop_loss_pct(self):
return self._stop_loss_pct.Value
@property
def candle_type(self):
return self._candle_type.Value
def OnStarted2(self, time):
super(kositbablo_10_strategy, self).OnStarted2(time)
rsi_buy = RelativeStrengthIndex()
rsi_buy.Length = self.rsi_buy_period
rsi_sell = RelativeStrengthIndex()
rsi_sell.Length = self.rsi_sell_period
ema_long = ExponentialMovingAverage()
ema_long.Length = self.ema_long_period
ema_short = ExponentialMovingAverage()
ema_short.Length = self.ema_short_period
self.StartProtection(
takeProfit=Unit(2.0, UnitTypes.Percent),
stopLoss=Unit(float(self.stop_loss_pct), UnitTypes.Percent))
subscription = self.SubscribeCandles(self.candle_type)
subscription.Bind(rsi_buy, rsi_sell, ema_long, ema_short, self.process_candle).Start()
def process_candle(self, candle, rsi_buy, rsi_sell, ema_long, ema_short):
if candle.State != CandleStates.Finished:
return
rsi_buy = float(rsi_buy)
rsi_sell = float(rsi_sell)
ema_long = float(ema_long)
ema_short = float(ema_short)
buy_cond = rsi_buy < 48 and ema_long > ema_short
sell_cond = rsi_sell > 60 and ema_long > ema_short
if buy_cond and self.Position <= 0:
if self.Position < 0:
self.BuyMarket()
self.BuyMarket()
elif sell_cond and self.Position >= 0:
if self.Position > 0:
self.SellMarket()
self.SellMarket()
def CreateClone(self):
return kositbablo_10_strategy()