Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

If statement logic to place trade on that current day?

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

    If statement logic to place trade on that current day?

    Hi,

    My goal is to buy based on a condition of the price of the opening candle and trade on that candle. like if I have a condition to buy based on a series of conditions in an if statement but I also include the open price of the day I can place the trade on that day NOT the next day.

    I am using OnBarUpdate() but have tried OnTick() but that doesn't seem to work. I understand that OnBarUpdate() would not work here, because it is set up to read this conditional if statment information and make or not make the trade the NEXT DAY.

    You see in my example, I want to buy on a simple condition for illustration purposes if the current days open is greater than the past day candle then buy that candle's open. See in this example I wanted to buy the SECOND GREEN CANDLE from right to left, but because it only applies the trade based on the logic to the next day the trade occurred on the subsequent candle.

    protected override void OnBarUpdate()
    {​

    double followingCandleClose = Close[0];


    Print("Time Date " + Time[0].Date);

    // Get the bar index for the current trading day
    int currentDayBarIndex = Bars.GetBar(Time[0].Date);
    Print("Current Day Bar Index: " + currentDayBarIndex);

    // Get the index of the first bar of the current trading day
    int firstBarOfDayIndex = CurrentBar - (Bars.GetBar(Time[0]) - currentDayBarIndex);

    // Get the open price of the current trading day
    double currentDayOpen = Open[firstBarOfDayIndex];
    Print("Current Day Open: " + currentDayOpen);​


    if (
    currentDayOpen >= followingCandleClose​
    )
    {
    EnterLong(OrderQuantity, "Buy4");
    }​
    }

    Output Window:
    Time Date 4/30/2024 12:00:00 AM
    Current Day Bar Index: 3886
    Current Day Open: 2688.5
    Current Time: 17:00:00
    Current Day Open: 2688.5
    Following Candle Close: 17832
    Time Date 5/1/2024 12:00:00 AM
    Current Day Bar Index: 3887
    Current Day Open: 2688.5
    Current Time: 17:00:00
    Current Day Open: 2688.5
    Following Candle Close: 17699
    Time Date 5/2/2024 12:00:00 AM
    Current Day Bar Index: 3888
    Current Day Open: 2688.5
    Current Time: 17:00:00
    Current Day Open: 2688.5
    Following Candle Close: 17910.5

    I've tried Calculate = Calculate.OnEachTick; instead of Calculate = Calculate.OnBarClose;

    same results

    Please help.

    Thank you,

    MatHatter​
    Attached Files
    Last edited by MatHatter; 08-14-2024, 01:56 PM.

    #2
    Hello MatHatter,

    Since the bar type is a 1 day bar, OnBarUpdate() is only going to run after the bar closes when using Calculate.OnBarClose.

    Unfortunately, there would not be a work around for this in NinjaTrader 7. (I'm assuming you are using NinjaTrader 7 as you have posted in the NinjaTrader 7 section of the forums)

    You may want to consider using NinjaTrader 8. In NinjaTrader 8, to trigger an action before the bar closes in historical (backtest), you would need enable TickReplay, set Calculate to Calculate.OnEachTick or .OnPriceChange in State.Configure, and submit orders to the barsInProgressIndex of an added 1-tick series.

    See the support article below about intra-bar granularity.
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Hi Chelsea,

      I am using: 8.1.3.1 64-bit. Sorry, I didn't know there was a difference in where to post. I have a hard time reading those things sometimes, so if you could help me with some pseudo code or something that would be great. I see I should have posted here: Strategy Development - NinjaTrader Support Forum

      From,

      Mat

      Comment


        #4
        Hi, Thanks, I was able to set up the tick replay in options and when I re-ran the strategy it came up as a box I can tick. I will try to Calculate.OnEachTick, but after this, I am not sure what to do...

        Comment


          #5
          Hello MatHatter,

          You will need to enable TickReplay and assign Calculate = Calculate.OnPriceChange; in OnStateChange() when State == State.Configure.


          Then you will need to add a 1 tick series with AddDataSeries(). (If there is only 1 added series this will be BarsInProgressIndex 1)


          Then you will need to call the order methods using 1 as the barsInProgressIndex parameter.
          EnterLong(int barsInProgressIndex, int quantity, string signalName)
          Chelsea B.NinjaTrader Customer Service

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by Geovanny Suaza, 02-11-2026, 06:32 PM
          0 responses
          633 views
          0 likes
          Last Post Geovanny Suaza  
          Started by Geovanny Suaza, 02-11-2026, 05:51 PM
          0 responses
          364 views
          1 like
          Last Post Geovanny Suaza  
          Started by Mindset, 02-09-2026, 11:44 AM
          0 responses
          105 views
          0 likes
          Last Post Mindset
          by Mindset
           
          Started by Geovanny Suaza, 02-02-2026, 12:30 PM
          0 responses
          567 views
          1 like
          Last Post Geovanny Suaza  
          Started by RFrosty, 01-28-2026, 06:49 PM
          0 responses
          568 views
          1 like
          Last Post RFrosty
          by RFrosty
           
          Working...
          X