SmartQuant Discussion

Automated Quantitative Strategy Development, SmartQuant Product Discussion and Technical Support Forums
It is currently Sat Sep 26, 2020 10:50 am

All times are UTC + 3 hours




Post new topic Reply to topic  [ 5 posts ] 
Author Message
PostPosted: Mon Oct 15, 2012 5:29 pm 
Offline

Joined: Tue Sep 11, 2012 12:41 pm
Posts: 41
Hi,

I have a very basic question regarding the use of user defined indicators:

I defined a indicator: Lowest Close for the last N days. I can draw it in the instrument charts correctly. But I cannot access its data through the property 'Last' in my strategy. When I compile the code, it shows an error: an object reference is required for the non-static filed, method, or property 'OpenQuant.API.Indicator.Last.get'

Can anyone give me some hints on what's wrong with my codes?

I defined the indicator as:
Code:
using System;
using System.ComponentModel;
using System.Collections.Generic;
using System.Text;

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

namespace SampleIndicators
{
    public class UserLowestClose : UserIndicator
    {
        private int length;
        private BarData barData;

        [Category("Parameters"), Description("Length")]
        public int Length
        {
            get { return length; }
            set { length = value; }
        }

        public BarData BarData
        {
            get { return barData; }
            set { barData = value; }
        }

        public UserLowestClose(ISeries series, int length, BarData barData)
            : base(series)
        {
            this.length = length;
            this.barData = barData;

            this.Name = "User Lowest Close, Length = " + length;
        }

        public UserLowestClose(ISeries series, int length)
            : this(series, length, BarData.Close)
        {
        }

        public override double Calculate(int index)
        {
            if (index >= length - 1)
            {
                double lowNew = Input[index, barData];

                // calculate low
                for (int i = index; i >= index - length + 1; i--)
                {
                    if (Input[i, barData] < lowNew)
                        lowNew = Input[i, barData];
                }
                return lowNew;
            }
            else
                return double.NaN;
        }

    }
}


Thank you very much for your help in advance!


Top
 Profile  
 
PostPosted: Mon Oct 15, 2012 6:35 pm 
Offline

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

what do you do to access indicator Last property? Can you show us a code sample?

Regards,
Anton


Top
 Profile  
 
PostPosted: Mon Oct 15, 2012 7:05 pm 
Offline

Joined: Tue Sep 11, 2012 12:41 pm
Posts: 41
Yes, sure.

I want to back test a simple double 7 strategy:

Code:
using System;
using System.Drawing;

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

public class MyStrategy : Strategy
{   [Parameter ("Buy Look Back","Setup")]
   int Length1 = 7;
   
   [Parameter ("Sell Look Back","Setup")]
   int Length2 = 7;
   
   [Parameter ("Length of MA","Setup")]
   int MALength = 50;
   
   SMA sma;
   int countIn;
   int countOut;
   double preClose;
   double lowForSeveralDays;
   int indexForLow;
   double highForSeveralDays;
   int indexForHigh;
   int daysPassed;
   
   Order buyStopOrder;
   Order sellStopOrder;
   
   int openedPosition;
   
   public override void OnStrategyStart()
   {
      sma = new SMA(Bars,MALength,Color.Yellow);
      Draw (sma,0);
      UserLowestClose lowestClose = new UserLowestClose(Bars,Length1);
      Draw(lowestClose,2);
      UserHighestClose highestClose = new UserHighestClose(Bars,Length2);
      Draw(highestClose,2);
      openedPosition = 0;
   }

   public override void OnBar(Bar bar)
   {   if (sma.Count == 0)
         return;
      
      if (!HasPosition)
      {
         if (buyStopOrder != null)
         {
            buyStopOrder.Cancel();
            sendBuyStopOrder (bar.Close);
         }
         else
            if ((bar.Close == UserLowestClose.Last) && (bar.Close>sma.Last))
               sendBuyStopOrder (bar.Close);
      }
      else
      {
         if (sellStopOrder!=null)
         {   
            sellStopOrder.Cancel();
            sendSellStopOrder(bar.Close);
         }
         else
            if (bar.Close == UserHighestClose.Last)
               sendSellStopOrder(bar.Close);
      }
   
   }
   
   private void sendBuyStopOrder(double lastprice)
   {
      buyStopOrder = BuyStopOrder(Math.Floor(50000/lastprice),lastprice+0.1,"Entry");
      buyStopOrder.Send();
   }
   private void sendSellStopOrder(double lastprice)
   {
      sellStopOrder = SellStopOrder(Position.Qty,lastprice-0.1,"Exit");
      sellStopOrder.Send();
   }
   
}



So here I am using these UserLowestClose/UserHighestClose indicator to judge whether today's close is 7 day high or 7 day low. But I can't use .Last property as it returns error I described before: "An object reference is required for the non-static field, method, or property'OpenQuant.API.Indicator.Last.get' "


Top
 Profile  
 
PostPosted: Mon Oct 15, 2012 7:54 pm 
Offline

Joined: Tue Aug 05, 2003 3:43 pm
Posts: 6817
You define UserLowestClose lowestClose local variable in OnStrategyStart method and use it in OnBar method. This can not work. Moreover, you try to get Last from a class, not from a variable in OnBar.

Please check your code.

Regards,
Anton


Top
 Profile  
 
PostPosted: Mon Oct 15, 2012 8:16 pm 
Offline

Joined: Tue Sep 11, 2012 12:41 pm
Posts: 41
Dr. Anton Fokin wrote:
You define UserLowestClose lowestClose local variable in OnStrategyStart method and use it in OnBar method. This can not work. Moreover, you try to get Last from a class, not from a variable in OnBar.

Please check your code.

Regards,
Anton


Thank you so much, Anton. I modified the code and it works now. I appreciate a lot for your help!


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 5 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