如何在不同功能中重用价值? [英] How to re-use value in different functions?

查看:86
本文介绍了如何在不同功能中重用价值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用MotiveWave编写研究程序,该程序用于(日间)交易.该研究是其自己的班级. (有关MotiveWave SDK的信息,请参见: https://www.motivewave.com/sdk/javadoc/overview-summary.html )

I am programming a Study in MotiveWave, a program used for (day)trading. The study is its own class. (info about MotiveWave's SDK found here: https://www.motivewave.com/sdk/javadoc/overview-summary.html)

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

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

My study uses 2 different timeframes: the 1 hour and the 4 hour bars. Both are calculated in a different function. Otherwise formulated: both use a different dataseries, as shown in the code below.

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

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小时时间范围内的代码.它只计算2个移动平均线

This is the code for the 4 hour timeframe. It simply calculates 2 moving averages

 @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':

I would now like to use those 2 values, 'ma9' and 'ma11' in another function, on the 1 hour timeframe:

 @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,使它们成为全局"文件,并可以在其他功能中重复使用它们?

How can I export the ma9 and the ma11 so they become 'global' and I can re-use them in this other function ?

推荐答案

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

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). 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).

也许这个例子可能有用.

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

这篇关于如何在不同功能中重用价值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆