Back

Insufficient buying power

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);
}
Update Backtest








SetHoldings("Symbol",1.0) will try and use 100% of your portfolio value to purchase the Symbol. There are many reasons why this won't work -- even in backtesting.

Depending on your margin, brokerages might not let you place trades at 100% of your cash. E.g. You might get margin calls because you're too close to 100% utilization. This depends on the style of your account (cash / margin).

Even if you have a margin brokerage account you might have existing real holdings which are using some of your cash. You can't set-holdings 1.0 while you have existing holdings (i.e. you'll need to liquidate the other symbols).

In this case you're also setting your live tradeCashAllocation -- if it exceeds your buying power in IB it won't trade successfully.
1

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.


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