【问题标题】:How to re-use value in different functions?如何在不同的功能中重用价值?
【发布时间】:2019-01-20 13:04:36
【问题描述】:

我正在编写 MotiveWave 研究,这是一个用于(日间)交易的程序。该研究是它自己的类。 (有关 MotiveWave 的 SDK 的信息可在此处找到:https://www.motivewave.com/sdk/javadoc/overview-summary.html

  public class L_V2 extends com.motivewave.platform.sdk.study.Study 

我的研究使用 2 个不同的时间范围:1 小时和 4 小时柱。两者都是在不同的函数中计算的。否则制定:两者都使用不同的数据序列,如下面的代码所示。

我有两个值,在 4 小时时间范围内计算,称为“ma9”和“ma11”,我想在 1 小时时间范围内的“if”语句中使用它们。

这是 4 小时时间范围的代码。它只是计算 2 个移动平均线

 @Override
  protected void calculateValues(DataContext ctx)
 {
  int maPeriodTF2 = getSettings().getInteger(MA_PERIOD_TF2);
  int ma2PeriodTF2 = getSettings().getInteger(MA2_PERIOD_TF2);
//Object maInput = getSettings().getInput(MA_INPUT, Enums.BarInput.CLOSE);
BarSize barSizeTF2 = getSettings().getBarSize(MA_BARSIZE_TF2);
DataSeries series2 = ctx.getDataSeries(barSizeTF2);

StudyHeader header = getHeader();
boolean updates = getSettings().isBarUpdates() || (header != null && header.requiresBarUpdates());

// Calculate Moving Average for the Secondary Data Series
  for(int i = 1; i < series2.size(); i++) {
  if (series2.isComplete(i)) continue;
  if (!updates && !series2.isBarComplete(i)) continue;

   // MA TF2
  Double ma9 = series2.ma(getSettings().getMAMethod(MA_METHOD_TF2), i, maPeriodTF2, getSettings().getInput(MA_INPUT_TF2));
  Double ma11 = series2.ma(getSettings().getMAMethod(MA2_METHOD_TF2), i, ma2PeriodTF2, getSettings().getInput(MA2_INPUT_TF2));

  series2.setDouble(i, Values.MA9_H4, ma9);
  series2.setDouble(i, Values.MA11_H4, ma11);
}

// Invoke the parent method to run the "calculate" method below for the primary (chart) data series
super.calculateValues(ctx);

我现在想在 1 小时的时间范围内在另一个函数中使用这两个值,“ma9”和“ma11”:

 @Override  
  protected void calculate(int index, DataContext ctx)

  DataSeries series=ctx.getDataSeries();

 if (ma9 < ma11 && other conditions) 

{ctx.signal(index, Signals.YOU_SHOULD_BUY, "This would be my buying signal", series.getClose(index));
}

如何导出 ma9 和 ma11 使它们成为“全局”并且我可以在其他功能中重新使用它们?

【问题讨论】:

  • 要么将其保存在类级别,要么将其作为参数传递。
  • 这不会返回预期的结果。我添加了“静态双ma9;静态双ma11;”在我的代码开头并在 MotiveWave 的调试器中打印出它的值。对于 4 小时柱系列,我可以看到 i 得到不同的值,但对于 1 小时柱系列,ma9 和 ma11 的值保持不变(它们总是取最高/最新 i (索引)的值计算值。如何使“计算”函数使用“计算值”的变化值?

标签: java forex


【解决方案1】:

基本上,这个想法是将值存储在某处,或者在计算后适当地传递它们。 有一个基于单例的 java 模式,允许您在类中存储/检索值(使用集合:HashMap)。可以使用带有 HashMap 标准操作(put、get)的构造 Singelton.getInstance(),在基于预定义(key,value)的任何类中添加、重试任何值。

也许这个例子可能有用。

import java.util.Hashtable;

class Singleton extends Hashtable<String, Object> {
private static final long serialVersionUID = 1L;
private static Singleton one_instance = null;

private Singleton() {
};

public static Singleton getInstance() {
    one_instance = (one_instance == null) ? new Singleton() : one_instance;
    return one_instance;
}

}

import java.util.Random;

public class Reuse {

public static void main(String[] args) {
    Reuse r = new Reuse();
    Compute c = r.new Compute();
    Singleton.getInstance().put("r1", c.getRandom());
    Singleton.getInstance().put("r2", c.getRandom());
    Singleton.getInstance().put("n", c.getName());
    System.out.println(Singleton.getInstance().get("r1"));//print  random_number_1
    System.out.println(Singleton.getInstance().get("r2"));//print  random_number_2
    System.out.println(Singleton.getInstance().get("n"));// print  name (value for key n)
}



class Compute
{
    public Double getRandom()
    {
        return new Random().nextDouble();
    }

    public String getName()
    {
        return "name";
    }
}
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-09-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-05
    • 1970-01-01
    • 2020-03-02
    • 1970-01-01
    相关资源
    最近更新 更多