Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Having trouble with Start behavior

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Having trouble with Start behavior

    Hello, last night i ran this algo and it worked fine. But now i do it and its not working correctly.

    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();
    }
    }
    }
    }

    #2
    Hello tusktooth,

    A NinjaScript Strategy is not going to see manually submitted orders. Manual orders are not going to update order override methods like OnOrderUpdate() / OnExecutionUpdate() and these will not affect the strategy position.

    Using Adopt Account Position Start Behavior would change the strategy position to match the account position at the time the strategy is enabled, which you could use to send protective orders. (Note, the entry orders that entered the position would still not update the strategies order override methods)

    Below is a link to the support article and video on start behavior and example of adopt account position.



    You could use the Addon approach (typically used in an indicator instead of a strategy) to add event handler methods to the Account OrderUpdate and ExecutionUpdate events.



    Below are links to examples that use the Addon approach to add Account event handler methods.
    ProfitCaseStopTrailIndicatorExample and ProfitChaseStopTrailAddonExample - https://support.ninjatrader.com/s/ar...ase-Stop-Trail
    AtmStrategyIdentifier - https://ninjatraderecosystem.com/use...egyidentifier/
    Chelsea B.NinjaTrader Customer Service

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by argusthome, 03-08-2026, 10:06 AM
    0 responses
    113 views
    0 likes
    Last Post argusthome  
    Started by NabilKhattabi, 03-06-2026, 11:18 AM
    0 responses
    60 views
    0 likes
    Last Post NabilKhattabi  
    Started by Deep42, 03-06-2026, 12:28 AM
    0 responses
    40 views
    0 likes
    Last Post Deep42
    by Deep42
     
    Started by TheRealMorford, 03-05-2026, 06:15 PM
    0 responses
    43 views
    0 likes
    Last Post TheRealMorford  
    Started by Mindset, 02-28-2026, 06:16 AM
    0 responses
    81 views
    0 likes
    Last Post Mindset
    by Mindset
     
    Working...
    X