Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Strategy not working with Indicator .OnBarClose

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

    Strategy not working with Indicator .OnBarClose

    Hello guys,

    Recently I developed my own indicator. I also developed a strategy based on it. To finalize the process I decided to polish the code and to optimize its performance.

    But I found something very strange.

    My indicator uses Calculate.OnBarClose. As I know this setting is pinpointing how often the OnBarUpdate() method will be invoked. Meanwhile it gets real-time data by OnMarketData().
    My goal is all the calculations to be executed once at the bar close when all the data for the current bar is acquired. I use Plot() as expected. The plot values are set when the bar is closing, before the next bar is started (as I understand from the NinjaTrader API documentation). The plot is displayed on the currently processed bar.
    The indicator is working as expected and perfectly.

    I also created a Strategy, that also works perfectly. I want this strategy to open position ON THE NEXT bar. I mean on the first tick when the next bar is opened. This way my strategy is using the Plot values from the previous bar to enter a position in tthe currently opened new bar. My strategy however is Calculate.OnEachTick, because I use managed approach and I need to refresh and to update the StopLoss and TakeProfit on every tick so that the orders are executed precisly. However, I check if IsFirstTickOfBar to check the conditions based on the previous bar if the strategy is going to open a position.

    And here is the thing:
    When I update the Plot values in the indicator in OnBarUpdate() I also have an IF (IsFirstTickOfBar) clause, and the strategy is working fine. However, if I remove IF (IsFirstTickOfBar) clause from the indicator, the strategy is no longer working and do not open any position.

    On the chart when testing the indicator there is no difference in the Plot() values (checked by the middle mouse button click and databox). The plot values are situated just the same place and values as when IF (IsFirstTickOfBar) clause is available and not available. From an indicator point of view, there is no difference in the end result.

    Any advices or explanations are welcome.
    Could someone give me a hint why this is working in this way?


    Last edited by nrgful; 01-19-2025, 04:22 AM.

    #2
    I think I grasp what is the problem. I will write an answer for the others who are having some problems and want to learn something new.

    I added a Print() line of code to print the Calculate setting at the beginning of the OnBarUpdate() indicator method. When I run the indicator on the chart it is showing OnBarClose as I set it into the indicator. However, when running the strategy, it is showing OnEachTick - as it is set to the strategy. This is changing the behaviour of the indicator and is very villainous.

    This should be kept in mind when developing an indicator for strategy use purposes.
    That is why I need to keep my condition check IF (IsFirstTickOfBar) into the OnBarUpdate() in the indicator, regardless of the Calculate.OnBarClose setting on it. That means that I need to support the both of the states Calculate.OnBarClose and Calculate.OnEachTick to the indicator for making it possible to be used in strategy and at the same time be optimized when using as indicator on the chart.

    Best wishes

    Comment


      #3
      Hello nrgful,

      I'm glad you were able to work out the issue as being a sequence of events issue and that you shared your findings.
      Chelsea B.NinjaTrader Customer Service

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by kujista, 04-25-2023, 02:22 AM
      5 responses
      67 views
      0 likes
      Last Post bltdavid  
      Started by KaizenNU, Today, 06:28 PM
      0 responses
      9 views
      0 likes
      Last Post KaizenNU  
      Started by bertochi, Today, 05:25 PM
      0 responses
      15 views
      0 likes
      Last Post bertochi  
      Started by ntbone, 02-12-2025, 01:51 AM
      4 responses
      27 views
      0 likes
      Last Post ntbone
      by ntbone
       
      Started by XanderT, Today, 03:38 PM
      4 responses
      13 views
      0 likes
      Last Post XanderT
      by XanderT
       
      Working...
      X