The below script is working fine - It buys above 20 SMA and sells below 20 SMA. I'm trying to add a new logic to reverse my position from long to short if the closed bar is below the executed position, and vice versa. Please add it to my code
Something like this?
if (Position.MarketPosition == MarketPosition.Short && shortEntry.OrderState == OrderState.Filled && Close[0] > shortEntry.AverageFillPrice)
{
EnterLong();
BELOW IS MY CODE
//This namespace holds Strategies in this folder and is required. Do not change it.
namespace NinjaTrader.NinjaScript.Strategies
{
public class TESTTEST : Strategy
{
private SMA SMA1;
private Order longEntry;
private Order shortEntry;
protected override void OnStateChange()
{
if (State == State.SetDefaults)
{
Description = @"Enter the description for your new custom Strategy here.";
Name = "TESTTEST";
Calculate = Calculate.OnEachTick;
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;
// Disable this property for performance gains in Strategy Analyzer optimizations
// See the Help Guide for additional information
IsInstantiatedOnEachOptimizationIteration = true;
}
else if (State == State.Configure)
{
SMA1 = SMA(20);
SMA1.Plots[0].Brush = Brushes.Goldenrod;
AddChartIndicator(SMA1);
SMA1 = SMA(20);
}
}
protected override void OnBarUpdate()
{
if (CurrentBars[0] < 1)
return;
// Set 1
if (CrossAbove(Close, SMA1, 1))
{
EnterLong(Convert.ToInt32(DefaultQuantity), "");
}
// Set 2
if (CrossBelow(Close, SMA1, 1))
{
EnterShort(Convert.ToInt32(DefaultQuantity), "");
}
}
}
}
Comment