2024-06-06 18:35:40 +00:00
|
|
|
#region Using declarations
|
|
|
|
using NinjaTrader.Cbi;
|
2024-08-02 20:26:39 +00:00
|
|
|
using NinjaTrader.Data;
|
2024-06-06 18:35:40 +00:00
|
|
|
using NinjaTrader.NinjaScript.Indicators;
|
|
|
|
using System.ComponentModel.DataAnnotations;
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
namespace NinjaTrader.NinjaScript.Strategies
|
|
|
|
{
|
|
|
|
public class SonicRBot : Strategy
|
|
|
|
{
|
|
|
|
private ATR atr;
|
2024-08-02 20:26:39 +00:00
|
|
|
private SMA longTermTrend;
|
|
|
|
private OpeningRange openingRange;
|
2024-06-06 18:35:40 +00:00
|
|
|
private SonicRDragon dragon;
|
|
|
|
|
|
|
|
private bool lookingLong;
|
|
|
|
private bool lookingShort;
|
|
|
|
|
|
|
|
private bool waitingForLongBreak;
|
|
|
|
private bool waitingForShortBreak;
|
|
|
|
|
|
|
|
private double breakPrice;
|
|
|
|
|
|
|
|
protected override void OnStateChange()
|
|
|
|
{
|
|
|
|
if (State == State.SetDefaults)
|
|
|
|
{
|
|
|
|
Description = @"Inspired by the Sonic R System created by sonicdeejay on the Forex Factory forums";
|
|
|
|
Name = "Sonic R Bot";
|
|
|
|
Calculate = Calculate.OnBarClose;
|
|
|
|
EntriesPerDirection = 1;
|
|
|
|
EntryHandling = EntryHandling.AllEntries;
|
2024-08-02 20:26:39 +00:00
|
|
|
IsExitOnSessionCloseStrategy = false;
|
2024-06-06 18:35:40 +00:00
|
|
|
ExitOnSessionCloseSeconds = 30;
|
|
|
|
IsFillLimitOnTouch = false;
|
|
|
|
MaximumBarsLookBack = MaximumBarsLookBack.TwoHundredFiftySix;
|
|
|
|
OrderFillResolution = OrderFillResolution.Standard;
|
|
|
|
Slippage = 0;
|
|
|
|
StartBehavior = StartBehavior.WaitUntilFlat;
|
|
|
|
TimeInForce = TimeInForce.Gtc;
|
|
|
|
TraceOrders = false;
|
|
|
|
RealtimeErrorHandling = RealtimeErrorHandling.StopCancelClose;
|
|
|
|
StopTargetHandling = StopTargetHandling.PerEntryExecution;
|
|
|
|
BarsRequiredToTrade = 100;
|
|
|
|
IsInstantiatedOnEachOptimizationIteration = true;
|
|
|
|
|
|
|
|
DragonPeriod = 34;
|
|
|
|
ATRPeriod = 14;
|
|
|
|
ATRMultiplier = 2.0;
|
2024-08-02 20:26:39 +00:00
|
|
|
LongTermTrendPeriod = 20;
|
|
|
|
OpeningRangePeriod = 3;
|
|
|
|
LongOnly = true;
|
|
|
|
}
|
|
|
|
else if (State == State.Configure)
|
|
|
|
{
|
|
|
|
AddDataSeries(BarsPeriodType.Day, 1);
|
|
|
|
|
|
|
|
// Data series required by the opening range indicator, must be loaded here.
|
|
|
|
AddDataSeries(BarsPeriodType.Minute, 1);
|
|
|
|
AddDataSeries(Instrument.FullName, BarsPeriod, "US Equities RTH");
|
2024-06-06 18:35:40 +00:00
|
|
|
}
|
|
|
|
else if (State == State.DataLoaded)
|
|
|
|
{
|
|
|
|
atr = ATR(ATRPeriod);
|
2024-08-02 20:26:39 +00:00
|
|
|
longTermTrend = SMA(BarsArray[1], LongTermTrendPeriod);
|
|
|
|
openingRange = OpeningRange(OpeningRangePeriod, OpeningRangeBarType.Minutes);
|
2024-06-06 18:35:40 +00:00
|
|
|
dragon = SonicRDragon(DragonPeriod);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
protected override void OnBarUpdate()
|
|
|
|
{
|
2024-08-02 20:26:39 +00:00
|
|
|
if (BarsInProgress != 0 || CurrentBars[1] < 0)
|
2024-06-06 18:35:40 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
if (CurrentBar < BarsRequiredToTrade)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (Position.MarketPosition != MarketPosition.Flat)
|
2024-08-02 20:26:39 +00:00
|
|
|
{
|
|
|
|
if (Position.MarketPosition == MarketPosition.Long && Close[0] < dragon.LowEMA[0])
|
|
|
|
ExitLong();
|
|
|
|
else if (Position.MarketPosition == MarketPosition.Short && Close[0] > dragon.HighEMA[0])
|
|
|
|
ExitShort();
|
|
|
|
|
2024-06-06 18:35:40 +00:00
|
|
|
return;
|
2024-08-02 20:26:39 +00:00
|
|
|
}
|
2024-06-06 18:35:40 +00:00
|
|
|
|
2024-08-02 20:26:39 +00:00
|
|
|
if (Open[0] < dragon.HighEMA[0] && Close[0] > dragon.HighEMA[0] && Close[0] > longTermTrend[0] &&
|
|
|
|
openingRange.ORH[0] > 0.0 && Close[0] > openingRange.ORH[0])
|
2024-06-06 18:35:40 +00:00
|
|
|
{
|
|
|
|
lookingLong = true;
|
|
|
|
|
|
|
|
lookingShort = false;
|
|
|
|
waitingForLongBreak = false;
|
|
|
|
waitingForShortBreak = false;
|
|
|
|
}
|
2024-08-02 20:26:39 +00:00
|
|
|
else if (Open[0] > dragon.LowEMA[0] && Close[0] < dragon.LowEMA[0] && Close[0] < longTermTrend[0] &&
|
|
|
|
openingRange.ORL[0] > 0.0 && Close[0] < openingRange.ORL[0])
|
2024-06-06 18:35:40 +00:00
|
|
|
{
|
|
|
|
lookingShort = true;
|
|
|
|
|
|
|
|
lookingLong = false;
|
|
|
|
waitingForLongBreak = false;
|
|
|
|
waitingForShortBreak = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (lookingLong && Close[0] < Open[0])
|
|
|
|
{
|
|
|
|
waitingForLongBreak = true;
|
|
|
|
breakPrice = Close[1];
|
|
|
|
lookingLong = false;
|
|
|
|
}
|
|
|
|
else if (lookingShort && Close[0] > Open[0])
|
|
|
|
{
|
|
|
|
waitingForShortBreak = true;
|
|
|
|
breakPrice = Close[1];
|
|
|
|
lookingShort = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (waitingForLongBreak && Close[0] > breakPrice)
|
|
|
|
{
|
2024-08-02 20:26:39 +00:00
|
|
|
SetTrailStop(CalculationMode.Ticks, (ATRMultiplier * atr[0]) / TickSize);
|
2024-06-06 18:35:40 +00:00
|
|
|
EnterLong();
|
|
|
|
waitingForLongBreak = false;
|
|
|
|
}
|
2024-08-02 20:26:39 +00:00
|
|
|
else if (!LongOnly && waitingForShortBreak && Close[0] < breakPrice)
|
2024-06-06 18:35:40 +00:00
|
|
|
{
|
2024-08-02 20:26:39 +00:00
|
|
|
SetTrailStop(CalculationMode.Ticks, (ATRMultiplier * atr[0]) / TickSize);
|
2024-06-06 18:35:40 +00:00
|
|
|
EnterShort();
|
|
|
|
waitingForShortBreak = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public override string DisplayName
|
|
|
|
{
|
|
|
|
get { return Name; }
|
|
|
|
}
|
|
|
|
|
|
|
|
#region Properties
|
|
|
|
[NinjaScriptProperty]
|
|
|
|
[Range(1, int.MaxValue)]
|
2024-07-07 03:32:02 +00:00
|
|
|
[Display(Name = "Dragon Period", Description = "Period for the Sonic R Dragon", GroupName = "Sonic R Bot", Order = 1)]
|
2024-06-06 18:35:40 +00:00
|
|
|
public int DragonPeriod { get; set; }
|
|
|
|
|
|
|
|
[Range(1, int.MaxValue), NinjaScriptProperty]
|
2024-07-07 03:32:02 +00:00
|
|
|
[Display(Name = "ATR Period", GroupName = "Sonic R Bot", Order = 2)]
|
2024-06-06 18:35:40 +00:00
|
|
|
public int ATRPeriod { get; set; }
|
|
|
|
|
|
|
|
[Range(0.1, double.MaxValue), NinjaScriptProperty]
|
2024-07-07 03:32:02 +00:00
|
|
|
[Display(Name = "ATR Multiplier", GroupName = "Sonic R Bot", Order = 3)]
|
2024-06-06 18:35:40 +00:00
|
|
|
public double ATRMultiplier { get; set; }
|
2024-08-02 20:26:39 +00:00
|
|
|
|
|
|
|
[Range(1, int.MaxValue), NinjaScriptProperty]
|
|
|
|
[Display(Name = "Long Term Trend Period", GroupName = "Sonic R Bot", Order = 4)]
|
|
|
|
public int LongTermTrendPeriod { get; set; }
|
|
|
|
|
|
|
|
[NinjaScriptProperty]
|
|
|
|
[Display(Name = "Opening Range Period", GroupName = "Sonic R Bot", Order = 5)]
|
|
|
|
public int OpeningRangePeriod { get; set; }
|
|
|
|
|
|
|
|
[NinjaScriptProperty]
|
|
|
|
[Display(Name = "Long Only", GroupName = "Sonic R Bot", Order = 6)]
|
|
|
|
public bool LongOnly { get; set; }
|
2024-06-06 18:35:40 +00:00
|
|
|
#endregion
|
|
|
|
}
|
|
|
|
}
|