Hi all,
I'm testing some ways to protect from loses trades, so I made this trailling loss protection.
It work like the trailling stop for winning trades, but in inverted direction.
If the trade is losing 15 pips, and come back losing only 5 pips, close the trade (step one).
If the trade is losing 30 pips, and come back losing 15 pips, close the trade (step two).
And one more step with the same logic. All the params is customizable.
To use, include this variables on your EA:
extern bool UseTrailingLoss = True;
extern int TrailingStopLoss1 = 15;
extern int TrailingStepLoss1 = 5;
extern int TrailingStopLoss2 = 30;
extern int TrailingStepLoss2 = 15;
extern int TrailingStopLoss3 = 45;
extern int TrailingStepLoss3 = 30;
int SlipPage = 3; // Your SlipPage
int MAGIC = 1111111; //Your EA Magic Number
int orderloss[100]; // Max open trades of your EA is the array size
Inside int start():
if (UseTrailingLoss) TrailingLoss();
And here is the code of trailing stop (the comments is in portuguese because I'm Brazilian):
Code:
void TrailingLoss() {
for (int i=0; i<OrdersTotal(); i++) {
//Print("I:",i);
if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) {
if (OrderSymbol()==Symbol() && OrderMagicNumber()==MAGIC) {
if (OrderType()==OP_BUY) {
if (orderloss[i]>0)
{
if (orderloss[i]==1)
{
if (OrderOpenPrice()-Bid<TrailingStepLoss1*Point)
{
//Print("Atingiu o preço de retorno1");
OrderClose(OrderTicket(), OrderLots(), Bid, SlipPage, White);
orderloss[i]=0;
}
}
if (orderloss[i]==2)
{
if (OrderOpenPrice()-Bid<TrailingStepLoss2*Point)
{
//Print("Atingiu o preço de retorno2");
OrderClose(OrderTicket(), OrderLots(), Bid, SlipPage, White);
orderloss[i]=0;
}
}
if (orderloss[i]==3)
{
if (OrderOpenPrice()-Bid<TrailingStepLoss3*Point)
{
//Print("Atingiu o preço de retorno3");
OrderClose(OrderTicket(), OrderLots(), Bid, SlipPage, White);
orderloss[i]=0;
}
}
if (OrderOpenPrice()-Bid>TrailingStopLoss2*Point)
{
//Print("Antes de mudar2:", orderloss[i]);
//Print("Atingiu Loss Point2");
//Print("I:",i);
orderloss[i]=2;
//Print("Depois de mudar:2", orderloss[i]);
}
if (OrderOpenPrice()-Bid>TrailingStopLoss3*Point)
{
//Print("Antes de mudar3:", orderloss[i]);
//Print("Atingiu Loss Point3");
//Print("I:",i);
orderloss[i]=3;
//Print("Depois de mudar3:", orderloss[i]);
}
}
else
{
if (OrderOpenPrice()-Bid>TrailingStopLoss1*Point)
{
//Print("Antes de mudar:", orderloss[i]);
//Print("Atingiu Loss Point");
//Print("I:",i);
orderloss[i]=1;
//Print("Depois de mudar:", orderloss[i]);
}
else
{
orderloss[i]=0;
}
}
}
if (OrderType()==OP_SELL) {
if (orderloss[i]>0)
{
if (orderloss[i]==1)
{
if (Ask-OrderOpenPrice()<TrailingStepLoss1*Point)
{
//Print("Atingiu o preço de retorno1");
OrderClose(OrderTicket(), OrderLots(), Bid, SlipPage, White);
orderloss[i]=0;
}
}
if (orderloss[i]==2)
{
if (Ask-OrderOpenPrice()<TrailingStepLoss2*Point)
{
//Print("Atingiu o preço de retorno2");
OrderClose(OrderTicket(), OrderLots(), Bid, SlipPage, White);
orderloss[i]=0;
}
}
if (orderloss[i]==3)
{
if (Ask-OrderOpenPrice()<TrailingStepLoss3*Point)
{
//Print("Atingiu o preço de retorno3");
OrderClose(OrderTicket(), OrderLots(), Bid, SlipPage, White);
orderloss[i]=0;
}
}
if (Ask-OrderOpenPrice()>TrailingStopLoss2*Point)
{
//Print("Antes de mudar2:", orderloss[i]);
//Print("Atingiu Loss Point2");
//Print("I:",i);
orderloss[i]=2;
//Print("Depois de mudar:2", orderloss[i]);
}
if (Ask-OrderOpenPrice()>TrailingStopLoss3*Point)
{
//Print("Antes de mudar3:", orderloss[i]);
//Print("Atingiu Loss Point3");
//Print("I:",i);
orderloss[i]=3;
//Print("Depois de mudar3:", orderloss[i]);
}
}
else
{
if (Ask-OrderOpenPrice()>TrailingStopLoss1*Point)
{
//Print("Antes de mudar:", orderloss[i]);
//Print("Atingiu Loss Point");
//Print("I:",i);
orderloss[i]=1;
//Print("Depois de mudar:", orderloss[i]);
}
else
{
orderloss[i]=0;
}
}
}
}
}
}
}