The boilerplate for my code needs to do the following:

  1. Read a list of stocks to trade from a dropbox link
  2. Market order newly added symbols in the dropbox link and then create a trailing stop loss once the order fills
  3. Close any removed symbols the next day

I have all of this logic prepared but am running into two problems: I'm getting an access denied log which I assume is from the algo trying to access my dropbox link (even though I made the link public). I also am not sure how to submit a trailing stop loss order. I see it in the lean docs but it isn't clear how I submit that type of order.

Since the backtest returned an error, I can't seem to attach it to the post, so I am showing it below.

using QuantConnect.Algorithm;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using QuantConnect.Data.Market;
using QuantConnect.Data.UniverseSelection;
using QuantConnect.Securities;

namespace QuantConnect
{
public class Earnings : QCAlgorithm
{
// the changes from the previous universe selection
private SecurityChanges _changes = SecurityChanges.None;
// only used in backtest for caching the file results
private readonly Dictionary<DateTime, List<string>> _backtestSymbolsPerDay = new Dictionary<DateTime, List<string>>();
List<Security> toOpen = new List<Security>();
List<Security> toClose = new List<Security>();

public override void Initialize()
{
// this sets the resolution for data subscriptions added by our universe
UniverseSettings.Resolution = Resolution.Tick;

// set our start and end for backtest mode
SetStartDate(2017, 8, 18);
SetEndDate(2017, 8, 24);
SetCash(30000);

// Schedule events
Schedule.On(DateRules.EveryDay("SPY"), TimeRules.AfterMarketOpen("SPY", 5), () => {OpenPositions();});
Schedule.On(DateRules.EveryDay("SPY"), TimeRules.AfterMarketOpen("SPY", 10), () => { ClosePositions(); });

// define a new custom universe that will trigger each day at midnight
AddUniverse("my-dropbox-universe", Resolution.Daily, dateTime =>
{
const string liveUrl = @"https://www.dropbox.com/s/yd7bzfsouzcphix/live_stocks.csv?dl=1";
const string backtestUrl = @"https://www.dropbox.com/s/151j6a4i8rpn37p/backtest_stocks.csv?dl=1";
var url = LiveMode ? liveUrl : backtestUrl;
using (var client = new WebClient())
{
// handle live mode file format
if (LiveMode)
{
// fetch the file from dropbox
var file = client.DownloadString(url);
// if we have a file for today, break apart by commas and return symbols
if (file.Length > 0) return file.ToCsv();
// no symbol today, leave universe unchanged
return Universe.Unchanged;
}

// backtest - first cache the entire file
if (_backtestSymbolsPerDay.Count == 0)
{
// fetch the file from dropbox only if we haven't cached the result already
var file = client.DownloadString(url);

// split the file into lines and add to our cache
foreach (var line in file.Split(new[] { '\n', '\r' }, StringSplitOptions.RemoveEmptyEntries))
{
var csv = line.ToCsv();
var date = DateTime.ParseExact(csv[0], "yyyyMMdd", null);
var symbols = csv.Skip(1).ToList();
_backtestSymbolsPerDay[date] = symbols;
}
}

// if we have symbols for this date return them, else specify Universe.Unchanged
List<string> result;
if (_backtestSymbolsPerDay.TryGetValue(dateTime.Date, out result))
{
return result;
}
return Universe.Unchanged;
}
});
}

public void OnData(TradeBars data){}

public override void OnSecuritiesChanged(SecurityChanges changes) {
Debug("In on securities changed");
foreach (Security sec in changes.RemovedSecurities) {
if (sec.Invested) {
Debug(String.Format("Adding {0} to list for removal", sec.Symbol.ToString()));
toClose.Add(sec);
}
}
foreach (Security sec in changes.AddedSecurities){
Debug(String.Format("Adding {0} to list for opening", sec.Symbol.ToString()));
toOpen.Add(sec);
}
}

public void OpenPositions()
{
// Create Market Order and trailing stop order
var weighting = 1m / toOpen.Count;
foreach (Security sec in toOpen)
{
SetHoldings(sec.Symbol, weighting);
}
}

public override void OnOrderEvent(OrderEvent fill)
{
string message = String.Format("Order {0} {1} x {2} at {3} commission={4} OrderId={5}",
fill.Status.ToString(),
fill.FillQuantity,
fill.Symbol,
fill.FillPrice,
fill.OrderFee,
fill.OrderId);
Debug(message);

if (fill.Status.ToString() == "Filled")
{
Debug("How do I submit the trailing stop loss");
// LimitOrder(fill.Symbol, -1*fill.FillQuantity, [STOP TRAILING PERCENT], "TRAILINGSTOPLOSS");
}
}

public void ClosePositions()
{
// Close any remaining
foreach (Security sec in toClose)
{
SetHoldings(sec.Symbol, 0);
}
}
}
}