Trailing SL???

 

Hello,

The following EA based on Moving Average works perfectly on EUR/USD H4 200-6, EUR/USD and AUD/USD H4 200-6. I'm pretty sure that replacing the normal SL by trailing SL would make this EA more reliable especially on EUR/USD H4 20-6 and EUR/USD H1 20-6. Could somebody tell me how to rewrite the following code to to add trailing SL? I'm a complete novice in writing codes, so I need a detailed description what to do.

Thans,

Martin

//+------------------------------------------------------------------+
//| Moving Average.mq4 |
//| Copyright © 2005, MetaQuotes Software Corp. |
//| https://www.metaquotes.net// |
//+------------------------------------------------------------------+
#define MAGICMA 20050610

extern double Lots = 0.1;
extern double StopLoss = 30;
extern double MaximumRisk = 0.005;
extern double DecreaseFactor = 3;
extern double MovingPeriod = 20;
extern double MovingShift = 6;
//+------------------------------------------------------------------+
//| Calculate open positions |
//+------------------------------------------------------------------+
int CalculateCurrentOrders(string symbol)
{
int buys=0,sells=0;
//----
for(int i=0;i<OrdersTotal();i++)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) break;
if(OrderSymbol()==Symbol() && OrderMagicNumber()==MAGICMA)
{
if(OrderType()==OP_BUY) buys++;
if(OrderType()==OP_SELL) sells++;
}
}
//---- return orders volume
if(buys>0) return(buys);
else return(-sells);
}
//+------------------------------------------------------------------+
//| Calculate optimal lot size |
//+------------------------------------------------------------------+
double LotsOptimized()
{
double lot=Lots;

//---- select lot size
lot=0.1;

//---- return lot size
if(lot<0.1) lot=0.1;
return(lot);
}
//+------------------------------------------------------------------+
//| Check for open order conditions |
//+------------------------------------------------------------------+
void CheckForOpen()
{
double ma;
int res;
//---- go trading only for first tiks of new bar
if(Volume[0]>1) return;
//---- get Moving Average
ma=iMA(NULL,0,MovingPeriod,MovingShift,MODE_SMA,PRICE_CLOSE,0);
//---- sell conditions
if(Open[1]>ma && Close[1]<ma)
{
res=OrderSend(Symbol(),OP_SELL,LotsOptimized(),Bid,3,Bid+StopLoss*Point,0,"",MAGICMA,0,Red);
return;
}
//---- buy conditions
if(Open[1]<ma && Close[1]>ma)
{
res=OrderSend(Symbol(),OP_BUY,LotsOptimized(),Ask,3,Ask-StopLoss*Point,0,"",MAGICMA,0,Blue);
return;
}
//----
}
//+------------------------------------------------------------------+
//| Check for close order conditions |
//+------------------------------------------------------------------+
void CheckForClose()
{
double ma;
//---- go trading only for first tiks of new bar
if(Volume[0]>1) return;
//---- get Moving Average
ma=iMA(NULL,0,MovingPeriod,MovingShift,MODE_SMA,PRICE_CLOSE,0);
//----
for(int i=0;i<OrdersTotal();i++)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) break;
if(OrderMagicNumber()!=MAGICMA || OrderSymbol()!=Symbol()) continue;
//---- check order type
if(OrderType()==OP_BUY)
{
if(Open[1]>ma && Close[1]<ma) OrderClose(OrderTicket(),OrderLots(),Bid,3,White);
break;
}
if(OrderType()==OP_SELL)
{
if(Open[1]<ma && Close[1]>ma) OrderClose(OrderTicket(),OrderLots(),Ask,3,White);
break;
}
}
//----
}
//+------------------------------------------------------------------+
//| Start function |
//+------------------------------------------------------------------+
void start()
{
//---- check for history and trading
if(Bars<100 || IsTradeAllowed()==false) return;
//---- calculate open orders by current symbol
if(CalculateCurrentOrders(Symbol())==0) CheckForOpen();
else CheckForClose();
//----
}
//+------------------------------------------------------------------+---+

 

Or attach the file

 
qjol:

Or attach the file


Here it is...
Files:
 

Do you want to cancel, the current closing condition, and move to Trailing SL?

 
qjol:

Do you want to cancel, the current closing condition, and move to Trailing SL?


Yes
 

didn't checked it

Files:
 
qjol:

didn't checked it


Thanks a lot...It seems to work well :-)
 
billy90:

Thanks a lot...It seems to work well :-)

After backtesting the EA, it seems that the only closing condition is the trailing stop...It's my fault because I told you to change the closing conditions, but I actually ment just changing the stoploss
 
 
billy90:

Here it is...

I change for 4 or 5 decimal broker, and put switce for autoclose and trailing .... try it
 
pannek:

I change for 4 or 5 decimal broker, and put switce for autoclose and trailing .... try it

The results are not bad at all! But I think the EA would work even better if you just add trailing stop to the original strategy. I tested the EA with trailing stop, which completely replaced the original closing condition and the results are worse than without trailing stop...What I would like to do with the original code is to add trailing stop but to keep the original closing condition as well. Coul you try to make such code? I guarantee a high profitability of such code :-)

Thanks

Reason: