Run code Once Per new bar

Created at 04 Mar 2024, 12:25
How’s your experience with the cTrader Platform?
Your feedback is crucial to cTrader's development. Please take a few seconds to share your opinion and help us improve your trading experience. Thanks!
MI

mihlali700

Joined 22.12.2023

Run code Once Per new bar
04 Mar 2024, 12:25


I am failing to understand why nothing I do can make code only run once , and then wait for a new bar to run again . I have searched this forum for an answer but found no answer .

Here are two methods I found on this forum and they both don't work .

 

 

 [Indicator(AccessRights = AccessRights.None)]
    public class Testing1 : Indicator
    {   

        [Parameter(DefaultValue = "Hello world!")]
        public string Message { get; set; }

        [Output("Main")]
        public IndicatorDataSeries Result { get; set; }

         int lastIndex; 
        protected override void Initialize()
        {
             lastIndex = -1;
        }

        public override void Calculate(int index)
        {
       
              if (index != lastIndex)
           { 
                   
                OnBar( index);
                lastIndex = index;
        
            }
            
        }
        
        
           private void OnBar(int index)
        {  

             
                  Print(" print only once at a new bar " + index);   
                    
                
         }
         
         
         

        }

 



[Indicator(AccessRights = AccessRights.None)]
    public class Testing2 : Indicator
    {
        [Parameter(DefaultValue = "Hello world!")]
        public string Message { get; set; }

        [Output("Main")]
        public IndicatorDataSeries Result { get; set; }

        protected override void Initialize()
        {

        }

        public override void Calculate(int index)
        {
        
        
         if (!IsLastBar)
                Print(" print only once at a new  bar at the moment" + index);
        
          
        }
        
        
        
    }

 

These both produce these results in the log :



Cleary running at every tick instead of the once per bar . Here its running on the 1 chart meaning its supposed to print once and then print again after an Hour 


@mihlali700
Replies

firemyst
06 Mar 2024, 09:54 ( Updated at: 06 Mar 2024, 14:00 )

Duplicate. See this thread:

 

https://ctrader.com/forum/indicator-support/43045

 

 


@firemyst