```
// Add these using declarations at the top if they are not already present
using NinjaTrader.Cbi;
using NinjaTrader.NinjaScript.Indicators;
using NinjaTrader.Gui;
using System;
namespace NinjaTrader.NinjaScript.Strategies
{
public class EMACross : Strategy
{
private EMA fastEma;
private EMA slowEma;
private double trailingStopValue;
// Marker series to plot markers on the chart
private Series<double> sidewaysMarkerSeries;
protected override void OnStateChange()
{
if (State == State.SetDefaults)
{
// Your existing settings...
trailingStopValue = 6.0; // Set your desired trailing stop value (in points or ticks)
Calculate = Calculate.OnEachTick;
// Add a new series for markers
sidewaysMarkerSeries = new Series<double>(this);
}
else if (State == State.Configure)
{
// Do nothing here or add additional configuration
}
}
protected override void OnBarUpdate()
{
if (BarsInProgress != 0)
return;
// Check if EMA indicators are not yet initialized
if (fastEma == null || slowEma == null)
{
// Initialize EMA indicators
fastEma = EMA(Close, 10);
slowEma = EMA(Close, 20);
return;
}
// Continue with your existing strategy logic
// Check trading hours
if (!IsRegularMarketHours())
{
// Avoid trading outside regular market hours
return;
}
// Check for EMA crossover
if (CrossAbove(fastEma, slowEma, 1))
{
// Enter long position logic
EnterLong("TradeLong");
Draw.ArrowUp(this, "tag1", true, 0, Low[0] - TickSize, Brushes.Red);
// Set take profit for the long position
// SetProfitTarget("EMACrossLong", CalculationMode.Ticks, 3);
// Set trailing stop for the long position
//SetTrailStop("TradeLong", CalculationMode.Ticks, 16, false); // 'false' indicates a non-simulated stop
// Set stop-loss for the long position (adjust the value as needed)
//SetStopLoss("TradeLong", CalculationMode.Ticks, 10, false);
}
else if (CrossBelow(fastEma, slowEma, 1))
{
// Enter short position logic
EnterShort("TradeShort");
Draw.ArrowUp(this, "tag1", true, 0, Low[0] - TickSize, Brushes.Red);
// Set take profit for the short position
// SetProfitTarget("EMACrossShort", CalculationMode.Ticks, 3);
// Set trailing stop for the short position
//SetTrailStop("TradeShort", CalculationMode.Ticks, 16, false); // 'false' indicates a non-simulated stop
// Set stop-loss for the short position (adjust the value as needed)
//SetStopLoss("TradeShort", CalculationMode.Ticks, 10, false);
}
}
private bool IsRegularMarketHours()
{
TimeSpan currentTime = Time[0].TimeOfDay;
return currentTime >= new TimeSpan(9, 0, 0) && currentTime <= new TimeSpan(19, 0, 0);
}
}
}
```

Comment