Created
November 5, 2021 08:43
-
-
Save dkav6/5a04e1243d9fe5a2bb154316be7a40e2 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class EmaCross(Strategy): | |
# Define the two EMA lags as *class variables* | |
# for later optimization | |
n1 = 5 | |
n2 = 10 | |
def init(self): | |
# Precompute two moving averages | |
self.ema1 = self.I(EMA, self.data.Close, self.n1) | |
self.ema2 = self.I(EMA, self.data.Close, self.n2) | |
def next(self): | |
# If ema1 crosses above ema2, buy the asset | |
if crossover(self.ema1, self.ema2): | |
self.buy() | |
# Else, if ema1 crosses below ema2, sell it | |
elif crossover(self.ema2, self.ema1): | |
self.position.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment