2024-06-06 18:35:40 +00:00
|
|
|
#region Using declarations
|
|
|
|
using NinjaTrader.Cbi;
|
|
|
|
using NinjaTrader.NinjaScript.Indicators;
|
|
|
|
using System.ComponentModel.DataAnnotations;
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
//This namespace holds Strategies in this folder and is required. Do not change it.
|
|
|
|
namespace NinjaTrader.NinjaScript.Strategies
|
|
|
|
{
|
|
|
|
public class SonicRBot : Strategy
|
|
|
|
{
|
|
|
|
private ATR atr;
|
|
|
|
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;
|
|
|
|
IsExitOnSessionCloseStrategy = true;
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
else if (State == State.DataLoaded)
|
|
|
|
{
|
|
|
|
atr = ATR(ATRPeriod);
|
|
|
|
dragon = SonicRDragon(DragonPeriod);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
protected override void OnBarUpdate()
|
|
|
|
{
|
|
|
|
if (BarsInProgress != 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (CurrentBar < BarsRequiredToTrade)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (Position.MarketPosition != MarketPosition.Flat)
|
|
|
|
return;
|
|
|
|
|
2024-07-07 03:46:23 +00:00
|
|
|
if (Open[0] < dragon.HighEMA[0] && Close[0] > dragon.HighEMA[0])
|
2024-06-06 18:35:40 +00:00
|
|
|
{
|
|
|
|
lookingLong = true;
|
|
|
|
|
|
|
|
lookingShort = false;
|
|
|
|
waitingForLongBreak = false;
|
|
|
|
waitingForShortBreak = false;
|
|
|
|
}
|
2024-07-07 03:46:23 +00:00
|
|
|
else if (Open[0] > dragon.LowEMA[0] && Close[0] < dragon.LowEMA[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;
|
|
|
|
}
|
|
|
|
|
|
|
|
double atrValue = atr[0];
|
|
|
|
double stopLoss = ATRMultiplier * atrValue;
|
|
|
|
double profitTarget = ATRMultiplier * atrValue;
|
|
|
|
|
|
|
|
if (waitingForLongBreak && Close[0] > breakPrice)
|
|
|
|
{
|
|
|
|
SetStopLoss(CalculationMode.Ticks, stopLoss / TickSize);
|
|
|
|
SetProfitTarget(CalculationMode.Ticks, profitTarget / TickSize);
|
|
|
|
EnterLong();
|
|
|
|
waitingForLongBreak = false;
|
|
|
|
}
|
|
|
|
else if (waitingForShortBreak && Close[0] < breakPrice)
|
|
|
|
{
|
|
|
|
SetStopLoss(CalculationMode.Ticks, stopLoss / TickSize);
|
|
|
|
SetProfitTarget(CalculationMode.Ticks, profitTarget / TickSize);
|
|
|
|
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; }
|
|
|
|
#endregion
|
|
|
|
}
|
|
|
|
}
|