Announcement

Collapse

Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

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:
    [COLOR="DarkRed"]Print("CurrentBar: "+CurrentBar);[/COLOR]
    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:
    [COLOR="DarkRed"]if (CurrentBar < 41) return;[/COLOR]
    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 funk10101, Today, 12:02 AM
      1 response
      11 views
      0 likes
      Last Post NinjaTrader_LuisH  
      Started by GLFX005, Today, 03:23 AM
      1 response
      6 views
      0 likes
      Last Post NinjaTrader_Erick  
      Started by nandhumca, Yesterday, 03:41 PM
      1 response
      13 views
      0 likes
      Last Post NinjaTrader_Gaby  
      Started by The_Sec, Yesterday, 03:37 PM
      1 response
      11 views
      0 likes
      Last Post NinjaTrader_Gaby  
      Started by vecnopus, Today, 06:15 AM
      0 responses
      1 view
      0 likes
      Last Post vecnopus  
      Working...
      X