I have a strategy that rellies on the fact that portfolio is invested or not:

if (!Portfolio[_btcSymbol].Invested && someCondition)
{    
	SetHoldings(_btcSymbol, 1);    
	Debug("Purchased BTC");
}
else if (Portfolio[_btcSymbol].Invested && SomeOtherCondition)
{    
	SetHoldings(_btcSymbol, 0);    
	Debug("Sold BTC");
}

 

When the if statement to buy executes I have aquired BTC, however when the “else if” is executed it does not sell all the btc, I am left with some small amount like 0.004 or so, so the Portfolio[_btcSymbol].Invested is always true and no more buy signals can be executed and the sell operations afterwards do not sell the remaining 0.004 btc. What is the way around it? All the tutorial show to use Portfolio[_btcSymbol].Invested.