Actually commit code for 3CR bot (oops!)

This commit is contained in:
moshferatu 2024-07-12 14:27:10 -07:00
parent 0523d06e12
commit b78c16409c

View File

@ -0,0 +1,104 @@
#region Using declarations
using NinjaTrader.Cbi;
using NinjaTrader.Data;
using NinjaTrader.NinjaScript.Indicators;
using System.ComponentModel.DataAnnotations;
#endregion
namespace NinjaTrader.NinjaScript.Strategies
{
public class ThreeCandleReversalBot : Strategy
{
private ThreeCandleReversal threeCandleReversal;
private ATR atr;
private OpeningRange openingRange;
protected override void OnStateChange()
{
if (State == State.SetDefaults)
{
Description = @"Credit to @BestForexMethod";
Name = "3CR 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 = 20;
IsInstantiatedOnEachOptimizationIteration = true;
ThreeCandleReversalLookback = 5;
ATRPeriod = 10;
ATRMultiplier = 2;
}
else if (State == State.Configure)
{
// Data series required by the opening range indicator, must be loaded here.
AddDataSeries(BarsPeriodType.Minute, 1);
AddDataSeries(Instrument.FullName, BarsPeriod, "US Equities RTH");
}
else if (State == State.DataLoaded)
{
threeCandleReversal = ThreeCandleReversal(ThreeCandleReversalLookback);
atr = ATR(ATRPeriod);
openingRange = OpeningRange(5, OpeningRangeBarType.Minutes);
}
}
protected override void OnBarUpdate()
{
if (BarsInProgress != 0)
return;
if (CurrentBar < BarsRequiredToTrade)
return;
if (threeCandleReversal.DetectedPatterns[0] != null)
{
double atrValue = atr[0];
double stopLoss = ATRMultiplier * atrValue;
double profitTarget = ATRMultiplier * atrValue;
SetStopLoss(CalculationMode.Ticks, stopLoss / TickSize);
SetProfitTarget(CalculationMode.Ticks, profitTarget / TickSize);
if (threeCandleReversal.DetectedPatterns[0].IsSwingHigh &&
openingRange.ORL[0] > 0.0 && Close[0] < openingRange.ORL[0])
{
EnterShort();
}
else if (threeCandleReversal.DetectedPatterns[0].IsSwingLow &&
openingRange.ORH[0] > 0.0 && Close[0] > openingRange.ORH[0])
{
EnterLong();
}
}
}
public override string DisplayName
{
get { return Name; }
}
[Range(1, int.MaxValue), NinjaScriptProperty]
[Display(Name = "3CR Lookback", GroupName = "3CR Bot", Order = 1)]
public int ThreeCandleReversalLookback { get; set; }
[Range(1, int.MaxValue), NinjaScriptProperty]
[Display(Name = "ATR Period", GroupName = "3CR Bot", Order = 2)]
public int ATRPeriod { get; set; }
[Range(0.1, double.MaxValue), NinjaScriptProperty]
[Display(Name = "ATR Multiplier", GroupName = "3CR Bot", Order = 3)]
public double ATRMultiplier { get; set; }
}
}