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 NullPointStrategies, Yesterday, 05:17 AM
            0 responses
            62 views
            0 likes
            Last Post NullPointStrategies  
            Started by argusthome, 03-08-2026, 10:06 AM
            0 responses
            134 views
            0 likes
            Last Post argusthome  
            Started by NabilKhattabi, 03-06-2026, 11:18 AM
            0 responses
            75 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
            50 views
            0 likes
            Last Post TheRealMorford  
            Working...
            X