Example, how to get data from the iMA indicator (note that the indicator handler needs to be created ONE times in OnInit ()):
//+------------------------------------------------------------------+ //| iMAGet.mq5 | //| Copyright © 2017, Vladimir Karputov | //| http://wmua.ru/slesar/ | //+------------------------------------------------------------------+ #property copyright "Copyright © 2017, Vladimir Karputov" #property link "http://wmua.ru/slesar/" #property version "1.001" //--- input parameters input bool InpGetArray=true; // get values into an array input uchar InpCount=9; // count -> only if "get values into an array"==true input int InpStartPos=1; // start position -> only if "get values into an array"==true input int ma_period=10; // period of ma input int ma_shift=0; // shift input ENUM_MA_METHOD ma_method=MODE_SMA; // type of smoothing input ENUM_APPLIED_PRICE applied_price=PRICE_CLOSE; // type of price input ENUM_TIMEFRAMES period=PERIOD_CURRENT; // timeframe //--- int handle_iMA; // variable for storing the handle of the iMA indicator //+------------------------------------------------------------------+ //| Expert initialization function | //+------------------------------------------------------------------+ int OnInit() { //--- create handle of the indicator iMA handle_iMA=iMA(Symbol(),period,ma_period,ma_shift,ma_method,applied_price); //--- if the handle is not created if(handle_iMA==INVALID_HANDLE) { //--- tell about the failure and output the error code PrintFormat("Failed to create handle of the iMA indicator for the symbol %s/%s, error code %d", Symbol(), EnumToString(Period()), GetLastError()); //--- the indicator is stopped early return(INIT_FAILED); } //--- return(INIT_SUCCEEDED); } //+------------------------------------------------------------------+ //| Expert deinitialization function | //+------------------------------------------------------------------+ void OnDeinit(const int reason) { //--- } //+------------------------------------------------------------------+ //| Expert tick function | //+------------------------------------------------------------------+ void OnTick() { //--- if(!InpGetArray) { double ma_0=iMAGet(0); double ma_1=iMAGet(1); Comment("mode \"get values into an array\" -> false","\n", "MA #0: ",DoubleToString(ma_0,Digits()+1),"\n", "MA #1: ",DoubleToString(ma_1,Digits()+1)); } else { double arr_ma[]; if(iMAGetArray(InpStartPos,InpCount,arr_ma)) Comment("mode \"get values into an array\" -> true","\n", "start position: ",IntegerToString(InpStartPos),", amount to copy: ",IntegerToString(InpCount),"\n", "arr_ma[0]: ",DoubleToString(arr_ma[0],Digits()+1),"\n", "arr_ma[",IntegerToString(InpCount-1),"]: ",DoubleToString(arr_ma[InpCount-1],Digits()+1)); } } //+------------------------------------------------------------------+ //| Get value of buffers for the iMA | //+------------------------------------------------------------------+ double iMAGet(const int index) { double MA[1]; //--- reset error code ResetLastError(); //--- fill a part of the iMABuffer array with values from the indicator buffer that has 0 index if(CopyBuffer(handle_iMA,0,index,1,MA)<0) { //--- if the copying fails, tell the error code PrintFormat("Failed to copy data from the iMA indicator, error code %d",GetLastError()); //--- quit with zero result - it means that the indicator is considered as not calculated return(0.0); } return(MA[0]); } //+------------------------------------------------------------------+ //| Get value of buffers for the iMA in the array | //+------------------------------------------------------------------+ bool iMAGetArray(const int start_pos,const int count,double &arr_buffer[]) { /* int CopyBuffer( int indicator_handle, // indicator handle int buffer_num, // indicator buffer number int start_pos, // start position int count, // amount to copy double buffer[] // target array to copy ); */ //--- bool result=true; if(!ArrayIsDynamic(arr_buffer)) { Print("This a no dynamic array!"); return(false); } ArrayFree(arr_buffer); //--- reset error code ResetLastError(); //--- fill a part of the iMABuffer array with values from the indicator buffer that has 0 index int copied=CopyBuffer(handle_iMA,0,start_pos,count,arr_buffer); if(copied<0) { //--- if the copying fails, tell the error code PrintFormat("Failed to copy data from the iMA indicator, error code %d",GetLastError()); //--- quit with zero result - it means that the indicator is considered as not calculated return(false); } else if(copied<count) { PrintFormat("%d elements from %d were copied",copied,count); DebugBreak(); return(false); } //--- return(result); } //+------------------------------------------------------------------+
After that, you can already start using the trade class CTrade.
Files:
iMAGet.mq5
12 kb

You are missing trading opportunities:
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
Registration
Log in
You agree to website policy and terms of use
If you do not have an account, please register
Okay so I'm very new to this and have very little understanding of trading and I'm just helping someone else out. I have a very simple task: to open a new sell or buy position if certain conditions were met, but I can't seem to work this out. I've tried googling but nothing came out.
I've tried this
void OnTick()
{
CTrade trade;
MqlTick Tick;
bool buyPositionIsOpen = false;
bool sellPositionIsOpen = false;
int mA = iMA (_Symbol, Ma_timeframe, Ma_period, Ma_shift, Ma_method, AppliedPrice);
if (mA - X <= Tick.ask && sellPositionIsOpen == false)
{
trade.Sell(VOLUME, _Symbol, Tick.ask, SL, TP, "");
sellPositionIsOpen = true;
}
if (mA + X >= Tick.bid && buyPositionIsOpen == false)
{
trade.Buy(VOLUME, _Symbol, Tick.bid, SL, TP, "");
buyPositionIsOpen = true;
}
}
(I've also tried using this:
bool PositionOpen(
const string symbol, // symbol
ENUM_ORDER_TYPE order_type, // order type to open position
double volume, // position volume
double price, // execution price
double sl, // Stop Loss price
double tp, // Take Profit price
const string comment="" // comment
)
but it didn't work)
so how do I open a sell or buy position exactly?