コード例 #1
0
class DualEMATaLib(TradingAlgorithm):
    """ Dual Moving Average Crossover algorithm.

    This algorithm buys apple once its short moving average crosses
    its long moving average (indicating upwards momentum) and sells
    its shares once the averages cross again (indicating downwards
    momentum).

    """

    def __init__(self, dbhandler, *args, **kwargs):
        super(DualEMATaLib, self).__init__(*args, **kwargs)
        self.dbhandler = dbhandler
        self.mstockid = self.dbhandler.stock.ids[0]

    def initialize(self, short_window=20, long_window=40):
        # Add 2 mavg transforms, one with a long window, one
        # with a short window.
        self.short_ema_trans = EMA(timeperiod=short_window)
        self.long_ema_trans = EMA(timeperiod=long_window)
        self.real_obv_trans = OBV()

        # To keep track of whether we invested in the stock or not
        self.invested = False

    def handle_data(self, data):
        self.short_ema = self.short_ema_trans.handle_data(data)
        self.long_ema = self.long_ema_trans.handle_data(data)
        self.real_obv = self.real_obv_trans.handle_data(data)
        if self.short_ema is None or self.long_ema is None or self.real_obv is None:
            return

        self.buy = False
        self.sell = False

        # buy/sell rule
        if (self.short_ema > self.long_ema).all() and not self.invested:
            self.order(self.mstockid, 1000)
            self.invested = True
            self.buy = True
        elif (self.short_ema < self.long_ema).all() and self.invested:
            self.order(self.mstockid, -1000)
            self.invested = False
            self.sell = True

        # save to recorder
        signals = {
            'open': data[self.mstockid].open,
            'high': data[self.mstockid].high,
            'low': data[self.mstockid].low,
            'close': data[self.mstockid].close,
            'volume': data[self.mstockid].volume,
            'short_ema': self.short_ema[self.mstockid],
            'long_ema': self.long_ema[self.mstockid],
            'buy': self.buy,
            'sell': self.sell
        }

        self.record(**signals)
コード例 #2
0
ファイル: dual_ema_talib.py プロジェクト: AMarempudi/zipline
    def initialize(self, short_window=20, long_window=40):
        # Add 2 mavg transforms, one with a long window, one
        # with a short window.
        self.short_ema_trans = EMA(timeperiod=short_window)
        self.long_ema_trans = EMA(timeperiod=long_window)

        # To keep track of whether we invested in the stock or not
        self.invested = False
コード例 #3
0
ファイル: dual_ema_talib.py プロジェクト: zixan/zipline
    def initialize(self, short_window=20, long_window=40):
        # Add 2 mavg transforms, one with a long window, one
        # with a short window.
        self.short_ema_trans = EMA(timeperiod=short_window)
        self.long_ema_trans = EMA(timeperiod=long_window)

        # To keep track of whether we invested in the stock or not
        self.invested = False
コード例 #4
0
class DualEMATaLib(TradingAlgorithm):
    """ Dual Moving Average Crossover algorithm.

    This algorithm buys apple once its short moving average crosses
    its long moving average (indicating upwards momentum) and sells
    its shares once the averages cross again (indicating downwards
    momentum).

    """
    def __init__(self, dbhandler, *args, **kwargs):
        super(DualEMATaLib, self).__init__(*args, **kwargs)
        self.dbhandler = dbhandler
        self.mstockid = self.dbhandler.stock.ids[0]

    def initialize(self, short_window=20, long_window=40):
        # Add 2 mavg transforms, one with a long window, one
        # with a short window.
        self.short_ema_trans = EMA(timeperiod=short_window)
        self.long_ema_trans = EMA(timeperiod=long_window)
        self.real_obv_trans = OBV()

        # To keep track of whether we invested in the stock or not
        self.invested = False

    def handle_data(self, data):
        self.short_ema = self.short_ema_trans.handle_data(data)
        self.long_ema = self.long_ema_trans.handle_data(data)
        self.real_obv = self.real_obv_trans.handle_data(data)
        if self.short_ema is None or self.long_ema is None or self.real_obv is None:
            return

        self.buy = False
        self.sell = False

        # buy/sell rule
        if (self.short_ema > self.long_ema).all() and not self.invested:
            self.order(self.mstockid, 1000)
            self.invested = True
            self.buy = True
        elif (self.short_ema < self.long_ema).all() and self.invested:
            self.order(self.mstockid, -1000)
            self.invested = False
            self.sell = True

        # save to recorder
        signals = {
            'open': data[self.mstockid].open,
            'high': data[self.mstockid].high,
            'low': data[self.mstockid].low,
            'close': data[self.mstockid].close,
            'volume': data[self.mstockid].volume,
            'short_ema': self.short_ema[self.mstockid],
            'long_ema': self.long_ema[self.mstockid],
            'buy': self.buy,
            'sell': self.sell
        }

        self.record(**signals)
コード例 #5
0
def initialize(context):
    context.asset = symbol('AAPL')

    # Add 2 mavg transforms, one with a long window, one with a short window.
    context.short_ema_trans = EMA(timeperiod=20)
    context.long_ema_trans = EMA(timeperiod=40)

    # To keep track of whether we invested in the stock or not
    context.invested = False
コード例 #6
0
    def initialize(self):
        self.asset = self.symbol('AAPL')

        # Add 2 mavg transforms, one with a long window, one with a short window.
        self.short_ema_trans = ta.EMA(timeperiod=20)
        self.long_ema_trans = EMA(timeperiod=40)

        # To keep track of whether we invested in the stock or not
        self.invested = False
コード例 #7
0
ファイル: dual_ema_talib.py プロジェクト: AMarempudi/zipline
class DualEMATaLib(TradingAlgorithm):

    """Dual Moving Average Crossover algorithm.

    This algorithm buys apple once its short moving average crosses
    its long moving average (indicating upwards momentum) and sells
    its shares once the averages cross again (indicating downwards
    momentum).

    """

    def initialize(self, short_window=20, long_window=40):
        # Add 2 mavg transforms, one with a long window, one
        # with a short window.
        self.short_ema_trans = EMA(timeperiod=short_window)
        self.long_ema_trans = EMA(timeperiod=long_window)

        # To keep track of whether we invested in the stock or not
        self.invested = False

    def handle_data(self, data):
        self.short_ema = self.short_ema_trans.handle_data(data)
        self.long_ema = self.long_ema_trans.handle_data(data)
        if self.short_ema is None or self.long_ema is None:
            return

        self.buy = False
        self.sell = False

        if (self.short_ema > self.long_ema).all() and not self.invested:
            self.order('AAPL', 100)
            self.invested = True
            self.buy = True
        elif (self.short_ema < self.long_ema).all() and self.invested:
            self.order('AAPL', -100)
            self.invested = False
            self.sell = True

        self.record(AAPL=data['AAPL'].price,
                    short_ema=self.short_ema['AAPL'],
                    long_ema=self.long_ema['AAPL'],
                    buy=self.buy,
                    sell=self.sell)
コード例 #8
0
ファイル: dual_ema_talib.py プロジェクト: zixan/zipline
class DualEMATaLib(TradingAlgorithm):
    """Dual Moving Average Crossover algorithm.

    This algorithm buys apple once its short moving average crosses
    its long moving average (indicating upwards momentum) and sells
    its shares once the averages cross again (indicating downwards
    momentum).

    """
    def initialize(self, short_window=20, long_window=40):
        # Add 2 mavg transforms, one with a long window, one
        # with a short window.
        self.short_ema_trans = EMA(timeperiod=short_window)
        self.long_ema_trans = EMA(timeperiod=long_window)

        # To keep track of whether we invested in the stock or not
        self.invested = False

    def handle_data(self, data):
        self.short_ema = self.short_ema_trans.handle_data(data)
        self.long_ema = self.long_ema_trans.handle_data(data)
        if self.short_ema is None or self.long_ema is None:
            return

        self.buy = False
        self.sell = False

        if self.short_ema > self.long_ema and not self.invested:
            self.order('AAPL', 100)
            self.invested = True
            self.buy = True
        elif self.short_ema < self.long_ema and self.invested:
            self.order('AAPL', -100)
            self.invested = False
            self.sell = True

        self.record(AAPL=data['AAPL'].price,
                    short_ema=self.short_ema['AAPL'],
                    long_ema=self.long_ema['AAPL'],
                    buy=self.buy,
                    sell=self.sell)
コード例 #9
0
class DualEmaTalib(zp.TradingAlgorithm):
    def __init__(self, *args, **kwargs):
        super(DualEmaTalib, self).__init__(
            *args, **kwargs)  #mid 采用super(child,self).method()格式调用某个类的父类的方法。

    def initialize(self):
        self.asset = self.symbol('AAPL')

        # Add 2 mavg transforms, one with a long window, one with a short window.
        self.short_ema_trans = ta.EMA(timeperiod=20)
        self.long_ema_trans = EMA(timeperiod=40)

        # To keep track of whether we invested in the stock or not
        self.invested = False

    def handle_data(self, data):
        #mid only long
        short_ema = self.short_ema_trans.handle_data(data)
        long_ema = self.long_ema_trans.handle_data(data)
        if short_ema is None or long_ema is None:
            return

        buy = False
        sell = False

        if (short_ema > long_ema).all() and not self.invested:
            self.order(self.asset, 100)
            self.invested = True
            buy = True
        elif (short_ema < long_ema).all() and self.invested:
            self.order(self.asset, -100)
            self.invested = False
            sell = True

        self.record(AAPL=data[self.asset].price,
                    short_ema=short_ema[self.asset],
                    long_ema=long_ema[self.asset],
                    buy=buy,
                    sell=sell)