icustom function in expert 0.0 problem

 

hi. i write a custom indicator . it currectly work when used. but when use in expert and use icustom function, it return 0.0. what is problem?

my indicator code:

//+------------------------------------------------------------------+
//|                                                 Psar2T-Rsi2T.mq5 |
//|                                  Copyright 2022, MetaQuotes Ltd. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2022, MetaQuotes Ltd."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property indicator_chart_window
#property indicator_buffers 2
#property indicator_plots   2
//--- plot Psar2TRsi2TBuyPos
#property indicator_label1  "Psar2TRsi2TBuyPos"
#property indicator_type1   DRAW_ARROW
#property indicator_color1  clrGreen
#property indicator_style1  STYLE_SOLID
#property indicator_width1  1
//--- plot Psae2TRsi2TimeSelPos
#property indicator_label2  "Psar2TRsi2TimeSellPos"
#property indicator_type2   DRAW_ARROW
#property indicator_color2  clrRed
#property indicator_style2  STYLE_SOLID
#property indicator_width2  1
//--- input parameters
input int      Period;
input int      RsiBuyLevel;
input int      RsiSellLevel;
//--- indicator buffers
double Psar2TRsi2TBuyPos[];
double Psar2TRsi2TSellPos[];
int UpTFRsiHandle;
int PTFRsiHandle;
int UpTFPsarHandle;
int PTFPsarHandle;
double UpTFRsi[];
double PTFRsi[];
double UpTFPsar[];
double PTFPsar[];
int j;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping
   SetIndexBuffer(0,Psar2TRsi2TBuyPos,INDICATOR_DATA);
   SetIndexBuffer(1,Psar2TRsi2TSellPos,INDICATOR_DATA);
   PlotIndexSetInteger(0,PLOT_ARROW,119);
   PlotIndexSetInteger(1,PLOT_ARROW,119);

   
   ENUM_TIMEFRAMES UpTF;
   ENUM_TIMEFRAMES PTF;
   PTF=_Period;
   switch(PTF){
      case PERIOD_M30:PTF=PERIOD_H2;j=4;break;case PERIOD_H1:UpTF=PERIOD_H4;j=4;break;
      case PERIOD_H4:UpTF=PERIOD_D1;j=6;break;case PERIOD_D1:UpTF=PERIOD_W1;j=7;break;
      };
      
      
   PTFRsiHandle=iRSI(_Symbol,PTF,14,PRICE_CLOSE);
   UpTFRsiHandle=iRSI(_Symbol,UpTF,14,PRICE_CLOSE);
   PTFPsarHandle=iSAR(_Symbol,PTF,0.02,0.2);
   UpTFPsarHandle=iSAR(_Symbol,UpTF,0.02,0.2);
 
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| 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[])
  {
//---
   CopyBuffer(UpTFRsiHandle,0,0,rates_total,UpTFRsi);
   CopyBuffer(PTFRsiHandle,0,0,rates_total,PTFRsi);
   CopyBuffer(PTFPsarHandle,0,0,rates_total,PTFPsar);
   CopyBuffer(UpTFPsarHandle,0,0,rates_total,UpTFPsar);
   
   ENUM_TIMEFRAMES PTF2;
   ENUM_TIMEFRAMES UpTF2;
   PTF2=_Period;
   switch(PTF2){
      case PERIOD_M30:PTF2=PERIOD_H2;j=4;break;case PERIOD_H1:UpTF2=PERIOD_H4;j=4;break;
      case PERIOD_H4:UpTF2=PERIOD_D1;j=6;break;case PERIOD_D1:UpTF2=PERIOD_W1;j=7;break;
      };
         
             
      for(int i=prev_calculated;i<rates_total;i++)
         {
         int k=iBarShift(_Symbol,PTF2,time[i],true);
        // int l=iBarShift(_Symbol,PERIOD_H4,time[i],true);
         //Print("prev=",time[i],"k=",k,"    l=",l,"rsi",PTFRsi[i]);
         
         
         bool Buyif=PTFRsi[i]<40 &&
                     PTFPsar[i]<low[i];
                     UpTFPsar[k]<low[k] &&
                     UpTFRsi[k]<40;
                     
   
         bool Sellif=PTFRsi[i]>60 &&
                     PTFPsar[i]>high[i];
                     UpTFPsar[k]>high[k] &&
                     UpTFRsi[k]>60;
   
                     

         if(Buyif)
            {Print("bypos");
            Psar2TRsi2TBuyPos[i]=0.995*low[i];
            Psar2TRsi2TSellPos[i]=EMPTY_VALUE;
            //Buyif=false;
            }
         else if(Sellif)
            {Print("sellpos");
            Psar2TRsi2TBuyPos[i]=EMPTY_VALUE;
            Psar2TRsi2TSellPos[i]=1.005*high[i];
            //Sellif=false;
            }
         else 
            {Psar2TRsi2TBuyPos[i]=EMPTY_VALUE;
            Psar2TRsi2TBuyPos[i]=EMPTY_VALUE;
            }
         }
      //}
//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+

my expert code:

//+------------------------------------------------------------------+
//|                                                 RsiPsar2Time.mq5 |
//|                                  Copyright 2022, MetaQuotes Ltd. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2022, MetaQuotes Ltd."
#property link      "https://www.mql5.com"
#property version   "1.00"
#include <Trade\Trade.mqh>
//--- object of class CTrade
CTrade ctrad;



int RP2THandle;
int PsarHandle;
int i;

double RP2T[];
double Psar[];


//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---

   RP2THandle=iCustom(_Symbol,_Period,"2");
   PsarHandle=iSAR(_Symbol,_Period,0.02,0.2);
   ArraySetAsSeries(RP2T,true);
   ArraySetAsSeries(Psar,true);

//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
   
   
   
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---

   int PosTicket;
   string PosSym;
   long PosType;
   
   CopyBuffer(RP2THandle,0,0,5,RP2T);
   CopyBuffer(PsarHandle,0,0,5,Psar);

   
   MqlTick Tick;
   SymbolInfoTick(_Symbol,Tick);
   
   MqlRates Rates[];
   CopyRates(_Symbol,_Period,0,5,Rates);
   ArraySetAsSeries(Rates,true);
   
   MqlTradeRequest Trade={};
   MqlTradeResult TradeResult={};
   
   int PTN=PositionsTotal();
   bool IsSell=false;
   bool IsBuy=false;


   
   for(i=0;i<=PTN;i++)
      {
      PosTicket=PositionGetTicket(i);
      PosSym=PositionGetSymbol(i);
      PosType=PositionGetInteger(POSITION_TYPE);
      if(PosType==POSITION_TYPE_BUY){IsBuy=true;}
      if(PosType==POSITION_TYPE_SELL){IsSell=true;}
      if(PosType==POSITION_TYPE_BUY && Psar[1]>Rates[1].high){ctrad.PositionClose(PosTicket,10);}
      if(PosType==POSITION_TYPE_SELL && Psar[1]<Rates[1].low){ctrad.PositionClose(PosTicket,10);}
      }
      

   Print(RP2T[1]);
   
   
   if(RP2T[1]>Rates[1].high && !IsSell)
      {Trade.action=TRADE_ACTION_DEAL;
      Print("RP2T[1]=",RP2T[1],"Rates[1].low",Rates[1].low,"PTN=",PTN,",IsSell=",IsSell,",IsBuy=",IsBuy,",Tick.bid=",Tick.bid,",Tick.ask=",Tick.ask,"PosTicket",PosTicket,"PosType",PosType,"PosSym",PosSym);
      Trade.deviation=5;
      Trade.magic=16515;
      Trade.price=Tick.bid;
      Trade.symbol=_Symbol;
      Trade.type=ORDER_TYPE_SELL;
      Trade.type_filling=ORDER_FILLING_IOC;
      Trade.volume=0.01;
      OrderSend(Trade,TradeResult);
      }
   if(RP2T[1]<Rates[1].low && !IsBuy)
      {Trade.action=TRADE_ACTION_DEAL;
      Print("RP2T[1]=",RP2T[1],"Rates[1].low",Rates[1].low,"PTN=",PTN,",IsSell=",IsSell,",IsBuy=",IsBuy,",Tick.bid=",Tick.bid,",Tick.ask=",Tick.ask,"PosTicket",PosTicket,"PosType",PosType,"PosSym",PosSym);
      Trade.deviation=5;
      Trade.magic=16515;
      Trade.price=Tick.ask;
      Trade.symbol=_Symbol;
      Trade.type=ORDER_TYPE_BUY;
      Trade.type_filling=ORDER_FILLING_IOC;
      Trade.volume=0.01;
      OrderSend(Trade,TradeResult);
      }

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

    
    
    
    
Step on New Rails: Custom Indicators in MQL5
Step on New Rails: Custom Indicators in MQL5
  • www.mql5.com
I will not list all of the new possibilities and features of the new terminal and language. They are numerous, and some novelties are worth the discussion in a separate article. Also there is no code here, written with object-oriented programming, it is a too serous topic to be simply mentioned in a context as additional advantages for developers. In this article we will consider the indicators, their structure, drawing, types and their programming details, as compared to MQL4. I hope that this article will be useful both for beginners and experienced developers, maybe some of them will find something new.
 

Please edit your post (don't create a new post) and replace your code properly (with "</>" or Alt-S), or attach the original file directly with the "+ Attach file" button below the text box.

NB! Very important! DO NOT create a new post. EDIT your original post. An also remove all unnecessary white-space from your code.

 

Forum on trading, automated trading systems and testing trading strategies

help

Alain Verleyen, 2023.02.13 19:47

Please change your topic subject to something meaningful.

I will not even read a topic just asking for "Help", and I am certainly not the only one.


Reason: