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);

}

Author