Simple EA doesnt copy the right Closing prices (CopyClose()) - why?

 

Hey, I made an EA that sells once the price goes below the EMA, and closes position, once the last bar closed above the EMA.

This code is very simple but still, the EA doesnt copy the right close prices of the last bar, and I dont know why. I printed all the last 10 closeprices of the last 10 bars, and the values are wrong.

Am I right that the function CopyClose(_Symbol,_Period,0,10,lastBarClose); copies the close prices of the last 10 bars to the array lastBarClose? or which function should I use to get closing prices of the last bars?

The EA opens thousands of short positions and closes them the same second, and the price never even went above the EMA line.

 

//+------------------------------------------------------------------+
//|                                     #X_Simple_Moving_Average.mq5 |
//|                        Copyright 2016, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2016, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#include <Trade\Trade.mqh>

input int ma_period= 20;        // EMA period
int emaHandle;
double emaArray[];
double lastBarClose[11];
CTrade traderClass;
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- Do we have sufficient bars to work
   if(Bars(_Symbol,_Period)<60) // total number of bars is less than 60?
     {
      Alert("We have less than 60 bars on the chart, an Expert Advisor terminated!!");
      return(-1);
     }
  emaHandle=iMA(NULL,_Period,ma_period,0,MODE_EMA,PRICE_CLOSE);
   return(0);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//--- release indicator handles
   IndicatorRelease(emaHandle);
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---
   double Ask = SymbolInfoDouble(_Symbol,SYMBOL_ASK);   // Ask price
   double Bid = SymbolInfoDouble(_Symbol,SYMBOL_BID);   // Bid price 
   ArraySetAsSeries(emaArray,true);
//   ArraySetAsSeries(lastBarClose,true);
   CopyClose(_Symbol,_Period,0,10,lastBarClose);

   //--- Copy the new values of our indicators to buffers (arrays) using the handle
   if(CopyBuffer(emaHandle,0,0,5,emaArray)<0)
     {
      Alert("Error copying EMA indicator Buffers - error:",GetLastError(),"!!");
      return;
     }
/*   if(PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_BUY && Bid<emaArray[0]){
      traderClass.PositionClose(_Symbol,2);
      Print("closed position because ",Bid," is smaller than ",emaArray[0]);
      }*/
   if(PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_SELL && lastBarClose[1]>emaArray[1]){  //if last bar closed above EMA, close position
      traderClass.PositionClose(_Symbol,2);
      Print("closed position because ",lastBarClose[1]," is bigger than ",emaArray[0]);
      }
/*   if(!PositionSelect(_Symbol) && Bid>emaArray[0]){      //open Long position
      traderClass.PositionOpen(_Symbol,ORDER_TYPE_BUY,0.5,Bid,0,0,NULL);
      Print("i am bying because ",Bid," is bigger than ",emaArray[0]);
      Sleep(1000*3600);
      }*/
   if(!PositionSelect(_Symbol) && Bid<emaArray[0]){      //open short position once the price goes below EMA line
      traderClass.PositionOpen(_Symbol,ORDER_TYPE_SELL,0.5,Bid,0,0,NULL);
      Print("i am selling because ",Bid," is smaller than ",emaArray[0]);
      }
  }
//+------------------------------------------------------------------+
 
LX1991:

Hey, I made an EA that sells once the price goes below the EMA, and closes position, once the last bar closed above the EMA.

This code is very simple but still, the EA doesnt copy the right close prices of the last bar, and I dont know why. I printed all the last 10 closeprices of the last 10 bars, and the values are wrong.

Am I right that the function CopyClose(_Symbol,_Period,0,10,lastBarClose); copies the close prices of the last 10 bars to the array lastBarClose? or which function should I use to get closing prices of the last bars?

The EA opens thousands of short positions and closes them the same second, and the price never even went above the EMA line.

 


Because emaArray is set serie as true, then lastBarClose should also be set series as a true.

 
Roberto Jacobs:


Because emaArray is set serie as true, then lastBarClose should also be set series as a true.

thank you for the answer, I tried it now again with the second ArraySetAsSeries (already tried it before I made this post and now again) and nothing changed. Problem is that CopyClose() function does not work, it copies the wrong data into the array. I printed the close prices of the last 10 bars and looked at the chart and compared them, they are wrong by a few pips. I also tried using 
MqlRates rates[];
int copied=CopyRates(_Symbol,_Period,0,30,rates);
instead of CopyClose(), but still the values are wrong. 
 

fixed

//+------------------------------------------------------------------+
//|                                     #X_Simple_Moving_Average.mq5 |
//|                        Copyright 2016, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2016, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#include <Trade\Trade.mqh>

input int ma_period= 20;        // EMA period
int emaHandle;
double emaArray[];
double lastBarClose[];
CTrade traderClass;
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- Do we have sufficient bars to work
   if(Bars(_Symbol,_Period)<60) // total number of bars is less than 60?
     {
      Alert("We have less than 60 bars on the chart, an Expert Advisor terminated!!");
      return(-1);
     }
  emaHandle=iMA(NULL,_Period,ma_period,0,MODE_EMA,PRICE_CLOSE);
   return(0);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//--- release indicator handles
   IndicatorRelease(emaHandle);
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---
   double Ask = SymbolInfoDouble(_Symbol,SYMBOL_ASK);   // Ask price
   double Bid = SymbolInfoDouble(_Symbol,SYMBOL_BID);   // Bid price 
   
   
   ArraySetAsSeries(emaArray,true);
   
   ArrayResize(lastBarClose,10);
   ArraySetAsSeries(lastBarClose,true);
   CopyClose(_Symbol,_Period,0,10,lastBarClose);
   
   Comment(lastBarClose[0]);


   //--- Copy the new values of our indicators to buffers (arrays) using the handle
   if(CopyBuffer(emaHandle,0,0,5,emaArray)<0)
     {
      Alert("Error copying EMA indicator Buffers - error:",GetLastError(),"!!");
      return;
     }

  }
//+------------------------------------------------------------------+



 
Nguyen Nga:

fixed

thank you I really appreciate your help! really, thank you so much you literally saved my day.
 

Wow, am trying to work on something and am thinking of using the copy close function, I have not used it before actually so i need your help, that's how i got to this thread, this is my first time of seeking for help here so please pardon my mistakes.

Okay here its, am trying to code an EA that places a pending order based on the cross over of two moving averages and the sign of the MACD BAR(+/-), but the condition is that the price of the order has to be 3 pips below/above the closing price of the candle bar where the MA crossover occurred.

So am thinking of using a copyclose() function to copy closing price from when the cross over happened, so i can use it in my Order function.

is this possible please, am still trying to figure it out, I can provide more information if its not clear enough please.


Thank you for your prompt response 

Reason: