I have a simple below code where I want to place a long order when the price close above 13 EMA and Short order with price close below 13 EMA.
And when the order is placed i want a Trail Stop set with 20 ticks as default.
Now the issue is with TSL in place the Long and Short orders are not places based on 13 EMA until TSL is hit.
For example a long order is placed when the price crossed 13 EMA and then Trail Stop is set. When the price falls below 13 EMA i want the long order to be closed and Short order to be triggered, But the order is not getting triggered until Stop is hit.
Can someone help me in fixing the code
region Using declarations
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;
using System.Windows.Media;
using System.Xml.Serialization;
using NinjaTrader.Cbi;
using NinjaTrader.Gui;
using NinjaTrader.Gui.Chart;
using NinjaTrader.Gui.SuperDom;
using NinjaTrader.Gui.Tools;
using NinjaTrader.Data;
using NinjaTrader.NinjaScript;
using NinjaTrader.Core.FloatingPoint;
using NinjaTrader.NinjaScript.Indicators;
using NinjaTrader.NinjaScript.DrawingTools;
#endregion
//This namespace holds Strategies in this folder and is required. Do not change it.
namespace NinjaTrader.NinjaScript.Strategies
{
public class VamsiStratTSL3 : Strategy
{
private EMA EMA1;
protected override void OnStateChange()
{
if (State == State.SetDefaults)
{
Description = @"Simple Price Cross EMa Strat with SL";
Name = "VamsiStratTSL3";
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;
TrailStop = 20;
}
else if (State == State.Configure)
{
AddDataSeries(Data.BarsPeriodType.Minute, 5);
AddDataSeries(Data.BarsPeriodType.Minute, 2);
}
else if (State == State.DataLoaded)
{
EMA1 = EMA(Close, 13);
SetTrailStop(@"", CalculationMode.Ticks, TrailStop, false);
}
}
protected override void OnBarUpdate()
{
if (BarsInProgress != 0)
return;
if (CurrentBars[0] < 1)
return;
// Set 1
if (CrossAbove(Close, EMA1, 1))
{
EnterLong(Convert.ToInt32(DefaultQuantity), "");
}
// Set 2
if (CrossBelow(Close, EMA1, 1))
{
EnterShort(Convert.ToInt32(DefaultQuantity), "");
}
}
region Properties
[NinjaScriptProperty]
[Range(0, int.MaxValue)]
[Display(Name="TrailStop", Order=1, GroupName="Parameters")]
public int TrailStop
{ get; set; }
#endregion
}
}
Comment