Example #1
0
    def profit_calculator(self):
        self._setter()

        if self._validator():
            for date in self._cal_date_list:
                if self._strategy_type == StrategyType.RSI_HF:
                    trader = threading.Thread(target=Strategy.rsi_high_freq,
                                              args=(date, ))
                    trader.start()
                    trader.join()
                elif self._strategy_type == StrategyType.RSI_LF:
                    trader = threading.Thread(target=Strategy.rsi_low_freq,
                                              args=(date, ))
                    trader.start()
                    trader.join()

                current_price = StockTrend.get_price_by_date(date)
                current_capital = (Capital.CASH +
                                   Capital.STOCK_VOLUME * current_price)
                Capital.CAPITAL_TREND[date] = current_capital
                logging.info('[{}] current capital is {}'.format(
                    date, current_capital))

            profit = Capital.CASH + Capital.STOCK_VOLUME * StockTrend.get_price_by_date(
                Stock.END_DATE) - Capital.INITIAL
            logging.info('[Profit] total profit is {}'.format(profit))

        else:
            logging.error("[Exit] Thread would be exited!")
 def trade_date_collection(date):
     start_date = DateUtil.rough_datetime(date, years=-1)
     price_list = list(
         StockTrend.stock_trend_info(Stock.SH_INDEX, start_date,
                                     date).items())
     date_list = [DateUtil.datetime_formatter(x[0]) for x in price_list]
     return date_list
 def stock_sma(date, days=200):
     start_date = DateUtil.rough_datetime(date, years=-1)
     price_list = list(
         StockTrend.stock_trend_info(Stock.SH_INDEX, start_date,
                                     date).items())[:days]
     price_list = [float(x[1]['close']) for x in price_list]
     sma = sum(price_list) / 200
     return sma
Example #4
0
    def _setter(self):
        Stock.CODE = self._stock_code
        Stock.START_DATE = self._cal_date_list[0]
        Stock.END_DATE = self._cal_date_list[-1]
        Capital.CASH = self._cash
        Capital.STOCK_VOLUME = self._volume
        Capital.STOCK = Capital.STOCK_VOLUME * StockTrend.get_price_by_date(
            Stock.START_DATE)
        Capital.INITIAL = Capital.STOCK + Capital.CASH

        logging.info('[Capital] Initial capital in cash is {}'.format(
            Capital.CASH))
        logging.info('[Capital] Initial capital in stock is {}'.format(
            Capital.STOCK))
        logging.info(
            '[Capital] Total initial capital is {}'.format(Capital.CASH +
                                                           Capital.STOCK))
Example #5
0
    def rsi_high_freq(date):
        market_status = StockMarket.market_status(date)
        rsi = StockIndex.rsi(date)
        grades = (rsi - 50) / 50
        if grades >= 0:
            alpla = 1 if market_status == MarketStatus.BEAR else 0.8
            volume = Capital.STOCK_VOLUME
            selling_volume = abs(int(volume * grades * alpla))
            TradeUtil.sell(selling_volume, date)
            logging.info('[{}] sell {}'.format(date, selling_volume))

        else:
            alpla = 1 if market_status == MarketStatus.BULL else 0.8
            volume = Capital.CASH // StockTrend.get_price_by_date(date)
            buying_volume = abs(int(volume * grades * alpla))
            TradeUtil.buy(buying_volume, date)
            logging.info('[{}] buy {}'.format(date, buying_volume))
    def rsi(date, days=14):
        start_date = DateUtil.rough_datetime(date, months=-1)
        trend_list = list(
            StockTrend.stock_trend_info(Stock.CODE, start_date,
                                        date).items())[1:days + 1]
        price_list = [float(x[1]['close']) for x in trend_list]

        rise_sum, down_sum = 0, 0
        for i in range(len(price_list) - 1):
            balance = price_list[i + 1] - price_list[i]
            if balance > 0:
                rise_sum += balance
            else:
                down_sum -= balance
        if (rise_sum + down_sum) != 0:
            rsi = 100 * rise_sum / (rise_sum + down_sum)
        else:
            rsi = 50
        return rsi
Example #7
0
    def rsi_low_freq(date):
        market_status = StockMarket.connors_market_status(date)
        exit_point = StockMarket.connors_exit_condition(date)
        rsi_2 = StockIndex.rsi_2(date)

        if rsi_2 > 80 and exit_point != MarketCondition.LONG:
            alpha = 0.8 if market_status == MarketStatus.BULL else 1
            grade = (rsi_2 - 80) / 10 if rsi_2 < 90 else pow(
                (rsi_2 - 80) / 10, 2)
            volume = Capital.STOCK_VOLUME
            selling_volume = abs(int(volume * grade * alpha / 4))
            TradeUtil.sell(selling_volume, date)
            logging.info('[{}] sell {}'.format(date, selling_volume))

        elif rsi_2 < 20 and exit_point != MarketCondition.SHORT:
            alpha = 1 if market_status == MarketStatus.BULL else 0.8
            grade = (20 - rsi_2) / 10 if rsi_2 > 10 else pow(
                (20 - rsi_2) / 10, 2)
            volume = Capital.CASH // StockTrend.get_price_by_date(date)
            buying_volume = abs(int(volume * grade * alpha / 4))
            TradeUtil.buy(buying_volume, date)
            logging.info('[{}] buy {}'.format(date, buying_volume))
Example #8
0
 def buy(volume, date):
     Capital.CASH -= volume * StockTrend.get_price_by_date(date)
     Capital.STOCK += volume * StockTrend.get_price_by_date(date)
     Capital.STOCK_VOLUME += volume
Example #9
0
 def sell(volume, date):
     Capital.CASH += volume * StockTrend.get_price_by_date(date)
     Capital.STOCK -= volume * StockTrend.get_price_by_date(date)
     Capital.STOCK_VOLUME -= volume
Example #10
0
 def show():
     StockTrend.stock_trend_chart(Capital.CAPITAL_TREND)
 def connors_exit_condition(date):
     price = StockTrend.get_price_by_date(date)
     if price >= StockMarket.stock_sma(date, days=5):
         return MarketCondition.SHORT
     else:
         return MarketCondition.LONG
 def connors_market_status(date):
     price = StockTrend.get_price_by_date(date)
     if price < StockMarket.stock_sma(date):
         return MarketStatus.BEAR
     else:
         return MarketStatus.BULL
#!/usr/bin/env python
# -*- coding: utf-8 -*-

from common.constants import Stock
from utils.stock_trend_util import StockTrend


def set_value(code, start_date, end_date):
    Stock.CODE = code
    Stock.START_DATE = start_date
    Stock.END_DATE = end_date


if __name__ == '__main__':
    set_value(600170, '20180101', '20180223')
    stock_list = StockTrend.stock_trend_info(Stock.CODE, Stock.START_DATE,
                                             Stock.END_DATE)
    StockTrend.stock_trend_chart(stock_list)