You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何修改Tradovate平台JavaScript移动平均交叉直方图指标逻辑

Tradovate移动平均交叉直方图指标修改方案

调整逻辑说明

基于你提出的绘制规则,此处默认假设你提到的「period2Avg的HIGH」为用K线最高价计算的period2周期均线,「period2Avg的LOW」为用K线最低价计算的period2周期均线,如果是其他判断逻辑可以在此基础上调整。
本次修改核心调整点:

  • init方法中新增2个period2周期的均线实例,分别计算最高价、最低价对应的均线值
  • map方法中新增判断规则,仅符合条件时返回差值,否则返回0不绘制柱形

修改后完整代码

const predef = require("./tools/predef");
const meta = require("./tools/meta");
const SMA = require("./tools/SMA");
const EMA = require("./tools/EMA");
const WMA = require("./tools/WMA");

class TEMP {
    init() {
        // 初始化period1均线(收盘价计算)
        switch(this.props.period1type) {
          case "Simple":
              this.period1Avg = SMA(this.props.period1);
              break;
          case "Exponential":
              this.period1Avg = EMA(this.props.period1);
              break;
          case "Weighted":
              this.period1Avg = WMA(this.props.period1);
              break;        
          default:
              this.period1Avg = SMA(this.props.period1);          
        }
        // 初始化period2的三类均线:收盘价、最高价、最低价计算
        switch(this.props.period2type) {
          case "Simple":
              this.period2Avg = SMA(this.props.period2);
              this.period2HighAvg = SMA(this.props.period2);
              this.period2LowAvg = SMA(this.props.period2);
              break;
          case "Exponential":
              this.period2Avg = EMA(this.props.period2);
              this.period2HighAvg = EMA(this.props.period2);
              this.period2LowAvg = EMA(this.props.period2);
              break;
          case "Weighted":
              this.period2Avg = WMA(this.props.period2);
              this.period2HighAvg = WMA(this.props.period2);
              this.period2LowAvg = WMA(this.props.period2);
              break;        
          default:
              this.period2Avg = SMA(this.props.period2);
              this.period2HighAvg = SMA(this.props.period2);
              this.period2LowAvg = SMA(this.props.period2);          
        }
    }

    map(d) {
        const close = d.value();
        const p1Val = this.period1Avg(close);
        const p2Val = this.period2Avg(close);
        const p2HighVal = this.period2HighAvg(d.high());
        const p2LowVal = this.period2LowAvg(d.low());
        
        let diff = 0;
        let plotColor = {};
        // 符合绿柱条件
        if (p1Val > p2HighVal) {
            diff = p1Val - p2Val;
            plotColor = { color: "green" };
        } 
        // 符合红柱条件
        else if (p1Val < p2LowVal) {
            diff = p1Val - p2Val;
            plotColor = { color: "red" };
        }
        
        return {
            value: diff,
            style: { value: plotColor }
        };
    }
}

module.exports = {
    name: "TEMP",
    description: "TEMP",
    calculator: TEMP,
    params: {
        period1: predef.paramSpecs.period(8),
        period1type: predef.paramSpecs.enum({value1: "Simple", value2: "Exponential", value3: "Weighted"}),
        period2: predef.paramSpecs.period(34),
        period2type: predef.paramSpecs.enum({value1: "Simple", value2: "Exponential", value3: "Weighted"})
    },
    tags: [predef.tags.MovingAverage],
    inputType: meta.InputType.BARS,
    areaChoice: meta.AreaChoice.NEW,
    plots: {
        value: { displayOnly: true }
    },
    plotter: predef.plotters.histogram
};

内容的提问来源于stack exchange,提问作者gravityflyer

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.30 11:18:01