Cl1 (Crude Oil Forecast) 5 Day SMA Systenatic Strategy: Source Code for QuantConnect

This content was featured on:

You can see the results and clone the algorithm here:

Below is also the original source code:

File 1:

namespace QuantConnect {

public partial class SwingTraderPro : QCAlgorithm
{

//variables
private Signal tradeSignal;
private const string Symbol = “USO”;
decimal leverage = 2;
private SimpleMovingAverage sma;
private SimpleMovingAverage smanow;

/// Initialize our algorithm:
public override void Initialize(){
SetStartDate(2014,7, 01);
SetEndDate(2015, 6, 30);
SetCash(100000);
AddSecurity(SecurityType.Equity, Symbol, Resolution.Minute, false, leverage, false);
SetRunMode(RunMode.Series);

AddData(“SignalStregth”, Resolution.Minute);

// create a 5 day exponential moving average
sma = SMA(Symbol, 5, Resolution.Daily);

// create a 1 day exponential moving average
smanow = SMA(Symbol, 1, Resolution.Daily);
}

public void OnData(Signal data){
tradeSignal = data;
}

//Run ondata
public void OnData(TradeBars data)
{

if (!sma.IsReady) return;

var holdings = Portfolio[Symbol].Quantity;

if (tradeSignal != null) {
if ((tradeSignal.Sig1m > 0 && smanow > sma) || (tradeSignal.Sig1m < 0 && smanow < sma)){ if (tradeSignal.Sig3d > 0 && holdings <= 50 ){
SetHoldings(Symbol, 1.0);
}
if (tradeSignal.Sig3d < 0 && holdings >= -50 ){
SetHoldings(Symbol, -1.0);
}
}

}
}
}
}

File 2:
namespace QuantConnect {

public class Signal : BaseData
{
// Initialize Varaiable
public decimal Sig3d = 0;
public decimal Sig1m = 0;

public override string GetSource(SubscriptionDataConfig config, DateTime date, DataFeedEndpoint datafeed){
return “https://www.dropbox.com/s/brjdnfwvbqakjnr/cl1signals.csv?dl=1”;
}

public override BaseData Reader(SubscriptionDataConfig config, string line, DateTime date, DataFeedEndpoint datafeed){
Signal signal = new Signal();
try {
string[] data = line.Split(‘,’);
signal.Time = DateTime.Parse(data[0]); // Make sure we only get this data AFTER trading day – don’t want forward bias.
signal.Sig3d = Convert.ToDecimal(data[1]);
signal.Sig1m = Convert.ToDecimal(data[2]);
}
catch (Exception){
return null;
}
return signal;
}
}
}

 

quant connect