J'essaie de terminer un premier jet d'EA très simple : achat ou vente au croisement de deux moyennes mobiles simples.
Il y a une gestion des stop loss, take profit, breakeven, et trailing stop.
Mon problème est que l'erreur 130 apparaît pour la fonction ModifyOrder. Je n'arrive vraiment pas à savoir pourquoi et comment résoudre ce problème. La documentation officielle ne m'avance pas beaucoup...
Code :
Code : Tout sélectionner
//+------------------------------------------------------------------+
//| CroisementMM.mq4 |
//| Brice |
//| |
//+------------------------------------------------------------------+
#property copyright "Brice"
#property link ""
//---- parametres saisis
extern int StopLoss=5;
extern int BreakEven=5;
extern bool Pyramidage=false;
extern int TakeProfit=50;
extern int TrailingStop=5;
extern int Period_MA_Small=3;
extern int Period_MA_Big=14;
//+------------------------------------------------------------------+
//| expert initialization function |
//+------------------------------------------------------------------+
int init()
{
//----
//----
return(0);
}
//+------------------------------------------------------------------+
//| expert deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
//----
//----
return(0);
}
//+------------------------------------------------------------------+
//| expert start function |
//+------------------------------------------------------------------+
int start()
{
double MA_Small, MA_Big;
int buyON=0;
int sellON=0;
MA_Small=iMA(Symbol(),0,Period_MA_Small,0,MODE_SMA,PRICE_CLOSE,0);
MA_Big=iMA(Symbol(),0,Period_MA_Big,0,MODE_SMA,PRICE_CLOSE,0);
//Code ci-dessous visant à savoir s'il y a déjà un ordre d'achat ou de vente ouvert
if (OrdersTotal()>0)
{
for (int cnt=0;cnt<OrdersTotal();cnt++)
{
OrderSelect(cnt, SELECT_BY_POS);
if(OrderType()==OP_BUY) buyON=1;
if(OrderType()==OP_SELL) sellON=1;
}
}
if(MA_Small>MA_Big && buyON==0) //Si la moyenne mobile de petite période est au-dessus de la MM de grande période
{
OrderSend(Symbol(),OP_BUY,0.1,Ask,3,0,0); //Ordre d'achat. Les stop loss et take profit sont donnés dans la fonction OrdersList()
}
if(MA_Small<MA_Big && sellON==0) //Si le contraire
{
OrderSend(Symbol(),OP_SELL,0.1,Bid,3,0,0); //Ordre de vente
}
if (OrdersTotal()>0) OrdersList();
return(0);
}
//+------------------------------------------------------------------+
//Traitement de chaque ordre, pour fixer un stop loss, un take profit, un breakeven et un trailing stop
//Très inspiré du code de Christophe Sangouard disponible sur o-bo.com
void OrdersList()
{
for (int cnt=0;cnt<OrdersTotal();cnt++)
{
OrderSelect(cnt, SELECT_BY_POS);
if (OrderSymbol()==Symbol())
{
if (OrderType()==OP_BUY)
{
//----Stop Loss
if (OrderStopLoss()==0)
{
OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice()-StopLoss*Point,OrderOpenPrice()+TakeProfit*Point,0,LightGreen);
return(0);
}
//---- Trailing Stop
if (Bid-OrderStopLoss()>Point*TrailingStop)
{
OrderModify(OrderTicket(),OrderOpenPrice(),OrderStopLoss()+TrailingStop*Point,OrderTakeProfit(),0,LightGreen);
}
//---- BreakEven
if (Bid-OrderOpenPrice()>Point*BreakEven&&OrderStopLoss()<OrderOpenPrice())
{
//OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice()+StopLoss*Point,OrderTakeProfit(),0,LightGreen);
if(Pyramidage==true)
{
OrderSend(Symbol(),OP_BUY,0.1,Ask,30,0,0,NULL,123456,Red);
}
else
{
return(0);
}
}
}
if (OrderType()==OP_SELL)
{
//---- Stop Loss
if (OrderStopLoss()==0)
{
OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice()+StopLoss*Point,OrderOpenPrice()-TakeProfit*Point,0,Yellow);
return(0);
}
//---- Trailing Stop
if (OrderStopLoss()-Ask>Point*TrailingStop)
{
OrderModify(OrderTicket(),OrderOpenPrice(),OrderStopLoss()-TrailingStop*Point,OrderTakeProfit(),0,LightGreen);
}
//---- BreakEven
if (OrderOpenPrice()-Ask>Point*BreakEven&&OrderStopLoss()>OrderOpenPrice())
{
OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice()-StopLoss*Point,OrderTakeProfit(),0,LightGreen);
if(Pyramidage==true)
{
OrderSend(Symbol(),OP_SELL,0.1,Bid,30,0,0,NULL,123456,Red);
}
else
{
return(0);
}
}
}
}
}
}
//+------------------------------------------------------------------+
Merci !