Back

Creating ticks from bars/Begnning/End of day execution

The following questions only apply to historical simulations.

Given that daily bar data is more readily available historically and daily trading simulations are faster with daily data, is there a way to create open, high, low, and close ticks for the daily bar data so that these can be processed with OnTick?  

Assuming yes, a follow on question would be can the high/low ordering be changed?

Finally, is there a way to execute a a trade at the beginning/end of the day basing the trade decision on the open/close prices of daily bar data?  While not completely accurate, it is plausable to use the open/close prices and execute a trade given the same price(s) to create a reasonable simulation.

Thanks.

Update Backtest








0

The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.


You can use the `OnData(Tradebars data)` event handler to get tradebar data. You can specify what resolution of data you'd like using the AddSecurity/AddEquity methods. I'm not sure what you mean by change the ordering?

You can submit MarketOnOpen orders for the brokerages which support it to have your order filled by the opening bar. 

0

The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.


I believe we are talking about different things.  For example, can OnData execute a fill on the same bar that the event is received, e.g., the make a trade decision based on the open and place a market on close on the same bar?

Regarding the MarketOnOpen orders, I'm not referring to that order type.  As an extension to my example above, a trade decision is made on the open price and an execution is made on the same price.  This is slightly different than a MarketOnOpen order because the decsion is made on the open versus the execution guaranteed.

In the above, the only available data is daily data.  Bar resolution cannot be set below daily but there are 4 ticks available: O, H, L, C for the day.  Note, that while not ideal, historical simulation sometimes is a compromise between having complete information (higher resolution data) and developing a system that is semi-realistic based upon the data that is available.
If the bar data is broken into 4 ticks and processed through OnTick, the ordering of the H and L can become important in certain systems.  

Please let me know.

Thanks.

 

0

@Duane, I believe what you are asking is not possible natively with the QC platform, but there might be a solution using the LEAN engine locally.

When backtesting with Daily resolution data, you only receive TradeBar objects in OnData, containing
Time, Open, High, Low, Close fields. When this TradeBar is emitted, the "algorithm time" is the time of the Close (i.e. the bar is complete). Although we have four data points in each TradeBar, we only have one decision point for each bar, on the Close.

Given only this information, there is no way to know with certainty whether the High happened before or after the Low: you can only guess, perhaps with some distance comparisons (such as if the Open is closer to the High than it is to the Low, then the sequence was O-H-L-C else O-L-H-C).

If I understood the question correctly, you are asking if a TradeBar can be split into 4 Ticks: 
if you use a local installation of Lean, you could certainly convert Daily data files to your own Tick resolution data files (with four rows per day instead of one) and then run a backtest using OnData(Ticks) instead of OnData(TradeBar), with the assumption that you guessed the correct H/L sequence every day.

Hope this helps

0

The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.


@Stefano, I plan to run locally, so your solution will work but if possible, I would like to avoid converting data since it requires extra processing, coding, maintenance, 2x the storage, etc.

For completeness for anyone reading, the solution would require 6 ticks: 2 opens, H, L, and 2 closes to accomidate the trade on open price and trade on close price.

@Jared, it would be great to have this functionality native in a future release.  The solution would probably require either a setting to break the daily bar into 6 ticks (as mentioned above) or alternatively to split the daily bar into 4 ticks and add an OnBarOpening and OnBarClosing event handlers which would be called before the open and close tick, respecitively.  This would give 6 decision points for daily bar data.

Do you think this could be planned for a future release and if yes, when?

Thanks.

0

@Jared, any thoughts on my question in my last post?

Thanks.

0

@Duane this is fairly edge case and can be solved inside your algorithm with a few lines of logic when you receive the TradeBar. The general rule we apply is that if its not useful for 80-90% of the users we don't build it into LEAN but prefer to encourage you to build it into your algorithm.

0

The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.


@Jared, Conceptually, how can this be done with a few lines of code, or any number of lines of code, in OnData or with TradeBar?  Per Stefano's comment, there is only one decision point for a TradeBar which is the close.

I'm all for doing this programmatically using TradeBars but based on the comments above I don't see a solution. 

Please advise. 

0

Using daily data; write 3-4 lines to fire any events as you like - potentially calling the OnData(Ticks) event handler.

You'll can't know what/when the Highs and Lows for the day occur until the very end of the day; so the 4 ticks you're after can't be fired until the very end of the day anyway.

If you find a way to know the high of the day the moment it happens, please let me know  :) 

OnData(TradeBars)
{
    // Break day into X ticks
    // ForEachTick -> Fire separate event handler
}

 

0

The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.


Got it.  I'm not yet familiar with LEAN's capabilities and other platforms prevent the capability you outline.  Regarding the ordering of high/low, it is true that one cannot know when each occurs until after the day but the technique is only meant to prevent best case results when using daily data and therefore it is of use prior to the triggering of close.  Using your pseudo code and based on the system, the high and low ticks can be triggered in an order to prevent a best case result.

I will let you know the month after I figure out how to determine when the high  (or low) of the day is actually occurring.

I appreciate your insights and given the above, LEAN's capabilities w.r.t. to the use case in this post allows for great flexibility.

Thanks.

 

0

The following comes oh-so-close.

Does anyone have any insight why the candles show the data out of phase (details after example)?

public class CustomChartingAlgorithm : QCAlgorithm
{
private readonly DateTime _startDate = new DateTime(2013, 10, 4);
private readonly DateTime _endDate = new DateTime(2013, 10, 11);
private TradeBarConsolidator _dayConsolidator = new TradeBarConsolidator(TimeSpan.FromDays(1));
private Chart _spyChart;

/// <summary>
/// Called at the start of your algorithm to setup your requirements:
/// </summary>
public override void Initialize()
{
//Set the date range you want to run your algorithm:
SetStartDate(_startDate);
SetEndDate(_endDate);

//Set the starting cash for your strategy:
SetCash(100000);

//Add any stocks you'd like to analyse, and set the resolution:
// Find more symbols here: http://quantconnect.com/data
var under = AddSecurity(SecurityType.Equity, "SPY", Resolution.Minute);
_spyChart = new Chart("SPY");
_spyChart.AddSeries(new Series("Candle", SeriesType.Candle));
AddChart(_spyChart);

_dayConsolidator.DataConsolidated += onDayConsolidator;
SubscriptionManager.AddConsolidator("SPY", _dayConsolidator);
}

private void onDayConsolidator(object sender, TradeBar consolidated)
{
// chart OHLC backdated as if it were today's only data
var t = consolidated.Time; //bar's time begins at midnight
_spyChart.Series["Candle"].AddPoint(t.AddHours(9.5), consolidated.Open);
_spyChart.Series["Candle"].AddPoint(t.AddHours(10), consolidated.High);
_spyChart.Series["Candle"].AddPoint(t.AddHours(11), consolidated.Low);
_spyChart.Series["Candle"].AddPoint(t.AddHours(16), consolidated.Close);
}
}

By out of phase, I mean the chart, ends up showing candles that mix up data from different days. The first 2 candles look OK, but after that...

For example. Oct8 bar comes out (OHLC)
167.62  165.36  165.48 165.80

Which, in other words, the bar is actually showing as if OHLC were
Oct8High, Oct8Low, Oct8Close, Oct9Open //Yes, Oct9

Yet, I have verified the raw data being emitted by onDayConsolidator is:

Oct 09, 2013  165.80 166.20 164.53 165.60
Oct 08, 2013  167.40 167.62 165.36 165.48
Oct 07, 2013  167.42 168.45 167.25 167.43  

0

Turns out you can generate Candle charts, but you have to work around a little bug in QC charting. Unfortunately the miniature rendering in the project below doesn't show the bars, but it does work.

The crux of the trick is to plot the open, hi, lo, close in that order while setting the respective HLOC points' timestanps 1 minute apart. Like this:

private void onFinalMarketClose(String name, DateTime utcTime) {
var t = utcTime.ConvertFromUtc(_equityTZ);

//work around bug in QC charting. Only works if no trading on weekends. Otherwise, let's hope bug gets fixed.
t += TimeSpan.FromSeconds((double)t.DayOfWeek);
var equityBar = _equityDayConsolidator.WorkingData as TradeBar;

// Candle
_equityChart.Series["Candle"].AddPoint(t + TimeSpan.FromMinutes(1), equityBar.Open);
_equityChart.Series["Candle"].AddPoint(t + TimeSpan.FromMinutes(2), equityBar.High);
_equityChart.Series["Candle"].AddPoint(t + TimeSpan.FromMinutes(3), equityBar.Low);
_equityChart.Series["Candle"].AddPoint(t + TimeSpan.FromMinutes(4), equityBar.Close);
}

 

0


Update Backtest





0

The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.


Loading...

This discussion is closed