Works on the strategy tester but not on live trading

 
Hi I have made this ea which works fine on the strategy tester but when I put it on my vps for live trading it doesn't open any trades and I can't seem to figure out why. Hope someone can help!
//+------------------------------------------------------------------+
//|                                                       Daily1.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

extern int TakeProfit = 50;
extern int StopLoss = 15;
extern bool UseTrailingStop = true;
extern int WhenToTrail = 20;
extern int TrailAmount = 20;
extern double LotSize = 1.0;
extern int MagicNumber = 1234;
double Pips;






//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
   double ticksize = MarketInfo(Symbol(),MODE_TICKSIZE);
   if(ticksize == 0.00001 || ticksize == 0.001)
   Pips = ticksize*10;
   else Pips = ticksize;
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
   
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---
   double movingAverage = iMA(NULL,0,50,0,MODE_EMA,PRICE_CLOSE,0);
   double K0=iStochastic(_Symbol,_Period,5,3,3,MODE_SMA,0,MODE_MAIN,0);
   double D0=iStochastic(_Symbol,_Period,5,3,3,MODE_SMA,0,MODE_SIGNAL,0);
   double K1=iStochastic(_Symbol,_Period,5,3,3,MODE_SMA,0,MODE_MAIN,1);
   double D1=iStochastic(_Symbol,_Period,5,3,3,MODE_SMA,0,MODE_SIGNAL,1);
   
   if(Close[0]<movingAverage)
   if(K0 < 20)
   if(Close[0]>Open[1])
   if(OrdersTotal()==0)
   OrderSend(Symbol(),OP_BUY,LotSize,Ask,3,Ask-(StopLoss*Pips),Ask+(TakeProfit*Pips),NULL,MagicNumber,0,Green);  

   if(Close[0]>movingAverage)
   if(K0 > 80)  
   if(Close[0]<Open[1])
   if(OrdersTotal()==0) 
   OrderSend(Symbol(),OP_SELL,LotSize,Bid,3,Bid+(StopLoss*Pips),Bid-(TakeProfit*Pips),NULL,MagicNumber,0,Red);
  
   for(int b=OrdersTotal()-1;b>=0;b--) 
   if(OrderSelect(b,SELECT_BY_POS,MODE_TRADES))  
   if(OrderMagicNumber()==MagicNumber) 
   if(OrderSymbol()==Symbol())
   if(OrderType()==OP_BUY)
   if(Bid-OrderOpenPrice()>WhenToTrail*Pips)
   if(OrderStopLoss()<Bid-TrailAmount*Pips)
   OrderModify(OrderTicket(),OrderOpenPrice(),Bid-(TrailAmount*Pips),OrderTakeProfit(),0,CLR_NONE);
   
   for(int s=OrdersTotal()-1;s>=0;s--)
   if(OrderSelect(s,SELECT_BY_POS,MODE_TRADES))
   if(OrderMagicNumber()== MagicNumber)
   if(OrderSymbol()==Symbol())
   if(OrderType()==OP_SELL)
   if(OrderOpenPrice()-Ask>WhenToTrail*Pips)
   if(OrderStopLoss()>Ask+TrailAmount*Pips)
   OrderModify(OrderTicket(),OrderOpenPrice(),Ask+(TrailAmount*Pips),OrderTakeProfit(),0,CLR_NONE);   
  
   }



//+------------------------------------------------------------------+
 
OrderSend(Symbol(),OP_BUY,LotSize,Ask,3,Ask-(StopLoss*Pips),Ask+(TakeProfit*Pips),NULL,MagicNumber,0,Green);  

Check the return of OrderSend() and print the error.

 
In future please post in the correct section
I will move your topic to the MQL4 and Metatrader 4 section.
 
Keith Watford:
In future please post in the correct section
I will move your topic to the MQL4 and Metatrader 4 section.
Oh sorry I didn't mean to! thank you!
 
Like this?
   if(Close[0]>movingAverage)
   if(K0 > 80)  
   if(Close[0]<Open[1] && Close[0]>Close[2])
   if(OrdersTotal()==0) 
   OrderSend(Symbol(),OP_SELL,LotSize,Bid,3,Bid+(StopLoss*Pips),Bid-(TakeProfit*Pips),NULL,MagicNumber,0,Red);
   Print("OrderSend failed with error #",GetLastError());

Keith Watford:

Check the return of OrderSend() and print the error.

 
henrygeer:
Like this?

Read the documentation for OrderSend(). It even gives you an example of dealing with errors.

 
Keith Watford:

Read the documentation for OrderSend(). It even gives you an example of dealing with errors.

   if(Close[0]>Open[1])
   if(OrdersTotal()==0)
   ticket=OrderSend(Symbol(),OP_BUY,LotSize,Ask,3,Ask-(StopLoss*Pips),Ask+(TakeProfit*Pips),NULL,MagicNumber,0,Green);  
   if(ticket<0) 
     { 
      Print("OrderSend failed with error #",GetLastError()); 
     } 
   else 
      Print("OrderSend placed successfully"); 
      
      
   if(Close[0]<Open[1])
   if(OrdersTotal()==0) 
   ticket=OrderSend(Symbol(),OP_SELL,LotSize,Bid,3,Bid+(StopLoss*Pips),Bid-(TakeProfit*Pips),NULL,MagicNumber,0,Red);
   if(ticket<0) 
     { 
      Print("OrderSend failed with error #",GetLastError()); 
     } 
   else 
      Print("OrderSend placed successfully");   

I copy and pasted the highlighted piece of code above from one of your comments on another thread, but when I ran the code it just rapidly fired out lots of "OrderSend placed successfully" messages without actually placing any trades. Do you know why this is?

 
henrygeer:

I copy and pasted the highlighted piece of code above from one of your comments on another thread, but when I ran the code it just rapidly fired out lots of "OrderSend placed successfully" messages without actually placing any trades. Do you know why this is?

Yes.

   if(Close[0]>Open[1])
      if(OrdersTotal()==0)
         ticket=OrderSend(Symbol(),OP_BUY,LotSize,Ask,3,Ask-(StopLoss*Pips),Ask+(TakeProfit*Pips),NULL,MagicNumber,0,Green);
         
   //The following code is completely independent of the above if so will be executed regardless of whether the conditions are met.
         
   if(ticket<0)
     {
      Print("OrderSend failed with error #",GetLastError());
     }
   else
      Print("OrderSend placed successfully");

You should get into the habit of using the styler as it will often reveal such errors. Unrelated code will not be indented.

 
henrygeer:

I have deleted your other topic as it seems to be related.

Do not double post!

You've been told already....

Keith Watford:
In future please post in the correct section
I will move your topic to the MQL4 and Metatrader 4 section.

Yet you posted in the wrong section again!

 
Keith Watford:

I have deleted your other topic as it seems to be related.

Do not double post!

You've been told already....

Yet you posted in the wrong section again!

I'm so sorry I thought I posted it in the expert advisors section! Won't happen again I promise
 
Keith Watford:

Yes.

You should get into the habit of using the styler as it will often reveal such errors. Unrelated code will not be indented.

Why is the bottom part of code independant of the top part? Everywhere I've seen this code it has been written the same way? Like on this page: https://www.mql5.com/en/forum/139592 Also what is the styler?
What are Function return values ? How do I use them ?
What are Function return values ? How do I use them ?
  • 2012.05.20
  • www.mql5.com
I see many, many people posting code and asking questions which can usually be answered by simply checking the return values from a few Functions a...
Reason: