EA creates multiple indicator instances using iCustom - page 2 12 New comment Vladimir Karputov 2020.09.06 12:12 #11 Minimum code: //+------------------------------------------------------------------+ //| Coro1.mq5 | //| Copyright 2020, MetaQuotes Software Corp. | //| http://www.mql5.com | //+------------------------------------------------------------------+ #property copyright "2020, MetaQuotes Software Corp." #property link "http://www.mql5.com" //--- int handle_iCustom; // variable for storing the handle of the iCustom indicator //+------------------------------------------------------------------+ //| Expert initialization function | //+------------------------------------------------------------------+ int OnInit() { //--- create handle of the indicator iCustom handle_iCustom=iCustom(Symbol(),Period(),"Corola",20,40,PRICE_CLOSE); //--- if the handle is not created if(handle_iCustom==INVALID_HANDLE) { //--- tell about the failure and output the error code PrintFormat("Failed to create handle of the iCustom 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) { //--- if(handle_iCustom!=INVALID_HANDLE) IndicatorRelease(handle_iCustom); } //+------------------------------------------------------------------+ //| Expert tick function | //+------------------------------------------------------------------+ void OnTick() { string signal=""; double short_period[]; double long_period[]; ArraySetAsSeries(long_period,true); ArraySetAsSeries(short_period,true); CopyBuffer(handle_iCustom,0,0,3,short_period); CopyBuffer(handle_iCustom,1,0,3,long_period); double shortValue0=short_period[0]; double longValue0=long_period[0]; double shortValue1=short_period[1]; double longValue1=long_period[1]; if(longValue0<-0.8&&shortValue0<-0.8) if((shortValue0<longValue0) && (shortValue1>longValue1)) { signal="Buy"; } if(longValue0>0.8&&shortValue0>0.8) if((shortValue0>longValue0) && (shortValue1<longValue1)) { signal="Sell"; } Comment("Signal: ",signal); } //+------------------------------------------------------------------+ Files: Coro1.mq5 3 kb Leandro Tadeu Silvestrini Junior 2020.09.06 20:25 #12 Vladimir Karputov: Minimum code: Thank you, so much. Solved the problem. But my entry and exit not work, Vladimir Karputov 2020.09.06 20:45 #13 Silverveins: Thank you, so much. Solved the problem. But my entry and exit not work, Please attach ( ) two MQ5 (ONLY MQ5) files: the indicator file and the FIXED advisor file. Leandro Tadeu Silvestrini Junior 2020.09.06 21:24 #14 Vladimir Karputov: Anexe ( ) dois arquivos MQ5 (SOMENTE MQ5): o arquivo indicador e o arquivo FIXED advisor. I Can´t find Corola.mq5 Files: Coro.mq5 5 kb Vladimir Karputov 2020.09.07 07:08 #15 Silverveins : I Can´t find Corola.mq5 If you want help - Upload MQ5 files - I'm not going to download and run the CLOSED file. Leandro Tadeu Silvestrini Junior 2020.09.07 07:13 #16 Vladimir Karputov: Se você quiser ajuda - Carregar arquivos MQ5 - não vou baixar e executar o arquivo CLOSED . Sorry, I found. Files: Corola.mq5 13 kb Coro.mq5 5 kb Vladimir Karputov 2020.09.07 08:03 #17 Silverveins: Sorry, I found. Indicator 'Corola.mq5' //+------------------------------------------------------------------+ //| Corola.mq5 | //| Copyright 2020, MetaQuotes Software Corp. | //| http://www.mql5.com | //+------------------------------------------------------------------+ #property copyright "2020, MetaQuotes Software Corp." #property link "http://www.mql5.com" #property version "1.003" #property indicator_separate_window #property indicator_buffers 2 #property indicator_plots 2 #property indicator_label1 "Short" #property indicator_type1 DRAW_LINE #property indicator_color1 clrSilver #property indicator_width1 2 #property indicator_label2 "Long" #property indicator_type2 DRAW_LINE #property indicator_color2 clrRed #property indicator_width2 2 //--- input parameters input int InpPeriodShort=20; // Short period input int InpPeriodLong =40; // Long period input ENUM_APPLIED_PRICE InpPrice =PRICE_CLOSE; // Price //--- double vals[],vall[]; int _longPeriod, _shortPeriod; double SSyy,SSy,LSyy,LSy,_ts2,_tl2; //+------------------------------------------------------------------+ //| Custom indicator initialization function | //+------------------------------------------------------------------+ int OnInit() { SetIndexBuffer(0,vals,INDICATOR_DATA); SetIndexBuffer(1,vall,INDICATOR_DATA); //--- _longPeriod =MathMax(InpPeriodShort,InpPeriodLong); _shortPeriod=MathMin(InpPeriodShort,InpPeriodLong); for(int k=0,y=0; k<_longPeriod; k++,y--) { if(k<_shortPeriod) { SSy += y; SSyy += y*y; } LSy += y; LSyy += y*y; } _ts2=_shortPeriod*SSyy-SSy*SSy; _tl2=_longPeriod *LSyy-LSy*LSy; //--- IndicatorSetString(INDICATOR_SHORTNAME,StringFormat("Correlation trend (%i,%i)",_shortPeriod,_longPeriod)); return(INIT_SUCCEEDED); } //+------------------------------------------------------------------+ //| Indicator deinitialization function | //+------------------------------------------------------------------+ void OnDeinit(const int reason) { //--- } //+------------------------------------------------------------------+ //| Custom indicator iteration function | //+------------------------------------------------------------------+ int OnCalculate(const int rates_total, const int prev_calculated, const datetime &time[], const double &open[], const double &high[], const double &low[], const double &close[], const long &tick_volume[], const long &volume[], const int &spread[]) { int limit=prev_calculated-1; if(prev_calculated==0) limit=0; //--- struct sWorkStruc { double price; struct sCalcStruct { double Sx; double Sxx; double Sxy; }; sCalcStruct sums[2]; }; static sWorkStruc m_work[]; static int m_workSize=-1; if(m_workSize<rates_total) m_workSize=ArrayResize(m_work,rates_total+500,2000); //--- for(int i=limit; i<rates_total; i++) { m_work[i].price=getPrice(InpPrice,open,high,low,close,i); if(i>_shortPeriod) { m_work[i].sums[0].Sx =m_work[i-1].sums[0].Sx + m_work[i].price - m_work[i-_shortPeriod].price; m_work[i].sums[0].Sxx=m_work[i-1].sums[0].Sxx + m_work[i].price*m_work[i].price - m_work[i-_shortPeriod].price*m_work[i-_shortPeriod].price; m_work[i].sums[0].Sxy=m_work[i-1].sums[0].Sxy - m_work[i].sums[0].Sx + m_work[i].price + (_shortPeriod-1)*m_work[i-_shortPeriod].price; } else { m_work[i].sums[0].Sx =m_work[i].price; m_work[i].sums[0].Sxx=m_work[i].price*m_work[i].price; m_work[i].sums[0].Sxy=0; for(int k=1,y=-1; k<_shortPeriod && i>=k; k++,y--) { m_work[i].sums[0].Sx += m_work[i-k].price; m_work[i].sums[0].Sxx += m_work[i-k].price*m_work[i-k].price; m_work[i].sums[0].Sxy += m_work[i-k].price*y; } } //--- if(i>_longPeriod) { m_work[i].sums[1].Sx =m_work[i-1].sums[1].Sx + m_work[i].price - m_work[i-_longPeriod].price; m_work[i].sums[1].Sxx=m_work[i-1].sums[1].Sxx + m_work[i].price*m_work[i].price - m_work[i-_longPeriod].price*m_work[i-_longPeriod].price; m_work[i].sums[1].Sxy=m_work[i-1].sums[1].Sxy - m_work[i].sums[1].Sx + m_work[i].price + (_longPeriod-1)*m_work[i-_longPeriod].price; } else { m_work[i].sums[1].Sx =m_work[i].price; m_work[i].sums[1].Sxx=m_work[i].price*m_work[i].price; m_work[i].sums[1].Sxy=0; for(int k=1,y=-1; k<_longPeriod && i>=k; k++,y--) { m_work[i].sums[1].Sx += m_work[i-k].price; m_work[i].sums[1].Sxx += m_work[i-k].price*m_work[i-k].price; m_work[i].sums[1].Sxy += m_work[i-k].price*y; } } //--- double _ts1=_shortPeriod*m_work[i].sums[0].Sxx-m_work[i].sums[0].Sx*m_work[i].sums[0].Sx; double _tl1=_longPeriod *m_work[i].sums[1].Sxx-m_work[i].sums[1].Sx*m_work[i].sums[1].Sx; vals[i]=(_ts1>0 && _ts2>0) ? (_shortPeriod*m_work[i].sums[0].Sxy-m_work[i].sums[0].Sx*SSy)/MathSqrt(_ts1*_ts2) : 0; vall[i]=(_tl1>0 && _tl2>0) ? (_longPeriod *m_work[i].sums[1].Sxy-m_work[i].sums[1].Sx*LSy)/MathSqrt(_tl1*_tl2) : 0; } //--- return the prev_calculated value for the next call return(rates_total); } template <typename T> double getPrice(ENUM_APPLIED_PRICE price, T& open[], T& high[], T& low[], T& close[], int i) { switch(price) { case PRICE_CLOSE: return(close[i]); case PRICE_HIGH: return(high[i]); case PRICE_LOW: return(low[i]); case PRICE_OPEN: return(open[i]); case PRICE_MEDIAN: return((high[i]+low[i])/2.0); case PRICE_TYPICAL: return((high[i]+low[i]+close[i])/3.0); case PRICE_WEIGHTED: return((high[i]+low[i]+close[i]+close[i])/4.0); } return(0); } //+------------------------------------------------------------------+ Expert advisor 'Coro.mq5' //+------------------------------------------------------------------+ //| Coro.mq5 | //| Copyright 2020, MetaQuotes Software Corp. | //| http://www.mql5.com | //+------------------------------------------------------------------+ #property copyright "2020, MetaQuotes Software Corp." #property link "http://www.mql5.com" #property tester_indicator "Corola" //--- int handle_iCustom; // variable for storing the handle of the iCustom indicator //+------------------------------------------------------------------+ //| Expert initialization function | //+------------------------------------------------------------------+ int OnInit() { //--- create handle of the indicator iCustom handle_iCustom=iCustom(Symbol(),Period(),"Corola",20,40,PRICE_CLOSE); //--- if the handle is not created if(handle_iCustom==INVALID_HANDLE) { //--- tell about the failure and output the error code PrintFormat("Failed to create handle of the iCustom 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) { //--- if(handle_iCustom!=INVALID_HANDLE) IndicatorRelease(handle_iCustom); } //+------------------------------------------------------------------+ //| Expert tick function | //+------------------------------------------------------------------+ void OnTick() { string signal=""; double short_period[]; double long_period[]; ArraySetAsSeries(long_period,true); ArraySetAsSeries(short_period,true); CopyBuffer(handle_iCustom,0,0,3,short_period); CopyBuffer(handle_iCustom,1,0,3,long_period); //--- if(long_period[0]<-0.8 && short_period[0]<-0.8) if((short_period[1]<long_period[1]) && (short_period[0]>long_period[1])) { signal="Buy"; } if(long_period[0]>0.8 && short_period[0]>0.8) if((short_period[1]>long_period[1]) && (short_period[0]<long_period[0])) { signal="Sell"; } if(signal=="Buy" || signal=="Sell") Comment("Signal: ",signal); } //+------------------------------------------------------------------+ Files: Corola.mq5 13 kb Coro.mq5 6 kb 12 New comment
Minimum code:
Minimum code:
Thank you, so much. Solved the problem.
But my entry and exit not work,
Thank you, so much. Solved the problem.
But my entry and exit not work,
Anexe ( ) dois arquivos MQ5 (SOMENTE MQ5): o arquivo indicador e o arquivo FIXED advisor.
I Can´t find Corola.mq5
I Can´t find Corola.mq5
If you want help - Upload MQ5 files - I'm not going to download and run the CLOSED file.
Se você quiser ajuda - Carregar arquivos MQ5 - não vou baixar e executar o arquivo CLOSED .
Sorry, I found.
Sorry, I found.
Indicator 'Corola.mq5'
Expert advisor 'Coro.mq5'