Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Enter OnBarClose but adjust my stoploss OnTickUpdate

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

    Enter OnBarClose but adjust my stoploss OnTickUpdate

    How do I enter a position using OnBarClose, but my stop loss updates OnTickUpdate?

    This is the code I have so far. It enters OnBarClose, but I cannot figure out how to update my position OnTickUpdate. I was thinking of calculating the indicators OnBarClose, while keeping the strategy OnTickUpdate. However, I don't know what is the best way to do this. Please help

    / Check if Flat
    if (Position.MarketPosition == MarketPosition.Flat)
    {
    CurrentStopPrice = 0;
    }

    if (CurrentBars[0] < 1)
    return;

    // Long Entry
    if ((RSI1.Avg[0] >= 50)
    && (WaveTrendV21[1] < 53)
    && (SMA1[0] <= Close[0])
    && (CrossAbove(WaveTrendV21.Values[0], WaveTrendV21.Values[1], 1))
    && (Position.MarketPosition == MarketPosition.Flat)
    && (State == State.Realtime))
    {
    EnterLong(Convert.ToInt32(DefaultQuantity), @"longEntry");
    CurrentTriggerPrice = (Close[0] + (TrailFrequency * TickSize)) ;
    CurrentStopPrice = (Close[0] + (TrailStopDistance * TickSize)) ;
    }

    // Stop Update
    if ((Position.MarketPosition == MarketPosition.Long)
    && (Close[0] > CurrentTriggerPrice))
    {
    CurrentTriggerPrice = (Close[0] + (TrailFrequency * TickSize)) ;
    CurrentStopPrice = (Close[0] + (TrailStopDistance * TickSize)) ;
    }

    // Position Close
    if (CurrentStopPrice != 0)
    {
    ExitLongStopMarket(Convert.ToInt32(DefaultQuantity ), CurrentStopPrice, @"", "");
    }

    #2
    Hello goelon,

    Thanks for your post.

    You would have to have the strategy run Calculate.OnEachTick and then segment parts of your code that need to run only once per bar by using the system bool IsFirstTickOfBar.
    Reference: https://ninjatrader.com/support/help...ttickofbar.htm

    Here is a link to an educational example that demonstrates coding OnEachTick and using IsFirstickOfBar for once per bar events: https://ninjatrader.com/support/help...either_cal.htm

    Comment


      #3
      I don't think that works. I need to run the indicators on the bar close or on the first tick of the bar. If I make my entries enter on the first tick of the bar, the indicator is still running on the ticks.

      I am running my strategy with a Renko chart to eliminate the extra movement. I need to run the indicators on the bar close to make sure it is running like the Renko.

      Comment


        #4
        Hello goelon,

        Thanks for your reply.

        This is the only way in which you can have some code run OnEachTick and some code run once per bar.

        As far as your indicators, yes they would run with the same calculate settings as the host strategy but you don't have to look at the currently forming bar [0] of the indicator value, you can use IsFirstTickOfBar and then check the bars ago of [1] of the indicator which is the just closed bar value of the indicator, thus only working with the OnBarClose values.

        Comment


          #5
          But in this case the strategy will not be back testable? Or would it be?

          Comment


            #6
            What is we use a separate DataSeries for the OnEachTick handler? Something like AddDataSeries(BarsPeriodType.Tick, 1);

            Comment


              #7
              Hello councilsoft,

              To test a OnEachTick strategy you would need to use one of the realtime modes like Playback or just a reatlime connection. Historical tests are all run OnBarClose.

              You can simulate intrabar granularity by using a secondary series, that is not exactly the same concept as OnEachTick but you could use the primary series CurrentBar to determine when "bars" are formed and use the tick series for more granular logic.

              Comment

              Latest Posts

              Collapse

              Topics Statistics Last Post
              Started by NullPointStrategies, Yesterday, 05:17 AM
              0 responses
              55 views
              0 likes
              Last Post NullPointStrategies  
              Started by argusthome, 03-08-2026, 10:06 AM
              0 responses
              132 views
              0 likes
              Last Post argusthome  
              Started by NabilKhattabi, 03-06-2026, 11:18 AM
              0 responses
              73 views
              0 likes
              Last Post NabilKhattabi  
              Started by Deep42, 03-06-2026, 12:28 AM
              0 responses
              45 views
              0 likes
              Last Post Deep42
              by Deep42
               
              Started by TheRealMorford, 03-05-2026, 06:15 PM
              0 responses
              49 views
              0 likes
              Last Post TheRealMorford  
              Working...
              X