SmartQuant Discussion

Automated Quantitative Strategy Development, SmartQuant Product Discussion and Technical Support Forums
It is currently Thu Oct 01, 2020 11:45 am

All times are UTC + 3 hours




Post new topic Reply to topic  [ 4 posts ] 
Author Message
 Post subject: error on DPO indicator
PostPosted: Mon Dec 31, 2012 4:31 am 
Offline

Joined: Fri Sep 28, 2012 8:16 am
Posts: 144
Formula:
DPO = Price – SMA of (( N / 2 ) + 1 )

test code:
Code:
public class MyStrategy : Strategy
{
   DPO dpo;
   SMA sma;
   
   int length = 14;
   
   public override void OnStrategyStart()
   {
      dpo = new DPO(Bars,length,BarData.Close);
      sma = new SMA(Bars,length,BarData.Close);
      
      Draw(dpo,2);
      Draw(sma,0);
   }

   public override void OnBar(Bar bar)
   {
      if(dpo.Count<1)
         return;
      if(sma.Count<length)
         return;
      
      double d1 = bar.Close - sma.Ago(length/2);
      double d2 = bar.Close - sma.Ago(length/2+1);
      Console.WriteLine("{0},DPO:{1},{2},{3}",
         bar.DateTime,dpo.Last,d1,d2);
   }
}


output:
Quote:
6/4/2012 12:00:00 AM,DPO:-115.36,-115.36,-124.08
6/5/2012 12:00:00 AM,DPO:-94.5066666666667,-94.5066666666667,-106.56
6/6/2012 12:00:00 AM,DPO:-85.2133333333336,-85.2133333333331,-95.5066666666667
6/7/2012 12:00:00 AM,DPO:-94.7066666666669,-94.7066666666665,-101.213333333333
6/8/2012 12:00:00 AM,DPO:-107.253333333334,-107.253333333333,-112.106666666667


DPO == d1 in OpenQuant,but the d2 is the right result.


Top
 Profile  
 
PostPosted: Mon Dec 31, 2012 10:39 am 
Offline

Joined: Tue Aug 05, 2003 3:43 pm
Posts: 6817
Hi,

I've checked implementation of DPO and it looks like

Code:
      
                public static double Value(TimeSeries input, int index, int length, BarData option)
      {
         if (index >= length / 2 + length - 1 + input.FirstIndex)
         {
            double price = input[index, option];
            
            double sum = 0;

            for(int i = index - length / 2; i > index - length - length / 2; i--)
               sum += input[i, option];

            sum /= length;
            
            double DPO = price - sum;

            return  DPO;
         }
         else
            return double.NaN;
      }


It might be so that = is missing in the for loop. Am I right?

Regards,
Anton


Top
 Profile  
 
PostPosted: Mon Dec 31, 2012 3:56 pm 
Offline

Joined: Fri Sep 28, 2012 8:16 am
Posts: 144
Here is my code:
Code:
using System;

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

class DPO1: UserIndicator
{
   private BarData option;
   private int length;

   public DPO1(ISeries series, int length, BarData barData)
      : base(series)
   {
      this.length = length;
      this.option = barData;
      this.Name = "DPO";
   }

   public override double Calculate(int index)
   {
      //if (index >= length / 2 + length - 1)
      if (index > length / 2 + length - 1)
      {
         double price = Input[index, option];
           
         double sum = 0;

         //for(int i = index - length / 2; i >index - length - length / 2; i--)
         for(int i = index - length / 2 - 1; i >= index - length - length / 2; i--)
            sum += Input[i, option];

         sum /= length;
           
         double DPO = price - sum;

         return  DPO;
      }
      else
         return double.NaN;
   }
}

public class MyStrategy : Strategy
{
   DPO1 dpo;
   SMA sma;
   
   int length = 14;
   
   public override void OnStrategyStart()
   {
      dpo = new DPO1(Bars,length,BarData.Close);
      sma = new SMA(Bars,length,BarData.Close);
     
      Draw(dpo,2);
      Draw(sma,0);
   }

   public override void OnBar(Bar bar)
   {
      if(dpo.Count<1)
         return;
      if(sma.Count<length)
         return;
     
      double d1 = bar.Close - sma.Ago(length/2);
      double d2 = bar.Close - sma.Ago(length/2+1);
      Console.WriteLine("{0},DPO:{1},{2},{3}",
         bar.DateTime,dpo.Last,d1,d2);
   }
}



Top
 Profile  
 
PostPosted: Mon Dec 31, 2012 5:47 pm 
Offline

Joined: Tue Aug 05, 2003 3:43 pm
Posts: 6817
OK, thanks. We are going to have a long chain of New Year holidays (7th of January is Christmas in Russia), so that we will be back to office on 9th only. Although we can fix the code base before 9th, we will have no chance to release an update. I guess you or your customers can use your custom indicator code meanwhile.

Regards,
Anton


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 5 guests


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