Hi, I have a problem here in that I coded this EA to have trail stops running as a percentage of total profit. I have different percentages to use fro different profit levels. I seem to have a strange numbers that are recalculating the trail stop and also the trail stop starts from the initial stop loss rather than the initial order entry. The EA actually works judging from the trading I did last night apart from the problems mentioned above. To get it to work properly I had to set the initial stop loss to zero. Any help appreciated
I am not sure what I have done wrong:
//+------------------------------------------------------------------+
//| expert start function |
//+------------------------------------------------------------------+
{double PointValue;
for (int i = 0; i < OrdersTotal(); i++)
{
OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
//Calculate the point value in case there are extra digits in the quotes
if (MarketInfo(OrderSymbol(), MODE_POINT) == 0.00001) PointValue = 0.0001;
else if (MarketInfo(OrderSymbol(), MODE_POINT) == 0.001) PointValue = 0.01;
else PointValue = MarketInfo(OrderSymbol(), MODE_POINT);
//Normalize trailing stop value to the point value
double TSTP = InitialTrailingStop * PointValue;
double TSTP2 = SecTrailingStop * PointValue;
double TSTP3 = ThirdTrailingStop * PointValue;
double TSTP4 = FourthTrailingStop * PointValue;
double TSTP5 = FifthTrailingStop * PointValue;
if (OrderType() == OP_BUY)
{
if ((trailing_stop_percentage > 0) && (Bid - OrderOpenPrice() >= TSTP))
{ TSTP = NormalizeDouble((Bid - OrderOpenPrice()) * trailing_stop_percentage / 100, Digits);
{ if (OrderStopLoss() < (Bid - TSTP))
{ OrderModify(OrderTicket(), OrderOpenPrice(), Bid - TSTP, OrderTakeProfit(), Red);
}
}
}
}
{ if ((sec_trailing_stop_percentage > 0) && (Bid - OrderOpenPrice() >= TSTP2))
{ TSTP2 = NormalizeDouble((Bid - OrderOpenPrice()) * sec_trailing_stop_percentage / 100, Digits);
{ if (OrderStopLoss() < (Bid - TSTP2))
{ OrderModify(OrderTicket(), OrderOpenPrice(), Bid - TSTP2, OrderTakeProfit(), Red);
}
}
}
}
{ if ((thr_trailing_stop_percentage > 0) && (Bid - OrderOpenPrice() >= TSTP3))
{ TSTP3 = NormalizeDouble((Bid - OrderOpenPrice()) * thr_trailing_stop_percentage / 100, Digits);
{ if (OrderStopLoss() < (Bid - TSTP3))
{ OrderModify(OrderTicket(), OrderOpenPrice(), Bid - TSTP3, OrderTakeProfit(), Red);
}
}
}
}
{if ((fourth_trailing_stop_percentage > 0) && (Bid - OrderOpenPrice() >= TSTP4))
{ TSTP4 = NormalizeDouble((Bid - OrderOpenPrice()) * fourth_trailing_stop_percentage / 100, Digits);
{ if (OrderStopLoss() < (Bid - TSTP4))
{ OrderModify(OrderTicket(), OrderOpenPrice(), Bid - TSTP4, OrderTakeProfit(), Red);
}
}
}
}
{if ((fifth_trailing_stop_percentage > 0) && (Bid - OrderOpenPrice() >= TSTP5))
{ TSTP5 = NormalizeDouble((Bid - OrderOpenPrice()) * fifth_trailing_stop_percentage / 100, Digits);
{ if (OrderStopLoss() < (Bid - TSTP5))
{ OrderModify(OrderTicket(), OrderOpenPrice(), Bid - TSTP5, OrderTakeProfit(), Red);
}
}
}
else if ((OrderStopLoss() != Bid - StopLoss * PointValue) && (StopLoss != 0))
OrderModify(OrderTicket(), OrderOpenPrice(), Bid - StopLoss * PointValue, OrderTakeProfit(), Red);
else if (OrderType() == OP_SELL)
if ((trailing_stop_percentage > 0) && (OrderOpenPrice() - Ask >= TSTP))
{ TSTP = NormalizeDouble((OrderOpenPrice() - Ask) * trailing_stop_percentage / 100, Digits);
{ if ((OrderStopLoss() > (Ask + InitialTrailingStop * PointValue)) || (OrderStopLoss() == 0))
{OrderModify(OrderTicket(), OrderOpenPrice(), Ask + TSTP, OrderTakeProfit(), Red);
}
}
}
if ((sec_trailing_stop_percentage > 0) && (OrderOpenPrice() - Ask >= TSTP2))
{ TSTP2 = NormalizeDouble((OrderOpenPrice() - Ask) * sec_trailing_stop_percentage / 100, Digits);
{ if ((OrderStopLoss() > (Ask + SecTrailingStop * PointValue)) || (OrderStopLoss() == 0))
{ OrderModify(OrderTicket(), OrderOpenPrice(), Ask + TSTP2, OrderTakeProfit(), Red);
}
}
}
if ((thr_trailing_stop_percentage > 0) && (OrderOpenPrice() - Ask >= TSTP3))
{ TSTP3 = NormalizeDouble((OrderOpenPrice() - Ask) * thr_trailing_stop_percentage / 100, Digits);
{ if ((OrderStopLoss() > (Ask + ThirdTrailingStop * PointValue)) || (OrderStopLoss() == 0))
{ OrderModify(OrderTicket(), OrderOpenPrice(), Ask + TSTP3, OrderTakeProfit(), Red);
}
}
}
{ if ((fourth_trailing_stop_percentage > 0) && (OrderOpenPrice() - Ask >= TSTP4))
{ TSTP4 = NormalizeDouble((OrderOpenPrice() - Ask) * fourth_trailing_stop_percentage / 100, Digits);
{ if ((OrderStopLoss() > (Ask + FourthTrailingStop * PointValue)) || (OrderStopLoss() == 0))
{ OrderModify(OrderTicket(), OrderOpenPrice(), Ask + TSTP4, OrderTakeProfit(), Red);
}
}
}
}
if ((fifth_trailing_stop_percentage > 0) && (OrderOpenPrice() - Ask >= TSTP5))
{ TSTP5 = NormalizeDouble((OrderOpenPrice() - Ask) * fifth_trailing_stop_percentage / 100, Digits);
{ if ((OrderStopLoss() > (Ask + FifthTrailingStop * PointValue)) || (OrderStopLoss() == 0))
{ OrderModify(OrderTicket(), OrderOpenPrice(), Ask + TSTP5, OrderTakeProfit(), Red);
}
}
}
else if ((OrderStopLoss() != Ask + StopLoss * PointValue) && (StopLoss != 0))
OrderModify(OrderTicket(), OrderOpenPrice(), Ask + StopLoss * PointValue, OrderTakeProfit(), Red);
}
}
//----
//----
return(0);
}
//+------------------------------------------------------------------
I am not sure what I have done wrong:
//+------------------------------------------------------------------+
//| expert start function |
//+------------------------------------------------------------------+
{double PointValue;
for (int i = 0; i < OrdersTotal(); i++)
{
OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
//Calculate the point value in case there are extra digits in the quotes
if (MarketInfo(OrderSymbol(), MODE_POINT) == 0.00001) PointValue = 0.0001;
else if (MarketInfo(OrderSymbol(), MODE_POINT) == 0.001) PointValue = 0.01;
else PointValue = MarketInfo(OrderSymbol(), MODE_POINT);
//Normalize trailing stop value to the point value
double TSTP = InitialTrailingStop * PointValue;
double TSTP2 = SecTrailingStop * PointValue;
double TSTP3 = ThirdTrailingStop * PointValue;
double TSTP4 = FourthTrailingStop * PointValue;
double TSTP5 = FifthTrailingStop * PointValue;
if (OrderType() == OP_BUY)
{
if ((trailing_stop_percentage > 0) && (Bid - OrderOpenPrice() >= TSTP))
{ TSTP = NormalizeDouble((Bid - OrderOpenPrice()) * trailing_stop_percentage / 100, Digits);
{ if (OrderStopLoss() < (Bid - TSTP))
{ OrderModify(OrderTicket(), OrderOpenPrice(), Bid - TSTP, OrderTakeProfit(), Red);
}
}
}
}
{ if ((sec_trailing_stop_percentage > 0) && (Bid - OrderOpenPrice() >= TSTP2))
{ TSTP2 = NormalizeDouble((Bid - OrderOpenPrice()) * sec_trailing_stop_percentage / 100, Digits);
{ if (OrderStopLoss() < (Bid - TSTP2))
{ OrderModify(OrderTicket(), OrderOpenPrice(), Bid - TSTP2, OrderTakeProfit(), Red);
}
}
}
}
{ if ((thr_trailing_stop_percentage > 0) && (Bid - OrderOpenPrice() >= TSTP3))
{ TSTP3 = NormalizeDouble((Bid - OrderOpenPrice()) * thr_trailing_stop_percentage / 100, Digits);
{ if (OrderStopLoss() < (Bid - TSTP3))
{ OrderModify(OrderTicket(), OrderOpenPrice(), Bid - TSTP3, OrderTakeProfit(), Red);
}
}
}
}
{if ((fourth_trailing_stop_percentage > 0) && (Bid - OrderOpenPrice() >= TSTP4))
{ TSTP4 = NormalizeDouble((Bid - OrderOpenPrice()) * fourth_trailing_stop_percentage / 100, Digits);
{ if (OrderStopLoss() < (Bid - TSTP4))
{ OrderModify(OrderTicket(), OrderOpenPrice(), Bid - TSTP4, OrderTakeProfit(), Red);
}
}
}
}
{if ((fifth_trailing_stop_percentage > 0) && (Bid - OrderOpenPrice() >= TSTP5))
{ TSTP5 = NormalizeDouble((Bid - OrderOpenPrice()) * fifth_trailing_stop_percentage / 100, Digits);
{ if (OrderStopLoss() < (Bid - TSTP5))
{ OrderModify(OrderTicket(), OrderOpenPrice(), Bid - TSTP5, OrderTakeProfit(), Red);
}
}
}
else if ((OrderStopLoss() != Bid - StopLoss * PointValue) && (StopLoss != 0))
OrderModify(OrderTicket(), OrderOpenPrice(), Bid - StopLoss * PointValue, OrderTakeProfit(), Red);
else if (OrderType() == OP_SELL)
if ((trailing_stop_percentage > 0) && (OrderOpenPrice() - Ask >= TSTP))
{ TSTP = NormalizeDouble((OrderOpenPrice() - Ask) * trailing_stop_percentage / 100, Digits);
{ if ((OrderStopLoss() > (Ask + InitialTrailingStop * PointValue)) || (OrderStopLoss() == 0))
{OrderModify(OrderTicket(), OrderOpenPrice(), Ask + TSTP, OrderTakeProfit(), Red);
}
}
}
if ((sec_trailing_stop_percentage > 0) && (OrderOpenPrice() - Ask >= TSTP2))
{ TSTP2 = NormalizeDouble((OrderOpenPrice() - Ask) * sec_trailing_stop_percentage / 100, Digits);
{ if ((OrderStopLoss() > (Ask + SecTrailingStop * PointValue)) || (OrderStopLoss() == 0))
{ OrderModify(OrderTicket(), OrderOpenPrice(), Ask + TSTP2, OrderTakeProfit(), Red);
}
}
}
if ((thr_trailing_stop_percentage > 0) && (OrderOpenPrice() - Ask >= TSTP3))
{ TSTP3 = NormalizeDouble((OrderOpenPrice() - Ask) * thr_trailing_stop_percentage / 100, Digits);
{ if ((OrderStopLoss() > (Ask + ThirdTrailingStop * PointValue)) || (OrderStopLoss() == 0))
{ OrderModify(OrderTicket(), OrderOpenPrice(), Ask + TSTP3, OrderTakeProfit(), Red);
}
}
}
{ if ((fourth_trailing_stop_percentage > 0) && (OrderOpenPrice() - Ask >= TSTP4))
{ TSTP4 = NormalizeDouble((OrderOpenPrice() - Ask) * fourth_trailing_stop_percentage / 100, Digits);
{ if ((OrderStopLoss() > (Ask + FourthTrailingStop * PointValue)) || (OrderStopLoss() == 0))
{ OrderModify(OrderTicket(), OrderOpenPrice(), Ask + TSTP4, OrderTakeProfit(), Red);
}
}
}
}
if ((fifth_trailing_stop_percentage > 0) && (OrderOpenPrice() - Ask >= TSTP5))
{ TSTP5 = NormalizeDouble((OrderOpenPrice() - Ask) * fifth_trailing_stop_percentage / 100, Digits);
{ if ((OrderStopLoss() > (Ask + FifthTrailingStop * PointValue)) || (OrderStopLoss() == 0))
{ OrderModify(OrderTicket(), OrderOpenPrice(), Ask + TSTP5, OrderTakeProfit(), Red);
}
}
}
else if ((OrderStopLoss() != Ask + StopLoss * PointValue) && (StopLoss != 0))
OrderModify(OrderTicket(), OrderOpenPrice(), Ask + StopLoss * PointValue, OrderTakeProfit(), Red);
}
}
//----
//----
return(0);
}
//+------------------------------------------------------------------