Hi Everyone!

Why on added securities I'm getting securities with price = 0 ?

It makes my algorithm divide by zero.

Do I do something wrong?

Thanks!

namespace QuantConnect.Algorithm.CSharp
{
public class PStocks : QCAlgorithm
{
//Vars
SecurityChanges _changes = SecurityChanges.None;
//---------------------------------------------------------------------
//Current Stop Loss & Take Profit Orders
//List<SL_Order> _orders;
//---------------------------------------------------------------------
//Const Vars
const decimal _cash = 10000;
const Resolution _res = Resolution.Hour;
const int _stopLossPercent = 1;
const int _takeProfitPercent = 2;
const int _numberOfSymbols = 10;
const decimal _priceLimit = 10;
//---------------------------------------------------------------------

public override void Initialize()
{
SetStartDate(2010, 1, 1);
SetEndDate(2010, 7, 1);
SetCash(_cash);

//_orders = new List<SL_Order>(_numberOfSymbols);

UniverseSettings.Resolution = _res;
UniverseSettings.Leverage = 1m;
AddUniverse(CoarseSelectionFilter);
}

public void OnData(TradeBars data)
{
if(_changes == SecurityChanges.None) return;

var cashPerSymbol = (int)Math.Floor(Portfolio.Cash * 0.9m) / _numberOfSymbols;

foreach (var security in _changes.RemovedSecurities)
{
if (security.Invested)
{
Liquidate(security.Symbol);
}
}
foreach (var security in _changes.AddedSecurities)
{
var symbol = security.Symbol;
var currentPrice = security.Close;
Debug($"Security Symbol: {symbol}, Price: {currentPrice}");
var shares = (int)Math.Floor(cashPerSymbol / currentPrice);
var StopLimitOrderID = StopLimitOrder(symbol, shares, currentPrice - (currentPrice * _stopLossPercent / 100), currentPrice + (currentPrice * _takeProfitPercent / 100));
//_orders.Add(new SL_Order(StopLimitOrderID));
}

_changes = SecurityChanges.None;
}

public IEnumerable<Symbol> CoarseSelectionFilter(IEnumerable<CoarseFundamental> coarse)
{
var sortedByDollarVolume = coarse.OrderByDescending(x => x.DollarVolume);
var top = sortedByDollarVolume.Where(x => x.Price < _priceLimit).Take(_numberOfSymbols);
return top.Select(x => x.Symbol);
}

public override void OnSecuritiesChanged(SecurityChanges changes)
{
_changes = changes;
Log($"OnSecuritiesChanged({UtcTime:o}):: {changes}");
}

public override void OnOrderEvent(OrderEvent fill)
{
Log($"OnOrderEvent({UtcTime:o}):: {fill}");
}

public override void OnEndOfDay()
{

}
}
}