Not sure what's wrong but got a run time error first on LIVE. The broker (IB) rejected the order, which I presumed was because of insufficient funds. I figured I was going after the entire portfolio to short (using SetHoldings, -1.0). So I took the code offline and backtested with the following changes but
1) still am getting Insufficient funds error. Any help is much appreciated.
2) Also, how to stop runtime error from console during backtesting?
Strategy is simple. Short on slow/fast MA cross, liquidate viceversa. Am using min interval
------
public void OnData(TradeBars data)
{
if (!slow.IsReady) return;
//if (previous.Date == data.Time.Date) return;
if (Time.Hour <= 9 || Time.Hour > 14) return;
const decimal tolerance = 0.00001m;
var holdings = Portfolio[Symbol].Quantity;
// Set the maximum cash allocation to a trade depending on if its live mode.
decimal tradeCashAllocation = 50000;
if (LiveMode) {
tradeCashAllocation = 10000;
}
var orderSize = Math.Floor(tradeCashAllocation / data[Symbol].Close);
if (holdings >= 0 && data.ContainsKey(Symbol) )
//if (!Portfolio.HoldStock && data.ContainsKey(Symbol))
{
// if the slow is greater than the fast, we'll go short
if (slow > fast * (1 + tolerance))
{
Order(Symbol, -orderSize);
//Debug("Purchased " + Symbol + " on " + Time.ToString("u"));
Log("SOLD @ orderSize>> " + orderSize + " " + Securities[Symbol].Price);
//SetHoldings(Symbol, -0.9);
}
}
// Liquidate a) if currently short b) if the slow MA is less than the fast MA
//if (Portfolio.HoldStock && data.ContainsKey(Symbol) && slow < fast)
if (holdings < 0 && data.ContainsKey(Symbol) && slow < fast)
{
//Debug("Closed " + Symbol + " on " + Time.ToString("u"));
Log("LIQUIDATE >> " + Securities[Symbol].Price);
Liquidate(Symbol);
}
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.
Sri Vanama
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!