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.
TradeBars previous = new TradeBars();
public void OnData(TradeBars bars)
{
//Do stuff. then save the bar to the class variable.
previous = bars;
}
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.
RollingWindow window = new RollingWindow(10); // create a 10 day window
public override void OnTradeBar(Dictionary data)
{
TradeBar bar;
if (data.TryGetValue("SPY", out bar))
{
window.Update(bar.Close);
if (window.IsReady)
{
decimal tenDayChange = window[0] - window[9];
}
}
}
using System;
using System.Collections;
using System.Collections.Generic;
namespace QuantConnect
{
///
/// Interface type used to pass windows around without worry of external modification
///
/// The type of data in the window
public interface IReadOnlyWindow : IEnumerable
{
///
/// Gets the size of this window
///
int Size { get; }
///
/// Gets the current number of elements in this window
///
int Count { get; }
///
/// Gets the number of samples that have been added to this window over its lifetime
///
decimal Samples { get; }
///
/// Indexes into this window, where index 0 is the most recently
/// entered value
///
/// the index, i
/// the ith most recent entry
T this[int i] { get; }
///
/// Gets a value indicating whether or not this window is ready, i.e,
/// it has been filled to its capacity, this is when the Size==Count
///
bool IsReady { get; }
///
/// Gets the most recently removed item from the window. This is the
/// piece of data that just 'fell off' as a result of the most recent
/// add. If no items have been removed, this will throw an exception.
///
T MostRecentlyRemoved { get; }
}
///
/// This is a window that allows for list access semantics,
/// where this[0] refers to the most recent item in the
/// window and this[Count-1] refers to the last item in the window
///
/// The type of data in the window
public class RollingWindow : IReadOnlyWindow
{
private int _tail;
private decimal _samples;
private T _mostRecentlyRemoved;
private readonly List _list;
private readonly object _lock = new object();
///
/// Gets the size of this window
///
public int Size
{
get { return _list.Capacity; }
}
///
/// Gets the current number of elements in this window
///
public int Count
{
get { return _list.Count; }
}
///
/// Gets the number of samples that have been added to this window over its lifetime
///
public decimal Samples
{
get { return _samples; }
}
public T MostRecentlyRemoved
{
get
{
if (!IsReady)
{
throw new InvalidOperationException("No items have been removed yet!");
}
return _mostRecentlyRemoved;
}
}
public RollingWindow(int size)
{
_list = new List(size);
}
///
/// Indexes into this window, where index 0 is the most recently
/// entered value
///
/// the index, i
/// the ith most recent entry
public T this[int i]
{
get
{
if (i >= Count)
{
throw new ArgumentOutOfRangeException("i", i, string.Format("Must be between 0 and Count {{{0}}}", Count));
}
return _list[(Count + _tail - i - 1)%Count];
}
set
{
if (i >= Count)
{
throw new ArgumentOutOfRangeException("i", i, string.Format("Must be between 0 and Count {{{0}}}", Count));
}
_list[(Count + _tail - i - 1) % Count] = value;
}
}
///
/// Gets a value indicating whether or not this window is ready, i.e,
/// it has been filled to its capacity, this is when the Size==Count
///
public bool IsReady
{
get { return Samples > Count; }
}
///
/// Adds an item to this window and shifts all other elements
///
/// The item to be added
public void Add(T item)
{
lock (_lock)
{
_samples++;
if (Size == Count)
{
// keep track of what's the last element
// so we can reindex on this[ int ]
_mostRecentlyRemoved = _list[_tail];
_list[_tail] = item;
_tail = (_tail + 1)%Size;
}
else
{
_list.Add(item);
}
}
}
///
/// Clears this window of all data
///
public void Clear()
{
lock (_lock)
{
_list.Clear();
}
}
public IEnumerator GetEnumerator()
{
// we make a copy on purpose so the enumerator isn't tied
// to a mutable object, well it is still mutable but out of scope
var temp = new List(Count);
lock (_lock)
{
for (int i = 0; i < Count; i++)
{
temp.Add(this[i]);
}
}
return temp.GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
}
}
AddSecurity(SecurityType.Equity, "SPY", Resolution.Minute); //Will fire every minute
AddData("SPY_YAHOO", Resolution.Minute); // Will fire at start of day with full OHLC.
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.
AddData("SPY_DAILY", Resolution.Minute);
AddData("VIX_DAILY", Resolution.Minute);
AddData("TCELL_DAILY", Resolution.Minute);
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.
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.
AddData("YAHOO/INDEX_SPY");
AddData("YAHOO/IBM");
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.
To unlock posting to the community forums please complete at least 30% of Boot Camp.
You can
continue your Boot Camp training progress from the terminal. We
hope to see you in the community soon!