Exit a trade when the distance between a Fast MA and a Slow MA reduces by a set amount

 

I am trying to code an EA that exits a trade when the distance between a customised Fast MA and a customised Slow MA reduces by a set amount.

The fast MA is L1

The slow MA is L5

L1 and L5 are produced by a separate indicator called CW

If the difference in value between them reduces by a certain amount, which is set as an external variable "Reduction_Amount", the trade closes. Obviously this would need to be an Absolute Number to cater for Long and Short positions.

Anyone got some suggestions for a newbie??

 
Willforth:

Anyone got some suggestions for a newbie??

Write the EA and see how well it works.
 
Willforth:
Anyone got some suggestions for a newbie??
Since there are no slaves here, there are only two choices: learn to code or pay someone. We're not going to code it FOR you. We are willing to HELP you.
 

Guys,

sorry, this came out wrong! I have no intention of asking someone to code for me. I want to learn. Please forgive my somewhat blunt request.

This is the code I have written:-

I have an external variable:-

extern int MAReduction=2; //Number of previous bars

The code I have written for exit is below

//--------------------------------------------------------------- 5 --

double L_1=iCustom(NULL,0,"CW",4,0);

double L_5=iCustom(NULL,0,"CW",5,0);

double MA_c=L_1;

double MA_p=L_1-MAReduction;

//-------------------------------------------------------------- 5b --

//Trade Exit Criteria

if ((MA_c<MA_p)&&(MA_p>MA_c)) //Criterion for closing a Buy

{

Cls_B=true;

}

if ((MA_c>MA_p)&&(MA_p<MA_c)) //Criterion for closing a Sell

{

Cls_S=true;

}

At the moment, the EA is closing the trades as fast as it is opening them. It's like a Scalper with PMS!!

 

Please use this to post code . . . it makes it easier to read.

 

I don't follow why you are subtracting a number of bars from a price ?

extern int MAReduction = 2; //Number of previous bars


double L_1 = iCustom(NULL,0,"CW",4,0);  //  price value


double MA_p = L_1 - MAReduction;    // <----  price - number of bars ( 2 )
 

MA_c is the current value for L_1

What I want to know is what was the value of L_1 2 bars before (MAReduction) which would then be assigned to MA_p.

I am beginning to think this will be impossible within the EA. It will have to be calculated within the separate indicator

Oh and thanks, next time I will use the SRC button

 
Willforth:

MA_c is the current value for L_1

What I want to know is what was the value of L_1 2 bars before (MAReduction) which would then be assigned to MA_p.

Like this ?

extern int MAReduction = 2; 


double L_1 = iCustom(NULL,0,"CW",4,0);  //  price value


double MA_p = L_1 - iCustom(NULL,0,"CW",4,MAReduction);  // <---  change the shift to MAReduction (2)
 

If you lived locally, I would come round there and hug you (in a manly kinda way!)

Thanks!!!