简体   繁体   English

MQL 到 Pine 脚本的转换产生不同的结果

[英]MQL to Pine Script conversion yields different results

I am trying to convert an MQL4 indicator into Pine Script but it yields a totally different output;我正在尝试将 MQL4 指标转换为 Pine 脚本,但它会产生完全不同的 output; the indicator values do not match.指标值不匹配。 The MQL4 indicator line is smoother compared to PineScript与 PineScript 相比,MQL4 指标线更平滑

The MQL4 indicator code is MQL4 指标代码是

#property indicator_separate_window
#property indicator_buffers 1
#property indicator_color1 Crimson
#property indicator_width1 2

extern int period = 20;

double Signal_Buffer[];

int init() {
  IndicatorDigits( Digits + 4 );
  IndicatorShortName("Indi (" + string(period) + ")");
  SetIndexBuffer(0, Signal_Buffer);
  SetIndexStyle(0, DRAW_LINE);
  SetIndexLabel(0, "Indi (" + string(period) + ")");
  return (0);
}


int start() {
  int bars_loaded = IndicatorCounted();
  if (bars_loaded > 0) bars_loaded--;
  int total_bars = Bars - bars_loaded;

  double current_high    = 0;
  double current_low     = 0;
  double high_low_median = 0;
  double signal          = 0;
  double a = 0;
  double b = 0;
  for (int i = 0; i < total_bars; i++) {
    current_high     = High[iHighest(Symbol(), Period(), MODE_HIGH, period, i)];
    current_low      = Low[iLowest(Symbol(), Period(), MODE_LOW, period, i)];
    high_low_median  = (High[i] + Low[i]) / 2.0;
    a                = 0.66 * ( (high_low_median - current_low) / (current_high - current_low) - 0.5 ) + 0.67 * b;
    a                = MathMin( MathMax( a, -0.999 ), 0.999 );
    Signal_Buffer[i] = MathLog( ( a + 1.0 ) / ( 1 - a ) ) / 2.0 + signal / 2.0;
    b                = a;
    signal           = Signal_Buffer[i];
  }
  return (0);
}

and the Pine Script code is Pine 脚本代码是

//@version=5
indicator(title='Indi', shorttitle='Indi')

per = input.int( defval=20, title='Period', minval=1 )

a = 0.0
b = 0.0
signal = 0.0

h = ta.highest( high, per )
l = ta.lowest( low, per )
m = hl2

a := 0.66 * ( (m - l) / (h - l) - 0.5 ) + 0.67 * b
a := math.min( math.max( a, -0.999 ), 0.999 )
signal := math.log( ( a + 1.0 ) / ( 1 - a ) ) / 2.0 + signal / 2.0
b := a

plot( signal )

I think maybe you need to replace b with nz(a[1] in this line:我想也许你需要在这一行中用nz(a[1]替换b

a                = 0.66 * ( (high_low_median - current_low) / (current_high - current_low) - 0.5 ) + 0.67 * b;

to

a                = 0.66 * ( (high_low_median - current_low) / (current_high - current_low) - 0.5 ) + 0.67 * nz(a[1]);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM