Skip to content

Instantly share code, notes, and snippets.

@vasilescur
Last active July 1, 2020 03:54
Show Gist options
  • Save vasilescur/2223a842c36a5c0f792569769176bd2d to your computer and use it in GitHub Desktop.
Save vasilescur/2223a842c36a5c0f792569769176bd2d to your computer and use it in GitHub Desktop.
namespace QuantConnect {
public class BasicTemplateAlgorithm: QCAlgorithm {
decimal lastPrice;
decimal DELTA = 3;
decimal AMOUNT = 1;
public override void Initialize() {
// backtest parameters
SetStartDate(2016, 1, 1);
SetEndDate(DateTime.Now);
// cash allocation
SetCash(1000000);
AddEquity("SPY", Resolution.Minute);
this.lastPrice = -1;
}
/*
* New data arrives here.
* The "Slice" data represents a slice of time.
*/
public override void OnData(Slice data) {
TradeBars bars = data.Bars;
// Get just this bar.
TradeBar bar;
if (!bars.ContainsKey("SPY")) {
throw new Exception("Could not find SPY in bar.");
}
bar = bars["SPY"];
// First piece of data? Set up last price.
if (this.lastPrice == -1) {
this.lastPrice = bar.Close;
return;
}
// Don't hold anything? Buy 100 shares.
if (!Portfolio.HoldStock) {
Buy("SPY", 100);
return;
}
// Went down? Buy.
if (bar.Close < this.lastPrice - this.DELTA) {
Buy("SPY", this.AMOUNT);
}
// Went up? Sell.
if (bar.Close > this.lastPrice + this.DELTA) {
Sell("SPY", this.AMOUNT);
}
// Update last price
this.lastPrice = bar.Close;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment