SmartQuant Discussion
http://www.smartquant.com/forums/

Multiple bar sizes in Strategy, BarSeries, Charts
http://www.smartquant.com/forums/viewtopic.php?f=60&t=5994
Page 1 of 1

Author:  jherr [ Tue Oct 16, 2007 9:36 am ]
Post subject:  Multiple bar sizes in Strategy, BarSeries, Charts

I'm trying to draw quote info such as the highest Quote.Ask for each bar on each bar-size/chart in a strategy (without knowing the number or size of bars ahead of time).

I built a 1 second BarSeries to hold the Open, High, Low, Close for quoteAsk. I used BarSeries.Compress in OnBar to make the larger barseries for the charts. An SMA of length 1 is drawn on the charts. It's very slow.

I'm having a hard time synchronizing my quotes-barseries to the charts. Values from the larger barsize chart keep appearing on the smaller barsize chart. There's no way to issue separate Draw commands for each bar size so I'm kind of stuck.

How do you create a BarSeries that can be used to draw on each bar-size chart in the strategy? I must be doing something wrong because it shouldn't be this hard.

[Update]
The issue where data from the larger bar size was showing up on the smaller bar size chart went away. It must have been the way I was adding values to the series.

The main issue still remaining is that the Draw command draws on the chart of every barsize. There seems to be no way to restrict the Draw to a single barsize chart. This is a big limitation for me.

Please confirm if this is a bug or a design limitation. Any chance this will be improved soon?

Charting and analyzing multiple timeframes would also be easier if the number of bar sizes was available in OnStrategyStart.

Why doesn't each bar size have it's own instance of strategy class? I find myself working extra hard to keep any variables or series separate by barsize.

Author:  jherr [ Wed Oct 17, 2007 1:28 am ]
Post subject: 

Here's my code to draw the high ask and low bid on the charts. The problem is the 1 minute lines are useless on the 6 second chart and the 6 second lines are useless on the 1 minute chart. How do you get it to draw just one set of lines per chart at the right bar size?



Code:

//Add 6 second bars and 1 minute bars to the strategy

using System;
using System.Drawing;
using OpenQuant.API;
using OpenQuant.API.Indicators;

public class MyStrategy : Strategy
{
   BarSeries bidBars;
   BarSeries askBars;
    TimeSeries chartBid6;
    TimeSeries chartAsk6;
    TimeSeries chartBid60;
    TimeSeries chartAsk60;
   
   public override void OnStrategyStart()
   {

        askBars = new BarSeries("askBars");
        bidBars = new BarSeries("bidBars");

        chartAsk6 = new TimeSeries("askBars", Color.Cyan);
        chartBid6 = new TimeSeries("bidBars", Color.Pink);
        chartAsk60 = new TimeSeries("askBars", Color.Cyan);
        chartBid60 = new TimeSeries("bidBars", Color.Pink);

      Draw(chartAsk6, 0);
      Draw(chartBid6, 0);
      Draw(chartAsk60, 0);
      Draw(chartBid60, 0);

    }
    public override void OnBar(Bar bar)
    {
        if (askBars.Count > 0)
        {
            if (bar.Size == 6)
            {
                chartAsk6.Add(bar.BeginTime, askBars.HighestHigh(bar.BeginTime, bar.EndTime));
            }
            else
            {
                chartAsk60.Add(bar.BeginTime, askBars.HighestHigh(bar.BeginTime, bar.EndTime));
            }
        }
        if (bidBars.Count > 0)
        {
            if (bar.Size == 6)
            {
                chartBid6.Add(bar.BeginTime, bidBars.LowestLow(bar.BeginTime, bar.EndTime));
            }
            else
            {
                chartBid60.Add(bar.BeginTime, bidBars.LowestLow(bar.BeginTime, bar.EndTime));
            }

        }
    }
   public override void OnQuote(Quote quote)
   {
        if (Bars.Count > 0)
        {
            BuildTimeBars(1, quote.DateTime, quote.Ask, quote.AskSize, askBars);
            BuildTimeBars(1, quote.DateTime, quote.Bid, quote.BidSize, bidBars);
        }
    }


    public static void BuildTimeBars(long barSize, DateTime time, double price, long volume, BarSeries bars)
    {
        if (barSize < 1)
        {
            throw (new Exception("barSize must be > 0"));
        }

        DateTime nextBarEndTime = new DateTime();
        if (bars.Count > 0)
        {   //calculate the end time of the last bar
            long lastBarEndInSeconds = bars.Last.EndTime.Ticks / 10000000;
            long nextBarEndInSeconds = ((lastBarEndInSeconds + barSize) / barSize) * barSize;
            nextBarEndTime = nextBarEndTime.AddSeconds(nextBarEndInSeconds);
        }
        if (time < nextBarEndTime)
        {    //merge bar into previous bar
            bars.Add(BarType.Time, barSize, bars.Last.BeginTime, time,
                bars.Last.Open,
                Math.Max(bars.Last.High, price),
                Math.Min(bars.Last.Low, price),
                price,
                volume,
                0);  //Replaces the last bar
        }
        else
        {
            //Add new bar
            bars.Add(BarType.Time, barSize,
                time, time,
                price, price, price, price,
                volume, 0);
            //OnBarOpen(bars.Last);
        }
    }



}


Attachments:
1min.jpg
1min.jpg [ 56.98 KiB | Viewed 3930 times ]
6sec.jpg
6sec.jpg [ 50.94 KiB | Viewed 3930 times ]

Author:  Baraz Sergey [ Wed Oct 17, 2007 4:37 pm ]
Post subject: 

Hi,

It is not possible to do it in the current version. It is a useful feature so we will add in a next version.

Regards,
Sergey.

Author:  jherr [ Wed Oct 17, 2007 5:23 pm ]
Post subject: 

Baraz Sergey wrote:
Hi,

It is not possible to do it in the current version. It is a useful feature so we will add in a next version.

Regards,
Sergey.


Thanks Sergey.

Please see my feature request if you havn't already seen it:
http://www.smartquant.com/forums/viewtopic.php?t=5996

-Jeremy

Page 1 of 1 All times are UTC + 3 hours
Powered by phpBB® Forum Software © phpBB Group
https://www.phpbb.com/