I delved into C# but came across a problem that I seem unable to solve:
I designed a financial back test and trading engine and as part of that I like to implement a "Strategy Container". This strategy container should serve two purposes. (1) hold instances of different strategies that perform mathematical computations, and (2) provide accessors in order to pass new tick data into the strategy instances and to receive certain information back from the strategy instances.
The different strategies I mentioned above should be unique classes which, however, derive from a base class. I want to perform completely different operations within those strategies thus I thought designing them as separate classes that derive from one base class would be feasible. One complication is that I need multiple instances of each strategy class that is contained in the Strategy Container. Each strategy class also holds a list of stock symbols and each symbol should get its on strategy instance.
Another important point of the Strategy container is that it can create, instantiate, and later also invoke each strategy instance.
I wanted to ask some of you what kind of ideas you have how I could go about designing and implementing this:
I do not ask for full source code but for ideas what you think how I should design this thing and how I could accomplish each of above points. This is something for myself and wont turn into any commercial product. I am very comfortable with writing code that implements the mathematical bits and pieces but I am less familiar with design patterns and system architecture.
Edit: Graymatter, I played a bit around and seems you provided exactly what I was looking for. Thanks a lot.
class Program
{
static void Main(string[] args)
{
List<IStrategy> container = new List<IStrategy>();
container.Add(new StrategyOne());
container[0].AddValue(50);
Console.ReadLine();
}
}
public interface IStrategy
{
void AddValue(int value);
}
public class StrategyOne : StrategyBase
{
public override void Calculates()
{
Console.WriteLine("This is my value: " + myValue);
}
}
public class StrategyBase : IStrategy
{
protected int myValue;
public void AddValue(int value)
{
Console.WriteLine("Run Strategy in Base");
myValue = value;
Calculates();
}
public virtual void Calculates()
{
}
}
You should really look at using interfaces for this type of system. The interface is your contract with the strategy and you can any number of different implementations for the interface. Your container would then contain interfaces.
Something like:
public interface IStrategy
{
void RunStrategy(Quote quote);
}
Then your implementations would be something like this:
public class StrategyOne : IStrategy
{
void RunStrategy(Quote quote)
{
}
}
The container could be:
List<IStrategy> container = new List<IStrategy>();