How to code? - page 322

 

Hi,

my request is for info only, for the moment.

If i have an indicator that create an offline chart without calculate the past but only the live data it is possible to attach it in the strategy tester template and create an offline chart with the data from the history?

Thank you.

 
mladen:
jayjonbeach,

You are testing the value of trendCurr over and over in the loop without changing it (so it will always return NONE)

Try putting the iCustom() in the loop and then test that value. Something like this :

if (trendCurr>0)

{

for(int i=1; i<=lookback; i++)

{

int testValue = iCustom(NULL,StepMaTimeFrame,"StepMA_v7",Length,Kv,StepSize,MA_Mode,Advance,Percentage,UpDownShift,HighLow,ColorMode,5,StepMaBarToTest+i);

if (testValue<0) return(LONG);

}

return(NONE);

}

Dear sir. I have only two words about this, but they bear repeating...

THANK YOU, THANK YOU and THANK YOU!!!

Indeed, your code worked perfectly, and I'd wager you saved me another12 or many more hours of examining other 'for' loops, trial and error etc before I finally stumbled across what you posted.

I actually thought that I wanted to test trendCurr over and over in the loop but I see the error now thanks to you, and I see how your solution of changing the Stepbartotest with "i" is indeed the perfect solution, using "i" here is actually key yet doesn't seem entirely obvious, at least to a novice like me it might have been some time before I figured this out...

While your code worked as intended, my entire block is not, there is a very interesting unintended outcome to having this loop run inside the LONG criteria:

if(TwentyOneEMA > FiftyFiveEMA)

{

if (trendCurr>0) // check Step filter current

{

if (Bid > TwentyOneEMA)

{

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

{

int testValue = iCustom(NULL,StepMaTimeFrame,"StepMA_v7",Length,Kv,StepSize,MA_Mode,Advance,Percentage,UpDownShift,HighLow,ColorMode,5,StepMaBarToTest+i);

if (testValue<0) return(LONG);

}

return(NONE);

}

}

}

What happens now, is when the ifs are met, the loop is tested and if true it buys, if not it does nothing, great. However, the whole block of code is acting like a "while" loop, since as long as if(TwentyOneEMA > FiftyFiveEMA) is true, the rest of the code below it KEEPS running, and if other conditions all become true, the sucker buys! I see the obvious error of my code structure here.... (I'm thinking another similar for loop could test the EMA's, to make sure the EMA cross is a NEW one)

This isn't necessarily a bad thing however (as some analysis would imply), in fact this is kind of like what my next goal was, in a different way, it needs much more testing but for now I am going to leave this revision in place on this bot and start the next revisions on a copy.

What I was going to do after I got it looking at the past, was if the past loop turns out false, I wanted it to check the NEXT 10 bars for a Step signal, and if it becomes true, take the trade (I was thinking about using a while loop for this?). So this is sort of happening already, except there is NO 10 bar limit in its look forward, and I'm not sure how I should structure the whole thing to work correctly. (I think I could check Step signal, and if true look for new MA cross in last 10 bars but I have to repeat all the block for this and think there must be better more efficient/elegant way)

Thank you again for your help, this is going to come in VERY handy in future coding I am sure.

 

Hi,

I try to modify the indicator attached. Pratically when a moving everage touch the price it alarm and change the time frame where it is touched. I put a delay in the alarm that is respect but after an alarm if i change the timeframe he ingnore it.

It can be fixed?

Thank youmedie_mobili.mq4

Files:
 

dasio

With indicators you can not change that. Indicators are resetting global scope and static variables to default values when you do that, so you can not keep some values when changing time frame or symbol. You could use the global variables (the ones managed with "GlobalVariable..." functions) but that would make your code very complicated (for multiple instances, multiple symbols, ...) and in my opinion it is not worth the effort

dasio:
Hi,

I try to modify the indicator attached. Pratically when a moving everage touch the price it alarm and change the time frame where it is touched. I put a delay in the alarm that is respect but after an alarm if i change the timeframe he ingnore it.

It can be fixed?

Thank youmedie_mobili.mq4
 

...

Just one addition to previous post : one thing that is not so widely known - EAs are treating those variables (global scope and static variables) differently - they are not reset when time frame or symbol is changed. Here is a very, very simple EA that will show to those that are interested in it how those values are not reset in EAs like in indicators

int counter = 0;

int init()

{

return(0);

}

int start()

{

static int counterStart = 0;

counterStart++;

counter++;

Comment("counter from start : "+counterStart," counter using global scope : "+counter);

return(0);

}

That even is a cause for some errors in some Eas (if the EA does not take into account that the variable is not going to be reset when time frame or symbol are changed some very odd errors in EA work can happen)

 
mladen:
dasio From the code I can not see what is the intention of the code. Anyway, here is a code in which you can chose if you wish to use the true range as the "basic" value of the indicator (it is the first part of your code) or you wish to use the second part of your code as the "basic" value

Another time thank you mladen.

It is possible to add another line based on this condition?

Draw an horizontal line = to the everage of the Work[] that are > of Avg[] always based on days period?

Thank you

 

Signal Amendment

Hi Mladen,

I want to add stop selling or stop buying signal to this code.....e.g if stochastic signal line is over the main, stop selling e.t.c,I added it to the code but it is not working...pls help

if(Period()==240)

{

static datetime lastAlerted=0;

double ist_main=iStochastic(NULL,240,8,3,3,MODE_SMA,0,MODE_MAIN,0);

double ist_signal=iStochastic(NULL,240,8,3,3,MODE_SMA,0,MODE_SIGNAL,0);

double RSIP1=iRSI(NULL,240,14,PRICE_CLOSE,0);

double RSIP2=iRSI(NULL,240,70,PRICE_CLOSE,0);

double b4enCCI=iCCI(NULL,240,6,PRICE_TYPICAL,1);

double nowenCCI=iCCI(NULL,240,6,PRICE_TYPICAL,0);

double b4trCCI=iCCI(NULL,240,14,PRICE_TYPICAL,1);

double nowtrCCI=iCCI(NULL,240,14,PRICE_TYPICAL,0);

// alerts

if((ist_main>ist_signal)&&(RSIP1>RSIP2)&&(nowenCCI >0&&nowenCCI>b4enCCI)&&(nowtrCCI>0&&nowtrCCI>b4trCCI)&&(lastAlerted!=Time[0]))

{

lastAlerted=Time[0]; Alert("Buy Arrow","\n","Current time is ",TimeToStr(CurTime()),"\n",Symbol());

}

if((ist_main<ist_signal)&&(RSIP1<RSIP2)&&(nowenCCI <0&&nowenCCI<b4enCCI)&&(nowtrCCI<0&&nowtrCCI<b4trCCI)&&(lastAlerted!=Time[0]))

{

lastAlerted=Time[0]; Alert("Sell Arrow","\n","Current time is ",TimeToStr(CurTime()),"\n",Symbol());

}

}

 

You can try something like this :

if(Period()==240)

{

static datetime lastAlerted=0;

double ist_main=iStochastic(NULL,240,8,3,3,MODE_SMA,0,MOD E_MAIN,0);

double ist_signal=iStochastic(NULL,240,8,3,3,MODE_SMA,0,M ODE_SIGNAL,0);

double RSIP1=iRSI(NULL,240,14,PRICE_CLOSE,0);

double RSIP2=iRSI(NULL,240,70,PRICE_CLOSE,0);

double b4enCCI=iCCI(NULL,240,6,PRICE_TYPICAL,1);

double nowenCCI=iCCI(NULL,240,6,PRICE_TYPICAL,0);

double b4trCCI=iCCI(NULL,240,14,PRICE_TYPICAL,1);

double nowtrCCI=iCCI(NULL,240,14,PRICE_TYPICAL,0);

// alerts

alertType = "do nothing";

if((ist_main>ist_signal)&&(RSIP1>RSIP2)&&(nowenCCI >0&&nowenCCI>b4enCCI)&&(nowtrCCI>0&&nowtrCCI>b4trC CI)&&(lastAlerted!=Time[0]))

{

lastAlerted=Time[0]; Alert("Buy Arrow","\n","Current time is ",TimeToStr(CurTime()),"\n",Symbol()); alertType="buy";

}

if((ist_main<ist_signal)&&(RSIP1<RSIP2)&&(nowenCCI <0&&nowenCCI<b4enCCI)&&(nowtrCCI<0&&nowtrCCI<b4trC CI)&&(lastAlerted!=Time[0]))

{

lastAlerted=Time[0]; Alert("Sell Arrow","\n","Current time is ",TimeToStr(CurTime()),"\n",Symbol()); alertType="sell";

}

if (alertType=="do nothing")

{

lastAlerted=Time[0]; Alert("Stop previous action","\n","Current time is ",TimeToStr(CurTime()),"\n",Symbol());

}

}
Mastercash:
Hi Mladen,

I want to add stop selling or stop buying signal to this code.....e.g if stochastic signal line is over the main, stop selling e.t.c,I added it to the code but it is not working...pls help

if(Period()==240)

{

static datetime lastAlerted=0;

double ist_main=iStochastic(NULL,240,8,3,3,MODE_SMA,0,MODE_MAIN,0);

double ist_signal=iStochastic(NULL,240,8,3,3,MODE_SMA,0,MODE_SIGNAL,0);

double RSIP1=iRSI(NULL,240,14,PRICE_CLOSE,0);

double RSIP2=iRSI(NULL,240,70,PRICE_CLOSE,0);

double b4enCCI=iCCI(NULL,240,6,PRICE_TYPICAL,1);

double nowenCCI=iCCI(NULL,240,6,PRICE_TYPICAL,0);

double b4trCCI=iCCI(NULL,240,14,PRICE_TYPICAL,1);

double nowtrCCI=iCCI(NULL,240,14,PRICE_TYPICAL,0);

// alerts

if((ist_main>ist_signal)&&(RSIP1>RSIP2)&&(nowenCCI >0&&nowenCCI>b4enCCI)&&(nowtrCCI>0&&nowtrCCI>b4trCCI)&&(lastAlerted!=Time[0]))

{

lastAlerted=Time[0]; Alert("Buy Arrow","\n","Current time is ",TimeToStr(CurTime()),"\n",Symbol());

}

if((ist_main<ist_signal)&&(RSIP1<RSIP2)&&(nowenCCI <0&&nowenCCI<b4enCCI)&&(nowtrCCI<0&&nowtrCCI<b4trCCI)&&(lastAlerted!=Time[0]))

{

lastAlerted=Time[0]; Alert("Sell Arrow","\n","Current time is ",TimeToStr(CurTime()),"\n",Symbol());

}

}
 

Well I am stuck again. I am trying to correct the EA from looking into the future due to the way my code was structured.

My signal code starts off with an "if", and as long as that is true, the rest of the code following keeps executing, as it should (this wasn't an issue before inserting the first for loop but now it is). So I have tried a bunch of different things to correct this, but nothing is working I have also checked the manual but nothing stood out there, maybe two "for" loops in same block is not allowed?

Here is what I think is my best effort (just showing LONG for brevity), no errors but the EA makes no trades, so likely I have screwed up the loop again:

if (NumOrders == 0)

{

if(TwentyOneEMA > FiftyFiveEMA+Separation)

{

if (trendCurr>0)

{

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

int testValue1 = iMA(NULL,0,EMA1,0,MODE_EMA,PRICE_CLOSE,i);

int testValue2 = iMA(NULL,0,EMA2,0,MODE_EMA,PRICE_CLOSE,i);

if (testValue1 < testValue2-Separation2)

{

if (Bid TwentyOneEMA) // news filter

{

for(i=0; i<=lookback; i++)

{

int testValue3 = iCustom(NULL,StepMaTimeFrame,"StepMA_v7",Length,Kv,StepSize,MA_Mode,Advance,Percentage,UpDownShift,HighLow,ColorMode,5,StepMaBarToTest+i);

if (testValue3 <0 && NumOrders < maxorders) return(LONG);

}

return(NONE);

}

}

}

}

}

==========

Note I tried if (testValue1 < testValue2-Separation2) continue; but it flagged 'continue' it as an error If 2 loops are not allowed, then I guess my next task of adding another loop might be misguided...

 
jayjonbeach:
Well I am stuck again. I am trying to correct the EA from looking into the future due to the way my code was structured.

My signal code starts off with an "if", and as long as that is true, the rest of the code following keeps executing, as it should (this wasn't an issue before inserting the first for loop but now it is). So I have tried a bunch of different things to correct this, but nothing is working I have also checked the manual but nothing stood out there, maybe two "for" loops in same block is not allowed?

Here is what I think is my best effort (just showing LONG for brevity), no errors but the EA makes no trades, so likely I have screwed up the loop again:

if (NumOrders == 0)

{

if(TwentyOneEMA > FiftyFiveEMA+Separation)

{

if (trendCurr>0)

{

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

int testValue1 = iMA(NULL,0,EMA1,0,MODE_EMA,PRICE_CLOSE,i);

int testValue2 = iMA(NULL,0,EMA2,0,MODE_EMA,PRICE_CLOSE,i);

if (testValue1 < testValue2-Separation2)

{

if (Bid TwentyOneEMA) // news filter

{

for(i=0; i<=lookback; i++)

{

int testValue3 = iCustom(NULL,StepMaTimeFrame,"StepMA_v7",Length,Kv,StepSize,MA_Mode,Advance,Percentage,UpDownShift,HighLow,ColorMode,5,StepMaBarToTest+i);

if (testValue3 <0 && NumOrders < maxorders) return(LONG);

}

return(NONE);

}

}

}

}

}

==========

Note I tried if (testValue1 < testValue2-Separation2) continue; but it flagged 'continue' it as an error If 2 loops are not allowed, then I guess my next task of adding another loop might be misguided...

Okay I think I figured it out, it was the way I structured the for loop, because things were not going to "finish" directly after this first loop, it needed to be structured slightly different than the 2nd loop, at least that is the conclusion I came to looking at some other bots. Quick test shows the following working, more testing to do though

NumOrders = CalculateCurrentOrders();

if (NumOrders == 0)

{

if(TwentyOneEMA > FiftyFiveEMA+Separation)

{

if (trendCurr>0)

{

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

{

int testValue1 = iMA(NULL,0,EMA1,0,MODE_EMA,PRICE_CLOSE,i);

int testValue2 = iMA(NULL,0,EMA2,0,MODE_EMA,PRICE_CLOSE,i);

if (testValue1 <! testValue2-Separation2)

{

Print("No Trade");

break;

}

if (Bid TwentyOneEMA) continue; // news filter

for(int j=0; j<=lookback; j++)

{

int testValue3 = iCustom(NULL,StepMaTimeFrame,"StepMA_v7",Length,Kv,StepSize,MA_Mode,Advance,Percentage,UpDownShift,HighLow,ColorMode,5,StepMaBarToTest+j);

if (testValue3 <0 && NumOrders < maxorders) return(LONG);

}

return(NONE);

}

}

}

}

Reason: