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);
}
}
}