SmartQuant Discussion

Automated Quantitative Strategy Development, SmartQuant Product Discussion and Technical Support Forums
It is currently Mon Jul 23, 2018 1:11 am

All times are UTC + 3 hours




Post new topic Reply to topic  [ 9 posts ] 
Author Message
PostPosted: Sun Jan 23, 2011 12:47 pm 
Offline

Joined: Sat Jan 22, 2011 1:40 am
Posts: 73
Hello,

I modified OpenQuant strategy "5% Down-In-One-Day Panic Recovery" and
put comments around my code, just added 3 lines added and commented next line.

I specified 1 min bar in Market Data and want to use 15 min bar as well. The program stopped working and it complains about bigBars.Ago(1). What is wrong?
Before the change everything was OK.


There is the code:
[Parameter("Buy when a stock drops this number of percents in one day")]
double Percent = 0.5;

[Parameter("Order quantity (number of contracts to trade)")]
double Qty = 100;

Order buyOrder;

public override void OnBar(Bar bar)
{
// if we do not have a position, update the limit buy order to be 5% above today's close
if (!HasPosition)
{
// cancel the old limit order (it's out of date now)
if (buyOrder != null)
buyOrder.Cancel();

// issue a new buy order at 5% below today's close this order will execute tomorrow
// if price is matched
//My code
BarSeries bigBars = GetBars(15*60); //my code
double openPrice = bigBars.Ago(1).Close;//my code
double buy_price = openPrice* (1 - (Percent / 100)); //my code
//End of my code
//double buy_price = bar.Open * (1 - (Percent / 100)); //OQ code

buyOrder = BuyLimitOrder(Qty, buy_price, "Entry");
buyOrder.Send();
}

// else we opened a position today using our limit order from yesterday, so now close
// the position at the end of today. We expect that such a big drop was freaky, and that
// prices recovered during the day. If not, this order stops further losses.
else
Sell(Qty, "Exit");
}

public override void OnError(ProviderError error)
{
Console.WriteLine(error.Message);
}


Thank you very much,
Paul.


Top
 Profile  
 
PostPosted: Sun Jan 23, 2011 1:59 pm 
Offline

Joined: Tue Aug 05, 2003 3:43 pm
Posts: 6807
When you get the very first bar in OnBar, there is no Ago(1) bar yet.

Regards,
Anton


Top
 Profile  
 
PostPosted: Sun Jan 23, 2011 9:08 pm 
Offline

Joined: Sat Jan 22, 2011 1:40 am
Posts: 73
Anton,

Thank you very much for your reply. It means that I have to take care about initial start. Is there any setting in OpenQuant to do it automatically?

Regards,
Paul


Top
 Profile  
 
PostPosted: Sun Jan 23, 2011 9:19 pm 
Offline

Joined: Tue Aug 05, 2003 3:43 pm
Posts: 6807
I think you should simply check that you have enough bars in the bar series to get ago(n) bar.

if (Bars.Count > n)
{
Bar n_bars_ago_bar = Bars.Ago(n);

...
}

You shoud do the same if you want to use indicators, for exampe if you use SMA(14), you should check that you have 14 bars in the bar series.

Regards,
Anton


Top
 Profile  
 
PostPosted: Sun Jan 23, 2011 11:16 pm 
Offline

Joined: Sat Jan 22, 2011 1:40 am
Posts: 73
Anton,

Thank you very much,

I modified my code according to your suggestion and it does not work.

There is my code:
Code:
         BarSeries bigBars = GetBars(2*this.Bar.Size);  //my code
         if(   this.Bars.Count >10 )
         {
            double openPrice = bigBars.Ago(1).Close;//my code
            //         double buy_price = bar.Open * (1 - (Percent / 100)); //OQ code
            double buy_price = openPrice* (1 - (Percent / 100)); //my code

            buyOrder = BuyLimitOrder(Qty, buy_price, "Entry");
            buyOrder.Send();
         }


Everything is working if I use BarSeries bigBars = GetBars(this.Bar.Size);

What I did wrong and what I have to change?

Thank you very much,
Paul.


Top
 Profile  
 
PostPosted: Sun Jan 23, 2011 11:50 pm 
Offline

Joined: Tue Aug 05, 2003 3:43 pm
Posts: 6807
And what doesn't work exactly?


Top
 Profile  
 
PostPosted: Mon Jan 24, 2011 12:03 am 
Offline

Joined: Sat Jan 22, 2011 1:40 am
Posts: 73
I have a Strategy error:

Can not return bar 1 bars ago: bar series is too short.

There is a call stack:

System.ArgumentException: Can not return bar 1 bars ago: bar series is too short.
at SmartQuant.Series.BarSeries.Ago(Int32 n)
at OpenQuant.API.BarSeries.Ago(Int32 n)
at MyStrategy.OnBar(Bar bar) in c:\Users\Paul\Documents\OpenQuant\Projects\MyTest2\code.cs:line 33
at OpenQuant.Trading.StrategyRunner.OnNewBar(Instrument instrument, Bar bar)
at OpenQuant.Trading.StrategyRunner.SetNewBarSlice(Int64 barSize)

I have enough data for 1 min and 15 min bars. I tried 2 min as well and I do not have data for 2 min. The error message was the same.

Paul.


Top
 Profile  
 
PostPosted: Mon Jan 24, 2011 12:39 am 
Offline

Joined: Tue Aug 05, 2003 3:43 pm
Posts: 6807
Bars and BigBars are two different series. BigBars series doesn't have a bar when there already are several bars in Bars. Thus you should use BigBars.Count .

Regards,
Anton


Top
 Profile  
 
PostPosted: Mon Jan 24, 2011 1:31 am 
Offline

Joined: Sat Jan 22, 2011 1:40 am
Posts: 73
I tried to use bigBars.Count >2 and it did not work as well. I printed bigBars.Count and I noticed that I always have 0.
Then I added


Code:
   public override void OnStrategyStart()
   {
      bigBars = GetBars(15*this.Bar.Size); 
      DateTime datetime2 = DateTime.Now;
      DateTime datetime1 = datetime2.AddDays(-4);
      foreach (Bar bar in GetHistoricalBars(datetime1, datetime2, BarType.Time, 15*60))
         bigBars.Add(bar); 
   }

Now it works,
Thank you very much,
Paul.


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