17

Hi Everyone,

I'm learning to write programs in mql. I wrote a piece of code to identifier possible beginning of trend

i'm using three averages to do that: Moving average Period = 20  :  mediumMovingAvg

Moving average Period = 8     :  fastMovingAvg

Moving average Period = 60   :  slowMovingAvg

so the idea is,

1. When fastMovingAvg crosses mediumMovingAvg and slowMovingAvg going up this will be an indicator that there is possible up trend

2.  When fastMovingAvg crosses mediumMovingAvg and slowMovingAvg going down this will be an indicator that there is possible down trend

How ever when i test it seems to be only executing  'Comment("Up trend started");' when i test on strategy tester, can some please help!!

Here is the code below...

```void OnTick()

{
if(LastActiontime!=Time[0])
{

double TakeProfitLevel;
double StopLossLevel;

TakeProfitLevel=Bid-TakeProfit*Point;   //0.0001
StopLossLevel=Bid+StopLoss*Point;

double mediumMovingAvg=iMA(NULL,0,20,1,MODE_EMA,PRICE_CLOSE,0);
double lastMediumMovingAvg=iMA(NULL,0,20,1,MODE_EMA,PRICE_CLOSE,1);

double fastMovingAvg=iMA(NULL,0,8,0,MODE_EMA,PRICE_CLOSE,0);
double lastFastMovingAvg=iMA(NULL,0,8,0,MODE_EMA,PRICE_CLOSE,1);

double slowMovingAvg=iMA(NULL,0,80,0,MODE_SMA,PRICE_CLOSE,0);
double lastSlowMovingAvg=iMA(NULL,0,80,0,MODE_SMA,PRICE_CLOSE,1);

if((lastFastMovingAvg<lastMediumMovingAvg) && (fastMovingAvg>mediumMovingAvg))
{
if((lastFastMovingAvg<lastSlowMovingAvg) && (fastMovingAvg>slowMovingAvg))
{
Comment("Up trend started");
}
}

if((lastFastMovingAvg>lastMediumMovingAvg) && (fastMovingAvg<mediumMovingAvg))
{
if((lastFastMovingAvg>lastSlowMovingAvg) && (fastMovingAvg<slowMovingAvg))
{
Comment("Down trend started");
//SendMail("Signal SELL Alert("+Symbol()+")","Hey there! Just letting you know that you have a sell signal.");
}
}

LastActiontime=Time[0];
}
}```

Moderator
16563

Do not double post.

I have deleted your duplicate post.

Use the insert code button (Alt + S) when posting code. I have edited your post for you this time.

689

This code only executes when a new bar emerges. In this case the iMA values that you get for shift 0 and 1 are almost identical. You need to look at the last two bars like so:

```double mediumMovingAvg    =iMA(NULL,0,20,1,MODE_EMA,PRICE_CLOSE,1);
double lastMediumMovingAvg=iMA(NULL,0,20,1,MODE_EMA,PRICE_CLOSE,2);

double fastMovingAvg      =iMA(NULL,0, 8,0,MODE_EMA,PRICE_CLOSE,1);
double lastFastMovingAvg  =iMA(NULL,0, 8,0,MODE_EMA,PRICE_CLOSE,2);

double slowMovingAvg      =iMA(NULL,0,80,0,MODE_SMA,PRICE_CLOSE,1);
double lastSlowMovingAvg  =iMA(NULL,0,80,0,MODE_SMA,PRICE_CLOSE,2);
```

And then you check for a double upcross for long and a double downcross for short. That is, only when fast MA crosses medium MA and fast MA crosses slow MA you raise the alarm, how often is this to happen within a single tick?

I'd go with something like that:

```if(lastFastMovingAvg<lastMediumMovingAvg && fastMovingAvg>mediumMovingAvg // up cross
&&
mediumMovingAvg>slowMovingAvg)```

and similar for down trend:

```if(lastFastMovingAvg>lastMediumMovingAvg && fastMovingAvg<mediumMovingAvg // down cross
&&
mediumMovingAvg<slowMovingAvg)```