How to code? - page 270

 

IndicatorCounted and keeping indicator buffer in sync.

Hi,

My question is about sliding the buffer in custom indicator. I asked in another forum but no avail.

E.g:

Code:

#define MAX_LOOK_BACK 5

#property indicator_minimum 0

#property indicator_maximum 100

#property indicator_color1 DodgerBlue

#property indicator_separate_window

double rsiBuf[];

int start()

{

int limit;

int counted_bars = IndicatorCounted();

//---- check for possible errors

if(counted_bars < 0)

return(-1);

//---- the last counted bar will be recounted

if(counted_bars > 0)

counted_bars--;

limit=Bars-counted_bars;

//---- main loop

Print("indicator limit: ", limit);

if(MAX_LOOK_BACK < limit)

limit = MAX_LOOK_BACK;

//Calculate from right to left

for(int i = 0; i < limit; i++)

{

rsiBuf = iRSI(Symbol(), NULL, 14, PRICE_CLOSE, i);

}

}

While running indicator will calculate the latest RSI values of the latest 5 bars. (I put a MAXLOOK_BACK maximum value for easy testing) If for example in a situation that limit==2 (and limit==2 happens alot in my tests when calling via iCustom in a EA) then it will calculate bar 0 and bar 1 values and write to RSI[0] and RSI[1] respectively and won't touch the rest of the buffer. Every custom indicator I've seen is written this way. Shouldn't we slide the buffer if limit < MAX_LOOK_BACK ? as it overwrites the RSI[0] and RSI[1] but the previous values of these elements should be in RSI[2] and RSI[3] am I right?

To visualise :

Tick=0 AND limit = 5 ==> RSI[0] = 33.33 RSI[1] =44,44 RSI[2]=55,55 RSI[3]=66,66 RSI[4] = 77.77

Now assume latest 2 RSI values are 88,88 and 99,99 and array will look like below:

Tick=1 AND limit = 2 ==> RSI[0] = 88,88 RSI[1] =99.99 RSI[2]=55,55 RSI[3]=66,66 RSI[4] = 77.77

BUT shouldn't it be this:

Tick=1 AND limit = 2 ==> RSI[0] = 88,88 RSI[1] =99.99 RSI[2]=33,33 RSI[3]=44,44 RSI[4] = 55.55

When 2 new bars come shouldn't we slide previous bar values by 2? previousWhy all indicators written this way and there's no sliding of values. And why they draw trend lines correctly.

 

I am working on an EA which uses the Keltner band to exit a position (for both profit and loss) however anytime I average down with 2 lots the EA ignores the first entry. Is there any solution for this BESIDES entering with a new chart? Thanks

Files:
 
mezarashii:
I am working on an EA which uses the Keltner band to exit a position (for both profit and loss) however anytime I average down with 2 lots the EA ignores the first entry. Is there any solution for this BESIDES entering with a new chart? Thanks

Post the ea here.

 

The coder has not given me the source code yet, I hope this will be useful in its own right. Any feedback is greatly appreciated.

Files:
 

Conditional Delete Pending

I am looking for a conditional delete pending script or code.

Basically I want to be able to execute:

IF Ask is 200MA H1

Keeping any pending orders open/live below the 200MA

Any Ideas?

 
 
 

Obtain Weekly open prices for 10 Pairs concurrently

Hi, I am new with MT4. I am trying to automate my technique by writing a script to access weekly open prices of 10 pairs concurrently. I can't use a single EA or script to access a weekly chart one by one. Can anyone please advise me what is the best way to do that? Many thanks .....

 
novalight:
Hi, I am new with MT4. I am trying to automate my technique by writing a script to access weekly open prices of 10 pairs concurrently. I can't use a single EA or script to access a weekly chart one by one. Can anyone please advise me what is the best way to do that? Many thanks .....

Hi,

you need to combine iOpen function.

For example lets say that you want to show in comment open prices of gbpusd, eurusd and usdjpy

then code snippet would look like this:

double openPair1 = iOpen("EURUSD",PERIOD_W1,0);

double openPair2 = iOpen("GBPUSD",PERIOD_W1,0);

double openPair3 = iOpen("USDJPY",PERIOD_W1,0);

Comment("EURUSD weekly open: "+openPair1+\nGBPUSD weekly open: "+openPair1+"\nUSDJPY weekly open: "+openPair1);

 

I prefer to have the pairs as input along with 2 additional inputs.

the prefix and suffix inputs avoid the hasle when brokers add something to the basic pair name.

extern string pair_preffix = "";

extern string pair_suffix = ""; // enter m for IBFX mini accounts

extern string pair1 = "EURUSD;

extern string pair2 = "GBPUSD";

extern string pair3 = "USDJPY";

string myPair1, myPair2, myPair3;

int init()

{

myPair1 = pair_prefix + pair1 + pair_suffix;

myPair2 = pair_prefix + pair2 + pair_suffix;

myPair3 = pair_prefix + pair3 + pair_suffix;

return(0);

}

int start()

{

double openPair1 = iOpen(myPair1, PERIOD_W1,0);

double openPair2 = iOpen(myPair2, PERIOD_W1,0);

double openPair3 = iOpen(myPair3, PERIOD_W1,0);

Comment(pair1 + "weekly open: "+openPair1+"\n" + pair2 + " weekly open: "+openPair2+"\n" + pair3 " " weekly open: "+openPair3);

}

Of course I would use arrays for this when using 10 pairs.

Robert Hill aka MrPip