Can you believe I wrote my own trailing stop EA and I don't have any coding experience?

 

Here it is. I know the brackets are all messed up.

1. This EA does not modify trades but closes them when needed.

2. It can be attached to any chart window with an existing open trade.

3. The parameters are adjustable and defaults are as follows.

AT 10 it starts trailing at 7.

Till 12 it lags by 5 points.

from 12 to 25 it lags 7.

from 25 and on it lags 9.

4. But all values are adjustable.

5. I have also included a sort of delay in execution to avoid spikes, I have used number of ticks which are also adjustable.

6. Use it at your own risk.


*/
//----------------------Variables for Trailing Stop---------------------

extern double lots = 0.1; //Lot Size
extern int FirstSetPoint=10; //LeadFirst
extern int SecondSetPoint=12; //LeadSecond
extern int ThirdSetPoint=25; //LeadThird
extern int FirstTrailPoint=5; //TrailFirst
extern int SecondTrailPoint=7; //TrailSecond
extern int ThirdTrailPoint=10; //TrailThird
extern int FloatingTrailPoint=7; //Order Close Marker
extern int OrderClosePoint=7; //Order Close Marker 2
extern bool CloseOrderFlag = false;
extern int InProfitTicks = 10;
int total, i, PriceDifference, m=0;

//---------------------Variables for Stop Loss----------------------------

extern int n = 0;
extern int StopLoss = 20;
extern int InLossTicks = 6;


//+------------------------------------------------------------------+
//| expert initialization function |
//+------------------------------------------------------------------+
int init()
{
//----

//----
return(0);
}
//+------------------------------------------------------------------+
//| expert deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
//----

//----
return(0);
}
//+------------------------------------------------------------------+
//| expert start function |
//+------------------------------------------------------------------+
int start()
{
//--------Finding the correct trade for trailing stop

total = OrdersTotal();


for(i = 0; i < total; i++)
{
OrderSelect(i, SELECT_BY_POS, MODE_TRADES);

if(OrderSymbol() != Symbol()) continue;


//--------- Trailing Stop on a Buy Order


if(OrderType() == OP_BUY)
{

PriceDifference = ((Bid - OrderOpenPrice())/Point)/10;


if (PriceDifference <= OrderClosePoint && CloseOrderFlag == true)
{ m = m+1;}
else
{m = 0;}

{
if(m >= InProfitTicks){ OrderClose(OrderTicket(),lots,Bid,3,Green); OrderClosePoint =7; CloseOrderFlag = false; }

{
if (PriceDifference >= FirstSetPoint && PriceDifference <= SecondSetPoint)
{FloatingTrailPoint = FirstTrailPoint; CloseOrderFlag = true; }

{
if (PriceDifference > SecondSetPoint && PriceDifference <= ThirdSetPoint) {FloatingTrailPoint = PriceDifference - SecondTrailPoint; CloseOrderFlag = true;}

{
if (PriceDifference > ThirdSetPoint) { FloatingTrailPoint = PriceDifference - ThirdTrailPoint; CloseOrderFlag = true;}
{
if (FloatingTrailPoint > OrderClosePoint) {OrderClosePoint=FloatingTrailPoint;}

}

}
}

}

}
}

//------------------ Trailing Stop on a Sell Order

if(OrderType() == OP_SELL)
{

PriceDifference = ((OrderOpenPrice() - Ask)/Point)/10;


if (PriceDifference <= OrderClosePoint && CloseOrderFlag == true)
{ m = m+1;}
else
{m = 0;}

{


if(m >= InProfitTicks) { OrderClose(OrderTicket(),lots,Ask,3,Green); OrderClosePoint =7; CloseOrderFlag = false; }

{
if (PriceDifference >= FirstSetPoint && PriceDifference <= SecondSetPoint) {FloatingTrailPoint = FirstTrailPoint; CloseOrderFlag = true; }

{
if (PriceDifference > SecondSetPoint && PriceDifference <= ThirdSetPoint) {FloatingTrailPoint = PriceDifference - SecondTrailPoint; CloseOrderFlag = true;}

{
if (PriceDifference > ThirdSetPoint) { FloatingTrailPoint = PriceDifference - ThirdTrailPoint; CloseOrderFlag = true;}
{
if (FloatingTrailPoint > OrderClosePoint) {OrderClosePoint=FloatingTrailPoint;}



}
}
}
}

}
}
//------------------------------------Stop Loss----------------------------------------


if(PriceDifference <= -StopLoss) {n = n+1;}
else {n = 0;}

if(n >= InLossTicks && OrderType() == OP_BUY )
{ OrderClose(OrderTicket(),lots,Bid,3,Red); OrderClosePoint =7; CloseOrderFlag = false; }

if(n >= InLossTicks && OrderType() == OP_SELL )
{ OrderClose(OrderTicket(),lots,Ask,3,Red); OrderClosePoint =7; CloseOrderFlag = false; }






}




//----
return(0);
}
//+------------------------------------------------------------------+

 
When I read the title I just KNEW there had to be an incrementing order select loop nearby. Do me a favour and test this on a chart where there are a few open orders which ALL should be closed and let me know how you get on. CB
 

Ok. Now test it with your real money and see the results.


I'm not a doctor and then I do not audit patients, they could die.

I'm not an architect and I do not build gates, they could crash.


You are not a programmer but program, then you could only loose money. (and time).

 
cloudbreaker wrote >>
When I read the title I just KNEW there had to be an incrementing order select loop nearby. Do me a favour and test this on a chart where there are a few open orders which ALL should be closed and let me know how you get on. CB

Thank you for the reply - I will test and let you know.

 
george_davids:

Thank you for the reply - I will test and let you know.

I too dont have coding experience but am teaching my self, i have already written a few ea's that work !

dont let others put you down, keep it up and keep learning, one thing I have learnt is to test, test, test .....until its perfect !

 

buju wrote >>

one thing I have learnt is to test, test, test .....until its perfect !


good approach.

but if you want to end with something perfect, you will never end.

never, because its impossible to create something perfect...

;-)

 

Spicetrader - I can assure you I'm not trying to put you down.

Please do test with multiple open orders - all of which should be closed.

And let me know how you get on.


CB

 
cloudbreaker:

Spicetrader - I can assure you I'm not trying to put you down.

Please do test with multiple open orders - all of which should be closed.

And let me know how you get on.


CB

I am just a beginner and its my first try. This is never meant for multiple orders for the same currency. One order per currency.

 
It works on a single order for each currency pair for sure. I am certain it would work on multiple orders as well. I am attaching the EA here also
 
spicetrader:
It works on a single order for each currency pair for sure. I am certain it would work on multiple orders as well. I am attaching the EA here also


So create yourself 4 or 5 orders in the same chart ( all of which meet the criteria to be closed ) and run your EA. Then tell me how certain you are. :-)
 
cloudbreaker:


So create yourself 4 or 5 orders in the same chart ( all of which meet the criteria to be closed ) and run your EA. Then tell me how certain you are. :-)
Reason: