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 charlesugo_1, 05-26-2026, 05:03 PM
          0 responses
          66 views
          0 likes
          Last Post charlesugo_1  
          Started by DannyP96, 05-18-2026, 02:38 PM
          1 response
          149 views
          0 likes
          Last Post NinjaTrader_ChelseaB  
          Started by CarlTrading, 05-11-2026, 05:56 AM
          0 responses
          162 views
          0 likes
          Last Post CarlTrading  
          Started by CarlTrading, 05-10-2026, 08:12 PM
          0 responses
          99 views
          0 likes
          Last Post CarlTrading  
          Started by Hwop38, 05-04-2026, 07:02 PM
          0 responses
          286 views
          0 likes
          Last Post Hwop38
          by Hwop38
           
          Working...
          X