【问题标题】:Calculate VWAP using JavaScript使用 JavaScript 计算 VWAP
【发布时间】:2021-11-18 00:59:11
【问题描述】:

我有这个 Java 代码,我想重写为 JavaScript:

package example;

import java.util.List;

public class Tain {


    public static void main(String[] args) {

    }

    private class PriceData {

        private double volume;
        private double price;

        public double getVolume() {
            return volume;
        }

        public void setVolume(double volume) {
            this.volume = volume;
        }

        public double getPrice() {
            return price;
        }

        public void setPrice(double price) {
            this.price = price;
        }
    }

    private double calculateBaseValueVwap(List<PriceData> side, double positionStep) {
        double cumSum = 0d;
        double totalVolume = 0d;
        double amountLeft = positionStep;
        for(PriceData level : side) {
            if(totalVolume >= positionStep || amountLeft <= 0) {
                break;
            }
            double amountForPriceLevel = 0d;
            if(amountLeft > level.getVolume()) {
                amountForPriceLevel = level.getVolume();
                cumSum += level.getPrice() * level.getVolume();
                totalVolume += level.getVolume();
            }
            else {
                amountForPriceLevel = level.getVolume() - amountLeft;
                cumSum += level.getPrice() * amountForPriceLevel;
                totalVolume += amountForPriceLevel;
            }
            amountLeft -= amountForPriceLevel;
        }
        return cumSum / totalVolume;
    }

}

我试过了:

class Tain
{
    static
    main(args)
    {}
    class PriceData
    {
        #volume = 0.0;
        #price = 0.0;
        getVolume()
        {
            return this.#volume;
        }
        setVolume(volume)
        {
            this.volume = volume;
        }
        getPrice()
        {
            return this.#price;
        }
        setPrice(price)
        {
            this.price = price;
        }
    }
    calculateBaseValueVwap(side, positionStep)
    {
        var cumSum = 0.0;
        var totalVolume = 0.0;
        var amountLeft = positionStep;
        for (const level of side)
        {
            if (totalVolume >= positionStep || amountLeft <= 0)
            {
                break;
            }
            var amountForPriceLevel = 0.0;
            if (amountLeft > level.getVolume())
            {
                amountForPriceLevel = level.getVolume();
                cumSum += level.getPrice() * level.getVolume();
                totalVolume += level.getVolume();
            }
            else
            {
                amountForPriceLevel = level.getVolume() - amountLeft;
                cumSum += level.getPrice() * amountForPriceLevel;
                totalVolume += amountForPriceLevel;
            }
            amountLeft -= amountForPriceLevel;
        }
        return cumSum / totalVolume;
    }
}
Tain.main([]);

我不清楚如何重写 Java 对象 PriceData 并将其作为 JavaScript 列表发送。你能指导我在 JavaScript 中实现这个的正确方法是什么?

【问题讨论】:

    标签: javascript java


    【解决方案1】:

    可能您的问题与将PriceData 定义为Tain 的内部类有关。 AFAIK 即使在现代 Javascript 版本中,也没有明确的方法来定义嵌套类。

    出于同样的原因,正如其他答案中所指出的那样,您可以创建一个独立的 PriceData 类:老实说,我认为这将是最好的选择。

    作为替代方案,您可以尝试的另一种方法是将PriceData 定义为static。请看下面的代码:

    class Tain
    {
        static main(args) {}
        
        static PriceData = class {
            #volume = 0.0;
            #price = 0.0;
            getVolume(){
                return this.#volume;
            }
            setVolume(volume){
                this.volume = volume;
            }
            getPrice(){
                return this.#price;
            }
            setPrice(price){
                this.price = price;
            }
        }
    
        //...
    }
    

    它将允许您像这样使用PriceData

    const pa = new Tain.PriceData();
    pa.setPrice(12.4);
    pa.setVolume(200);
    const pb = new Tain.PriceData();
    pb.setPrice(20.4);
    pb.setVolume(100);
    

    在 Javascript 中,List 没有像 Java 中那样明确的概念,您需要创建一个包含这些对象的数组,以将该信息传递给 main 方法:

    const prices = [pa, pb];
    

    然后您可以使用类似的方法测试您的 Tain main 方法 - 我假设,因为问题不清楚:

    Tain.main(prices, 100);
    

    请注意 Javascript 中static 方法的定义,它们旨在处理虽然定义在类中但与该类的实例没有直接关系的东西。我这样说是因为,例如,如果在您的 main 方法中您应该使用 calculateBaseValueVwap,那么更好的方法是直接将 calculateBaseValueVwap 定义为 static 并仅使用该方法。在 Java 中,您可以定义一个 static main 方法来运行您的应用程序,而在 Javascript 中则不需要。

    虽然您当然可以使用类来完成这项任务,但一种更简单的方法可能是使用函数。

    我们先定义Tain

    Tain = function() {
    
        this.calculateBaseValueVwap = function(side, positionStep) {
            let cumSum = 0.0;
            let totalVolume = 0.0;
            let amountLeft = positionStep;
            for (let i = 0; i < side.length; i++) {  
                level = side[i];
                if(totalVolume >= positionStep || amountLeft <= 0) {
                    break;
                }
                let amountForPriceLevel = 0.0;
                if(amountLeft > level.volume) {
                    amountForPriceLevel = level.volume;
                    cumSum += level.price * level.volume;
                    totalVolume += level.volume;
                }
                else {
                    amountForPriceLevel = level.volume - amountLeft;
                    cumSum += level.price * amountForPriceLevel;
                    totalVolume += amountForPriceLevel;
                }
                amountLeft -= amountForPriceLevel;
            }
            return cumSum / totalVolume;
        }
    }
    

    为了能够访问PriceData,您可以创建一种class级别的函数来表示该概念,例如:

    Tain.PriceData = function(volume, price) {
        this.volume = volume;
        this.price = price;
    }
    

    如果需要,您可以为 volumeprice 属性定义 getter 和 setter。

    请注意,如果您也可以将PriceData 定义为独立函数;您甚至可以不使用函数来表示该概念,而是使用简单的 Javascript 对象:

    {
      price: 12.4,
      volume: 200
    }
    

    如前所述,在您的 Java 代码中并不清楚 main 方法是如何实现的,但出于测试目的,我们假设如下:

    Tain.main = function(side, positionStep) {
        const tain = new Tain();
        const baseValueVwap = tain.calculateBaseValueVwap(side, positionStep);
        console.log(`Base value vwap: ${baseValueVwap}`);
        return baseValueVwap;
    }
    

    然后,以与上述类似的方式测试代码:

    const pa = new Tain.PriceData(12.4, 200);
    const pb = new Tain.PriceData(20.4, 100);
    // Or, using directly objects
    // const pa = { price: 12.4,  volume: 200 };
    // const pb = { price: 20.4,  volume: 100 };
    
    const prices = [pa, pb];
    Tain.main(prices, 100);
    

    代码可以使用 ES6 语法进行改进,并且可以在很多方面进行改进,但它应该可以正常工作。

    如果你有机会,我的建议是使用 Javascript 对象表示法来表示 PriceData 和一个独立的函数 calculateBaseValueVwap,在某处定义 - 你的 HTML 页面、你的 Javascript 模块等等 - 这将是直接在您的代码中调用。

    【讨论】:

      【解决方案2】:

      您的代码实际上并没有那么大的问题。你只是将一个类嵌套在一个类中,这是 ES6 不允许的,而且你有一个额外的结束花括号。

      class Tain {
          static main(args) {}
      }
      
      class PriceData {
          #volume = 0.0;
          #price = 0.0;
          getVolume() {
              return this.#volume;
          }
          setVolume(volume) {
              this.volume = volume;
          }
          getPrice() {
              return this.#price;
          }
          setPrice(price) {
              this.price = price;
          }
          calculateBaseValueVwap(side, positionStep) {
              var cumSum = 0.0;
              var totalVolume = 0.0;
              var amountLeft = positionStep;
              for (const level of side) {
                  if (totalVolume >= positionStep || amountLeft <= 0) {
                      break;
                  }
                  var amountForPriceLevel = 0.0;
                  if (amountLeft > level.getVolume()) {
                      amountForPriceLevel = level.getVolume();
                      cumSum += level.getPrice() * level.getVolume();
                      totalVolume += level.getVolume();
                  } else {
                      amountForPriceLevel = level.getVolume() - amountLeft;
                      cumSum += level.getPrice() * amountForPriceLevel;
                      totalVolume += amountForPriceLevel;
                  }
                  amountLeft -= amountForPriceLevel;
              }
              return cumSum / totalVolume;
          }
      }
      
      
      Tain.main([]);

      【讨论】:

        猜你喜欢
        • 2015-05-31
        • 2019-11-02
        • 2017-12-04
        • 2014-06-05
        • 1970-01-01
        • 2020-12-11
        • 2011-11-22
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多