How to code? - page 301

 

Hi Mladen,

Thanks for the explanation, but what do you mean by the first closed bars value? Is it the first closed bars value or the current bars or after? What about then meaning of current+0 or current+1?

regards

Terrance

mladen:
Terrance

MODE_MAIN means you are reading in the value of the stochastic line. MODE_SIGNAL means you are reading in the value of the stochastic signal line.

As of SHIFT : it is the same for every indccator (even the custom ones). As an example : SHIFT=0 means current bars value, SHIFT=1 means the first closed bars value and so on ...
 

...

Terrance

Current bar is by definition still not a closed bar

First bar before the current bar is the first closed bar

tkuan77:
Hi Mladen,

Thanks for the explanation, but what do you mean by the first closed bars value? Is it the first closed bars value or the current bars or after? What about then meaning of current+0 or current+1?

regards

Terrance
 

Hi Mladen,

So am I right to say that:

current+0 is the same as the value 0,

current+1 is the same as the value 1,

current+2 is the same as the value 2,

and so on..... for SHIFT?

E.g:

iStochastic(NULL, 0, 3, 3, 3, MODE_SMMA, 0, MODE_SIGNAL, Current + 1); is the same as iStochastic(NULL, 0, 3, 3, 3, MODE_SMMA, 0, MODE_SIGNAL, 1);

Am I right to say so?

Regards

Terrance

mladen:
Terrance

Current bar is by definition still not a closed bar

First bar before the current bar is the first closed bar
 

...

Terrance

iStochastic(NULL, 0, 3, 3, 3, MODE_SMMA, 0, MODE_SIGNAL, 0); is the current

iStochastic(NULL, 0, 3, 3, 3, MODE_SMMA, 0, MODE_SIGNAL, 1); is the first closed (previous)

...

...

iStochastic(NULL, 0, 3, 3, 3, MODE_SMMA, 0, MODE_SIGNAL,Bars-1); is the oldest on the chart

PS: at this post you have sime more info about bars : https://www.mql5.com/en/forum/173124

tkuan77:
Hi Mladen,

So am I right to say that:

current+0 is the same as the value 0,

current+1 is the same as the value 1,

current+2 is the same as the value 2,

and so on..... for SHIFT?

E.g:

iStochastic(NULL, 0, 3, 3, 3, MODE_SMMA, 0, MODE_SIGNAL, Current + 1); is the same as iStochastic(NULL, 0, 3, 3, 3, MODE_SMMA, 0, MODE_SIGNAL, 1);

Am I right to say so?

Regards

Terrance
 

Hi Mladen,

Thanks for the great help as always!

Regards

Terrance

mladen:
Terrance

iStochastic(NULL, 0, 3, 3, 3, MODE_SMMA, 0, MODE_SIGNAL, 0); is the current

iStochastic(NULL, 0, 3, 3, 3, MODE_SMMA, 0, MODE_SIGNAL, 1); is the first closed (previous)

...

...

iStochastic(NULL, 0, 3, 3, 3, MODE_SMMA, 0, MODE_SIGNAL,Bars-1); is the oldest on the chart

PS: at this post you have sime more info about bars : https://www.mql5.com/en/forum/173124
 

A few problems

Hello all,

I've tested my code and its very almost there. I have a few questions I hope you can help me with.

I have put this in int init():

if (Bars < D1FastMAPeriod || Bars < D1SlowMAPeriod)

{

Alert("ERROR- INSUFFICIENT BARS TO CALCULATE SMA ON DAILY CHART");

return(0);

}

The SlowMAPeriod is 200. When I backtest from 01.01.2009 it raises this error even though I can clearly see on my chart that there are enough bars to calculate the 200SMA from 2008. Am I missing something here?

2. I place trades of the H4 chart but only in the direction of the daily trend. I draw values by using the following code:

SlowMACurrent = iMA(Symbol(), PERIOD_D1, D1SlowMAPeriod, 0, MODE_SMA, PRICE_CLOSE, 1);

FastMACurrent = iMA(Symbol(), PERIOD_D1, D1FastMAPeriod, 0, MODE_SMA, PRICE_CLOSE, 1);

Then when I am searching for a long position I reference it by using:

if (FastMACurrent > SlowMACurrent && ... etc ... )

Is this correct as it doesnt seem to be filtering my trades properly?

3. I also use a trend filter on the H4 chart by only taking long positions when price has exceeded the previous high of the past 120 periods (and then remains above the low of the 120 periods). I use this code:

Donchian_Low = Low;

Donchian_High = High;

static bool UpTrend = FALSE;

static bool DownTrend = FALSE;

if (Ask > Donchian_High) {UpTrend = TRUE; DownTrend = FALSE;}

if (Bid < Donchian_Low) {UpTrend = FALSE; DownTrend = TRUE;}

Then I use the following code (say for long positions):

if (FastMACurrent > SlowMACurrent && UpTrend == TRUE && DownTrend == FALSE ... etc ... )

But it doesnt seem to work because when I check my backtested chart against a Donchian Channel of 120 Periods it doesnt tie up. Any ideas?

Thanks in advance.

 

...

1. Place it at the start of the start() function. Init is unreliable when data like Bars is concerned

2. That condition is true whenever FastMACurrent > SlowMACurrent. Is that what you wanted or maybe you are looking fro crosses?

3. It sis probably in relation with point 2. You must "narrow" the conditions when orders can be entered since this way it covers too much posibilities

crsnape@btinternet.com:
Hello all,

I've tested my code and its very almost there. I have a few questions I hope you can help me with.

I have put this in int init():

if (Bars < D1FastMAPeriod || Bars < D1SlowMAPeriod)

{

Alert("ERROR- INSUFFICIENT BARS TO CALCULATE SMA ON DAILY CHART");

return(0);

}

The SlowMAPeriod is 200. When I backtest from 01.01.2009 it raises this error even though I can clearly see on my chart that there are enough bars to calculate the 200SMA from 2008. Am I missing something here?

2. I place trades of the H4 chart but only in the direction of the daily trend. I draw values by using the following code:

SlowMACurrent = iMA(Symbol(), PERIOD_D1, D1SlowMAPeriod, 0, MODE_SMA, PRICE_CLOSE, 1);

FastMACurrent = iMA(Symbol(), PERIOD_D1, D1FastMAPeriod, 0, MODE_SMA, PRICE_CLOSE, 1);

Then when I am searching for a long position I reference it by using:

if (FastMACurrent > SlowMACurrent && ... etc ... )

Is this correct as it doesnt seem to be filtering my trades properly?

3. I also use a trend filter on the H4 chart by only taking long positions when price has exceeded the previous high of the past 120 periods (and then remains above the low of the 120 periods). I use this code:

Donchian_Low = Low;

Donchian_High = High;

static bool UpTrend = FALSE;

static bool DownTrend = FALSE;

if (Ask > Donchian_High) {UpTrend = TRUE; DownTrend = FALSE;}

if (Bid < Donchian_Low) {UpTrend = FALSE; DownTrend = TRUE;}

Then I use the following code (say for long positions):

if (FastMACurrent > SlowMACurrent && UpTrend == TRUE && DownTrend == FALSE ... etc ... )

But it doesnt seem to work because when I check my backtested chart against a Donchian Channel of 120 Periods it doesnt tie up. Any ideas?

Thanks in advance.
 

Hi mladen,

1. OK will do.

2. Yes thats right I dont want to enter off the cross, just using it for a long/ short filter on shorter term charts, for instance if the fast MA is above the slow MA on the daily chart I only want it to consider long positions on the H4 chart. So I think I've coded this correctly?

3. Not sure if I know what you mean, but Ive put this under init start()

static bool UpTrend = FALSE;

static bool DownTrend = FALSE;

if (Ask > Donchian_High) {UpTrend = TRUE; DownTrend = FALSE;}

if (Bid < Donchian_Low) {UpTrend = FALSE; DownTrend = TRUE;}

Should I put the static bool variables at the very beginning to make them global? Could this be causing it?

 

...

3. What happens when Ask Donchian_Low (which is most of the time). Your static variables still show the "old" states even though it is not valid any more (it "inherits" the state and that way it is signaling that it is above or bellow even when it is not any more). Check if that is what is causing you problems

crsnape@btinternet.com:
Hi mladen,

1. OK will do.

2. Yes thats right I dont want to enter off the cross, just using it for a long/ short filter on shorter term charts, for instance if the fast MA is above the slow MA on the daily chart I only want it to consider long positions on the H4 chart. So I think I've coded this correctly?

3. Not sure if I know what you mean, but Ive put this under init start()

static bool UpTrend = FALSE;

static bool DownTrend = FALSE;

if (Ask > Donchian_High) {UpTrend = TRUE; DownTrend = FALSE;}

if (Bid < Donchian_Low) {UpTrend = FALSE; DownTrend = TRUE;}

Should I put the static bool variables at the very beginning to make them global? Could this be causing it?
 

Good point. Il have a look into that.

A question about functions, is it possible to call a function within a function? E.g. I have this function:

string GetWinLossPreviousShort (int LastOpenTicket, string WinLossPreviousShort)

{

if (... etc

Than later I call it:

double GetLotsLong (int LowRisk, int HighRisk, double SLDistanceLong, string GetWinLossPreviousShort)

Reason: