• If this is your first visit, you will have to register before you can post. To view messages, please scroll below and select the forum that you would like to visits. Questions? Be sure to check out the Forum FAQ.

Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Question for experienced mql4 coders

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

    Question for experienced mql4 coders

    Alright, I have a fully functional indicator. It is a massive processor hog. But, it is only this way when I first put it on a chart. The reason is because of the way CurrentBar works.

    If you aren't familiar, but want to have a crack at solving this problem, just put this code in the OnBarUpdate() method:

    Code:
    Print("CurrentBar: "+CurrentBar);
    What you will see in the data window is the bar reference number, for every bar on the chart, starting from bar zero thru whatever the most recently closed bar is.

    This is what is causing my problem. On every pointer update (this is not a new tick update), the indicator processes all the bars up to the CurrentBar reference. My indicator requires a minimum of 41 bars, so I have the code,

    Code:
    if (CurrentBar < 41) return;
    already in my indicator in the OnBarUpdate() method. So, any pointer reference up to 41 gets ignored.

    The problem is, it is common to have 300-500 bars on the chart when using the indicator. So, what happens is, on bar reference 41, it process those 41 bars, then 42 bars, then 43 bars, then 44 bars, etc., until the CurrentBar pointer finally hits the most recently closed bar on the chart.

    This results in, every time I drop my indie on a chart with a reasonable number of bars, or change any of the settings, I have to sit and wait about 5 minutes for it to update. I tried once with loading 500 bars on my chart, and I ended up waiting about 30 minutes before it finally displayed. So, the problem is a pretty big one.

    Now.....whatever you do......do NOT tell me increase the minimum number of bars required for CurrentBar. Telling me this means you haven't given this issue any serious thought. The number of bars on the chart must be allowed to change, and I cannot know what the end users will have on their charts. The issues being, if I set the minimum required number too high, then they may not be able to load enough data. If I set the minimum number too low....then they could be waiting for a couple hours because they had to change a setting in the indicator.

    So, here is what I need to know: Is there a way to instantly get a value for the total number of bars loaded on a chart? And, as I just got through pointing out, CurrentBar does not give this value instantly. It recursively processes through every bar on the chart before reaching the last bar.

    ANY useful ideas to solve this issue will be greatly appreciated!
    Last edited by Antny; 05-02-2014, 06:39 PM.

    #2
    Hi Antny

    NinjaScript has a function for returning the number of bars current loaded in a chart. It is called "Count"

    use it like this

    Print ("Number of bars in chart = "+Count);

    So if you want your indi only to run for the last 50 bar for example you would put this code at the beginning

    if (CurrentBar < (Count - 50)) return;

    Chances are (although without seeing your indicator I cant tell you) that your indi is capable of doing all its maths by just looking back at the relevant bars. Therefor you can have it only start running on the bar before the last like this

    if (CurrentBar < (Count - 2)) return;

    This should make your indi super fast.

    Comment


      #3
      Originally posted by marty087 View Post
      Hi Antny

      NinjaScript has a function for returning the number of bars current loaded in a chart. It is called "Count"

      use it like this

      Print ("Number of bars in chart = "+Count);

      So if you want your indi only to run for the last 50 bar for example you would put this code at the beginning

      if (CurrentBar < (Count - 50)) return;

      Chances are (although without seeing your indicator I cant tell you) that your indi is capable of doing all its maths by just looking back at the relevant bars. Therefor you can have it only start running on the bar before the last like this

      if (CurrentBar < (Count - 2)) return;

      This should make your indi super fast.
      Thank you! This is what I have been trying to find since I started trying to program in NinjaScript a few weeks ago. Happy days are ahead now!

      As for whether my indie is doing everything else it needs to, I already have that nailed down. The only issue was the recursive processing as CurrentBar updated the pointer. Now when i drop the indie on a chart, or change a setting, it only takes a few seconds....instead of several minutes.
      Last edited by Antny; 05-03-2014, 06:40 AM.

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by maggie3, Today, 02:05 AM
      0 responses
      3 views
      0 likes
      Last Post maggie3
      by maggie3
       
      Started by qewcool, Today, 01:14 AM
      0 responses
      3 views
      0 likes
      Last Post qewcool
      by qewcool
       
      Started by fredrikn, 05-08-2019, 04:01 AM
      17 responses
      669 views
      1 like
      Last Post emtrader  
      Started by zacharydw00, Yesterday, 10:19 PM
      0 responses
      3 views
      0 likes
      Last Post zacharydw00  
      Started by chafan11, Yesterday, 09:28 PM
      0 responses
      7 views
      0 likes
      Last Post chafan11  
      Working...
      X