听起来像是同一指标中的趋势和确认!
您好,感谢您分享您的指标代码,我尝试从 Trading View 转换 SqeezeMomentum 指标,以下是我的代码。我想听听您的意见!谢谢。
//+------------------------------------------------------------------------+ //|SqueezeMomentumIndicator.mq5 //|由 LazyBear | 从松树脚本转换而来。 //|| //+------------------------------------------------------------------------+ #property copyright "Converted from Pine Script by LazyBear" #property link "" #property version "1.02" #property indicator_separate_window #property indicator_buffers 4 #property indicator_plots 2 #property indicator_type1 DRAW_COLOR_HISTOGRAM #property indicator_color1 clrLime, clrGreen, clrRed, clrMaroon #property indicator_style1 STYLE_SOLID #property indicator_width1 4 #property indicator_label1 "Squeeze Momentum" #property indicator_type2 DRAW_COLOR_LINE #property indicator_color2 clrBlue, clrBlack, clrGray #property indicator_style2 STYLE_SOLID #property indicator_width2 2 #property indicator_label2 "Squeeze" //--- 输入参数 input int LengthBB = 20; // BB 长度 input double MultBB = 2.0; // BB MultFactor input int LengthKC = 20; // KC 长度 input double MultKC = 1.5; // KC MultFactor input bool UseTrueRange = true; // 使用 TrueRange (KC) //--- 指示器缓冲区 double MomentumBuffer[]; // 动量值 double MomentumColorBuffer[]; // 动感色彩 double SqueezeBuffer[]; // 挤压值 double SqueezeColorBuffer[]; // 挤压颜色 //+------------------------------------------------------------------+ //| 自定义指标初始化函数| //+------------------------------------------------------------------+ int OnInit() { //--- 指示器缓冲区映射 SetIndexBuffer(0, MomentumBuffer, INDICATOR_DATA); SetIndexBuffer(1, MomentumColorBuffer, INDICATOR_COLOR_INDEX); SetIndexBuffer(2, SqueezeBuffer, INDICATOR_DATA); SetIndexBuffer(3, SqueezeColorBuffer, INDICATOR_COLOR_INDEX); //--- 设置颜色索引 PlotIndexSetInteger(0, PLOT_COLOR_INDEXES, 4); // 直方图的 4 种颜色 PlotIndexSetInteger(1, PLOT_COLOR_INDEXES, 3); // 3 种颜色的线条 //--- 设置指标名称 IndicatorSetString(INDICATOR_SHORTNAME, "SQZMOM_LB [LazyBear]"); //--- 设置精度 IndicatorSetInteger(INDICATOR_DIGITS, 5); //--- 将数组设置为系列 ArraySetAsSeries(MomentumBuffer, true); ArraySetAsSeries(MomentumColorBuffer, true); ArraySetAsSeries(SqueezeBuffer, true); ArraySetAsSeries(SqueezeColorBuffer, true); return(INIT_SUCCEEDED); } //+------------------------------------------------------------------+ //| 简单移动平均线| //+------------------------------------------------------------------+ double SimpleMA(const double &price[], int period, int shift) { double sum = 0.0; for(int i = 0; i < period; i++) { sum += price[shift + i]; } return sum / period; } //+------------------------------------------------------------------+ //| 标准偏差| //+------------------------------------------------------------------+ double StdDev(const double &price[], int period, int shift) { double avg = SimpleMA(price, period, shift); double sum = 0.0; for(int i = 0; i < period; i++) { sum += MathPow(price[shift + i] - avg, 2); } return MathSqrt(sum / period); } //+------------------------------------------------------------------+ //| 线性回归| //+------------------------------------------------------------------+ double LinReg(const double &price[], int period, int shift) { double sum_x = 0.0; double sum_y = 0.0; double sum_xy = 0.0; double sum_x2 = 0.0; for(int i = 0; i < period; i++) { sum_x += i; sum_y += price[shift + i]; sum_xy += i * price[shift + i]; sum_x2 += i * i; } double m = period; double slope = (m * sum_xy - sum_x * sum_y) / (m * sum_x2 - sum_x * sum_x); double intercept = (sum_y - slope * sum_x) / m; return intercept; } //+------------------------------------------------------------------+ //| 自定义指标迭代函数| //+------------------------------------------------------------------+ 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 min_bars = MathMax(LengthBB, LengthKC) + 1; if(rates_total < min_bars) return(0); // 将数组设置为序列,以便正确索引 ArraySetAsSeries(high, true); ArraySetAsSeries(low, true); ArraySetAsSeries(close, true); // 计算起始点 int limit; if(prev_calculated == 0) limit = rates_total - min_bars; else limit = rates_total - prev_calculated; limit = MathMin(limit, rates_total - min_bars); // 为源计算准备数组 double source[]; ArrayResize(source, rates_total); ArraySetAsSeries(source, true); // 主计算循环 for(int i = limit; i >= 0; i--) { // 计算布林线 double basis = 0.0; for(int j = 0; j < LengthBB; j++) basis += close[i+j]; basis /= LengthBB; double dev = 0.0; for(int j = 0; j < LengthBB; j++) dev += MathPow(close[i+j] - basis, 2); dev = MultBB * MathSqrt(dev / LengthBB); double upperBB = basis + dev; double lowerBB = basis - dev; // 计算凯尔特纳通道 double ma = 0.0; for(int j = 0; j < LengthKC; j++) ma += close[i+j]; ma /= LengthKC; double range_sum = 0.0; for(int j = 0; j < LengthKC; j++) { double tr; if(UseTrueRange && i+j+1 < rates_total) tr = MathMax(high[i+j] - low[i+j], MathMax(MathAbs(high[i+j] - close[i+j+1]), MathAbs(low[i+j] - close[i+j+1]))); else tr = high[i+j] - low[i+j]; range_sum += tr; } double range_ma = range_sum / LengthKC; double upperKC = ma + MultKC * range_ma; double lowerKC = ma - MultKC * range_ma; // 计算挤压条件 bool sqzOn = (lowerBB > lowerKC) && (upperBB < upperKC); bool sqzOff = (lowerBB < lowerKC) && (upperBB > upperKC); bool noSqz = !sqzOn && !sqzOff; // 找出 KC 时段的最高点和最低点 double highest = high[i]; double lowest = low[i]; for(int j = 1; j < LengthKC; j++) { if(i+j >= rates_total) break; if(high[i+j] > highest) highest = high[i+j]; if(low[i+j] < lowest) lowest = low[i+j]; } // 计算 linreg 的源 double avg_hl = (highest + lowest) / 2; double avg_val = (avg_hl + ma) / 2; for(int j = 0; j < LengthKC; j++) { if(i+j >= rates_total) break; source[i+j] = close[i+j] - avg_val; } // 利用线性回归计算动量 MomentumBuffer[i] = LinReg(source, LengthKC, i); // 设置挤压线值(始终为 0) SqueezeBuffer[i] = 0; // 完全按照 Pine 脚本中的条件设置颜色 // bcolor = iff(val > 0, iff(val > nz(val[1]), lime, green), iff(val < nz(val[1]), red, maroon)) if(i < rates_total - 1) { if(MomentumBuffer[i] > 0) { if(MomentumBuffer[i] > MomentumBuffer[i+1]) MomentumColorBuffer[i] = 0; // 青柠 else MomentumColorBuffer[i] = 1; // 绿色 } else { if(MomentumBuffer[i] < MomentumBuffer[i+1]) MomentumColorBuffer[i] = 2; // 红色 else MomentumColorBuffer[i] = 3; // 栗色 } } else { // 第一小节 MomentumColorBuffer[i] = (MomentumBuffer[i] > 0) ? 0 : 2; // 青柠色或红色 } // scolor = noSqz ? blue : sqzOn ? black : gray if(noSqz) SqueezeColorBuffer[i] = 0; // 蓝色 else if(sqzOn) SqueezeColorBuffer[i] = 1; // 黑色 else SqueezeColorBuffer[i] = 2; // 灰色 } return(rates_total); } //+------------------------------------------------------------------+
附加的文件:
Squeze_Momentum.mq5
9 kb
Bollinger Squeeze Basic MT5:
布林挤压基本 MetaTrader 指标 - 这是一个基于动量、布林带和凯尔特纳通道的复杂指标。该指标在图表的独立窗口中绘制为动量柱状图和一系列显示当前布林带和凯尔特纳通道值之间关系的点。该指标适用于 MT4 和 MT5 版本的交易平台。
Author: Tuan Nguyen Van