Построение баров из trades
C#
Создание собственных баров дает вам возможность применять настраиваемую фильтрацию.
Функция как альтернатива втроенной (BuildBarsFromTrades).
Код
using System;
using System.Drawing;
using OpenQuant.API;
using OpenQuant.API.Indicators;
public class MyStrategy : Strategy
{
public override void OnTrade(Trade trade)
{
if(trade.Price > 0 && trade.Size > 0) {
BuildBarsFromTrades(BarType.Time,60,Trades,Bars);
//BuildBarsFromTrades(BarType.Tick,100,Trades,Bars);
//BuildBarsFromTrades(BarType.Volume,100,Trades,Bars);
}
}
public void BuildBarsFromTrades(BarType barType, long barSize, TradeSeries trades, BarSeries bars)
{
switch(barType)
{
case BarType.Time:
BuildTimeBarsFromTrades(barSize,trades.Last,bars);
break;
case BarType.Tick:
BuildTickBarsFromTrades(barSize,trades,bars);
break;
case BarType.Volume:
BuildVolumeBarsFromTrades(barSize,trades.Last,bars);
break;
}
}
public void BuildTimeBarsFromTrades(long barSize, Trade trade, BarSeries bars)
{
if(barSize < 1){
throw(new Exception("barSize must be > 0"));
}
= new DateTime();
DateTime nextBarEndTime 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.AddSeconds(nextBarEndInSeconds);
nextBarEndTime }
if(trade.DateTime < nextBarEndTime)
{ //merge bar into previous bar
.Add(BarType.Time,barSize,bars.Last.BeginTime, trade.DateTime,
bars.Last.Open,
bars.Max(bars.Last.High,trade.Price),
Math.Min(bars.Last.Low,trade.Price),
Math.Price,
Trade.Last.Volume + trade.Size,
bars0); //Replaces the last bar
}else{
//Add new bar
.Add(BarType.Time,barSize,
bars.DateTime,trade.DateTime,
trade.Price,trade.Price,trade.Price,trade.Price,
trade.Size,0);
trade}
}
public void BuildTickBarsFromTrades(long barSize, TradeSeries trades, BarSeries bars)
{
= trades.Last;
Trade trade if(barSize < 1){
throw(new Exception("barSize must be > 0"));
}
//check if the last bar is full
if(bars.Count > 0 && (((trades.Count / barSize)*barSize)< trades.Count))
{ //merge bar into previous bar
.Add(BarType.Tick,barSize,bars.Last.BeginTime, trade.DateTime,
bars.Last.Open,
bars.Max(bars.Last.High,trade.Price),
Math.Min(bars.Last.Low,trade.Price),
Math.Price,
trade.Last.Volume + trade.Size,
bars0); //Replaces the last bar
}else{
//Add a small amount of time to the new bar
//new bar must be later than last bar or it will replace the last bar.
= trade.DateTime;
DateTime newTradeTime if(bars.Count > 0 && bars.Last.BeginTime >= trade.DateTime){
= bars.Last.BeginTime.AddTicks(1);
newTradeTime }else{
= trade.DateTime;
newTradeTime }
//Add new bar
.Add(BarType.Tick,barSize,
bars,newTradeTime,
newTradeTime.Price,trade.Price,trade.Price,trade.Price,
trade.Size,0);
trade}
}
public void BuildVolumeBarsFromTrades(long barSize, Trade trade, BarSeries bars)
{
if(barSize < 1){
throw(new Exception("barSize must be > 0"));
}
//make sure not to access bars.Last if there are no bars
//volumeRemaining is the unfilled portion of the last bar
= trade.DateTime;
DateTime newTradeTime long volumeRemaining = 0;
if(bars.Count > 0) {
//new trade must be later than previous bar or it won't add.
if(bars.Last.BeginTime >= trade.DateTime){
= bars.Last.BeginTime.AddTicks(1);
newTradeTime }
= barSize - bars.Last.Volume;
volumeRemaining }
//merge with last volume bar
long volumeAdd = 0;
if(volumeRemaining > 0){
= Math.Min(volumeRemaining,trade.Size);
volumeAdd .Add(BarType.Volume,barSize,bars.Last.BeginTime, trade.DateTime,
bars.Last.Open,
bars.Max(bars.Last.High,trade.Price),
Math.Min(bars.Last.Low,trade.Price),
Math.Price,
trade.Last.Volume + volumeAdd,
bars0); //Replaces the last bar
}
//deduct the volume that was just added from volumeRemaining
= trade.Size - volumeAdd;
volumeRemaining //add new volume bars until there is no more volumeRemaining
while(volumeRemaining > 0){
= Math.Min(volumeRemaining,barSize);
volumeAdd .Add(BarType.Volume,barSize,
bars,newTradeTime,
newTradeTime.Price,trade.Price,trade.Price,trade.Price,
trade,0);
volumeAdd= volumeRemaining - volumeAdd;
volumeRemaining //new trade must be later than previous bar or it won't add.
= bars.Last.BeginTime.AddTicks(1);
newTradeTime }
}
} //MyStrategy()