Pinescript to MQL - Simple Signal Indicator

 

Hi coders, I'm trying to convert a Pinescript indicator to MQL4/5, but I've been having troubles, I know there's no such thing as the "nz()" function in MQL, but I also know there are ways to replace it:

(https://www.mql5.com/en/forum/439178)

but I'm a bit stuck because I don't have enough knowledge yet, could you help me? it's a relatively simple code.


//============ signal Generator ===============//
Piriod=input('15')

ch1 = security(tickerid, Piriod, open)
ch2 = security(tickerid, Piriod, close)

// Signals//
long = crossover(ch2,ch1)

short = crossunder(ch2,ch1)

last_long = long ? time : nz(last_long[1])
last_short = short ? time : nz(last_short[1])

long_signal = crossover(last_long, last_short) ? 1 : -1
short_signal = crossover(last_short, last_long) ? -1 : 1

plot(long_signal, title="Long", color=green)
plot(short_signal, title="Short", color=red)

plot(0, title="Trigger", color=white)


This indicator should be displayed in a separate window.

Thanks in advance :)




Could you please help me convert this pine script to mql4? Some of this like NZ function is very complex to me. - How to calculate the value of a stock market in one step?
Could you please help me convert this pine script to mql4? Some of this like NZ function is very complex to me. - How to calculate the value of a stock market in one step?
  • 2023.01.04
  • Runny Picasso
  • www.mql5.com
Double atr_ma = ima(null, 0, 20, 0, mode_ema, price_close, 1). This is the whole calculations: (for your question that what i want to do with this) // calculations. Edit : i assume it can only be price so here goes : we can make it a little more difficult😄
Files:
 
borgman bermúdez:

but I'm a bit stuck because I don't have enough knowledge yet, could you help me? it's a relatively simple code.

Multi-timeframe indicators are not where to start getting acquainted with MQL. The implementation of a multi-timeframe indicator cannot be simple in MQL.

But if you want to try bench pressing 150 kilograms during your first visit to the gym, you can search for information about multitimeframe on this site (articles, book).

 

If there is no description provided for what the indicator is supposed to do or how it's supposed to work, we can only guess by looking at the given code. That nz() function in pine is likely to be the least of your worries. It's just an error handling function if I'm not mistaken. It appears like some bull/bear power indicator.


not that simple, Pine makes it look simple

#property copyright "Copyright 2024, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property indicator_separate_window
#property indicator_buffers 3
#property indicator_plots 3
#property indicator_color1  clrGreen
#property indicator_type1  DRAW_LINE
#property indicator_color2  clrRed
#property indicator_type2  DRAW_LINE
#property indicator_color3  clrGray
#property indicator_type3  DRAW_LINE
#property indicator_minimum -1.08
#property indicator_maximum 1.08


input ENUM_TIMEFRAMES per = PERIOD_CURRENT; // Period

//--- indicator buffers
double LongBuffer[];
double ShortBuffer[];
double Zeromark[];

//--- variables for calculations
datetime lastLongTime = 0;
datetime lastShortTime = 0;
MqlRates ch1[], ch2[];

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
   SetIndexBuffer(0, LongBuffer);
   SetIndexBuffer(1, ShortBuffer);
   SetIndexBuffer(2, Zeromark);
   
   ArrayInitialize(LongBuffer, -1.0);
   ArrayInitialize(ShortBuffer, 1.0);
   ArrayInitialize(Zeromark, 0.0);
   
   return(INIT_SUCCEEDED);
  }


double crossover(MqlRates &_ch1[], MqlRates &_ch2[], int i){

   return _ch2[i].close > _ch1[i].open && _ch2[i-1].close < _ch1[i-1].open;
}

double crossunder(MqlRates &_ch1[], MqlRates &_ch2[], int i){

   return _ch2[i].close < _ch1[i].open && _ch2[i-1].close > _ch1[i-1].open;
}

//+------------------------------------------------------------------+
//| 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 prev = prev_calculated;
   
   int copied = CopyRates(_Symbol, per, 0, rates_total, ch1);
   if (copied < rates_total)
     {
      Print("Error copying open prices: ", GetLastError()); return prev;
     }

   copied = CopyRates(_Symbol, per, 0, rates_total, ch2);
   if (copied < rates_total)
     {
      Print("Error copying close prices: ", GetLastError()); return prev;
     }
     
   int start = rates_total-prev_calculated + (rates_total == prev_calculated);

   for (int i = MathMax(2, start); i < rates_total; i++){
 
      Zeromark[i] = 0.0; 
      LongBuffer[i] = -1;
      ShortBuffer[i] = 1;    
      
      bool longSignal = crossover(ch1, ch2, i);
      bool shortSignal = crossunder(ch1, ch2, i);

      if (longSignal){
         lastLongTime = time[i];
        }
      if (shortSignal){
         lastShortTime = time[i];
        }

      bool crossoverLong = (lastLongTime > lastShortTime);
      bool crossoverShort = (lastShortTime > lastLongTime);

      crossoverLong ? LongBuffer[i-1] = 1.0 : LongBuffer[i-2] = -1.0;
      crossoverShort ? ShortBuffer[i-1] = -1.0 : ShortBuffer[i-2] = 1.0;   
   }


   return (rates_total);
  }
//+------------------------------------------------------------------+