3

I'm not quite sure how to ask my question in C# terms, so please bear with the long-winded explanation.

I'm writing a stock trading algorithm. When the algo starts, it checks to see what kind of instrument it is applied to (in this case, either stock or futures), and then depending on the instrument, assigns a value to "double x".

If its a future instrument, then the assignment is a simple, flat value (in this case, "double x = 5;). However, if its a stock, I'd like "x" to be assigned to a value from another object - lets call the object "Algo2" and the value "y". So, in my script the assignment is as follows: "double x = Algo2.y" (note: that's the convention in the editor I'm using). This block of code is run only once when the algorithm begins.

What I'm trying to achieve here is to tell my algorithm to get the latest value of "Algo2.y" whenever "x" is used in a formula such as "EntryValue = Price + x". However, whats happening is that "x" is permanently assigned the value of "Algo2.y" at the start of the program, and since that block is never run again, remains that constant value throughout.

Can anyone help with the syntax so that instead of assigning a value to "x", it simply points to get the latest value of "Algo2.y" whevever it's called?

Thanks!

3
  • you can use a list to store all the y values get the latest y value from the list and make it Algo2.y Commented Apr 18, 2012 at 21:43
  • It sounds as if you need to use delegates to wire up your algorithms. I don't have time for a proper answer, but I'm sure someone can oblige. Commented Apr 18, 2012 at 21:45
  • Thanks Robert. Now that you mention it, I vaguely recall reading about delegates doing what I'm trying to achieve. I ended up using the Method solution suggested by another poster below, for the sake of simplicity, but delegates will be next on my list to learn! Commented Apr 18, 2012 at 21:56

8 Answers 8

2

Make 'x' a property, so that it fetches the value each time you ask for 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;
    }
  }
}
Sign up to request clarification or add additional context in comments.

Comments

1

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();

To update X.

2 Comments

Umm, how is this different from just assigning x = Algo2.y all the time? Without a function?
@Shedal I know, it isn't, but in case if he decides at a later time that the value of Algo2.y should be changed when retrieved, he'll be off a lot easier with changing my method rather than 10 statements wouldn't he? :)
1

I would use a method to return your latest value

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

This is quite hard coded, but it could be cleaned up using delegates and encapsulation of the algorithms, but at a low level - this is the idea. Also, some people prefer to use properties for this - Just don't use properties when the get has modifying affects

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 Comment

Thanks! I went with the Function, although its good to know I could use Properties as well.
1

May use something like:

double X {
  get { 
        if(isStock()) 
           return Algo2.y; 
        else 
           return 5;
  }
}

Comments

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();

10 Comments

totally over-complicating the problem.
@Servy no more than creating a class and a property just for that purpose.
You don't need a new class, just a property/method. You'd need all of that too. You can't just have a Func lying around like this, and there would need to be a method called to set it initially.
@Servy looks like Nicholas N is totally procedure-minded, so he's writing everything in one method ;-) Anyway, I don't see how my solution is more complicated than yours. Maybe it's just unusual for you.
You're creating two methods, each of which return a single value, and returning a pointer to one of those methods, rather than just having a single method that returns the correct value. As I said, you'll still need to have a method to assign your Func, and a way to expose getX to whoever needs it, each of which, independently, is practically the same amount of work as most of the other answers provided. The only advantage of your solution is that it would be more suitable for a program in which it fits entirely in Main without ever calling out to any other (non-anonomous) methods.
|
0

Give Algo2 a reference to Algo so that no 'double X' copy is needed. Algo can then dereference the actual value in Algo2 at any time, (thread-safety an issue?).

Comments

0

Value data types, such as int are always going to be copied by value, not as a reference. However, what you can do is architect your solution a little differently, and then it will provide the right value. For example:

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

Then, you replace your x with the ValueContainer instance wherever needed and use the CurrentValue property to get the value. You create each version with a different constructor then:

 ValueContainer container = null;

 if(stock)
    container = new ValueContainer(5);
 else
    container = new ValueContainer(Algo2);

Comments

0

What you need is a property wrapper for x to control the value that's returned, based on the instrument type. Here's an example, which will require some significant adaptation for your app.

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.