Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

OnMarketData() StopLoss

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

    OnMarketData() StopLoss

    Hi I'm trying to work with a StopLoss that triggers from Live Market Data. It works ok, but the when strategy is enabled, the first Long order it's submitted inmediately, altought CalculateOnBarClose= true....

    I know I can use SetStopLoss on initialize but, I'm just trying to understand the OnMarketData object for further developing code...

    Thanks in advance...

    #region Variables

    //Stop Loss
    private double sT = 0.001;

    #endregion

    protected override void Initialize()

    {
    //Settings
    ExitOnClose = false;
    CalculateOnBarClose = true;

    Add(PeriodType.Day, 1);
    }

    protected override void OnBarUpdate()

    {
    if (CurrentBars[0] > 50
    && CurrentBars[1] > 5);

    if (BarsInProgress == 0)

    {
    if (Positions[0].MarketPosition == MarketPosition.Flat)

    if (Close[0] >= Open[0])

    {
    EnterLong(100000, "Long");
    }
    }
    }

    protected override void OnMarketData(MarketDataEventArgs e)

    {
    if (BarsInProgress == 0)

    {
    //Exit Long-----------------------------------------------------------------------
    if (Positions[0].MarketPosition == MarketPosition.Long)

    if (e.MarketDataType == MarketDataType.Last)

    {
    //Stop Loss ---------------------------------------------------------------------
    if (Position.GetProfitLoss(e.Price, PerformanceUnit.Percent) < -ST)

    {
    ExitLong("Stop Loss", "Long");
    }
    }
    }
    }

    #region Properties

    [Description("Stop Loss")]
    [GridCategory("Parameters")]
    private double ST
    {
    get { return sT; }
    set { sT = Math.Max(0, value); }
    }

    #endregion

    #2
    Hello fercho,

    Thank you for your post.

    Please go to the NinjaTrader Control Center > Tools > Options > Strategies > NinjaScript. Is the 'Immediately submit live working historical orders' option enabled?
    If so, then this is why you are seeing the orders submitted on start up of the strategy.

    For information on the Strategies tab of the Options menu please visit the following link: http://www.ninjatrader.com/support/h...tegies_tab.htm

    Comment


      #3
      It's on "wait until flat before executing live"

      Comment


        #4
        Hello fercho,

        Thank you for your response.

        At the beginning of the OnBarUpdate() method try using a Historical check:
        Code:
        if(Historical)
        return;
        For information on Historical please visit the following link: http://www.ninjatrader.com/support/h...historical.htm

        Comment


          #5
          Hello fercho,

          Thank you for your response.

          Your condition to enter will always return true:
          Code:
          if (Close[0] >= Open[0]
          || Close[0] < Open[0])
          The current price will always either be great than, equal to, or less than the Open.

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by Geovanny Suaza, 02-11-2026, 06:32 PM
          0 responses
          639 views
          0 likes
          Last Post Geovanny Suaza  
          Started by Geovanny Suaza, 02-11-2026, 05:51 PM
          0 responses
          366 views
          1 like
          Last Post Geovanny Suaza  
          Started by Mindset, 02-09-2026, 11:44 AM
          0 responses
          107 views
          0 likes
          Last Post Mindset
          by Mindset
           
          Started by Geovanny Suaza, 02-02-2026, 12:30 PM
          0 responses
          569 views
          1 like
          Last Post Geovanny Suaza  
          Started by RFrosty, 01-28-2026, 06:49 PM
          0 responses
          572 views
          1 like
          Last Post RFrosty
          by RFrosty
           
          Working...
          X