Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Enter on tick but exit on bar close

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

    Enter on tick but exit on bar close

    Support,

    I have a strategy that needs to calculate entries on each tick but exit once the bar closes. I have added a secondary data series and have tried to send my orders to that tick data series, but the entry is still a bar late. Please review the code below:


    else if (State == State.Configure)
    {
    SetStopLoss("", CalculationMode.Currency, 200, false);
    AddDataSeries(Data.BarsPeriodType.Tick, 1);
    }
    }

    protected override void OnBarUpdate()
    {
    if (BarsInProgress != 0)
    return;

    if (CurrentBars[0] < 3)
    return;

    // Set 1
    if (Low[2] < Low[3])
    {
    EnterLong(1, 1, "Long: 1tick");
    }


    if(Position.GetUnrealizedProfitLoss(PerformanceUni t.Points, Close[0]) > 0)
    {
    ExitLong(Convert.ToInt32(DefaultQuantity), "", "");
    }








    #2
    Hello omermirza, thanks for your post.

    We have an existing example linked below demonstrating filtering logic between OnEachTick and OnBarClose (FirstTickOfBar).

    https://ninjatrader.com/support/help...either_cal.htm

    For an added 1 tick series running OnBarClose, the structure would be this:

    Code:
    OnBarUpdate()
    {
        if(BarsInProgress == 0)
        {
            //Primary series context
        }
    
        if(BarsInProgress == 1)
        {
            //Secondary series context
        }
    }
    The part in your snippet if (CurrentBars[0] < 3) is not letting code run because the only series you have are CurrentBars[0] and CurrentBars[1].

    Best regards,
    -ChrisL
    Last edited by NinjaTrader_ChrisL; 04-19-2021, 09:02 AM.

    Comment


      #3
      OK, I tried that and now my strategy won't enable. Is that the code for calculating entries on each tick and exits on bar close?

      Comment


        #4
        Hello omermirza, thanks for your reply.

        This is just an example code piece, it's showing how to separate the primary series from the secondary series but not doing anything else. If your strategy is not enabling, it is likely a BarsAgo issue, check the Log tab of the Control Center for any errors. The way to fix that would be a CurrentBars check e.g


        Code:
        OnBarUpdate()
        {
            if(CurrentBars[0] < 1 || CurrentBars[1] < 1) 
               return;
        
           //able to access index 0 and index 1 after this point.
        Kind regards,
        -ChrisL

        Comment


          #5
          Cool, I got it working but this section seems to be doing the comparison on the 1tick data series and not the primary data series which is a 4 hour chart:

          // Set 1
          if (Low[2] < Low[3])
          {
          EnterLong(1, 1, "Long: 1tick");
          }

          Comment


            #6
            Hello omermirza, thanks for your reply.

            That would imply you are checking this code when if(BarsInProgress == 0) is true, you would need to evaluate this within if(BarsInProgress == 1) to check it OnEachTick.

            Kind regards,
            -ChrisL

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by CarlTrading, 03-31-2026, 09:41 PM
            1 response
            78 views
            1 like
            Last Post NinjaTrader_ChelseaB  
            Started by CarlTrading, 04-01-2026, 02:41 AM
            0 responses
            40 views
            0 likes
            Last Post CarlTrading  
            Started by CaptainJack, 03-31-2026, 11:44 PM
            0 responses
            63 views
            2 likes
            Last Post CaptainJack  
            Started by CarlTrading, 03-30-2026, 11:51 AM
            0 responses
            63 views
            0 likes
            Last Post CarlTrading  
            Started by CarlTrading, 03-30-2026, 11:48 AM
            0 responses
            54 views
            0 likes
            Last Post CarlTrading  
            Working...
            X