我的指标在缓冲区中只有一个值,它给我一条直线

My indicator only has one value in the buffer , which is giving me a straight line

我正在尝试将我在 tradingview(使用 pinescript)上找到的指标转换为 MQL4:

============================================= =

BullTrend = (close - lowest(low, 50)) / atr(5)

BearTrend = (highest(high, 50) - close) / atr(5)

BearTrend2= -1*BearTrend 

Trend = BullTrend - BearTrend

============================================= =

代码应该在每个柱上执行我的 mql4 版本中显示的计算。但是,它显示的值与我尝试去的一样远。

如何确保在每个柱上执行计算,或者如果有更有效的编码方法,我将不胜感激。 :)

//+------------------------------------------------------------------+
//|                                                         BBPT.mq4 |
//|                                                          Maxwell |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Maxwell"
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict
#property indicator_separate_window
#property indicator_buffers 3
#property indicator_plots   3
//--- plot Trend
#property indicator_label1  "Trend"
#property indicator_type1   DRAW_LINE
#property indicator_color1  clrWhite
#property indicator_style1  STYLE_SOLID
#property indicator_width1  1
//--- plot BullTrend
#property indicator_label2  "BullTrend"
#property indicator_type2   DRAW_LINE
#property indicator_color2  clrMediumSeaGreen
#property indicator_style2  STYLE_SOLID
#property indicator_width2  1
//--- plot BearTrend2
#property indicator_label3  "BearTrend2"
#property indicator_type3   DRAW_LINE
#property indicator_color3  clrRed
#property indicator_style3  STYLE_SOLID
#property indicator_width3  1
//--- indicator buffers
double         TrendBuffer[];
double         BullTrendBuffer[];
double         BearTrend2Buffer[];


//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping
   SetIndexStyle(0,DRAW_LINE,STYLE_SOLID,2,clrWhite);
   SetIndexStyle(1,DRAW_LINE,STYLE_SOLID,2,clrGreen);
   SetIndexStyle(2,DRAW_LINE,STYLE_SOLID,2,clrRed);
   
   SetIndexLabel(0,"Trend");
   SetIndexLabel(1,"BullTrend");
   SetIndexLabel(2,"BearTrend2");
   
   
   SetIndexBuffer(0,TrendBuffer);
   SetIndexBuffer(1,BullTrendBuffer);
   SetIndexBuffer(2,BearTrend2Buffer);
   
//---
   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[])
  {
//---
   int uncalculatedBar = rates_total - prev_calculated;
         
   for(int i=0; i < uncalculatedBar; i++)
   { 
      double lowestPrice = Low[iLowest(Symbol(),0,MODE_LOW,50,0)];
      double highestPrice = High[iHighest(Symbol(),0,MODE_HIGH,50,0)];
      double lowestCloseCalc = (iClose(NULL,0,0) - lowestPrice);
      double highestCloseCalc = (highestPrice - iClose(NULL,0,0));
      double atr = (iATR(NULL,0,5,0));
      double BullTrend = lowestCloseCalc / (atr);
      double BearTrend = highestCloseCalc / (atr);
      double BearTrend2 = (-1 * BearTrend);
      double Trend = (BullTrend - BearTrend);
   
   
      TrendBuffer[i] = Trend;
      BullTrendBuffer[i] = BullTrend;
      BearTrend2Buffer[i] = BearTrend2;
   }
   
//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+

您已将所有计算放在一个循环中,但您已将它们硬编码为每次查看第 0 条而不是第 i 条。 Low[iLowest(Symbol(),0,MODE_LOW,50,0)] 应该是 Low[iLowest(Symbol(),0,MODE_LOW,50,i)] 等等...

//+------------------------------------------------------------------+
//|                                                         BBPT.mq4 |
//|                                                          Maxwell |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Maxwell"
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict
#property indicator_separate_window
#property indicator_buffers 3
#property indicator_plots   3
//--- plot Trend
#property indicator_label1  "Trend"
#property indicator_type1   DRAW_LINE
#property indicator_color1  clrWhite
#property indicator_style1  STYLE_SOLID
#property indicator_width1  1
//--- plot BullTrend
#property indicator_label2  "BullTrend"
#property indicator_type2   DRAW_LINE
#property indicator_color2  clrMediumSeaGreen
#property indicator_style2  STYLE_SOLID
#property indicator_width2  1
//--- plot BearTrend2
#property indicator_label3  "BearTrend2"
#property indicator_type3   DRAW_LINE
#property indicator_color3  clrRed
#property indicator_style3  STYLE_SOLID
#property indicator_width3  1
//--- indicator buffers
double         TrendBuffer[];
double         BullTrendBuffer[];
double         BearTrend2Buffer[];


//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping
   SetIndexStyle(0,DRAW_LINE,STYLE_SOLID,2,clrWhite);
   SetIndexStyle(1,DRAW_LINE,STYLE_SOLID,2,clrGreen);
   SetIndexStyle(2,DRAW_LINE,STYLE_SOLID,2,clrRed);
   
   SetIndexLabel(0,"Trend");
   SetIndexLabel(1,"BullTrend");
   SetIndexLabel(2,"BearTrend2");
   
   
   SetIndexBuffer(0,TrendBuffer);
   SetIndexBuffer(1,BullTrendBuffer);
   SetIndexBuffer(2,BearTrend2Buffer);
   
//---
   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[])
  {
//---
   int uncalculatedBar = rates_total - prev_calculated;
         
   for(int i=0; i < uncalculatedBar; i++)
   { 
      double lowestPrice = Low[iLowest(Symbol(),0,MODE_LOW,50,i)];
      double highestPrice = High[iHighest(Symbol(),0,MODE_HIGH,50,i)];
      double lowestCloseCalc = (iClose(NULL,0,i) - lowestPrice);
      double highestCloseCalc = (highestPrice - iClose(NULL,0,i));
      double atr = (iATR(NULL,0,5,i));
      double BullTrend = lowestCloseCalc / (atr);
      double BearTrend = highestCloseCalc / (atr);
      double BearTrend2 = (-1 * BearTrend);
      double Trend = (BullTrend - BearTrend);
   
   
      TrendBuffer[i] = Trend;
      BullTrendBuffer[i] = BullTrend;
      BearTrend2Buffer[i] = BearTrend2;
   }
   
//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+