Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

1 Bar Delay, when using Multiple DataSeries

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

    1 Bar Delay, when using Multiple DataSeries

    Hi,
    My code has 15 Data Series Loaded, & I'm trying to make it more efficient by having the main method only run on 1 iteration of the code, and not on every dataseries.

    Right now, if i move the main method into something like
    if (BarsInProgress == 0)
    {
    Main Method();
    }

    or
    if (BarsInProgress == 15) // ie the Last DataSeries
    {
    Main Method();
    }

    Then it will cause a delay in the Signal output.
    > So right now my only option is to have it run on every DataSeries , which is unnecessary
    ​​

    Is there a solution to this?
    Thanks in advance :-)

    Code:
            
            private bool runOnce = true;
            private int LastBarsArray = 0;​
    
            protected override void OnBarUpdate()
            {
    
                if (runOnce)
                {
                    var totalDataSeries = BarsArray.Length; // Get the total number of data series loaded
                    LastBarsArray = totalDataSeries - 1;
                    runOnce = false;
                }
    
                if (BarsInProgress != 0)
                {
                    UpdateSeries();
                    GetPrices();
    
                    Main_LogicA(); // The Signal is Fine, if i put the Main Methods Here. But it Runs on every DataSeries (ie 10+ )
                    Main_LogicB();
               }
    
    
                if (BarsInProgress == LastBarsArray)
                 {
                    Main_LogicA(); // If I Put the Main Method at the end of the OnBarUpdate. Then it Causes  the output Signal to delay by 1 bar
                    Main_LogicB();
                  }​
    
                      
            }​​
    
    private void Main_LogicA()
    {
    
    // Print the BarsInProgress index and the current close price for the series
    Print("BarsInProgress: " + BarsInProgress + " | CurrentBar: " + CurrentBar + " | Close Price: " + Closes[BarsInProgress][0]);
    
    }
    
    output
    BarsInProgress: 1 | CurrentBar: 20718 | Close Price: 1.08894
    BarsInProgress: 2 | CurrentBar: 20718 | Close Price: 0.85164
    BarsInProgress: 3 | CurrentBar: 20718 | Close Price: 1.48824
    BarsInProgress: 7 | CurrentBar: 20718 | Close Price: 0.96846​
    Last edited by yertle; 06-09-2024, 09:42 PM.

    #2
    Hello yertle,

    Thank you for your inquiry.

    OnBarUpdate() will still run for every data series added to the script. However, to prevent further processing from occurring, the suggested solution would be to have something like:

    Code:
    //prevent further processing unless the primary data series is being updated
    if (BarInProgress != 0)
    return;
    This would prevent further processing from occurring on any series that isn't the primary data series of the script.

    Calling your method when BIP != 0 (as it is currently being called in your code) would cause your method to be called for every added data series except the primary series.

    Please let us know if you have any further questions.

    Comment


      #3
      Perfect.
      Thank you

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by Mindset, 04-21-2026, 06:46 AM
      0 responses
      52 views
      0 likes
      Last Post Mindset
      by Mindset
       
      Started by M4ndoo, 04-20-2026, 05:21 PM
      0 responses
      71 views
      0 likes
      Last Post M4ndoo
      by M4ndoo
       
      Started by M4ndoo, 04-19-2026, 05:54 PM
      0 responses
      38 views
      0 likes
      Last Post M4ndoo
      by M4ndoo
       
      Started by cmoran13, 04-16-2026, 01:02 PM
      0 responses
      99 views
      0 likes
      Last Post cmoran13  
      Started by PaulMohn, 04-10-2026, 11:11 AM
      0 responses
      60 views
      0 likes
      Last Post PaulMohn  
      Working...
      X