| / | Forum |
|
MrH
2012.01.07 23:42
I want to create a moving sum of the sign of the bars: an up bar is +1, a down bar is -1, on a window of adjustable size: how do I work with a window? should be extern int window=10; and then?? for(int i=0; i<limit; i++) { if (iClose(0,0,i)>iClose(0,0,i+1)) sign[i]=1; else sign[i]=-1; sum[i]+=sum[i]; } |
|
MQL4 Language for Newbies. Difficult Questions in Simple Phrases. This is the second article from the series "MQL4 Language for Newbies". Now we will examine more complex and advanced constructions of the language, learn new options and see, how they can be applied in everyday practice. You will get acquainted with a new cycle type 'while', new condition type 'switch', operators 'break' and 'continue'. Besides you will learn to write your own functions and work with multidimensional arrays. And for a dessert I have prepared an explanation about a preprocessor. |
|
WHRoeder
2012.01.08 14:46
int extern int Window.Length = 10; : SetIndexDrawBegin(0, Window.Length+1); : int counted = IndicatorCounted(); if (counted <= Window.Length) counted = Window.Length+1; // Look back for(int iBar = Bars - 1 - counted; iBar >= 0; iBar--){ sum[iBar]=0; for (int iWin = iBar + Window.Length - 1; iWin >= iBar; iWin--) if (Close[iWin] > Close[iWin+1]) sum[iBar] += 1; else sum[iBar] -= 1; } |
|
dabbler
2012.01.08 15:43
WHRoeder: Yes, I thought about doing it that way, then I realised how horribly inefficient it would be, especially if the Window is quite long. It seems that you could probably use the built-in moving average on array function of the plus or minus values. |
|
WHRoeder
2012.01.08 16:39
Make it 100. How inefficient is it to test 100 values and increment/decrement 100 times per tick? Unnoticeable on a MHz machine and I'll bet yours is a GHz. Don't optimize until you know you have a problem. Then don't update bar zero (ibar >= 0 becomes iBar > 0). Now your testing/incrmenting once per new bar.
|
|
dabbler
2012.01.08 17:14
WHRoeder: Make it 100. How inefficient is it to test 100 values and increment/decrement 100 times per tick? Unnoticeable on a MHz machine and I'll bet yours is a GHz. Don't optimize until you know you have a problem. Then don't update bar zero (ibar >= 0 becomes iBar > 0). Now your testing/incrmenting once per new bar. That's true. I just hate the idea of inefficient code when all you need to do is add the new one and subtract the old one. I was brought up with punched cards, operating systems on mylar tape, and trying to fit code into the 1K memory of a ZX81! It's hard for me to code things non-optimally :-) And even then on my dual core GHz machine my indicators run too slowly, lagging behind the market noticeably, and I have been thinking about upgrading to a ram-disk hard drive to speed things up. EDIT: and of course when you change profiles, or change accounts, or change timeframe, and the indicator recalculates from scratch, this part of the calculation would then be 50x longer with the simple "unoptimized" method. |
|
MrH
2012.01.08 18:14
Thank you both.
|