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]);
}
}
//+------------------------------------------------------------------+