ta.rising and ta.falling in MQL5: strict test over length bars

Short answer

Neither has an MQL5 counterpart: write a loop of strict comparisons, src[i] > src[i-k] for every k from 1 to length for rising and the mirror for falling, returning false on the first failure or missing value.

ta.rising(source, length) returns true when the current value of source is strictly greater than each of the previous length values; ta.falling(source, length) returns true when it is strictly less than each of them. Neither has an MQL5 counterpart; the equivalent is a short loop of strict comparisons against the length earlier cells of an array, returning false as soon as one fails or one value is missing.

What ta.rising and ta.falling compute

The rule compares the current bar against each earlier bar in the window, not consecutive bars against each other. ta.rising(x, 3) is x > x[1] and x > x[2] and x > x[3]. With length = 1 it collapses to x > x[1], a one-bar up-step, which is how most scripts use it, for example to colour a moving average by slope. For larger lengths the two readings differ: the sequence 1, 5, 2, 6 ends with a value above all three predecessors, so it is rising with length 3 even though 5 to 2 was a down-step. It is "a fresh 3-bar high", not "three up-steps in a row".

Both functions return a bool. A na among the compared values makes the result false, and before length previous bars exist the result is false, not na. The comparisons are strict, so a flat stretch is neither rising nor falling.

The MQL5 equivalent

Pine: direction of a 10-bar SMA tested over len bars, plotted as +1, -1 or 0.

//@version=6
indicator("SMA direction")
len = input.int(3, "Length")
ma = ta.sma(close, 10)
dir = ta.rising(ma, len) ? 1 : ta.falling(ma, len) ? -1 : 0
plot(dir, "Direction", style=plot.style_histogram)

MQL5. The SMA is kept in a calculation buffer so that it has a history to compare against; RisingAt and FallingAt apply the strict test and the na guard.

//+------------------------------------------------------------------+
//| SMA direction: Pine's ta.rising / ta.falling in MQL5              |
//+------------------------------------------------------------------+
#property indicator_separate_window
#property indicator_buffers 2
#property indicator_plots   1
#property indicator_label1  "direction"
#property indicator_type1   DRAW_HISTOGRAM
#property indicator_color1  clrDodgerBlue
#property indicator_minimum -1.2
#property indicator_maximum 1.2

input int MaLen  = 10;
input int Length = 3;

double Dir[];
double Ma[];

// ta.rising(src, len): the current value is above EACH of the previous len
// values (strict). It is not "len consecutive up-steps": 1, 5, 2, 6 is
// rising with len=3 because 6 beats 1, 5 and 2.
bool RisingAt(const double &src[], const int i, const int len)
{
   if(len <= 0 || i < len || src[i] == EMPTY_VALUE) return false;
   for(int k = 1; k <= len; k++)
      if(src[i - k] == EMPTY_VALUE || !(src[i] > src[i - k])) return false;
   return true;
}

bool FallingAt(const double &src[], const int i, const int len)
{
   if(len <= 0 || i < len || src[i] == EMPTY_VALUE) return false;
   for(int k = 1; k <= len; k++)
      if(src[i - k] == EMPTY_VALUE || !(src[i] < src[i - k])) return false;
   return true;
}

int OnInit()
{
   SetIndexBuffer(0, Dir, INDICATOR_DATA);
   SetIndexBuffer(1, Ma,  INDICATOR_CALCULATIONS);
   return INIT_SUCCEEDED;
}

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 start = (prev_calculated > 1) ? prev_calculated - 1 : 0;
   for(int i = start; i < rates_total; i++)
   {
      if(i < MaLen - 1) { Ma[i] = EMPTY_VALUE; Dir[i] = 0.0; continue; }
      double sum = 0.0;
      for(int k = 0; k < MaLen; k++) sum += close[i - k];
      Ma[i] = sum / MaLen;
      Dir[i] = RisingAt(Ma, i, Length) ? 1.0 : (FallingAt(Ma, i, Length) ? -1.0 : 0.0);
   }
   return rates_total;
}

For the common length = 1 case the helper reduces to Ma[i] > Ma[i - 1], with the same EMPTY_VALUE check.

Where the two differ

TopicPine ScriptMQL5
RuleCurrent value strictly above (below) each of the previous length valuesLoop k = 1..len with > (<); return false on the first failure
Before length barsfalseGuard i < len or the loop reads before the array
nafalseTest each cell for EMPTY_VALUE; a comparison against it silently returns a boolean
Source without historyAny expressionNeeds an array: a buffer or a price array, not a local double
EqualityNeither rising nor fallingSame, as long as the comparisons stay strict

What the Stratlate converter emits

The emitter maps ta.rising and ta.falling to pine_rising(src, bar, len) and pine_falling(src, bar, len) when the source is a named array, and to pine_rising_step / pine_falling_step with a per-call-site history array when the source is a computed value; in the example above ta.rising(ma, len) became pine_rising_step(ma, __st0, __pineBar, (int)(len)) because ma is a scalar in the emitted program. The ta.falling call sits in the second branch of the ternary, so it is only evaluated on bars where the rising test failed; the engine gives such a call its own history of the bars on which it actually ran (pine_falling(__st1, pine_put(...), len)), which is Pine's rule for conditional ta.* calls. The runtime compares the current value against the preceding len non-na values with strict inequalities and returns false if the current value is na or fewer than len such values exist. One refinement in the runtime: na cells inside the window are skipped rather than failing the test, so a source with isolated gaps still gets a verdict from the len defined values before it. The mapping is exact and not reported as an approximation.

Pitfalls

  • Implementing "length consecutive up-steps". Symptom: fewer true bars than TradingView for any length above 1, visible as shorter coloured segments on a slope-coloured average.
  • Non-strict comparisons. Symptom: flat stretches of a rounded source count as rising and falling at the same time.
  • Comparing a plain double variable with "its previous value" held in another variable. Symptom: the previous value is the previous tick, not the previous bar, so the colour flickers on the forming bar.
  • Returning true for bars before the window exists. Symptom: the first bars of the chart are coloured rising or falling at random.
  • Calling the test on an iMA buffer without skipping its unseeded bars. Symptom: a false signal on the first bars where the average is not yet defined.

FAQ

Does ta.rising(x, 3) mean three up-bars in a row?

No. It means the current value is strictly above each of the previous three values. A sequence can dip in the middle and still be rising on the last bar if that bar exceeds all three.

What do ta.rising and ta.falling return on the first bars?

false, because the comparison values do not exist. They never return na; the result is always a bool.

Can both be true on the same bar?

No. Strictly greater than every previous value and strictly less than every previous value cannot both hold, and on a flat stretch both are false.

Convert the whole script

Paste a Pine Script indicator or strategy and get MQL5 with this and every other call mapped, checked against the original on the same bars, and every construct that could not be carried over reported.

Open the converter

Related