Hello all,
I have some problems with implementation of my strategy.
As I'm not experienced in programming and it's first time I deal with C#, could be that I've made some really basic mistakes.
Strategy description:
- filtering the stocks by coarse universe method (selecting stocks with price greater than 25 and volume greater than 500000),
- two indicators - simple moving average of highs and relative strength index,
- additional rolling window to store previous bar high and close values,
- all in minute bars resolution,
- long-only positions, investing 50% of capital per trade,
- going through stocks and buying when RSI lower than 5 AND previous bar high is under SMA of highs,
- sell when previous bar high crosses SMA of highs OR if 'take profit' 1% activated
Problems and questions:
1. Blocking point: I get the runtime error during backtest:
Runtime Error: Unable to create SPY Minute consolidator because SPY is registered for Daily data. Consolidators require higher resolution data to produce lower resolution data.
I guess that it's connected somehow with coarse universe selection, but I've no idea how to solve it.
2. Is my general approach for applying indicators for each of stocks from universe correct?
3. I didn't find anywhere definition of moving averages for highs, lows or volume.
Is my definition correct? How it will look like in case of volume - Field.Volume?
SMA(Stock.Symbol, hPeriods, Resolution.Minute, Field.High);
4. Is the rolling window the best option to get previous bar values?
RollingWindow
lastMinute.Add(data[Stock.Symbol]);
Or maybe would be better to implement History function.
5. If using rolling window, the last data from the window is the previous bar data or current bar data?
lastMinute[0].High
6. Regarding LimitOrder as it needs number of shares in integer argument - what is the best practice to get it automatically?
LimitOrder(Stock.Symbol, - Stock.Holdings.Quantity, takeProfit);
7. Is there search through codes published on forum possible? It's really hard to find any examples of some features implementation on forum. GitHub is working fine for that but there are not too many examples.
I will be really grateful for any help.
Some additional tips are also welcome.
Regards,
Andrzej Fudala
Michael Handschuh
SMA(Stock.Symbol, hPeriods, Resolution.Minute, x => ((TradeBar)x).Volume);
4. The rolling window is the best option, but you'll need a different one for each symbol. Maybe use a DictionaryAndrzej Fudala
Runtime Error: Sequence contains no elements (Open Stacktrace)
And I'm doing it like this:foreach (var security in sChanges.AddedSecurities) { Data.Add(security.Symbol, new SymbolData(security.Symbol, this)); }
On the other hand, what happens when new stocks are added to the universe and how to assure SymbolData objects will be created for them? And two questions regarding DataSymbol class itself. Have I defined correctly Quantity variable? Is the Close variable defined through Identity(symbol) the same what most actual price? I'll appreciate any additional remarks.Andrew McKinnon
Andrzej Fudala
public override void OnSecuritiesChanged(SecurityChanges changes) { sChanges = changes; foreach (var removed in changes.RemovedSecurities) { if (removed.Invested) { Liquidate(removed.Symbol); } } foreach (var added in changes.AddedSecurities) { Data.Add(added.Symbol, new SymbolData(added.Symbol, this)); } }
But anyway still same error occurs:Runtime Error: Sequence contains no elements (Open Stacktrace)
Has somebody idea what is the root cause of problem?Andrew McKinnon
Andrzej Fudala
Runtime Error: An item with the same key has already been added.
No data loaded for SPY because there were no tradeable dates for this security.
Jared Broad
foreach (var added in changes.AddedSecurities) { if (!Data.ContainsKey(added.Symbol)) Data.Add(added.Symbol, new SymbolData(added.Symbol, this)); }
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.
Andrzej Fudala
Jared Broad
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.
Andrzej Fudala
Michael Handschuh
List openOrders = Transactions.GetOpenOrders();
You can further filter the list if needed by other criteria using a Where clause:var spyOpenOrders = openOrders.Where(x => x.Symbol == "SPY").ToList();
EDIT: You can get the number of open using the following:var openOrdersCount = Transactions.GetOpenOrders().Count;
Andrzej Fudala
Michael Handschuh
var securitiesWithHoldings = Securities.Values.Where(sec => sec.HoldStock).ToList();
This will return all Security objects that hold stock into a list. You can then use the Count property to check the total number of securities that currently have holdings. All of the data needed is in memory and available to your algorithm. If I've still misunderstood your request, please provide a use case/scenario and maybe I can be of further assistance.Andrzej Fudala
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!