SmartQuant Discussion
http://www.smartquant.com/forums/

How to move common strategies functions to the base Strategy
http://www.smartquant.com/forums/viewtopic.php?f=64&t=9099
Page 1 of 1

Author:  Baraz Sergey [ Tue May 03, 2011 3:17 pm ]
Post subject:  How to move common strategies functions to the base Strategy

If several strategies use the same functionality - it is good idea to move this functionality to the "base" Strategy and inherit other strategies from the "base" one. The example below shows how to create "Report" method, that simply outputs some text to Console, in the base strategy and use it later in derived strategies. It also shows how to implement "default" handlers like OnBar, OnQuote etc. The base MyStrategy1 class implements OnBar method, which can be overriden in derived strategies.

To create the base strategy - create a Class Library in Visual Studio .NET and add the following code to it:
Code:
using System;

using OpenQuant.API;

namespace MyStrategyLib
{
   public class MyStrategy1 : Strategy
   {
      public override void OnBar(Bar bar)
      {
         Report(Bar.ToString());
      }

      public void Report(string text)
      {
         Console.WriteLine(text);
      }
   }
}


Build the project and add a reference to this project output dll from OQ.

Then create a new project and paste the following code in the code.cs file:
Code:
using System;
using System.Drawing;

using OpenQuant.API;
using OpenQuant.API.Indicators;

using MyStrategyLib;

public class MyDerivedStrategy : MyStrategy1
{
   public override void OnBar(Bar bar)
   {
      Buy(100);
      
      base.OnBar(bar);   
   }

   public override void OnPositionChanged()
   {
      Report("Position Qty " + Position.Qty);
   }
}


The strategy MyDerivedStrategy derives from base MyStrategy1 class. It overrides the OnBar method declated in MyStrategy1 class, so the MyStrategy1.OnBar method is called only because of the "base.OnBar(bar);" line. MyDerivedStrategy.OnPositionChanged handler code also calls Report method declared in MyStrategy1.

Page 1 of 1 All times are UTC + 3 hours
Powered by phpBB® Forum Software © phpBB Group
https://www.phpbb.com/