Last active
May 29, 2020 00:08
-
-
Save buddies2705/a5ad435d7395d57081d1a985fe1a114d to your computer and use it in GitHub Desktop.
Strategy Accumulator
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
| from __future__ import print_function | |
| from pyalgotrade import strategy | |
| from pyalgotrade.barfeed import quandlfeed, csvfeed | |
| from pyalgotrade.technical import ma | |
| class Accumulator(strategy.BacktestingStrategy): | |
| def __init__(self, feed, instrument, buy_offset, buy_percent): | |
| super(Accumulator, self).__init__(feed, 90000) | |
| self.__position = None | |
| self.__instrument = instrument | |
| self.__sma = ma.SMA(feed[instrument].getPriceDataSeries(), 5) | |
| self.offset = buy_offset | |
| self.buy_percent = buy_percent | |
| self.getBroker().getFillStrategy().setVolumeLimit(None) | |
| def onEnterOk(self, position): | |
| execInfo = position.getEntryOrder().getExecutionInfo() | |
| self.info("BUY at $%.2f" % (execInfo.getPrice())) | |
| def onEnterCanceled(self, position): | |
| self.__position = None | |
| def onExitOk(self, position): | |
| execInfo = position.getExitOrder().getExecutionInfo() | |
| self.info("SELL at $%.2f" % (execInfo.getPrice())) | |
| self.__position = None | |
| def onExitCanceled(self, position): | |
| # If the exit was canceled, re-submit it. | |
| self.__position.exitMarket() | |
| def onBars(self, bars): | |
| # Wait for enough bars to be available to calculate a SMA. | |
| # print(bars) | |
| bar = bars[self.__instrument] | |
| # self.info(bar.getClose()) | |
| # self.info(self.__sma[-1]) | |
| if self.__sma[-1] is None: | |
| return | |
| bar = bars[self.__instrument] | |
| # If a position was not opened, check if we should enter a | |
| # long position. | |
| shares = (self.getBroker().getCash() / bars[self.__instrument].getPrice()) | |
| if self.__position is None: | |
| if (bar.getPrice() * (1 + self.offset) < self.__sma[-1]): | |
| # Enter a buy market order. The order is good till canceled. | |
| self.__position = self.enterLong(self.__instrument, shares, True) | |
| # Check if we have to exit the position. | |
| elif not self.__position.exitActive(): | |
| if (bar.getPrice() * (1 - self.offset) > self.__sma[-1]): | |
| # Enter a buy market order. The order is good till canceled. | |
| self.__position.exitMarket() | |
| def getSMA(self): | |
| return self.__sma |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment