* pep8 conversion * PEP8 updates/fixes * Minor update --------- Co-authored-by: Jhonathan Abreu <jdabreu25@gmail.com>
This commit is contained in:
@@ -26,67 +26,67 @@ from AlgorithmImports import *
|
||||
class ConvertToFrameworkAlgorithm(QCAlgorithm):
|
||||
'''Demonstration algorithm showing how to easily convert an old algorithm into the framework.'''
|
||||
|
||||
FastEmaPeriod = 12
|
||||
SlowEmaPeriod = 26
|
||||
fast_ema_period = 12
|
||||
slow_ema_period = 26
|
||||
|
||||
def Initialize(self):
|
||||
def initialize(self):
|
||||
'''Initialise the data and resolution required, as well as the cash and start-end dates for your algorithm. All algorithms must initialized.'''
|
||||
|
||||
self.SetStartDate(2004, 1, 1)
|
||||
self.SetEndDate(2015, 1, 1)
|
||||
self.set_start_date(2004, 1, 1)
|
||||
self.set_end_date(2015, 1, 1)
|
||||
|
||||
self.symbol = self.AddSecurity(SecurityType.Equity, 'SPY', Resolution.Daily).Symbol
|
||||
self._symbol = self.add_security(SecurityType.EQUITY, 'SPY', Resolution.DAILY).symbol
|
||||
|
||||
# define our daily macd(12,26) with a 9 day signal
|
||||
self.macd = self.MACD(self.symbol, self.FastEmaPeriod, self.SlowEmaPeriod, 9, MovingAverageType.Exponential, Resolution.Daily)
|
||||
self._macd = self.macd(self._symbol, self.fast_ema_period, self.slow_ema_period, 9, MovingAverageType.EXPONENTIAL, Resolution.DAILY)
|
||||
|
||||
|
||||
def OnData(self, data):
|
||||
'''OnData event is the primary entry point for your algorithm. Each new data point will be pumped in here.
|
||||
def on_data(self, data):
|
||||
'''on_data event is the primary entry point for your algorithm. Each new data point will be pumped in here.
|
||||
Args:
|
||||
data: Slice object with your stock data'''
|
||||
# wait for our indicator to be ready
|
||||
if not self.macd.IsReady or not data.ContainsKey(self.symbol) or data[self.symbol] is None: return
|
||||
if not self._macd.is_ready or not data.contains_key(self._symbol) or data[self._symbol] is None: return
|
||||
|
||||
holding = self.Portfolio[self.symbol]
|
||||
holding = self.portfolio[self._symbol]
|
||||
|
||||
signalDeltaPercent = float(self.macd.Current.Value - self.macd.Signal.Current.Value) / float(self.macd.Fast.Current.Value)
|
||||
signal_delta_percent = float(self._macd.current.value - self._macd.signal.current.value) / float(self._macd.fast.current.value)
|
||||
tolerance = 0.0025
|
||||
|
||||
# if our macd is greater than our signal, then let's go long
|
||||
if holding.Quantity <= 0 and signalDeltaPercent > tolerance:
|
||||
# 1. Call EmitInsights with insights created in correct direction, here we're going long
|
||||
# The EmitInsights method can accept multiple insights separated by commas
|
||||
self.EmitInsights(
|
||||
if holding.quantity <= 0 and signal_delta_percent > tolerance:
|
||||
# 1. Call emit_insights with insights created in correct direction, here we're going long
|
||||
# The emit_insights method can accept multiple insights separated by commas
|
||||
self.emit_insights(
|
||||
# Creates an insight for our symbol, predicting that it will move up within the fast ema period number of days
|
||||
Insight.Price(self.symbol, timedelta(self.FastEmaPeriod), InsightDirection.Up)
|
||||
Insight.price(self._symbol, timedelta(self.fast_ema_period), InsightDirection.UP)
|
||||
)
|
||||
|
||||
# longterm says buy as well
|
||||
self.SetHoldings(self.symbol, 1)
|
||||
self.set_holdings(self._symbol, 1)
|
||||
|
||||
# if our macd is less than our signal, then let's go short
|
||||
elif holding.Quantity >= 0 and signalDeltaPercent < -tolerance:
|
||||
# 1. Call EmitInsights with insights created in correct direction, here we're going short
|
||||
# The EmitInsights method can accept multiple insights separated by commas
|
||||
self.EmitInsights(
|
||||
elif holding.quantity >= 0 and signal_delta_percent < -tolerance:
|
||||
# 1. Call emit_insights with insights created in correct direction, here we're going short
|
||||
# The emit_insights method can accept multiple insights separated by commas
|
||||
self.emit_insights(
|
||||
# Creates an insight for our symbol, predicting that it will move down within the fast ema period number of days
|
||||
Insight.Price(self.symbol, timedelta(self.FastEmaPeriod), InsightDirection.Down)
|
||||
Insight.price(self._symbol, timedelta(self.fast_ema_period), InsightDirection.DOWN)
|
||||
)
|
||||
|
||||
self.SetHoldings(self.symbol, -1)
|
||||
self.set_holdings(self._symbol, -1)
|
||||
|
||||
# if we wanted to liquidate our positions
|
||||
## 1. Call EmitInsights with insights create in the correct direction -- Flat
|
||||
|
||||
#self.EmitInsights(
|
||||
## 1. Call emit_insights with insights create in the correct direction -- Flat
|
||||
|
||||
#self.emit_insights(
|
||||
# Creates an insight for our symbol, predicting that it will move down or up within the fast ema period number of days, depending on our current position
|
||||
# Insight.Price(self.symbol, timedelta(self.FastEmaPeriod), InsightDirection.Flat)
|
||||
# Insight.price(self._symbol, timedelta(self.fast_ema_period), InsightDirection.FLAT)
|
||||
#)
|
||||
|
||||
# self.Liquidate()
|
||||
|
||||
# self.liquidate()
|
||||
|
||||
# plot both lines
|
||||
self.Plot("MACD", self.macd, self.macd.Signal)
|
||||
self.Plot(self.symbol.Value, self.macd.Fast, self.macd.Slow)
|
||||
self.Plot(self.symbol.Value, "Open", data[self.symbol].Open)
|
||||
self.plot("MACD", self._macd, self._macd.signal)
|
||||
self.plot(self._symbol.value, self._macd.fast, self._macd.slow)
|
||||
self.plot(self._symbol.value, "Open", data[self._symbol].open)
|
||||
|
||||
Reference in New Issue
Block a user