At 5pm Central USA time, the market opens. and i place a manual long or short order. Then i enable the algo and click "submit immediately and synchronize"
What should happen is the algo sees that its in a long position and manages it for me.
But whats happening today is it see's that im in a long position and closes my trade right away.
How can i get the behavior that i want: I place a manual order and the algo manages that order for me. The algo is a basic ema cross. Do i have to use adopt a position?
thank you for help.
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.Data;
using NinjaTrader.NinjaScript;
using NinjaTrader.Core.FloatingPoint;
using NinjaTrader.NinjaScript.Indicators;
using NinjaTrader.NinjaScript.DrawingTools;
#endregion
namespace NinjaTrader.NinjaScript.Strategies
{
public class BasicEMAStrategy1 : Strategy
{
private EMA ema9; // EMA with a period of 9
private EMA ema21; // EMA with a period of 21
protected override void OnStateChange()
{
if (State == State.SetDefaults)
{
Description = "Basic strategy with 9-period and 21-period EMAs";
Name = "BasicEMAStrategy1";
IsInstantiatedOnEachOptimizationIteration = false;
}
else if (State == State.DataLoaded)
{
// Instantiate EMAs with specified periods
ema9 = EMA(9);
ema21 = EMA(21);
// Set colors for EMAs
ema9.Plots[0].Brush = Brushes.Red;
ema21.Plots[0].Brush = Brushes.Blue;
// Add EMAs to the chart
AddChartIndicator(ema9);
AddChartIndicator(ema21);
}
}
protected override void OnBarUpdate()
{
if (CrossAbove(ema9, ema21, 1)) // Buy order when 9 crosses above 21
{
EnterLong();
}
else if (CrossBelow(ema9, ema21, 1)) // Sell order when 9 crosses below 21
{
EnterShort();
}
}
}
}

Comment