the MA in EA is different from The MA of an indicator
please help me
If you want to use the data of the iMA indicator, create an indicator handle. If you want to use the data of the iMACD indicator, create an indicator handle and use it. If you want to use the XXXX indicator data, create a XXXX indicator handle.
If you want to use the data of the iMA indicator, create an indicator handle. If you want to use the data of the iMACD indicator, create an indicator handle and use it. If you want to use the XXXX indicator data, create a XXXX indicator handle.
Like this one
//--- create handle of the indicatorif(type==Call_iMA)
handle=iMA(name,period,ma_period,ma_shift,ma_method,applied_price);
else
{
//--- fill the structure with parameters of the indicatorMqlParam pars[4];
//--- period
pars[0].type=TYPE_INT;
pars[0].integer_value=ma_period;
//--- shift
pars[1].type=TYPE_INT;
pars[1].integer_value=ma_shift;
//--- type of smoothing
pars[2].type=TYPE_INT;
pars[2].integer_value=ma_method;
//--- type of price
pars[3].type=TYPE_INT;
pars[3].integer_value=applied_price;
handle=IndicatorCreate(name,period,IND_MA,4,pars);
}
//--- if the handle is not createdif(handle==INVALID_HANDLE)
{
//--- tell about the failure and output the error codePrintFormat("Failed to create handle of the iMA indicator for the symbol %s/%s, error code %d",
name,
EnumToString(period),
GetLastError());
//--- the indicator is stopped earlyreturn(INIT_FAILED);
}
//--- show the symbol/timeframe the Moving Average indicator is calculated for
short_name=StringFormat("iMA(%s/%s, %d, %d, %s, %s)",name,EnumToString(period),
ma_period, ma_shift,EnumToString(ma_method),EnumToString(applied_price));
IndicatorSetString(INDICATOR_SHORTNAME,short_name);
//--- normal initialization of the indicatorreturn(INIT_SUCCEEDED);
If you want to use the data of the iMA indicator, create an indicator handle. If you want to use the data of the iMACD indicator, create an indicator handle and use it. If you want to use the XXXX indicator data, create a XXXX indicator handle.
i have tried but it is working check the code below
//+------------------------------------------------------------------+//| mimi.mq5 |//| Copyright 2020, MetaQuotes Software Corp. |//| https://www.mql5.com |//+------------------------------------------------------------------+#property copyright"Copyright 2020, MetaQuotes Software Corp."#property link"https://www.mql5.com"#property version"1.001"//+----------------------------------------------+// Trading algorithms |//+----------------------------------------------+#include <TradeAlgorithms.mqh>
//#include <Trade\PositionInfo.mqh>//#include <Trade\Trade.mqh>//#include <Trade\SymbolInfo.mqh>//#include <Trade\AccountInfo.mqh>//#include <Trade\DealInfo.mqh>//#include <Trade\OrderInfo.mqh>//#include <SmoothAlgorithms.mgh>//+----------------------------------------------+//| Enumeration for lot calculation options |//+----------------------------------------------+/*enum MarginMode - the enumeration is declared in the TradeAlgorithms.mqh file
{
FREEMARGIN=0, //MM from free funds on the account
BALANCE, //MM from the balance of funds on the account
LOSSFREEMARGIN, //MM for losses from free funds on the account
LOSSBALANCE, //MM for losses from the balance of funds on the account
LOT //Lot unchanged
}; *///+----------------------------------------------+//| Enumeration for defining a trend |//+----------------------------------------------+enum TrendMode
{
DIRECT=0, //by signals
AGAINST //against signals
};
enum Creation
{
Call_iMA, // use iMA
Call_IndicatorCreate // use IndicatorCreate
};
//+----------------------------------------------+//| Expert indicator input parameters |//+----------------------------------------------+inputdouble MM=0.1; //Money Managemntinput MarginMode MMMode=LOT; //Money Managent Modeinputint StopLoss_=1000; //stop loss in pointsinputint TakeProfit_=2000; //take profit in pointsinputint Deviation_=10; //Max. price deviation in pointsinputbool BuyPosOpen=true; //Permission to enter longinputbool SellPosOpen=true; //Permission to enter shortinputbool BuyPosClose=true; //Permission to exit longsinputbool SellPosClose=true; //Permission to get out of shorts//+----------------------------------------------+//| Input parameters of the RSI indicator |//+----------------------------------------------+inputENUM_TIMEFRAMES InpInd_Timeframe=PERIOD_M1; //indicator timeframeinput TrendMode Trend=DIRECT; //trade//----inputuint RSIPeriod=10; // RSI periodinputENUM_APPLIED_PRICE RSIPrice=PRICE_CLOSE; // priceinputuint HighLevel=88; // overbought levelinputuint LowLevel=12; // oversold level//----inputuint SignalBar=1; // bar number to receive the entry signal//+----------------------------------------------+//| Input parameters of the MA indicator |//+----------------------------------------------+inputint Inp_MA_ma_period = 5; // MA: averaging periodinputint Inp_MA_ma_shift = 0; // MA: horizontal shiftinputENUM_MA_METHOD Inp_MA_ma_method = MODE_SMA; // MA: smoothing typeinputENUM_APPLIED_PRICE Inp_MA_applied_price = PRICE_CLOSE; // MA: type of price//+----------------------------------------------+//---- Declaring integer variables to store the chart period in secondsint TimeShiftSec;
//---- Declaring integer variables for indicator handles//int InpInd_Handle;int handle_iRSI; // variable for storing the handle of the iRVI indicatorint handle_iMA; // variable for storing the handle of the iMA indicator//---- declaring integer data origin variablesint min_rates_total;
CIsNewBar NB;
input Creation type=Call_iMA; // type of the function //--- name of the indicator on a chartstring short_name;
//+------------------------------------------------------------------+//| Expert initialization function |//+------------------------------------------------------------------+intOnInit()
{
//---- create handle of the indicator RSI
handle_iRSI=iRSI(Symbol(),InpInd_Timeframe,RSIPeriod,RSIPrice);
if(handle_iRSI==INVALID_HANDLE)
{
Print(" Failed to get the indicator handle RSI");
return(INIT_FAILED);
}
//--- create handle of the indicator iMA
handle_iMA=iMA(Symbol(),InpInd_Timeframe,Inp_MA_ma_period,Inp_MA_ma_shift,Inp_MA_ma_method,handle_iRSI);
if(handle_iMA==INVALID_HANDLE)
{
Print(" Failed to get the indicator handle RSI");
return(INIT_FAILED);
}
//---- initialization of a variable to store the chart period in seconds
TimeShiftSec=PeriodSeconds(InpInd_Timeframe);
//---- Initializing data origin variables
min_rates_total=int(Inp_MA_ma_period);
min_rates_total+=int(3+SignalBar);
//--- completion of initializationreturn(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+//| Expert deinitialization function |//+------------------------------------------------------------------+voidOnDeinit(constint reason)
{
//----
GlobalVariableDel_(Symbol());
//----
}
//+------------------------------------------------------------------+//| Expert tick function |//+------------------------------------------------------------------+voidOnTick()
{
if(type==Call_iMA)
handle_iMA=iMA(Symbol(),InpInd_Timeframe,Inp_MA_ma_period,Inp_MA_ma_shift,Inp_MA_ma_method,Inp_MA_applied_price);
else
{
//--- fill the structure with parameters of the indicatorMqlParam pars[4];
//--- period
pars[0].type=TYPE_INT;
pars[0].integer_value=Inp_MA_ma_period;
//--- shift
pars[1].type=TYPE_INT;
pars[1].integer_value=Inp_MA_ma_shift;
//--- type of smoothing
pars[2].type=TYPE_INT;
pars[2].integer_value=Inp_MA_ma_method;
//--- type of price
pars[3].type=TYPE_INT;
pars[3].integer_value=Inp_MA_applied_price;
handle_iMA=IndicatorCreate(Symbol(),InpInd_Timeframe,IND_MA,4,pars);
}
//--- if the handle is not createdif(handle_iMA==INVALID_HANDLE)
{
//--- tell about the failure and output the error codePrintFormat("Failed to create handle of the iMA indicator for the symbol %s/%s, error code %d",
Symbol(),
EnumToString(InpInd_Timeframe),
GetLastError());
//--- the indicator is stopped early//return(INIT_FAILED);
}
//--- show the symbol/timeframe the Moving Average indicator is calculated for
short_name=StringFormat("iMA(%s/%s, %d, %d, %s, %s)",Symbol(),EnumToString(InpInd_Timeframe),
Inp_MA_ma_period, Inp_MA_ma_shift,EnumToString(Inp_MA_ma_method),EnumToString(Inp_MA_applied_price));
IndicatorSetString(INDICATOR_SHORTNAME,short_name);
//--- normal initialization of the indicator//return(INIT_SUCCEEDED);//---- checking the number of bars for sufficiency for calculationif(BarsCalculated(handle_iMA)<min_rates_total)
return;
//---- loading history for normal operation of functions IsNewBar() и SeriesInfoInteger()//LoadHistory(TimeCurrent()-PeriodSeconds(InpInd_Timeframe)-1,Symbol(),InpInd_Timeframe);//---- Declaring static variablesbool Recount=true;
bool BUY_Open=false,BUY_Close=false;
bool SELL_Open=false,SELL_Close=false;
datetime UpSignalTime=0,DnSignalTime=0;
//+----------------------------------------------+//| Definition of signals for deals |//+----------------------------------------------+if(!SignalBar || NB.IsNewBar(Symbol(),InpInd_Timeframe) || Recount) // checking for a new bar
{
//---- reset trading signals
BUY_Open=false;
SELL_Open=false;
BUY_Close=false;
SELL_Close=false;
Recount=false;
//---- Declaring local variablesdouble RSI[];
ArraySetAsSeries(RSI,true);
double MA[];
ArraySetAsSeries(MA,true);
//---- copying the newly appeared data into arraysif(CopyBuffer(handle_iRSI,0,SignalBar,2,RSI)!=2)
{
Recount=true;
return;
}
if(CopyBuffer(handle_iMA,0,SignalBar,2,MA)!=2)
{
Recount=true;
return;
}
if(Trend==DIRECT)
{
//---- Get signals to buyif(MA[1]>LowLevel)
{
if(MA[0]<=LowLevel)
{
if(BuyPosOpen)
BUY_Open=true;
if(SellPosClose)
SELL_Close=true;
}
UpSignalTime=datetime(SeriesInfoInteger(Symbol(),InpInd_Timeframe,SERIES_LASTBAR_DATE))+TimeShiftSec;
}
//---- Get signals for sellingif(MA[1]<HighLevel)
{
if(MA[0]>=HighLevel)
{
if(SellPosOpen)
SELL_Open=true;
if(BuyPosClose)
BUY_Close=true;
}
DnSignalTime=datetime(SeriesInfoInteger(Symbol(),InpInd_Timeframe,SERIES_LASTBAR_DATE))+TimeShiftSec;
}
}
if(Trend==AGAINST)
{
//---- Get signals to buyif(MA[1]>LowLevel)
{
if(MA[0]<=LowLevel)
{
if(SellPosOpen)
SELL_Open=true;
if(BuyPosClose)
BUY_Close=true;
}
DnSignalTime=datetime(SeriesInfoInteger(Symbol(),InpInd_Timeframe,SERIES_LASTBAR_DATE))+TimeShiftSec;
}
//---- Get signals for sellingif(MA[1]<HighLevel)
{
if(MA[0]>=HighLevel)
{
if(BuyPosOpen)
BUY_Open=true;
if(SellPosClose)
SELL_Close=true;
UpSignalTime=datetime(SeriesInfoInteger(Symbol(),InpInd_Timeframe,SERIES_LASTBAR_DATE))+TimeShiftSec;
}
}
}
//Print(BUY_Open,BUY_Close);
}
//+----------------------------------------------+//| Making deals |//+----------------------------------------------+//---- Close the long
BuyPositionClose(BUY_Close,Symbol(),Deviation_);
//---- Close the short
SellPositionClose(SELL_Close,Symbol(),Deviation_);
//---- Opening long
BuyPositionOpen(BUY_Open,Symbol(),UpSignalTime,MM,MMMode,Deviation_,StopLoss_,TakeProfit_);
//---- Opening the short
SellPositionOpen(SELL_Open,Symbol(),DnSignalTime,MM,MMMode,Deviation_,StopLoss_,TakeProfit_);
//----
}
//+------------------------------------------------------------------+
MQL5: language of trade strategies built-in the MetaTrader 5 Trading Platform, allows writing your own trading robots, technical indicators, scripts and libraries of functions
Did you finally get it?, because am working on a similar project though its still kinda hard for me to have my EA execute trades based on the MA as the RSI line
Did you finally get it?, because am working on a similar project though its still kinda hard for me to have my EA execute trades based on the MA as the RSI line
i have tried but the ea wont take any trade i want to use 5 sma line instead of rsi line
In this case, create an iMA indicator and get values from it.
In this case, create an iMA indicator and get values from it.
How can i do that
the MA in EA is different from The MA of an indicator
How can i do that
the MA in EA is different from The MA of an indicator
If you want to use the data of the iMA indicator, create an indicator handle. If you want to use the data of the iMACD indicator, create an indicator handle and use it. If you want to use the XXXX indicator data, create a XXXX indicator handle.
If you want to use the data of the iMA indicator, create an indicator handle. If you want to use the data of the iMACD indicator, create an indicator handle and use it. If you want to use the XXXX indicator data, create a XXXX indicator handle.
Like this one
If you want to use the data of the iMA indicator, create an indicator handle. If you want to use the data of the iMACD indicator, create an indicator handle and use it. If you want to use the XXXX indicator data, create a XXXX indicator handle.
i have tried but it is working check the code below
i have tried but it is working check the code below
I won't even look - I repeated a hundred times - THE INDICATOR HANDLE SHOULD BE CREATED ONCE! IT IS NECESSARY TO DO IT IN OnInit () !!!
Remember this! Get a forehead tattoo! Write in your journal.
Did you finally get it?, because am working on a similar project though its still kinda hard for me to have my EA execute trades based on the MA as the RSI line
Did you finally get it?, because am working on a similar project though its still kinda hard for me to have my EA execute trades based on the MA as the RSI line
No my friend I didnt get it
I'm in the same situation, you managed to fix it