I have initilized my backtest as so.
// don't do any margin stuff
SetBrokerageModel(Brokerages.BrokerageName.Default, AccountType.Cash);
//Cash allocation
SetCash(25000);
When I decide to make an order I check to see if I already have orders in place and that I have enough cash before setting the order as below.
// if we have not open orders for it already
decimal totalorders = 0.0m;
foreach (var v in Portfolio.Transactions.GetOpenOrders())
{
totalorders += slice[v.Symbol].Price * v.Quantity;
}
// if we have enough money to buy it
if (Portfolio.Cash - totalorders > 0.05m * 25000m)
{
SetHoldings(testbar.Symbol, 0.05);
}
When I run this locally using the open source Lean it works, but when I run it on Quantconnect it seems to not update the value of Portfolio.Cash and I end up making short orders. How can I check to see if I have enough cash?
Jared Broad
Hi Paul! Please share an algorithm to get more assistance. Its virtually impossible to debug from code snippets :)
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.
Paul McKinney
I assume you get a notification, but I shared it with you. Thanks!
Paul McKinney
Ok, I think I figured it out. GetOpenOrders() doesn't pick up orders I have placed within the current call to OnData(). I have to track them during that call. The next time OnData() is called it can call GetOpenOrders(). I think.... Here is my change.
// if we have not open orders for it already
decimal totalorders = 0.0m;
foreach (var v in Portfolio.Transactions.GetOpenOrders())
{
totalorders += slice[v.Symbol].Price * v.Quantity;
}
if (Portfolio.Cash - allopenorders - todaysorders > 0.05m * 25000m)
{
int orderamount = Decimal.ToInt32(25000m * 0.05m / testbar.Price);
todaysorders += testbar.Price * orderamount;
Jared Broad
Hey Paul, are you using daily data? What type of orders are you placing? If daily data + market orders then yes they'll be converted to Market on open (since its 4pm when the daily bar is created).
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.
Paul McKinney
Yes that is what I'm doing. I've setup the tickers to be daily when I add them. If I understand you correctly, with a high resolution, I could check GetOpenOrders while adding multiple orders in an OnData and I would get a list of them.
Jared Broad
Correct; with market orders the orders are filled synchronously - so the portfolio is updated by the next line of code. The Portfolio is instantly up to date.
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.
Paul McKinney
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!