No errors, no warnings, but it does not work !!! Help me ! (MQL 4)

 

Hello Everybody!


I am about to lose my mind. Why doesn't my code work? Where am i mistaken ? Could you pls guide me and gimme a hand? I really appreciate. Thanks in advance for your help and your time that you are gonna spare !


Here is the code. Please check it out.

//+------------------------------------------------------------------+
//|                                                    FROM ZERO.mq4 |
//|                        Copyright 2020, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2020, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+

//-------EXTERNAL VARIABLES--------//

extern int Slippage = 7;
extern int MagicNumber = 123;
extern double StopLoss = 15;//50;
extern double TakeProfit = 75;//100;
extern double LotSize = 0.01;
extern int maSHIFT= 0 ;
extern int TrailingStop = 100;


//-------GLOBAL VARIABLES--------//
double MyPoint;
int    MySlippage;


int OnInit()
  {
//---
   MyPoint = MyPoint();
   MySlippage = MySlippage();
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
   
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---
   

   
     //Open Buy Order
     
     //if(ONE_FASTma_pre1 < TEN_FASTma_pre1 && ONE_FASTma > TEN_FASTma)
    // {
      /*   int ticket = OrderSend(_Symbol,OP_BUY,LotSize,Ask,MySlippage,0,0,"BUY",MagicNumber);
            if(ticket<0)
            {
               Print("OrderSend failed with error #",GetLastError());
            }
            else
            {
               Print("OrderSend placed successfully");
            }
                       
         // Modify Buy Order
         bool res = OrderModify(ticket,OrderOpenPrice(),Ask-StopLoss*MyPoint,Ask+TakeProfit*MyPoint,0,Blue);
            if(!res)
            {
               Print("Error in OrderModify. Error code=",GetLastError());
            }
            else
            {
               Print("Order modified successfully.");
            }
            
//}*/

   if(TotalOpenOrders() == 0 && IsNewBar() == true)
   { 
      // Check Buy Entry
      if(BuySignal() == true)
         {
            OpenBuy();
            fnTrailingStop(TrailingStop);
         }
      
      // Check Sell Entry
      else if(SellSignal() == true)
         {
            OpenSell();
            fnTrailingStop(TrailingStop);
         }  
   }
      
  }
//+------------------------------------------------------------------+

   
// Get My Points   
double MyPoint()
{
   double CalcPoint = 0;
   
   if(_Digits == 2 || _Digits == 3) CalcPoint = 0.01;
   else if(_Digits == 4 || _Digits == 5) CalcPoint = 0.0001;
   
   return(CalcPoint);
}


// Get My Slippage
int MySlippage()
{
   int CalcSlippage = 0;
   
   if(_Digits == 2 || _Digits == 4) CalcSlippage = Slippage;
   else if(_Digits == 3 || _Digits == 5) CalcSlippage = Slippage * 10;
   
   return(CalcSlippage);
}



// Check if there is a new bar
bool IsNewBar()   
{        
   static datetime RegBarTime=0;
   datetime ThisBarTime = Time[0];
      
   if (ThisBarTime == RegBarTime)
   {
      return(false);
   }
   else
   {
      RegBarTime = ThisBarTime;
      return(true);
   }
} 

// Returns the number of total open orders for this Symbol and MagicNumber
int TotalOpenOrders()
{
   int total_orders = 0;
   
   for(int order = 0; order < OrdersTotal(); order++) 
   {
      if(OrderSelect(order,SELECT_BY_POS,MODE_TRADES)==false) break;
      
      if(OrderMagicNumber() == MagicNumber && OrderSymbol() == _Symbol)
         {
            total_orders++;
         }
   }
   
   return(total_orders);
}


// Buy Logic------------------------------------
bool BuySignal()
{

   
                  ////////////////// 1MA PERIODS
      double ONE_FASTma = iMA(Symbol(),PERIOD_H1,1,maSHIFT,MODE_EMA,PRICE_CLOSE,0);
      double ONE_FASTma_pre1 = iMA(Symbol(),PERIOD_H1,1,maSHIFT,MODE_EMA,PRICE_CLOSE,1);
      double ONE_FASTma_pre2 = iMA(Symbol(),PERIOD_H1,1,maSHIFT,MODE_EMA,PRICE_CLOSE,2);

          ////////////////// 10MA PERIODS
      double TEN_FASTma = iMA(Symbol(),PERIOD_H1,10,maSHIFT,MODE_EMA,PRICE_CLOSE,0);
      double TEN_FASTma_pre1 = iMA(Symbol(),PERIOD_H1,10,maSHIFT,MODE_EMA,PRICE_CLOSE,1);
      double TEN_FASTma_pre2 = iMA(Symbol(),PERIOD_H1,10,maSHIFT,MODE_EMA,PRICE_CLOSE,2); 

            ////////////////// 50MA PERIODS
      double FIFTY_FASTma = iMA(Symbol(),PERIOD_H1,50,maSHIFT,MODE_EMA,PRICE_CLOSE,0);
      double FIFTY_FASTma_pre1 = iMA(Symbol(),PERIOD_H1,50,maSHIFT,MODE_EMA,PRICE_CLOSE,1);
      double FIFTY_FASTma_pre2 = iMA(Symbol(),PERIOD_H1,50,maSHIFT,MODE_EMA,PRICE_CLOSE,2);

   if( ONE_FASTma_pre1 < TEN_FASTma_pre1 && ONE_FASTma > TEN_FASTma)
   {
      return(true);
   }
   else
   {
      return(false);
   }
}


// Sell Logic-----------------------------------
bool SellSignal()
{

                  ////////////////// 1MA PERIODS
      double ONE_FASTma = iMA(Symbol(),PERIOD_H1,1,maSHIFT,MODE_EMA,PRICE_CLOSE,0);
      double ONE_FASTma_pre1 = iMA(Symbol(),PERIOD_H1,1,maSHIFT,MODE_EMA,PRICE_CLOSE,1);
      double ONE_FASTma_pre2 = iMA(Symbol(),PERIOD_H1,1,maSHIFT,MODE_EMA,PRICE_CLOSE,2);

          ////////////////// 10MA PERIODS
      double TEN_FASTma = iMA(Symbol(),PERIOD_H1,10,maSHIFT,MODE_EMA,PRICE_CLOSE,0);
      double TEN_FASTma_pre1 = iMA(Symbol(),PERIOD_H1,10,maSHIFT,MODE_EMA,PRICE_CLOSE,1);
      double TEN_FASTma_pre2 = iMA(Symbol(),PERIOD_H1,10,maSHIFT,MODE_EMA,PRICE_CLOSE,2); 

            ////////////////// 50MA PERIODS
      double FIFTY_FASTma = iMA(Symbol(),PERIOD_H1,50,maSHIFT,MODE_EMA,PRICE_CLOSE,0);
      double FIFTY_FASTma_pre1 = iMA(Symbol(),PERIOD_H1,50,maSHIFT,MODE_EMA,PRICE_CLOSE,1);
      double FIFTY_FASTma_pre2 = iMA(Symbol(),PERIOD_H1,50,maSHIFT,MODE_EMA,PRICE_CLOSE,2);
      
   if(ONE_FASTma_pre1 > TEN_FASTma_pre1 && ONE_FASTma < TEN_FASTma)
   {
      return(true);
   }
   else
   {
      return(false);
   }
}


// Open Sell Order
void OpenSell()
{
   double ONE_FASTma = iMA(Symbol(),PERIOD_H1,1,maSHIFT,MODE_EMA,PRICE_CLOSE,0);
   double PendingPrice = ONE_FASTma - 25*MyPoint;
   //Open Sell Order
   int ticket = OrderSend(_Symbol,OP_SELLSTOP,LotSize,PendingPrice,MySlippage,StopLoss,TakeProfit,"SELL",MagicNumber);
      
      if(ticket<0)
      {
         Print("OrderSend failed with error #",GetLastError());
      }
      else
      {
         Print("OrderSend placed successfully");
      }
                 
   // Modify Sell Order
   bool res = OrderModify(ticket,OrderOpenPrice(),Bid+StopLoss*MyPoint,Bid-TakeProfit*MyPoint,0);
      
      if(!res)
      {
         Print("Error in OrderModify. Error code=",GetLastError());
      }
      else
      {
         Print("Order modified successfully.");
      }    
}

void OpenBuy()
{
   double ONE_FASTma = iMA(Symbol(),PERIOD_H1,1,maSHIFT,MODE_EMA,PRICE_CLOSE,0);
   double PendingPrice = ONE_FASTma + 27*MyPoint;
   // Open Buy Order
   int ticket = OrderSend(_Symbol,OP_BUYSTOP,LotSize,PendingPrice,MySlippage,StopLoss,TakeProfit,"BUY",MagicNumber);
      
      if(ticket<0)
      {
         Print("OrderSend failed with error #",GetLastError());
      }
      else
      {
         Print("OrderSend placed successfully");
      }
                 
   // Modify Buy Order
   bool res = OrderModify(ticket,OrderOpenPrice(),Ask-StopLoss*MyPoint,Ask+TakeProfit*MyPoint,0);
      
      if(!res)
      {
         Print("Error in OrderModify. Error code=",GetLastError());
      }
      else
      {
         Print("Order modified successfully.");
      }
}

// apply a trailing stop to any trades that match our magic number
void fnTrailingStop(int argTrailingStop)
{
// this variable will holds the total number of trades for the entire account
int all_trades=OrdersTotal();

// use a for loop to cycle through all of the trades, from 0 up to all_trades
for( int cnt=0;cnt<all_trades;cnt++ )
{
// use OrderSelect to get the info for each trade, cnt=0 the first time, then 1, 2, .., etc
// if OrderSelect fails it returns false, so we just continue
if( OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES) == false )
continue;

// compare the magic_number of our EA (as passed in as an input parameter) to the order’s magic number
// if they are equal, increment my_trades
if( MagicNumber == OrderMagicNumber() )
{
// if this trade has a profit greater than zero
if( OrderProfit() > 0 )
{
// for a Buy trade
if( OrderType() == OP_BUY )
{
// if the stoploss is within TrailingStop points of the current bid price
if( OrderStopLoss() < (Bid-(TrailingStop*Point)) )
{
// modify the order: everything stays the same but the stoploss
bool Buy_Modif = OrderModify(OrderTicket(), OrderOpenPrice(), (Bid-(Point*TrailingStop)), OrderTakeProfit(), 0, Yellow);
//return(0);
}
}

// for a Sell trade
if( OrderType() == OP_SELL )
{
// if the stoploss is within TrailingStop points of the current bid price
if( OrderStopLoss() > (Ask+(TrailingStop*Point)) )
{
// modify the order: everything stays the same but the stoploss
bool Sell_Modif = OrderModify(OrderTicket(),OrderOpenPrice(),(Ask+(TrailingStop*Point)),OrderTakeProfit(), 0, Purple);
//return(0);
}
}

}
}
}
}
 
emsahii:

Hello Everybody!


I am about to lose my mind. Why doesn't my code work? Where am i mistaken ? Could you pls guide me and gimme a hand? I really appreciate. Thanks in advance for your help and your time that you are gonna spare !


Here is the code. Please check it out.

Hi,

I see many problems but you can start from the following:

False:   int ticket = OrderSend(_Symbol,OP_SELLSTOP,LotSize,PendingPrice,MySlippage,StopLoss,TakeProfit,"SELL",MagicNumber);

Correct: int ticket = OrderSend(_Symbol,OP_SELLSTOP,LotSize,PendingPrice,MySlippage,PendingPrice+StopLoss*MyPoint,PendingPrice-TakeProfit*MyPoint,"SELL",MagicNumber);



False:   int ticket = OrderSend(_Symbol,OP_BUYSTOP,LotSize,PendingPrice,MySlippage,StopLoss,TakeProfit,"BUY",MagicNumber);

Correct: int ticket = OrderSend(_Symbol,OP_BUYSTOP,LotSize,PendingPrice,MySlippage,PendingPrice-StopLoss*MyPoint,PendingPrice+TakeProfit*MyPoint,"BUY",MagicNumber);
 
emsahii: Why doesn't my code work? Where am i mistaken ?
  1. Why did you post your MT4 question in the Root / MT5 General section instead of the MQL4 section, (bottom of the Root page?)
              General rules and best pratices of the Forum. - General - MQL5 programming forum
    Next time post in the correct place. The moderators will likely move this thread there soon.

  2. Use the debugger or print out your variables, including _LastError and prices and . Do you really expect us to debug your code for you?

  3. Had you done № 2 you would have found Arash is almost correct.
    You buy at the Ask and sell at the Bid. So for buy orders you pay the spread on open. For sell orders you pay the spread on close.
    1. Your buy order's TP/SL (or Sell Stop's/Sell Limit's entry) are triggered when the Bid reaches it. Not the Ask. Your SL is shorter by the spread and your TP would be longer. Don't you want the same/specified amount for either direction?
    2. Your sell order's TP/SL (or Buy Stop's/Buy Limit's entry) will be triggered when the Ask reaches it. To trigger at a specific Bid price, add the average spread.
                MODE_SPREAD (Paul) - MQL4 programming forum - Page 3 #25
    3. The charts show Bid prices only. Turn on the Ask line to see how big the spread is (Tools → Options (Control-O) → charts → Show ask line.)