Works on the strategy tester but not on live trading - page 2

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

You buy at the Ask and sell at the Bid.

  1. Your buy order's TP/SL (or Sell Stop's/Sell Limit's entry) are triggered when the Bid / OrderClosePrice reaches it. Using the Ask±n, makes your SL shorter and your TP longer, by the spread. Don't you want the specified amount used in either direction?

  2. Your sell order's TP/SL (or Buy Stop's/Buy Limit's entry) will be triggered when the Ask / OrderClosePrice 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.)
    Most brokers with variable spread widen considerably at end of day (5 PM ET) ± 30 minutes. My GBPJPY (OANDA) shows average spread = 26 points, but average maximum spread = 134.

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

Which is the wrong section.

Anything to do with MT4 should be posted in the  MQL4 and Metatrader 4 section.

 
Keith Watford:

Which is the wrong section.

Anything to do with MT4 should be posted in the  MQL4 and Metatrader 4 section.

Oh I am sorry, I shall post in that section from now on. Could you explain 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...
 
Styler - Developing programs - MetaEditor Help
Styler - Developing programs - MetaEditor Help
  • www.metatrader5.com
The styler quickly brings a source code design in line with the recommended standard. This makes the code look professional and easy to read. A well-designed code is much easier to analyze in the future both for its author and for other users. To launch Styler, click Styler in the Tools menu or press Ctrl+,. General Spaces and blank lines...
 
henrygeer:
Could you explain 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?

Do not just copy examples of code without understanding what it is doing.

Your earlier code....

   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"); 

Now use the styler

   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)   //This is not indented so is a different block of code not related to the above ifs
     {
      Print("OrderSend failed with error #",GetLastError());
     }
   else
      Print("OrderSend placed successfully");

Change your code using a pair of { }

   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"); 
   }

and use the styler

   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");
        }

now you can see that the complete block of code within the { } is executed only if the if condition is satisfied.

Reason: