SmartQuant Discussion

Automated Quantitative Strategy Development, SmartQuant Product Discussion and Technical Support Forums
It is currently Thu Sep 19, 2019 7:19 am

All times are UTC + 3 hours




Post new topic Reply to topic  [ 1 post ] 
Author Message
PostPosted: Tue May 03, 2011 3:17 pm 
Offline

Joined: Wed Oct 08, 2003 1:06 pm
Posts: 833
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.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 1 post ] 

All times are UTC + 3 hours


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron
Powered by phpBB® Forum Software © phpBB Group