//+------------------------------------------------------------------+
//| Retournements-UK100-V2.mq4 |
//+------------------------------------------------------------------+
input double TakeProfit =1000;
input double StopLoss =500;
input double Lots =5;
input double TrailingStop =50;
input double Seuil =350;
int bougie = 0;
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
void OnTick(void)
{
double SignalAchat;
double SignalVente;
double Achat;
double Vente;
double RSICloseBuy;
double RSICloseSell;
int cnt,ticket,total;
//--- Variables Internes
SignalAchat=iCustom(NULL, PERIOD_CURRENT, "Tendance", 1, 2) != 0 && iCustom(NULL, PERIOD_CURRENT, "Tendance", 1, 2) != EMPTY_VALUE && iCustom(NULL, PERIOD_CURRENT, "Tendance", 0, 1) != 0 && iCustom(NULL, PERIOD_CURRENT, "Tendance", 0, 1) != EMPTY_VALUE;
SignalVente=iCustom(NULL, PERIOD_CURRENT, "Tendance", 0, 2) != 0 && iCustom(NULL, PERIOD_CURRENT, "Tendance", 0, 2) != EMPTY_VALUE && iCustom(NULL, PERIOD_CURRENT, "Tendance", 1, 1) != 0 && iCustom(NULL, PERIOD_CURRENT, "Tendance", 1, 1) != EMPTY_VALUE;
RSICloseBuy=iRSI(NULL,PERIOD_CURRENT,14,PRICE_CLOSE,0)>95;
RSICloseSell=iRSI(NULL,PERIOD_CURRENT,14,PRICE_CLOSE,0)<5;
Achat=Bars > bougie && SignalAchat ;
Vente=Bars > bougie && SignalVente ;
total=OrdersTotal();
if(total<2)
{
//--- Vérification Marge Disponible
if(AccountFreeMargin()<(1*Lots))
{
Print("We have no money. Free Margin = ",AccountFreeMargin());
return;
}
//--- Possibilité Achat
if(Achat)
{
ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,3,Bid-StopLoss*Point,Ask+TakeProfit*Point,"Retournements-UK100-V2",666555,0,Green);
bougie=Bars;
if(ticket>0)
{
if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES))
Print("BUY order opened : ",OrderOpenPrice());
}
else
Print("Error opening BUY order : ",GetLastError());
return;
}
//--- Possibilité Vente
if(Vente)
{
ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,3,Ask+StopLoss*Point,Bid-TakeProfit*Point,"Retournements-UK100-V2",666555,0,Red);
bougie=Bars;
if(ticket>0)
{
if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES))
Print("SELL order opened : ",OrderOpenPrice());
}
else
Print("Error opening SELL order : ",GetLastError());
}
return;
}
//--- Conditions Fermeture Ordres (hors SL & TP)
for(cnt=0;cnt<total;cnt++)
{
if(!OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES))
continue;
if(OrderType()<=OP_SELL &&
OrderSymbol()==Symbol())
{
//--- Si Position Achat Ouverte
if(OrderType()==OP_BUY)
{
//--- Condition Fermeture
if(RSICloseBuy)
{
//--- Fermeture Achat
if(!OrderClose(OrderTicket(),OrderLots(),Bid,3,Violet))
Print("OrderClose error ",GetLastError());
return;
}
//--- Vérification Stop Suiveur
if(TrailingStop>0)
{
if(Bid-OrderOpenPrice()>Point*Seuil)
{
if(OrderStopLoss()<Bid-Point*TrailingStop)
{
//--- Modification Stop
if(!OrderModify(OrderTicket(),OrderOpenPrice(),Bid-Point*TrailingStop,OrderTakeProfit(),0,Green))
Print("OrderModify error ",GetLastError());
return;
}
}
}
}
else // Si Position Vente
{
//--- Condition Fermeture
if(RSICloseSell)
{
//--- Fermeture Vente
if(!OrderClose(OrderTicket(),OrderLots(),Ask,3,Violet))
Print("OrderClose error ",GetLastError());
return;
}
//--- Vérification Stop Suiveur
if(TrailingStop>0)
{
if((OrderOpenPrice()-Ask)>(Point*Seuil))
{
if((OrderStopLoss()>(Ask+Point*TrailingStop)) || (OrderStopLoss()==0))
{
//--- modify order and exit
if(!OrderModify(OrderTicket(),OrderOpenPrice(),Ask+Point*TrailingStop,OrderTakeProfit(),0,Red))
Print("OrderModify error ",GetLastError());
return;
}
}
}
}
}
}
//---
}
//+------------------------------------------------------------------+