EA in strategy tester work, on LIVE not ...need help

 

Hi all!

We made with friend EA breakout strategy but EA only work in strategy tester, on live trading (demo) not

Can someone please look at code and correct it.

Thanks to all

Here is code:

----------------------------------------

// static defines

#define MAGICMA 87265234
#define SLIPPAGE 0
#define COLOR_CLOSE White
#define COLOR_OPEN_BUY Blue
#define COLOR_OPEN_SELL Red
#define DUMMY_LO 99999999
#define DUMMY_HI -1
#define ONE_DAY_SECONDS 86400
#define SUNDAY 0
#define MONDAY 1

// external inputs

extern string samplingStartTime = "12:00";   //start scaning high/low from that period for entry  trade
extern string samplingEndTime   = "14:00";   // stop scaning high/low till that period
extern int historyBars = 1000;

extern string tradingStartTime = "14:00:01";  // start trading
extern string tradingEndTime   = "23:59";

extern double lots = 0.10;

extern double pipsEntry = 0.0002;  // entry in position over high or low in scaning period +/- pips
extern double pipsProfit = 0.002;
extern double positiveStopSell = 0.0002;  //if trade positive exit  +/- pips under high/low of candle
extern double negativeStopSell = 0.0020;   // stoploss

extern bool debug = true;

// internal global variables

// modes of operation
bool sampling = false;
bool trading = false;

// these two are discovered during sampling
double lo = DUMMY_LO; // initial dummy value
double hi = DUMMY_HI; // initial dummy value
bool justStarted = true;

datetime samplingStartTimeDT;
datetime samplingEndTimeDT;
datetime tradingStartTimeDT;
datetime tradingEndTimeDT;
int currentDayOfYear = -1;

bool buyOpened = false;
bool buyClosed = false;
bool sellOpened = false;
bool sellClosed = false;

int init() { return(0); }

int deinit() { return(0); }

void start() {
   if (badInputs()) {
      return;
   }

   bool prev_sampling = sampling;
   bool prev_trading = trading;

   determineProgramMode();
  
   string prev_mode = "";
   if (prev_sampling) {
      prev_mode = "Sampling";
   } else if (prev_trading) {
      prev_mode = "Trading";
   } else {
      prev_mode = "Nothing";
   }
  
   string new_mode = "";
   if (sampling) {
      new_mode = "Sampling";
   } else if (trading) {
      new_mode = "Trading";
   } else {
      new_mode = "Nothing";
   }
  
   if (prev_sampling && !sampling) {
      d("Sampling finished! lo=" + lo + " hi=" + hi);
   }
  
   if (prev_mode != new_mode) {
      d(prev_mode + " -> " + new_mode);
   }
  
   if (sampling) {
      //d("Sampling...");
      sample();
   } else if (trading) {
      //d("Trading...");
      int numOrders = CalculateCurrentOrders(Symbol());
      if (numOrders > 1 || numOrders < -1) {
         Print("Multiple existing orders detected: " + numOrders + ", bailing out...");
         return;
      }
      trade(numOrders);
   } else {
      //d("Nothing!?");
   }
}

void determineProgramMode() {

   // if we have an outstanding order, keep trading until it's closed
   if (sellOpened || buyOpened) {
      sampling = false;
      trading = true;
      return;
   }

   if (!IsTradeAllowed()) {
      d("Trade not allowed!?");
      sampling = false;
      trading = false;
      return;
   }
  
   // do not sample or trade on sunday
   if (DayOfWeek() == SUNDAY) {
      d("Sunday detected!");
      sampling = false;
      trading = false;
      return;
   }
  
   datetime currentTime = TimeCurrent();
   int dayOfYear = TimeDayOfYear(currentTime);
   if (currentDayOfYear != dayOfYear) {
      d("New day detected: " + dayOfYear);
     
      // a new day detected, reset everything
      // this assumes no trading is done...
      buyOpened = false;
      buyClosed = false;
      sellOpened = false;
      sellClosed = false;
      trading = false;
      sampling = false;
      lo = DUMMY_LO;
      hi = DUMMY_HI;
     
      samplingStartTimeDT = StrToTime(samplingStartTime);
      samplingEndTimeDT = StrToTime(samplingEndTime);
      tradingStartTimeDT = StrToTime(tradingStartTime);
      tradingEndTimeDT = StrToTime(tradingEndTime);
      currentDayOfYear = dayOfYear;
   }
  
   if (currentTime >= samplingStartTimeDT && currentTime <= samplingEndTimeDT) {
      sampling = true;
      trading = false;
   } else if (currentTime >= tradingStartTimeDT && currentTime <= tradingEndTimeDT) {
      sampling = false;
      trading = true;
   } else {
      sampling = false;
      trading = false;
   }

   if (lo == DUMMY_LO || hi == DUMMY_HI) {
      //d("Trade not possible. No sampling until now!?");
      trading = false;
   }
}

double historicalLo() {
   return (Low[iLowest(Symbol(), 0, MODE_LOW, historyBars, 0)]);
}

double historicalHi() {
   return (High[iHighest(Symbol(), 0, MODE_HIGH, historyBars, 0)]);
}

void sample() {

   if (justStarted) {
      //hi = historicalHi();
      //lo = historicalLo();
      justStarted = false;
      //d("Historical sampling! lo=" + lo + " hi=" + hi);
   }

   if (Ask > hi) {
      //d("Sampling reached new hi: " + hi);
      hi = Ask;
   } else if (Bid < lo) {
      //d("Sampling reached new lo: " + lo);
      lo = Bid;
   } else {
      //d("No change in hi and lo... hi " + hi + " lo " + lo + " bid " + Bid);
   }
}

void trade (int numOrders) {
   RefreshRates();
   if (numOrders == 0) {
      checkForOpen();
   } else {
      checkForClose();
   }
}

void checkForOpen() {
   //d("Check for open");
   int res;
   if (isOkForBuy()) {
      int exp = CurTime() + 60*PERIOD_D1;
      res = OrderSend(Symbol(), OP_BUY, lots, Ask, SLIPPAGE, 0, 0, "", MAGICMA, exp, COLOR_OPEN_BUY);
      if (res == -1) {
         Print("Error buying: " + GetLastError());
      } else {
         buyOpened = true;
      }
      return;
   } else if (isOkForSell()) {
      res = OrderSend(Symbol(), OP_SELL, lots, Bid, SLIPPAGE, 0, 0, "", MAGICMA, 0, COLOR_OPEN_SELL);
      if (res == -1) {
         Print("Error opening sell: " + GetLastError());
      } else {
         sellOpened = true;
      }
      return;
   }
}

bool isOkForSell() {
   if (lo - Bid >= pipsEntry && /*!buyOpened &&*/ !sellClosed) {
      d("selling because bid " + Bid + " - lo " + lo + " < pips: " + (Bid - lo));
      return (true);
   } else {
      return (false);
   }
}

bool isOkForBuy() {
   if (Ask - hi > pipsEntry && /*!sellOpened &&*/ !buyClosed) {
      return (true);
   } else {
      return (false);
   }
}

void checkForClose() {
   bool closed;
   int otot = OrdersTotal();
   for (int i = otot - 1; i >= 0; i--) {
      if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES) == false) break;
      if (OrderMagicNumber() != MAGICMA || OrderSymbol() != Symbol()) continue;
      // at this point we found our open order and we need to close it
      if (OrderType() == OP_BUY) {
         if (closeBuy()) {     
            //d("Closing BUY, ticket " + OrderTicket());
            closed = OrderClose(OrderTicket(), OrderLots(), Bid, SLIPPAGE, COLOR_CLOSE);
            if (!closed) {
               Print("Error closing buy order " + OrderTicket() + " - " + GetLastError());
            } else {
               buyOpened = false;
               buyClosed = true;
               //d("Closed buy order " + OrderTicket());
            }
         } else {
             //d("Did not close buy");
         }
      } else if (OrderType() == OP_SELL) {
         if (closeSell()) {
         //d("Closing SELL, ticket: " + OrderTicket());
         closed = OrderClose(OrderTicket(), OrderLots(), Ask, SLIPPAGE, COLOR_CLOSE);
         if (!closed) {
            Print("Error closing sell order " + OrderTicket() + " - " + GetLastError());
         } else {
            sellOpened = false;
            sellClosed = true;
            //Print("Closed sell order " + OrderTicket());
            }
         } else {
           //d("Did no close sell");
         }
      } else {
         //d("Non-closable order type " + OrderType());
      }
   }
}

bool closeSell() {
   bool positive = Ask >= High[1] + positiveStopSell;
   double profit = OrderOpenPrice() - Low[1];
   positive = positive && (profit >= pipsProfit);
   if (positive) {
      return (true);
   }
   bool negative = OrderOpenPrice() + negativeStopSell <= Ask;
   return (negative);
}

bool closeBuy() {
   bool positive = Bid <= Low[1] - positiveStopSell;
   double profit = High[1] - OrderOpenPrice();
   positive = positive && (profit >= pipsProfit);
   if (positive) {
      return (true);
   }
   bool negative = OrderOpenPrice() - negativeStopSell >= Bid;
   return (negative);
}

int CalculateCurrentOrders(string symbol) {
   int buys = 0;
   int sells = 0;
   // see all orders selected from trading pool (opened and pending orders)
   int ot = OrdersTotal();
   for(int i = ot - 1; i>=0; i--) {
      //d("Checking order " + i);
      // select an order for further processing
      // if the function fails -> break
      if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES) == false) {
         Print("Select failed!");
         break;
      }
      if (OrderSymbol() == Symbol() && OrderMagicNumber() == MAGICMA) {
         if (OrderType() == OP_BUY || OrderType() == OP_BUYSTOP) {
            buys++;
         } else if (OrderType() == OP_SELL || OrderType() == OP_SELLSTOP) {
            sells++;
         }
      }
   }
   //d("Calculated buys: " + buys + " and sells: " + sells);
   if (buys > 0) {
      return(buys);
   } else {
      return(-sells);
   }
}

bool badInputs() {
   return (false); // todo
}

void d(string s) {
   if (debug) {
      Print(s);
   }
 

Forum on trading, automated trading systems and testing trading strategies


Hello,

Please use the SRC button when you post code. Thank you.


This time, I edited it for you.


 
gass:

Hi all!

We made with friend EA breakout strategy but EA only work in strategy tester, on live trading (demo) not

Can someone please look at code and correct it.

Thanks to all

Here is code:

----------------------------------------

What doesn't work ? Please explain your problem.
 
As Alain said. Are you getting any journal errors? Haven't looked at all your code without more info but the first place I'd be starting is "IsTradeAllowed"
 
angevoyageur:
What doesn't work ? Please explain your problem.

Hi

Thank you for help

So...I start MT4 then put EA into chart window then EA start to look high and low in specified period ...for example EA start at 8h then EA start to look high and low from 12h till 14h (lunch time) then at 14:00:01 EA is ready to trade, offcourse if some candle breaks leveles high or low in specific period (12h till14h)

In Meta editor is no errors

When I start EA to LiVE trading (demo) then nothing happend...price go up or down but there is not buy or sell activity

Thanks

p.s. I did't know for SRC...my apologies. I will do it next time

 
gass:

Hi

Thank you for help

So...I start MT4 then put EA into chart window then EA start to look high and low in specified period ...for example EA start at 8h then EA start to look high and low from 12h till 14h (lunch time) then at 14:00:01 EA is ready to trade, offcourse if some candle breaks leveles high or low in specific period (12h till14h)

In Meta editor is no errors

When I start EA to LiVE trading (demo) then nothing happend...price go up or down but there is not buy or sell activity

Thanks

p.s. I did't know for SRC...my apologies. I will do it next time

Sorry, just need to ask one more time - are you seeing any journal errors (not MetaEditor errors). Does the EA have a happy face? Have you allowed auto trading in both options?

Cheers
Filter
 
Filter:
Sorry, just need to ask one more time - are you seeing any journal errors (not MetaEditor errors). Does the EA have a happy face? Have you allowed auto trading in both options?

Cheers
Filter

yes I have al smiles ...auto tradeing button is ON ...also in EA propertis Allow live trading is ON

In strategy tester journal is writen:  TestGenerator:unmatched data error (volume limit 1113 at 2015.01.05 00:00 exceeded) - - - I put test period from 5.1.14 till today and after that errors in journal EA start trading in strategy tester

In LIVE journal is writen: Expert loaded successfully .....automated trading enabled ...but no trading activity

Thank you

 
gass:

yes I have al smiles ...auto tradeing button is ON ...also in EA propertis Allow live trading is ON

In strategy tester journal is writen:  TestGenerator:unmatched data error (volume limit 1113 at 2015.01.05 00:00 exceeded) - - - I put test period from 5.1.14 till today and after that errors in journal EA start trading in strategy tester

In LIVE journal is writen: Expert loaded successfully .....automated trading enabled ...but no trading activity

Thank you

Print your variables values. Debug your code.
 
angevoyageur:
Print your variables values. Debug your code.

I click debug (play) and recieve resaults (picture attached)

Debug resaults

I have under basic programing skills ...so I hope that picture is helpful.

I assume that problem is in row 140 and 69

Thank you

 
gass:

I click debug (play) and recieve resaults (picture attached)


I have under basic programing skills ...so I hope that picture is helpful.

I assume that problem is in row 140 and 69

Thank you

No, it's not useful.
 
angevoyageur:
No, it's not useful.

Here is variables print screen

What I have to send more

Please try code at MT4 and you will see (in attach

Files:
BreakOut.mq4  10 kb
Reason: