Back

Add previous value of indicator to rolling window custom class of multiple securities in C#

Hi All,

I wish I didn't have to ask as I have seen multiple similar python examples in other posts but i still cant seem to apply it to my own code.

I have a custom class containing multiple indicators. At OnData, I can only seem to access the current indicator values, e.g. stockDatas[0].CCI.Current. What I would like to do is access the previous CCI value for each security too. 

To do this I believe I need a rolling window. Below what I am trying to do is make a rolling window containing the stockDatas list. What I cant seem to figure out, is that the rolling window in the below code isnt functioning properly because calling in the debugger:
stockDatas[0].STC_RW[0].CCI.Current.Value and stockDatas[0].STC_RW[1].CCI.Current.Value, yeilds the same value consistently.

 

Edit:

Any help would be greatly appreciated, thanks!
 


namespace QuantConnect
{
public class cci_test2 : QCAlgorithm
{
private string my_secs = "EURUSD,AUDUSD,GBPUSD";
List<string> symbolList;
List<StockDataClass> stockDatas = new List<StockDataClass>();

public override void Initialize()
{
SetStartDate(2012, 1, 1);
SetEndDate(2015, 1, 1);
SetCash(10000);

symbolList = new List<string>(my_secs.Split(new char[] { ',' }));
foreach (string ticker in symbolList)
{
AddSecurity(SecurityType.Forex, ticker, Resolution.Hour,true,50.0m,false);
StockDataClass stockData = new StockDataClass(ticker);
stockData.CCI = CCI(ticker, 20, MovingAverageType.Simple, Resolution.Hour);
stockData.ATR = ATR(ticker, 20, MovingAverageType.Simple, Resolution.Hour);
stockData.STC_RW = new RollingWindow<StockDataClass>(2);
stockDatas.Add(stockData);
}
}

public void OnData(TradeBars data)
{
for (int i = 0; i < stockDatas.Count; i++)
{
//This is where I am attempting to add the bars stockdata indicator values to a rolloing window.
stockDatas[i].STC_RW.Add(stockDatas[i]);
SetHoldings("EURUSD", 5);
}
}

}

class StockDataClass
{
public StockDataClass(string ticker)
{
Ticker = ticker;
}
public string Ticker;

public CommodityChannelIndex CCI;
public AverageTrueRange ATR;

public RollingWindow<StockDataClass> STC_RW;

}

}

Update Backtest







The algorithm was trying to add a class into the rolling window so all we needed was to add the specific CCI value. I created a rolling window for CCI and ATR and printed out the CCI rolling window values after the window was ready. 

Before: stockDatas[i].STC_RW.Add(stockDatas[i])

After: stockDatas[i].CCI_RW.Add(stockDatas[i].CCI.Current.Value);

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.


Hi Gurumeher, thank you for getting back to me. I was hoping to add the class to the rolling window. So I could look back previous instances of it.

However, your method does achieve exaclty what I need. Thank you I appreciate it!

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