AcceleratorBot USDJPY H4 Strategy
The AcceleratorBot strategy is a conversion of the original MQL4 expert designed for USDJPY on the H4 timeframe. It blends trend strength from the Average Directional Index (ADX), momentum from the Stochastic Oscillator and multi-timeframe Acceleration/Deceleration (AC) values. Candlestick patterns are used as directional filters.
Details
- Entry Criteria: Trend or momentum signals confirmed by candlestick filters.
- Long/Short: Both.
- Exit Criteria: Opposite signal, stop loss, take profit or trailing stop.
- Stops: Fixed and trailing.
- Default Values:
StopLossPoints= 750TakeProfitPoints= 9999TrailPoints= 0AdxPeriod= 14AdxThreshold= 20mX1= 0X2= 150X3= 500CandleType= TimeSpan.FromHours(4)
- Filters:
- Category: Trend and momentum
- Direction: Both
- Indicators: ADX, Stochastic, AC
- Stops: Yes
- Complexity: Advanced
- Timeframe: H4
- Seasonality: No
- Neural Networks: No
- Divergence: No
- Risk Level: Medium
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>
/// Accelerator bot strategy using ADX trend filter and Stochastic crossover.
/// In strong trends (ADX > threshold): trades on candle direction.
/// In ranges (ADX below threshold): trades on Stochastic K/D cross.
/// </summary>
public class AcceleratorBotUsdJpyH4Strategy : Strategy
{
private readonly StrategyParam<int> _adxPeriod;
private readonly StrategyParam<decimal> _adxThreshold;
private readonly StrategyParam<DataType> _candleType;
private decimal? _prevK;
private decimal? _prevD;
public int AdxPeriod { get => _adxPeriod.Value; set => _adxPeriod.Value = value; }
public decimal AdxThreshold { get => _adxThreshold.Value; set => _adxThreshold.Value = value; }
public DataType CandleType { get => _candleType.Value; set => _candleType.Value = value; }
public AcceleratorBotUsdJpyH4Strategy()
{
_adxPeriod = Param(nameof(AdxPeriod), 14)
.SetGreaterThanZero()
.SetDisplay("ADX Period", "Period for ADX calculation", "Indicators");
_adxThreshold = Param(nameof(AdxThreshold), 20m)
.SetDisplay("ADX Threshold", "Minimum ADX to use trend rules", "Indicators");
_candleType = Param(nameof(CandleType), TimeSpan.FromHours(4).TimeFrame())
.SetDisplay("Candle Type", "Timeframe for calculations", "General");
}
/// <inheritdoc />
public override IEnumerable<(Security sec, DataType dt)> GetWorkingSecurities()
{
return [(Security, CandleType)];
}
/// <inheritdoc />
protected override void OnReseted()
{
base.OnReseted();
_prevK = null;
_prevD = null;
}
/// <inheritdoc />
protected override void OnStarted2(DateTime time)
{
base.OnStarted2(time);
_prevK = null;
_prevD = null;
var adx = new AverageDirectionalIndex { Length = AdxPeriod };
var stochastic = new StochasticOscillator
{
K = { Length = 8 },
D = { Length = 3 },
};
var subscription = SubscribeCandles(CandleType);
subscription
.BindEx(adx, stochastic, ProcessCandle)
.Start();
var area = CreateChartArea();
if (area != null)
{
DrawCandles(area, subscription);
DrawIndicator(area, adx);
DrawIndicator(area, stochastic);
DrawOwnTrades(area);
}
}
private void ProcessCandle(ICandleMessage candle, IIndicatorValue adxVal, IIndicatorValue stochVal)
{
if (candle.State != CandleStates.Finished)
return;
if (!IsFormedAndOnlineAndAllowTrading())
return;
if (adxVal is not IAverageDirectionalIndexValue adxTyped || adxTyped.MovingAverage is not decimal adx)
return;
if (stochVal is not IStochasticOscillatorValue stochTyped || stochTyped.K is not decimal stochK || stochTyped.D is not decimal stochD)
return;
if (_prevK is null || _prevD is null)
{
_prevK = stochK;
_prevD = stochD;
return;
}
var bullCross = _prevK <= _prevD && stochK > stochD;
var bearCross = _prevK >= _prevD && stochK < stochD;
// In trending market: use ADX + candle direction
if (adx > AdxThreshold)
{
if (candle.ClosePrice > candle.OpenPrice && bullCross && Position <= 0)
BuyMarket();
else if (candle.ClosePrice < candle.OpenPrice && bearCross && Position >= 0)
SellMarket();
}
else
{
// In range: use stochastic crossover
if (bullCross && Position <= 0)
BuyMarket();
else if (bearCross && Position >= 0)
SellMarket();
}
_prevK = stochK;
_prevD = stochD;
}
}
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 AverageDirectionalIndex, StochasticOscillator
from StockSharp.Algo.Strategies import Strategy
class accelerator_bot_usd_jpy_h4_strategy(Strategy):
def __init__(self):
super(accelerator_bot_usd_jpy_h4_strategy, self).__init__()
self._adx_period = self.Param("AdxPeriod", 14) \
.SetDisplay("ADX Period", "Period for ADX calculation", "Indicators")
self._adx_threshold = self.Param("AdxThreshold", 20.0) \
.SetDisplay("ADX Threshold", "Minimum ADX to use trend rules", "Indicators")
self._candle_type = self.Param("CandleType", DataType.TimeFrame(TimeSpan.FromHours(4))) \
.SetDisplay("Candle Type", "Timeframe for calculations", "General")
self._prev_k = None
self._prev_d = None
@property
def adx_period(self):
return self._adx_period.Value
@property
def adx_threshold(self):
return self._adx_threshold.Value
@property
def candle_type(self):
return self._candle_type.Value
def OnReseted(self):
super(accelerator_bot_usd_jpy_h4_strategy, self).OnReseted()
self._prev_k = None
self._prev_d = None
def OnStarted2(self, time):
super(accelerator_bot_usd_jpy_h4_strategy, self).OnStarted2(time)
self._prev_k = None
self._prev_d = None
adx = AverageDirectionalIndex()
adx.Length = self.adx_period
stochastic = StochasticOscillator()
stochastic.K.Length = 8
stochastic.D.Length = 3
subscription = self.SubscribeCandles(self.candle_type)
subscription.BindEx(adx, stochastic, self.process_candle).Start()
area = self.CreateChartArea()
if area is not None:
self.DrawCandles(area, subscription)
self.DrawIndicator(area, adx)
self.DrawIndicator(area, stochastic)
self.DrawOwnTrades(area)
def process_candle(self, candle, adx_val, stoch_val):
if candle.State != CandleStates.Finished:
return
adx_ma = adx_val.MovingAverage
if adx_ma is None:
return
adx_ma = float(adx_ma)
stoch_k = stoch_val.K
stoch_d = stoch_val.D
if stoch_k is None or stoch_d is None:
return
stoch_k = float(stoch_k)
stoch_d = float(stoch_d)
if self._prev_k is None or self._prev_d is None:
self._prev_k = stoch_k
self._prev_d = stoch_d
return
bull_cross = self._prev_k <= self._prev_d and stoch_k > stoch_d
bear_cross = self._prev_k >= self._prev_d and stoch_k < stoch_d
adx_threshold = float(self.adx_threshold)
if adx_ma > adx_threshold:
if float(candle.ClosePrice) > float(candle.OpenPrice) and bull_cross and self.Position <= 0:
self.BuyMarket()
elif float(candle.ClosePrice) < float(candle.OpenPrice) and bear_cross and self.Position >= 0:
self.SellMarket()
else:
if bull_cross and self.Position <= 0:
self.BuyMarket()
elif bear_cross and self.Position >= 0:
self.SellMarket()
self._prev_k = stoch_k
self._prev_d = stoch_d
def CreateClone(self):
return accelerator_bot_usd_jpy_h4_strategy()