Beispiel #1
0
    def calc_trades(self):
        ma = self._prices.copy()

        ma = ma.sort_values(by=['TIMESTAMP'])
        ma['SLOW'] = ma.PRICE.rolling(window=self._slow,
                                      center=False).mean().round(2)
        ma['FAST'] = ma.PRICE.rolling(window=self._fast,
                                      center=False).mean().round(2)
        ma['DIFF'] = ma.FAST - ma.SLOW
        ma['MOVE'] = (ma.FAST - ma.SLOW) / ma.SLOW
        ma['REGIME'] = np.where(ma.MOVE - self._threshold > 0, 1, 0)
        ma['REGIME'] = np.where(ma.MOVE + self._threshold < 0, -1, ma.REGIME)
        sign_list = []
        last_sign = 0
        last_r = 0
        for r in ma.REGIME:
            sign = np.sign(r - last_r)
            if sign == last_sign:
                sign = 0
            if r == 0:
                sign = 0
            if sign != 0:
                last_sign = sign
            sign_list.append(sign)
            last_r = r
        ma['SIGN'] = sign_list
        ma['ID'] = np.zeros(len(ma.index))
        ma.index = range(len(ma.index))
        idx = next(i for i in ma.index if ma.SIGN[i] == 1)
        ma = ma.drop(ma.index[[range(0, idx)]])
        ma.index = range(len(ma.index))

        last = 0
        for i in ma.index:
            if ma.SIGN[i] == 0:
                ma.at[i, 'ID'] = 0
            else:
                if ma.SIGN[i] == -1:
                    ma.at[i, 'ID'] = last
                else:
                    ma.at[i, 'ID'] = last + 1
                    last = last + 1

        trades = pd.concat([
            pd.DataFrame({
                'PRICE': ma.loc[ma.SIGN == 1, 'PRICE'],
                'ID': ma.loc[ma.SIGN == 1, 'ID'],
                'TIMESTAMP': ma.loc[ma.SIGN == 1, 'TIMESTAMP'],
                'SIGN': 1
            }),
            pd.DataFrame({
                'PRICE': ma.loc[ma.SIGN == -1, 'PRICE'],
                'ID': ma.loc[ma.SIGN == -1, 'ID'],
                'TIMESTAMP': ma.loc[ma.SIGN == -1, 'TIMESTAMP'],
                'SIGN': -1
            }),
        ])
        trades = trades.sort_values(by=['TIMESTAMP'])
        trades.index = range(len(trades.index))

        self._trades = trades
        self._ma = ma

        Strategy.add_dates_to_df(self._ma)
        Strategy.add_dates_to_df(self._trades)
Beispiel #2
0
    def refresh_heikin_prices(self):
        ha_close_list = self._heikin_prices.CLOSE
        ha_open_list = self._heikin_prices.OPEN
        ha_high_list = self._heikin_prices.HIGH
        ha_low_list = self._heikin_prices.LOW
        ha_color_list = self._heikin_prices.COLOR
        ha_close_list_temp = []
        ha_open_list_temp = []
        ha_high_list_temp = []
        ha_low_list_temp = []
        ha_color_list_temp = []
        oldO, oldC = (-1, -1)
        if len(self._heikin_prices) > 0:
            oldO = self._heikin_prices.OPEN.iloc[-1]
            oldC = self._heikin_prices.CLOSE.iloc[-1]

        for i in xrange(len(self._heikin_prices), len(self._prices_step)):
            if oldO == -1:
                oldC = (
                    self._prices_step.OPEN[i] + self._prices_step.CLOSE[i] +
                    self._prices_step.HIGH[i] + self._prices_step.LOW[i]) / 4
                oldO = (self._prices_step.CLOSE[i] +
                        self._prices_step.OPEN[i]) / 2
            heikin = StrategyHEIKINClassic.calc_heikin(
                self._prices_step.OPEN[i], self._prices_step.HIGH[i],
                self._prices_step.LOW[i], self._prices_step.CLOSE[i], oldO,
                oldC)
            oldO, _, _, oldC = heikin
            ha_close_list_temp.append(heikin[3])
            ha_open_list_temp.append(heikin[0])
            ha_high_list_temp.append(heikin[1])
            ha_low_list_temp.append(heikin[2])
            ha_color_list_temp.append(
                np.where(
                    StrategyHEIKINClassic.is_bearish(heikin), 'RED',
                    np.where(StrategyHEIKINClassic.is_bullish(heikin), 'GREEN',
                             'WHITE')))
        ha_close_list = pd.concat(
            [ha_close_list, pd.Series(ha_close_list_temp)], ignore_index=True)
        ha_open_list = pd.concat(
            [ha_open_list, pd.Series(ha_open_list_temp)], ignore_index=True)
        ha_high_list = pd.concat(
            [ha_high_list, pd.Series(ha_high_list_temp)], ignore_index=True)
        ha_low_list = pd.concat(
            [ha_low_list, pd.Series(ha_low_list_temp)], ignore_index=True)
        ha_color_list = pd.concat(
            [ha_color_list, pd.Series(ha_color_list_temp)], ignore_index=True)

        self._heikin_prices = pd.DataFrame({
            'OPEN':
            ha_open_list,
            'HIGH':
            ha_high_list,
            'LOW':
            ha_low_list,
            'CLOSE':
            ha_close_list,
            'COLOR':
            ha_color_list,
            'PRICE':
            [] if not len(self._prices_step) else self._prices_step.CLOSE,
            'TIMESTAMP':
            [] if not len(self._prices_step) else self._prices_step.TIMESTAMP
        })
        Strategy.add_dates_to_df(self._heikin_prices)