C # Assigning a variable from another object

I'm not quite sure how to ask my question in C # terms, so please carry with a long explanation.

I am writing a stock trading algorithm. When the algorithm starts, it checks to which instrument it is applied (in this case, to stock or futures), and then assigns the value "double x" depending on the instrument.

If this is a future tool, then the assignment is a simple, flat value (in this case, “double x = 5;). However, if its margin, I would like the“ x ”to be assigned a value from another object - allows you to call the object“ Algo2 " and the value is “y.” Thus, in my script, the assignment is as follows: “double x = Algo2.y” (note: the convention in the editor I'm using). This block of code runs only once when the algorithm starts .

What I'm trying to achieve here is to tell my algorithm to get the last value of "Algo2.y" whenever "x" is used in a formula such as "EntryValue = Price + x". However, it happens that "x" is constantly assigned the value "Algo2.y" at the beginning of the program, and since this block never starts again, it remains unchanged.

Can someone help with the syntax so that instead of assigning the value "x", he would simply point to the last value "Algo2.y" if he would call?

Thank!

+3
source share
8 answers

Write a function for it:

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

In your main algorithm, now call:

x = getAlgo2YValue();

Refresh X.

+1
source

'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;
    }
  }
}
+2

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");
  }
}
+1

- :

double X {
  get { 
        if(isStock()) 
           return Algo2.y; 
        else 
           return 5;
  }
}
+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 a stock.
int xval = getX();
+1

Algo2 Algo, "double X". Algo Algo2 ( - ?).

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);
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 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
      }
   }
}
0

All Articles