简体   繁体   English

如何在不同功能中重用价值?

[英]How to re-use value in different functions?

I am programming a Study in MotiveWave, a program used for (day)trading. 我正在MotiveWave中编写研究程序,该程序用于(日间)交易。 The study is its own class. 该研究是其自己的班级。 (info about MotiveWave's SDK found here: https://www.motivewave.com/sdk/javadoc/overview-summary.html ) (有关MotiveWave SDK的信息,请参见: https : //www.motivewave.com/sdk/javadoc/overview-summary.html

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

My study uses 2 different timeframes: the 1 hour and the 4 hour bars. 我的研究使用2个不同的时间范围:1小时和4小时酒吧。 Both are calculated in a different function. 两者均以不同的函数计算。 Otherwise formulated: both use a different dataseries, as shown in the code below. 否则,公式如下:两者都使用不同的数据系列,如下面的代码所示。

I have two values, being calculated on the 4 hour timeframe, called 'ma9' and 'ma11' that I would like to use in an 'if'-statement on the 1 hour timeframe. 我有两个值,它们是在4小时的时间框架上计算的,分别称为“ ma9”和“ ma11”,我想在1小时的时间框架的“ if”语句中使用这些值。

This is the code for the 4 hour timeframe. 这是4小时时间表的代码。 It simply calculates 2 moving averages 它仅计算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);

I would now like to use those 2 values, 'ma9' and 'ma11' in another function, on the 1 hour timeframe: 我现在想在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));
}

How can I export the ma9 and the ma11 so they become 'global' and I can re-use them in this other function ? 我如何导出ma9和ma11以使它们成为“全局”文件,并可以在其他功能中重复使用它们?

Basically, the idea is to store somewhere the values or just pass them appropriately after being computed. 基本上,这个想法是将值存储在某个地方,或者只是在计算后适当地传递它们。 There is a java pattern based on singleton that allow you to store/retrieve values inside a class (using a collection : HashMap). 有一个基于单例的Java模式,允许您在类内存储/检索值(使用集合:HashMap)。 Any values could be added,retried in any classes based on predefined (key,value) using the construction Singelton.getInstance() with HashMap standard operation (put, get). 使用带有HashMap标准操作(放置,获取)的构造Singelton.getInstance(),可以基于预定义的(键,值)在任何类中添加或重试任何值。

Maybe this example could be useful. 也许这个例子可能有用。

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";
    }
}
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM