EA not trading when in DEMO testing

 

Good evening expert traders and programmers:

This seems a common enough problem, yet I've not managed to find a solution in any of the threads.

After writing and back testing a few EA's I have been trying to demo test and can not get the EA to trade. I have checked and rechecked the enabling dialogue boxes and even played with the Vista permissions, before uploading to an XP VPS. Print statements in log file confirm that trading signals are being received and when I tick the "ask manual confirmation" it opens the terminal's trading dialogue box and the resulting manually confirmed trade has a magic number corresponding to the EA.

Believing that the problem must be in my code I downloaded a very simple EA by Andrew Young and changed the trade conditions to be once every bar, just to test the automatic trading - same result as before.

I increased the allowable slippage to 50 (from 5) - still no change.

With my limited knowledge, I have exhausted the potential solutions - if anyone has further ideas I would be very appreciative.


thanks in advance


I have posted the code

//+------------------------------------------------------------------+
//|                                                       Simple.mq4 |
//|                                                     Andrew Young |
//|                                   http://www.easyexpertforex.com |
//+------------------------------------------------------------------+

#property copyright "Andrew Young"


// External variables
extern double LotSize = 0.1;
extern double StopLoss = 50;
extern double TakeProfit = 100;

extern int Slippage = 5;
extern int MagicNumber = 123;

extern int FastMAPeriod = 2;
extern int SlowMAPeriod = 20;


// Global variables
int BuyTicket;
int SellTicket;
double UsePoint;
int UseSlippage;


// Init function
int init()
        {
      UsePoint = PipPoint(Symbol());
      UseSlippage = GetSlippage(Symbol(),Slippage);
        }


// Start function
int start()
        {
                // Moving averages
                double FastMA = iMA(NULL,0,FastMAPeriod,0,0,0,0);
                double SlowMA = iMA(NULL,0,SlowMAPeriod,0,0,0,0);
                
                
                // Buy order 
                if(Close[1]> Close[2] && BuyTicket == 0)
                        {
                                                                                                           Print("Buy Signal");
                                OrderSelect(SellTicket,SELECT_BY_TICKET);
                
                                // Close order
                                if(OrderCloseTime() == 0 && SellTicket > 0)
                                        {
                                                double CloseLots = OrderLots();
                                                double ClosePrice = Ask;

                                                bool Closed = OrderClose(SellTicket,CloseLots,ClosePrice,UseSlippage,Red);
                                        }                               

                                double OpenPrice = Ask;
                                
                                // Calculate stop loss and take profit
                                if(StopLoss > 0) double BuyStopLoss = OpenPrice - (StopLoss * UsePoint);
                                if(TakeProfit > 0) double BuyTakeProfit = OpenPrice + (TakeProfit * UsePoint);
                                
                                // Open buy order
                                BuyTicket = OrderSend(Symbol(),OP_BUY,LotSize,OpenPrice,UseSlippage,BuyStopLoss,BuyTakeProfit,"Buy Order",MagicNumber,0,Green);


                                SellTicket = 0;
                        }
                
                
                // Sell Order 
                if(Close[1] < Close[2] && SellTicket == 0)
                        {
                                                                                Print("Sell Signal");
                                OrderSelect(BuyTicket,SELECT_BY_TICKET);
                
                                if(OrderCloseTime() == 0 && BuyTicket > 0)
                                        {
                                                CloseLots = OrderLots();
                                                ClosePrice = Bid;

                                                Closed = OrderClose(BuyTicket,CloseLots,ClosePrice,UseSlippage,Red);
                                        }               
                                
                                OpenPrice = Bid;
                                
                                if(StopLoss > 0) double SellStopLoss = OpenPrice + (StopLoss * UsePoint);
                                if(TakeProfit > 0) double SellTakeProfit = OpenPrice - (TakeProfit *    UsePoint);
                                
                                SellTicket = OrderSend(Symbol(),OP_SELL,LotSize,OpenPrice,UseSlippage,SellStopLoss,SellTakeProfit,"Sell Order",MagicNumber,0,Red);
        

                                BuyTicket = 0;
                        }
                        
                return(0);
        }


// Pip Point Function
double PipPoint(string Currency)
        {
                int CalcDigits = MarketInfo(Currency,MODE_DIGITS);
                if(CalcDigits == 2 || CalcDigits == 3) double CalcPoint = 0.01;
                else if(CalcDigits == 4 || CalcDigits == 5) CalcPoint = 0.0001;
                return(CalcPoint);
        }


// Get Slippage Function
int GetSlippage(string Currency, int SlippagePips)
        {
                int CalcDigits = MarketInfo(Currency,MODE_DIGITS);
                if(CalcDigits == 2 || CalcDigits == 4) double CalcSlippage = SlippagePips;
                else if(CalcDigits == 3 || CalcDigits == 5) CalcSlippage = SlippagePips * 10;
                return(CalcSlippage);
        }

 
  1. OrderSelect(BuyTicket,SELECT_BY_TICKET);
    if(OrderCloseTime() == 0 && BuyTicket > 0)
    Always test return codes. If the orderSelect fails such as when the order hit a stop, then OrderTicket() OrderCloseTime() etc are meaning less. Otherwise OrderCloseTime() will ALWAYS be zero.
  2. SellTicket = OrderSend(Symbol(),OP_SELL,LotSize,OpenPrice
    Always test return codes so you can find out WHY.
    SellTicket = OrderSend(Symbol(),OP_SELL,LotSize,OpenPrice...
    if (SellTicket < 0) Alert("OrderSend(sell) failed: ", GetLastError());

 
WHRoeder:
  1. Always test return codes. If the orderSelect fails such as when the order hit a stop, then OrderTicket() OrderCloseTime() etc are meaning less. Otherwise OrderCloseTime() will ALWAYS be zero.
  2. Always test return codes so you can find out WHY.

Thank you for that advice. I have been using Andrew Young's book to learn programming so my view of the MQL4 world is very restricted!

In a related thread I think I may have seen your comments that with ECN brokers you can not set TP and SL at the order send, but need to do it through an order modification.This seems to replicate the manual order method through the terminal.

I did notice and realized belatedly that when in "manual confirmation" mode, the EA did not place the SL and TP's - so perhaps that is the problem? I will make these changes also and wait for my broker to re-open Monday midnight (GMT + 3).


cheers

 
Don't need to wait, run it in the tester/visual mode
 
WHRoeder:
Don't need to wait, run it in the tester/visual mode

That won't work!

The problem is that it works fine in the tester (when back testing) but when I forward test using the demo account, it does not open the trades.

 
jgf:

That won't work!

The problem is that it works fine in the tester (when back testing) but when I forward test using the demo account, it does not open the trades.

Just in follow-up.

As I later suspected, my broker really works as an ECN and therefore, all that was needed was to send the order first and than modify it by adding SL and TP.

The bloody annoying thing was that the broker seemed unaware that this was a requirement!!!!

thanks for all the input

Reason: