ninjatrader/strategies/KnowSureThingBot.cs
2024-04-27 06:59:46 -07:00

73 lines
2.4 KiB
C#

#region Using declarations
using NinjaTrader.Cbi;
using NinjaTrader.NinjaScript.Indicators;
#endregion
//This namespace holds Strategies in this folder and is required. Do not change it.
namespace NinjaTrader.NinjaScript.Strategies
{
public class KnowSureThingBot : Strategy
{
KnowSureThing KST;
protected override void OnStateChange()
{
if (State == State.SetDefaults)
{
Description = @"";
Name = "Know Sure Thing 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;
}
else if (State == State.DataLoaded)
{
KST = KnowSureThing();
}
}
protected override void OnBarUpdate()
{
if (CurrentBar < BarsRequiredToTrade)
return;
if (BarsInProgress == 1)
return;
if (Position.MarketPosition == MarketPosition.Long && KST[0] < KST.MovingAverage[0])
{
ExitLong();
}
if (Position.MarketPosition == MarketPosition.Short && KST[0] > KST.MovingAverage[0])
{
ExitShort();
}
if (KST[0] > KST.MovingAverage[0] && KST[1] < KST.MovingAverage[1] && KST[0] <= -2.0)
{
SetProfitTarget(CalculationMode.Ticks, 12.0);
EnterLong();
}
if (KST[0] < KST.MovingAverage[0] && KST[1] > KST.MovingAverage[1] && KST[0] >= 2.0)
{
SetProfitTarget(CalculationMode.Ticks, 12.0);
EnterShort();
}
}
}
}