Problem Open Order Moving Average

 

Hello,

extern int iHeureDebut = 9; // Heure du debut de la plage horaire du filtre horaire
extern int iMinutesDebut = 30; // Minutes du debut de la plage horaire du filtre horaire
extern int iHeureFin = 20; // Heure de fin de la plage horaire du filtre horaire
extern int iMinutesFin = 30; // Minutes de fin de la plage horaire du filtre horaire
extern int iDistance = 5; // Distance en pips entre le plus haut/bas et le prix d'entrée
extern double dPourcentageRisque = 5; // Pourcentage de la balance du compte risquer chaque trade
extern int iStopSuiveur = 25; // Stop suiveur en pips
extern int iSlippage = 1;
int iDate; // Variable permettant de r?initialiser l'expert lors du passage à une nouvelle journ?e
int iDay = 0; // Variable permettant de r?initialiser le t?l?chargement du fichier lors du passage ? une nouvelle journ?e
int iTimeDebut; // Variable stockant la valeur de l'heure du d?but du filtre horaire en secondes
int iTimeFin; // Variable stockant la valeur de l'heure de fin du filtre horaire en secondes
int iTimeSeconds; // Variable stockant la valeur de l'heure actuelle en secondes
double takeprofit, stoploss; 
int MagicNumber = 010101;
double OpenPrice;
bool TradeAllowed;
int iCompteur;
double dSellLimite, dStopSuiveur, dBuyLimite;
extern int FastMAPeriod = 7;
extern int MediumMAPeriod = 20;
extern int SlowMAPeriod = 50;
extern int iStop = 2;
extern int iWideStop = 30;
extern int iLimite = 3;
extern int iWideLimite = 40;
int iBuyTicket;
int iSellTicket;
int iMagic = 1010100;
int iWideMagic = 1010101;
int ErrorCode;
string ErrDesc;
string ErrAlert;
string ErrLog;
double FastMA;
double MediumMA;
double SlowMA ;
double LastFastMA;
double LastMediumMA;
double LastSlowMA;
bool bErreur;


//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
  {
//----
  
  iTimeDebut = iHeureDebut * 3600 + iMinutesDebut * 60; // Conversion de l'heure du début du filtre horaire en secondes
  iTimeFin = iHeureFin * 3600 + iMinutesFin * 60; // Conversion de l'heure de fin du filtre horaire en secondes
  
   
//----
   return(0);
  }

//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
{
fTradeAllowed (iTimeDebut, iTimeFin);
fStopSuiveur (Symbol(), iStop, iMagic) ;

if (TradeAllowed)
        {
// buy order
if(iStop > 0) double dBuyStop = MarketInfo(Symbol(), MODE_BID) - (iStop * fPoint(Symbol()));
if(iWideStop > 0) double dBuyWideStop = MarketInfo(Symbol(), MODE_BID) - (iWideStop * fPoint(Symbol()));
if(iLimite > 0) double dBuyLimite = MarketInfo(Symbol(), MODE_ASK) + (iLimite * fPoint(Symbol()));
if(iWideLimite > 0) double dBuyWideLimite = MarketInfo(Symbol(), MODE_ASK) + (iWideLimite * fPoint(Symbol()));

// sell order
if(iStop > 0) double dSellStop = MarketInfo(Symbol(), MODE_ASK) + (iStop * fPoint(Symbol()));
if(iWideStop > 0) double dSellWideStop = MarketInfo(Symbol(), MODE_ASK) + (iWideStop * fPoint(Symbol()));
if(iLimite > 0) double dSellLimite = MarketInfo(Symbol(), MODE_BID) - (iLimite * fPoint(Symbol()));
if(iWideLimite > 0) double dSellWideLimite = MarketInfo(Symbol(), MODE_BID) - (iWideLimite * fPoint(Symbol()));


FastMA =  iMA(Symbol(), PERIOD_M1, FastMAPeriod, 0, 1, 0, 0);
MediumMA=  iMA(Symbol(), PERIOD_M1, MediumMAPeriod, 0, 1, 0, 0);
SlowMA=  iMA(Symbol(), PERIOD_M1, SlowMAPeriod, 0, 1, 0, 0);
LastFastMA=  iMA(Symbol(), PERIOD_M1, FastMAPeriod, 0, 1, 0, 1);
LastMediumMA=  iMA(Symbol(), PERIOD_M1, MediumMAPeriod, 0, 1, 0, 1);
LastSlowMA=  iMA(Symbol(), PERIOD_M1, SlowMAPeriod, 0, 1, 0, 1);

if(LastFastMA <= LastMediumMA && FastMA > MediumMA)
{
while(IsTradeContextBusy() == True) // Vérification si le canal de trading est occupé 
Sleep(10); // Pause de … seconde si le canal est occupé

iBuyTicket = OrderSend (Symbol(), OP_BUY, fLots(dPourcentageRisque, iStop), MarketInfo(Symbol(), MODE_ASK), fSlippage(Symbol(), iSlippage), 0, 0, 0, iMagic, 0, Green);
OrderSelect(iBuyTicket, SELECT_BY_POS);
    OrderModify(OrderTicket(), OrderOpenPrice(), dBuyStop, 0, 0, 0);
}

if (FastMA > MediumMA && LastFastMA <= LastSlowMA && FastMA > SlowMA) 
{
while(IsTradeContextBusy() == True) // Vérification si le canal de trading est occupé 
Sleep(10); // Pause de … seconde si le canal est occupé

iBuyTicket = OrderSend (Symbol(), OP_BUY, fLots(dPourcentageRisque, iStop), MarketInfo(Symbol(), MODE_ASK), fSlippage(Symbol(), iSlippage), 0, 0, 0, iWideMagic, 0, Green);
OrderSelect(iBuyTicket, SELECT_BY_POS);
    OrderModify(OrderTicket(), OrderOpenPrice(), dBuyWideStop, dBuyWideLimite, 0, 0);

for (iCompteur = OrdersTotal() - 1; iCompteur >= 0; iCompteur--) // Boucle pour vérifier toutes les positions ouvertes ou en attentes
{
OrderSelect (iCompteur, SELECT_BY_POS); // Sélection de chaque position une par une
while(IsTradeContextBusy() == True) // Vérification si le canal de trading est occupé 
Sleep(10); // Pause de … seconde si le canal est occupé

bErreur = OrderModify (OrderTicket(), OrderOpenPrice(), dBuyWideStop, dBuyWideLimite, 0, 0); // Modification du stop
}
}


if(LastFastMA >= LastMediumMA && FastMA < MediumMA)
{
while(IsTradeContextBusy() == True) // Vérification si le canal de trading est occupé 
Sleep(10); // Pause de … seconde si le canal est occupé

iSellTicket = OrderSend (Symbol(), OP_SELL, fLots(dPourcentageRisque, iStop), MarketInfo(Symbol(), MODE_BID), fSlippage(Symbol(), iSlippage), 0, 0, 0, iMagic, 0, Green);
OrderSelect(iSellTicket, SELECT_BY_POS);
    OrderModify(OrderTicket(), OrderOpenPrice(), dSellStop, 0, 0, 0);
}

if (FastMA < MediumMA && LastFastMA >= LastSlowMA && FastMA < SlowMA) 
{
while(IsTradeContextBusy() == True) // Vérification si le canal de trading est occupé 
Sleep(10); // Pause de … seconde si le canal est occupé

iSellTicket = OrderSend (Symbol(), OP_SELL, fLots(dPourcentageRisque, iStop), MarketInfo(Symbol(), MODE_BID), fSlippage(Symbol(), iSlippage), 0, 0, 0, iWideMagic, 0, Green);
OrderSelect(iSellTicket, SELECT_BY_POS);
    OrderModify(OrderTicket(), OrderOpenPrice(), dSellWideStop, dSellWideLimite, 0, 0);

for ( iCompteur = OrdersTotal() - 1; iCompteur >= 0; iCompteur--) // Boucle pour vérifier toutes les positions ouvertes ou en attentes
{
OrderSelect (iCompteur, SELECT_BY_POS); // Sélection de chaque position une par une
while(IsTradeContextBusy() == True) // Vérification si le canal de trading est occupé 
Sleep(10); // Pause de … seconde si le canal est occupé

bErreur = OrderModify (OrderTicket(), OrderOpenPrice(), dSellWideStop, dSellWideLimite, 0, 0); // Modification du stop
}
}       
}
return(0);
  }
//+------------------------------------------------------------------+

bool fTradeAllowed (int iTimeDebut, int iTimeFin)
{
 iTimeSeconds = Hour()*3600 + Minute()*60 + Seconds(); // Conversion de l'heure actuelle en secondes ;
if(iTimeSeconds >= iTimeDebut && iTimeSeconds < iTimeFin) // Vérification si l'heure est dans la plage horaire du filtre horaire
        TradeAllowed =  true ;
else TradeAllowed = false ;
return (TradeAllowed) ;
}



double fPoint(string sPaire) // Fonction pour déterminer la valeur correcte de Point
  {
  if((MarketInfo(sPaire, MODE_DIGITS)) == 2 || (MarketInfo(sPaire, MODE_DIGITS)) == 3) // Paires à 2 ou 3 décimales
    {
    double dPoint = 0.01; // Point = 0.01
    }
  if((MarketInfo(sPaire, MODE_DIGITS)) == 4 || (MarketInfo(sPaire, MODE_DIGITS)) == 5) // Paires à 4 ou 5 décimales
    {
    dPoint = 0.0001; // Point = 0.0001
    }
  return(dPoint); // Renvoi de la valeur correcte
  }



double fSlippage(string sPaire, int iPipsSlippage) // Fonction pour calculer la valeur correcte du slippage
  {
  if((MarketInfo(sPaire, MODE_DIGITS)) == 2 || (MarketInfo(sPaire, MODE_DIGITS)) == 4) // Paires à 2 ou 4 décimales
    {
    double dSlippage = iPipsSlippage; // Pas besoin de modifier la valeur
    }
  if((MarketInfo(sPaire, MODE_DIGITS)) == 3 || (MarketInfo(sPaire, MODE_DIGITS)) == 5) // Paires à 3 ou 5 décimales
    {
    dSlippage = iPipsSlippage * 10; // Nécéssité de multiplier la valeur par 10
    }
  return(dSlippage); // Renvoi de la valeur correcte
  }


double fLots(double dPourcentageRisque, int iStop) 
  {
  double dRisqueMax = (dPourcentageRisque/100) * AccountBalance(); 
  double dValeurPip = MarketInfo(Symbol(), MODE_TICKVALUE); 
  if(Point == 0.001 || Point == 0.00001) 
    {
    dValeurPip = dValeurPip * 10; 
    }
  double dLots = (dRisqueMax / iStop) / dValeurPip; 
  if(dLots < MarketInfo(Symbol(),MODE_MINLOT)) 
    dLots = MarketInfo(Symbol(),MODE_MINLOT); 
  if(dLots > MarketInfo(Symbol(),MODE_MAXLOT))
    dLots = MarketInfo(Symbol(),MODE_MAXLOT); 
  if(MarketInfo(Symbol(),MODE_LOTSTEP) == 0.01) 
    dLots = NormalizeDouble(dLots, 2); 
  else 
    dLots = NormalizeDouble(dLots, 1); 
  return(dLots); 
  }


void fStopSuiveur (string sSymbol, int iStopSuiveur, int iMagic) 
  {
  for(int iCompteur = OrdersTotal() - 1; iCompteur >= 0; iCompteur--) 
    {
    OrderSelect(iCompteur, SELECT_BY_POS); // Sélection de chaque position une par une

    double dStopSuiveurBuy = MarketInfo(sSymbol, MODE_BID) - (iStopSuiveur * fPoint(sSymbol)); 
    double dStopSuiveurSell = MarketInfo(sSymbol, MODE_ASK) + (iStopSuiveur * fPoint(sSymbol));

    if(OrderMagicNumber() == iMagic && OrderSymbol() == sSymbol  && OrderType() == OP_BUY && OrderStopLoss() < dStopSuiveurBuy) 
      {
      bool bErreur = OrderModify(OrderTicket(), OrderOpenPrice(), dStopSuiveurBuy, OrderTakeProfit(), 0); // Modification du stop
      if(bErreur == 0) // Vérification si une erreur est survenue
        {
        int iErreur = GetLastError(); // Récupération du numéro de l'erreur
        // Affectation du descriptif de l'erreur à la variable sErreur
        Print("Erreur modification position achat : "+iErreur ); // Affichage du descriptif de l'erreur
        }
      }

    if(OrderMagicNumber() == iMagic && OrderSymbol() == sSymbol && OrderType() == OP_SELL && OrderStopLoss() > dStopSuiveurSell) 
      {
      bErreur = OrderModify(OrderTicket(), OrderOpenPrice(), dStopSuiveurSell, OrderTakeProfit(), 0); // Modification du stop
      if(bErreur == 0) // Vérification si une erreur est survenue
        {
        iErreur = GetLastError(); // Récupération du numéro de l'erreur
       // Affectation du descriptif de l'erreur à la variable sErreur
        Print("Erreur modification position achat : "+iErreur); // Affichage du descriptif de l'erreur
        }
      }
    }
  }

I coded this EA but it never opened any trade, someone knows why ?

 
nikky: I coded this EA but it never opened any trade, someone knows why ?
is the OrderSend failing? What are Function return values ? How do I use them ? - MQL4 forum
 
add to that . . . did the OrderSelect() work ?  did the OrderModify() work ?  you are ignorant of this information . . . enlighten yourself with some information and you will have a much better chance of fixing your issue . . .  despite the saying,  ignorance is not bliss.
 
RaptorUK:
add to that . . . did the OrderSelect() work ?  did the OrderModify() work ?  you are ignorant of this information . . . enlighten yourself with some information and you will have a much better chance of fixing your issue . . .  despite the saying,  ignorance is not bliss.


i added
int ErrorCode = GetLastError ();
string ErrDesc = ErrorDescription (ErrorCode);
string ErrAlert = StringConcatenate (“argMode – error: “, ErrorCode, “: “, ErrDesc);
Alert (ErrAlert);
string ErrLog = StringConcatenate (“Ask: “, MarketInfo (argSymbol, MODE_ASK), “Bid: “, MarketInfo(argSymbol, MODE_BID)); 
Print(ErrLog); 
after every OrderSend or OrderModify, but no error was found, and no trade was open though...
 
nikky:

i added after every OrderSend or OrderModify, but no error was found, and no trade was open though...

No you didn't . . .   if you did your code would not compile.

If OrderSend()  is called it does one of two things,  it sends the order or causes an error . . .   if neither of those is happening then it is not being called.

Have you actually allowed your EA to trade live ?  do you have a smiley face in the top right hand corner of your chart ?

 

In MT4 . . .

 

In the EA settings . . .

 

On MT4 menu bar . . .

 

 

yes the EA is allowed to trade live, and i click on the button Expert Advisor.

There is no error displayed, but when the fast MA crosses the Slow MA above, no open trade is done. Is there any logical error in it ?

 
and i put
int ErrorCode = GetLastError ();
string ErrDesc = (ErrorCode);
string ErrAlert = StringConcatenate (“argMode – error: “, ErrorCode, “: “, ErrDesc);
Alert (ErrAlert);
string ErrLog = StringConcatenate (“Ask: “, MarketInfo (argSymbol, MODE_ASK), “Bid: “, MarketInfo(argSymbol, MODE_BID)); 
Print(ErrLog); 
indeed, but it's not responsible for my problem. i cut it in my first post because the EA was too long it said.
 

The way you are using functions doesn't make a lot of sense . . .  if a function is designed to returns a value then you should use that return value . . .

TradeAllowed = fTradeAllowed (iTimeDebut, iTimeFin);



bool fTradeAllowed (int iTimeDebut, int iTimeFin)
   {
   iTimeSeconds = Hour()*3600 + Minute()*60 + Seconds(); // Conversion de l'heure actuelle en secondes ;
   if(iTimeSeconds >= iTimeDebut && iTimeSeconds < iTimeFin) // Vérification si l'heure est dans la plage horaire du filtre horaire
      return(true);

   else return(false);
   }

  . . .  you can also use local variables in the function instead of globally declared variables.

 
nikky:
and i put indeed, but it's not responsible for my problem. i cut it in my first post because the EA was too long it said.

Is TradeAllowed true ?  add a Print or Comment statement to check . . .
 
i use the tradeallowed function :
int start()
{
fTradeAllowed (iTimeDebut, iTimeFin);
fStopSuiveur (Symbol(), iStop, iMagic) ;

if (TradeAllowed)
        {
// buy order ....
And trade is allowed between 9am to 20pm so yes actually it is true.
 
nikky:
and i put indeed, but it's not responsible for my problem. i cut it in my first post because the EA was too long it said.

Do something like this . . . 

         iBuyTicket = OrderSend (Symbol(), OP_BUY, fLots(dPourcentageRisque, iStop), MarketInfo(Symbol(), MODE_ASK), fSlippage(Symbol(), iSlippage), 0, 0, 0, iMagic, 0, Green);

         if(iBuyTicket == -1)
            {
            Print("Order Send failed, error # ", GetLastError() );
            }
         else
            {
            if(OrderSelect(iBuyTicket, SELECT_BY_POS))
               if(!OrderModify(OrderTicket(), OrderOpenPrice(), dBuyStop, 0, 0, 0))
                  Print("Order Modify failed, error # ", GetLastError() );
            }
Reason: