無料でロボットをダウンロードする方法を見る
Telegram上で私たちを見つけてください。
私たちのファンページに参加してください
興味深いスクリプト?
それではリンクにそれを投稿してください。-
他の人にそれを評価してもらいます
スクリプトを気に入りましたか?MetaTrader 5ターミナルの中でそれを試してみてください。
ライブラリ

PineCompat - TradingView Pine Script ta functions for MQL5 - MetaTrader 5のためのライブラリ

Kazuma Kamizono
Kazuma Kamizono
MQL5 / MQL4 / Pine Script v6 developer and active trader.
Seller on the MQL5 Market (indicators for MT4/MT5).
Services:
- TradingView Pine Script to MT5/MT4 conversion (bar-by-bar matching, with tester report)
- MT4 to MT5 migration
ビュー:
17
パブリッシュ済み:
\MQL5\Include\ \MQL5\Indicators\
MQL5フリーランス このコードに基づいたロボットまたはインジケーターが必要なら、フリーランスでご注文ください フリーランスに移動

PineCompat demo indicator on EURUSD H1Overview

PineCompat.mqh is a single include file for developers who port TradingView Pine Script indicators and strategies to MetaTrader 5. Each function keeps the meaning of its Pine counterpart, so a line such as up = ta.crossover(fast, slow) becomes bool up = Pine::Crossover(fast, slow, shift); and the MQL5 code can be compared with the Pine source line by line.

The package contains:

  • PineCompat.mqh - the library (put it in MQL5\Include\)
  • PineCompat_Demo.mq5 - a demo indicator that uses the library (put it in MQL5\Indicators\)

Conventions

  • All functions live in the namespace "Pine".
  • Arrays are SERIES arrays: index 0 is the newest bar. In OnCalculate, call ArraySetAsSeries(array, true) on close[], high[] etc. and on your buffers before you use them.
  • "shift" is the bar to evaluate (0 = current bar). Pine's x[1] is shift + 1.
  • Pine "na" is PINE_NA (= EMPTY_VALUE) for double results and PINE_NA_INT (= INT_MIN) for int results. Inputs equal to EMPTY_VALUE, NaN or Inf are treated as na. Use Pine::IsNa(x) and Pine::Nz(x, replacement).
  • No Print or Alert calls, no files, no DLLs, no web requests.

Functions

na helpers

  • IsNa(x), Nz(x, replacement), At(src, i) - na(x), nz(x), src[i] with range check

Crosses

  • Crossover(a, b, shift), Crossover(a, level, shift) - ta.crossover
  • Crossunder(a, b, shift), Crossunder(a, level, shift) - ta.crossunder
  • Cross(a, b, shift) - ta.cross

Event memory

  • ValueWhen(cond, src, occurrence, shift) - ta.valuewhen (cond may be a bool, int, long or double array)
  • BarsSince(cond, shift) - ta.barssince (PINE_NA_INT if never true)

Highest / Lowest

  • Highest, Lowest (src, length, shift) - ta.highest, ta.lowest
  • HighestBars, LowestBars (src, length, shift) - ta.highestbars, ta.lowestbars (Pine-style offset <= 0)

Pivots

  • PivotHigh, PivotLow (src, left, right, shift) - ta.pivothigh, ta.pivotlow

Change

  • Change(src, length, shift), Rising(src, length, shift), Falling(src, length, shift) - ta.change, ta.rising, ta.falling

Moving averages and oscillators (single value)

  • Sma(src, length, shift) - ta.sma
  • Ema(src, length, shift, lookback) - ta.ema (alpha = 2 / (length + 1), SMA seed)
  • Rma(src, length, shift, lookback) - ta.rma (Wilder, alpha = 1 / length, SMA seed)
  • Rsi(src, length, shift, lookback) - ta.rsi
  • TrueRange(high, low, close, shift) - ta.tr
  • Atr(high, low, close, length, shift, lookback) - ta.atr (= ta.rma(ta.tr, length))

Whole-array versions for OnCalculate (incremental, pass prev_calculated)

  • SmaOnArray, EmaOnArray, RmaOnArray (src, length, out, rates_total, prev_calculated)
  • RsiOnArray(src, length, out, upWork, downWork, rates_total, prev_calculated) - upWork/downWork: two INDICATOR_CALCULATIONS buffers
  • AtrOnArray(high, low, close, length, out, rates_total, prev_calculated)

Higher timeframe (request.security)

  • Security(symbol, tf, source, shift, mode), SecurityOpen / SecurityHigh / SecurityLow / SecurityClose
  • SecurityAt(symbol, tf, source, time, mode)
  • SecurityShiftAt(symbol, tf, time, mode) - returns the higher-TF bar index, for use with your own arrays (for example a higher-TF RSI)
  • mode PINE_SEC_CONFIRMED (default): the last higher-TF bar that was already closed when the chart bar opened. Same as request.security(..., src[1], lookahead = barmerge.lookahead_on). Does not repaint.
  • mode PINE_SEC_LIVE: the higher-TF bar that contains the chart bar. Same as request.security(..., src, lookahead = barmerge.lookahead_on). REPAINTS.

Checked against MT5 built-in indicators

  • Pine::Rsi matched MT5 iRSI on the same history (difference 1e-13 or less in our tests).
  • Pine::Rma matched iMA with MODE_SMMA.
  • Pine::Atr follows Pine's ta.atr, which is the RMA (Wilder) of the true range. MT5's iATR is a simple moving average (SMA) of the true range. The two are different indicators, and in our tests their values sometimes differed by tens of percent. This is a common reason why a Pine script and its MT5 port give different stop distances or filter results. Use Pine::Atr when porting Pine code.
  • Security with PINE_SEC_CONFIRMED was checked in the Strategy Tester: values shown on history were the same as the values seen in real time (no repaint).
  • The results have not been compared with TradingView itself. The tie rules below are the rules of this library.

Example

#property indicator_chart_window
#property indicator_buffers 2
#property indicator_plots   2

#include <PineCompat.mqh>

double FastBuf[], SlowBuf[];   // two INDICATOR_DATA buffers, set as series in OnInit

int OnInit()
  {
   SetIndexBuffer(0, FastBuf, INDICATOR_DATA);
   SetIndexBuffer(1, SlowBuf, INDICATOR_DATA);
   ArraySetAsSeries(FastBuf, true);
   ArraySetAsSeries(SlowBuf, true);
   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[])
  {
   ArraySetAsSeries(close, true);   // series order: 0 = newest bar
   ArraySetAsSeries(high, true);

   // fast = ta.ema(close, 9), slow = ta.ema(close, 21)
   if(Pine::EmaOnArray(close, 9, FastBuf, rates_total, prev_calculated) == 0) return(0);
   if(Pine::EmaOnArray(close, 21, SlowBuf, rates_total, prev_calculated) == 0) return(0);

   // ta.crossover(fast, slow)[1]  (evaluated on the last closed bar)
   bool crossedUp = Pine::Crossover(FastBuf, SlowBuf, 1);

   // ta.pivothigh(high, 5, 5)[1]: the pivot bar itself is 1 + 5 bars back
   double ph = Pine::PivotHigh(high, 5, 5, 1);
   if(!Pine::IsNa(ph))
     {
      // a pivot high was confirmed on bar 1
     }

   // request.security(syminfo.tickerid, "240", close[1], lookahead = barmerge.lookahead_on)
   double h4Close = Pine::SecurityClose(_Symbol, PERIOD_H4, 1);

   return(rates_total);
  }

The demo indicator (PineCompat_Demo.mq5) shows a complete example: RSI (ta.rsi), a signal EMA of the RSI (ta.ema), pivot dots (ta.pivothigh / ta.pivotlow drawn on the pivot bar), crossover arrows (ta.crossover / ta.crossunder) and a higher-timeframe RSI using the non-repainting Security mode.

Demo inputs: RSI length (14), Signal EMA length (9), Pivot left bars (5), Pivot right bars (5), Signals on closed bars only (true), Show higher-timeframe RSI (true), Higher timeframe (H4), Max higher-TF bars to load (5000).

The arrows and dots only show what the functions return. They are a coding example, not trade signals.

Notes

  • Series order is required. Without ArraySetAsSeries(..., true) the results are wrong.
  • When there is no price change at all, Pine's RSI returns 100 while MT5 iRSI returns 50. Pine::Rsi follows Pine.
  • Ema, Rma, Rsi and Atr are recursive: every value depends on all earlier bars. The first few hundred bars after the start of the loaded history can change when more history is loaded. Pine::Ema differs from iMA (MODE_EMA) only in this warm-up period, because the seed is different (Pine uses an SMA seed).
  • The "lookback" argument of the single-value functions limits how far back the recursion starts. It is faster but approximate. lookback = 0 uses the whole array.
  • The single-value functions walk the whole history on every call. Inside OnCalculate, use the *OnArray versions, which update only the new bars.
  • Pivots are confirmed "right" bars after the pivot bar, exactly as in Pine. To mark the pivot bar itself, draw at index shift + right (Pine's offset = -right).
  • Tie rules: Highest/Lowest return the most recent bar when values are equal. PivotHigh requires the candidate to be strictly higher than the left (older) bars and higher than or equal to the right (newer) bars, so a flat top is reported once, on its oldest bar. PivotLow is the mirror image.
  • PINE_SEC_LIVE repaints. Do not use it for signals or backtests.
  • If higher-timeframe data is not loaded yet, the Security functions return PINE_NA (SecurityShiftAt returns -1). Try again on the next call.
  • Security, SecurityOpen/High/Low/Close read raw bar prices. For an indicator on the higher timeframe, calculate it on your own higher-TF arrays and use SecurityShiftAt, as the demo does.

Disclaimer

PineCompat is a free programming example. It contains no trading method, gives no investment advice and does not suggest any result. Use it at your own responsibility.

TradingView and Pine Script are trademarks of TradingView, Inc. MetaTrader is a trademark of MetaQuotes Ltd. This project is not affiliated with or endorsed by either company.

VelaMayor MTF - Higher Timeframe Candle Overlay VelaMayor MTF - Higher Timeframe Candle Overlay

カスタマイズ可能な実体、独立した枠線、正確なヒゲを備え、高時間足のローソク足を低時間足のチャートに直接重ねて表示できます。 画面を切り替えることなく、マクロ的なトレンドの方向性、セッションの始値、流動性の範囲を簡単に把握できます。 ボックス境界が分離された超軽量エンジンと、堅牢なチャート変更時の自動復旧機能を搭載しています。

Margin Risk Simulator Margin Risk Simulator

現在の銘柄について、口座がマージンコールおよびストップアウトに達する価格を表示します。また、取引量の増加を想定した「仮定モード」、追加可能な最大取引量、およびその過程で発動する指値注文についても表示されます。

Session Range Boxes - Asia, London, New York with breakout alerts Session Range Boxes - Asia, London, New York with breakout alerts

Draws the high/low range of up to three trading sessions (default Asia, London, New York) as boxes, with optional extended level lines, range-size labels and one-time breakout alerts. Display tool only - it does not trade.

EA Template - signal-agnostic MT5 Expert Advisor framework (risk sizing, SL and TP, trailing, filters) EA Template - signal-agnostic MT5 Expert Advisor framework (risk sizing, SL and TP, trailing, filters)

A reusable Expert Advisor template for MT5. Write your own entry logic in one function (GetSignal returns +1, -1 or 0); the template handles lot sizing, SL/TP with stop-level checks, break-even, trailing stop, spread / time / weekday / news filters, order filling, retries and netting / hedging accounts. The included MA cross is an example signal only, not a trading strategy.