3

我不太确定如何用 C# 术语提出我的问题,所以请耐心等待冗长的解释。

我正在写一个股票交易算法。当算法启动时,它会检查它适用于哪种工具(在这种情况下,股票或期货),然后根据工具,为“double x”分配一个值。

如果它是未来的工具,那么分配是一个简单的平面值(在这种情况下,“double x = 5;)。但是,如果它是股票,我希望将“x”分配给另一个值对象 - 让我们调用对象“Algo2”和值“y”。因此,在我的脚本中,赋值如下:“double x = Algo2.y”(注意:这是我正在使用的编辑器中的约定)。此代码块仅在算法开始时运行一次。

我在这里想要实现的是告诉我的算法在“EntryValue = Price + x”等公式中使用“x”时获取“Algo2.y”的最新值。然而,发生的事情是“x”在程序开始时被永久分配了“Algo2.y”的值,并且由于该块永远不会再次运行,因此始终保持该恒定值。

任何人都可以帮助使用语法,而不是为“x”分配一个值,它只是指向获取“Algo2.y”的最新值,无论它被调用吗?

谢谢!

4

8 回答 8

2

使“x”成为一个属性,以便每次请求 x 时它都会获取值。

class StockInstrument
{
  public double Value //x isn't a good name, I'll use "Value"
  {
    get
    {
      if(...) return 5.0;
      else return Algo2.y;
    }
  }
}
于 2012-04-18T21:44:33.753 回答
1

我会使用一种方法来返回您的最新值

public double GetXValue()
{
  if (AlgoType == Algos.Futures)
  {
    return 5.0;
  }
  else if (AlgoType == Algos.Stock)
  {
    return Algo2.y;
  }
  //else
  throw new Exception("unknown algo type");
}

这是相当硬编码的,但可以使用委托和算法封装来清理它,但在低级别 - 这就是想法。此外,有些人更喜欢为此使用属性 - 只是在get具有修改影响时不要使用属性

public double X
{
  get
  {
    if (AlgoType == Algos.Futures)
    {
      return 5.0;
    }
    else if (AlgoType == Algos.Stock)
    {
      return Algo2.y;
    }
    //else
    throw new Exception("unknown algo type");
  }
}
于 2012-04-18T21:44:25.723 回答
1

可以使用类似的东西:

double X {
  get { 
        if(isStock()) 
           return Algo2.y; 
        else 
           return 5;
  }
}
于 2012-04-18T21:44:49.730 回答
1

为它写一个函数:

double getAlgo2YValue()
{
    return Algo2.y; // or Algo2.getY(), another function if you can't access it
}

在您的主要算法中,现在调用:

x = getAlgo2YValue();

更新 X。

于 2012-04-18T21:44:53.663 回答
1
Func<int> getX;

if(isFuture)
    getX = () => 5;
else
    getX = () => Algo.y;

// using getX() will always return the current value of Algo.y,
// in case it's a stock.
int xval = getX();
于 2012-04-18T21:47:58.607 回答
0

您需要的是一个属性包装器,用于x根据仪器类型控制返回的值。这是一个示例,它需要对您的应用程序进行一些重大调整。

public class Instrument
{
   // an example enum holding types
   public InstrumentType Type {get; set;}

   // x is not a great name, but following your question's convention...
   public double X
   {
      get
      {
         if(type == InstrumentType.Stock)
            return Algo2.y();
            // note that I changed this to be a method rather than a property
            // Algo2.y() should be static so it can be called without an instance
         else if(type == InstrumentType.Future)
            return 5.0;
         else 
            // return some default value here
      }
   }
}
于 2012-04-18T21:51:49.850 回答
0

给 Algo2 一个对 Algo 的引用,这样就不需要“双 X”副本。然后,Algo 可以随时取消引用 Algo2 中的实际值(线程安全问题?)。

于 2012-04-18T21:45:37.180 回答
0

值数据类型,例如int总是按值复制,而不是作为引用。但是,您可以做的是稍微不同地构建您的解决方案,然后它将提供正确的价值。例如:

 public class ValueContainer
 {
      protected Algo2 _reference = null;
      protected double _staticValue = 0;

      public double CurrentValue
      {
          get
          {
              if(_reference == null)
                 return _staticValue;

              return _reference.y;
          }
      }

      public ValueContainer(Algo2 reference)
      {
           _reference = reference;
      }

      public ValueContainer(double value)
      {
           _staticValue = value;
      }
 }

x然后,在需要的地方用ValueContainer实例替换你的,并使用CurrentValue属性来获取值。然后使用不同的构造函数创建每个版本:

 ValueContainer container = null;

 if(stock)
    container = new ValueContainer(5);
 else
    container = new ValueContainer(Algo2);
于 2012-04-18T21:45:39.637 回答