Back

StopLimitOrder on Universe

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()
{

}
}
}

 

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.


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