SmartQuant Discussion

Automated Quantitative Strategy Development, SmartQuant Product Discussion and Technical Support Forums
It is currently Wed Sep 18, 2019 12:17 pm

All times are UTC + 3 hours




Post new topic Reply to topic  [ 4 posts ] 
Author Message
PostPosted: Mon Oct 19, 2009 6:23 pm 
Offline

Joined: Wed Oct 07, 2009 9:28 pm
Posts: 5
Location: Germany
I would like to create a new Indicator from two existing ones.
For example, I would like to build a sum by adding two indicators (see code below).
Having more than one ISeries as input is a problem because only one of them can be set as base input for the UserIndicator. UserIndicator will have its Calculate() method called for each change in its base input. However, the second ISeries serving as input will not notify UserIndicator of its change.

I would like to know how I can notify UserIndicator of a change in a second input ISeries. Is that possible?


Code:
public class Addition: UserIndicator {
   
    private ISeries summand2;
   
    public Addition(String name, ISeries summand1, ISeries summand2) : base(summand1) {
        this.Name = name;
        this.summand2 = summand2;
    }

    public override double Calculate(int index) {
        if ((index >= 0) && (Input.Count > index) && (summand2.Count > index)) {
            return Input[index, BarData.Close] + summand2[index, BarData.Close];
        }
        else {
            return Double.NaN;     
        }
    }
   
    public ISeries getSummand1() {
        return Input;
    }
   
    public ISeries getSummand2() {
        return summand2;
    }
   
}


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 20, 2009 3:30 pm 
Offline

Joined: Wed Oct 08, 2003 1:06 pm
Posts: 833
Hi,

Quote:
I would like to know how I can notify UserIndicator of a change in a second input ISeries. Is that possible?


This is not possible, though perhaps in your situation you don't need it.

Ususally it makes sense to summarize elements of indicators with the same timestamp. In this case you have to be sure that the Calculate method is called when both indicators have been updated. All you need is to detect which series gets updated the second and use it as the Input series.

Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using OpenQuant.API;
using OpenQuant.API.Plugins;

namespace SumOfIndicators
{
   public class Sum : UserIndicator
   {
      Indicator indicator1;
      Indicator indicator2;

      public Sum(ISeries series1, ISeries series2)
         : base (series1)
      {
         indicator1 = series1 as Indicator;
         indicator2 = series2 as Indicator;

         this.Name = "Sum of Two";
      }

      public override double Calculate(int index)
      {
         DateTime dateTime = indicator1.GetDateTime(index);

         return indicator1[dateTime] + indicator2[dateTime];
      }
   }
}


In your strategy code you have to make sure that the indicator passed as a series1 gets updated after the indicator passed as series2 in the Sum constructor.

Regards,
Sergey.


Top
 Profile  
 
 Post subject: Re:
PostPosted: Thu Feb 21, 2013 2:02 pm 
Offline

Joined: Wed Feb 20, 2013 12:15 pm
Posts: 1
Baraz Sergey wrote:
In your strategy code you have to make sure that the indicator passed as a series1 gets updated after the indicator passed as series2 in the Sum constructor.


How can one make sure of the order if that is handled internally by OQ? Of course, there are trivial cases, and you can also implement your own synchronization mechanism for the general case, and use an auxiliary ISeries as an input. But in order to avoid this overhead, it would be helpful if the OQ Development Team could provide some guarantee on the order in which the indicators are updated. Say, for instance, that all the indicators driven by the same ISeries are updated in the order in which they were constructed in the strategy, or whatever other rule applies.

Thanks


Top
 Profile  
 
PostPosted: Fri Feb 22, 2013 11:40 am 
Offline

Joined: Wed Oct 08, 2003 1:06 pm
Posts: 833
Hi,

Yes, all indicators that use same ISeries as the input are updated in the order in which they were constructed in the strategy.

Regards,
Sergey.


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

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