예제 #1
0
def fun_02():
    date_list = date_range("2017-05-04", "2018-05-21")
    for date in date_list:
        code = "000001.XSHE"
        price_list = list()
        tur_list = list()
        highest_list = list()
        open_list = list()
        # 获取wmacd数据
        db_manager_wm = DBManager("wm_details")
        tk_details = db_manager_wm.find_by_key({"code": code})[0]
        for tk_item in [
                x for x in tk_details["price_list"] if x["close_price"] != 0
        ]:
            if time_cmp(str(date), tk_item["date_list"][-2]):
                price_list.append(tk_item["close_price"])
                tur_list.append(tk_item["total_volume"])
                highest_list.append(tk_item["max_price"])
                open_list.append(tk_item["open_price"])
        print(price_list, date)
예제 #2
0
class WmacdUtils:
    def __init__(self):
        self.db_manager_wm = DBManager("wm_details")
        self.db_manager_tk = DBManager("tk_details")

    # 初始化wmacd数据
    def init_w_time(self):
        # 初始化时间轴
        date_list = date_range("2016-01-04", "2018-12-30")
        # tk_details = self.dm.find_by_key({"code": code, "cur_timer": {"$in": cur_date_list}})
        code_list = self.db_manager_wm.get_code_list()
        for code_item in code_list:
            code = code_item["code"]
            print(code)
            tk_result = self.db_manager_tk.find_by_key({"code": code})[0]
            tk_details = sorted(tk_result["price_list"],
                                key=lambda x: cmp_datatime_02(x),
                                reverse=False)
            for index in range(len(date_list)):
                if datetime.datetime.strptime(date_list[index],
                                              "%Y-%m-%d").weekday() == 0:
                    cur_date_list = date_list[index:index + 7]
                    # 从数据库中获取这个时间段内的数据
                    cur_tk_details = [
                        x for x in tk_details
                        if x["cur_timer"] in cur_date_list
                    ]
                    try:
                        open_price_list = [
                            float(x["cur_open_price"]) for x in cur_tk_details
                        ]
                        max_price_list = [
                            float(x["cur_max_price"]) for x in cur_tk_details
                        ]
                        min_price_list = [
                            float(x["cur_min_price"]) for x in cur_tk_details
                        ]
                        close_price_list = [
                            float(x["cur_close_price"]) for x in cur_tk_details
                        ]
                        total_volume_list = [
                            int(x["cur_total_volume"].replace(",", ""))
                            for x in cur_tk_details
                        ]
                        total_money_list = [
                            int(x["cur_total_money"].replace(",", ""))
                            for x in cur_tk_details
                        ]
                    except Exception as e:
                        continue
                    if cur_tk_details:
                        wmacd_item = {
                            "frist_date": cur_date_list[0],
                            "date_list": cur_date_list,
                            "open_price": open_price_list[0],
                            "max_price": max(max_price_list),
                            "min_price": min(min_price_list),
                            "close_price": close_price_list[-1],
                            "total_volume": sum(total_volume_list),
                            "total_money": sum(total_money_list),
                        }
                    else:
                        wmacd_item = {
                            "frist_date": cur_date_list[0],
                            "date_list": cur_date_list,
                            "open_price": 0,
                            "max_price": 0,
                            "min_price": 0,
                            "close_price": 0,
                            "total_volume": 0,
                            "total_money": 0,
                        }
                    # 在数据库中添加一条记录
                    self.db_manager_wm.add_tk_item(code, wmacd_item)

    def update_w_macd(self, cur_date=datetime.datetime.now().date()):
        date_list = date_range("2016-01-04", "2018-12-30")
        for index in range(len(date_list)):
            # 匹配到当前时间所在的区间
            if datetime.datetime.strptime(date_list[index],
                                          "%Y-%m-%d").weekday() == 0:
                cur_date_list = date_list[index:index + 7]
                if str(cur_date) in cur_date_list:
                    ticker_list = self.db_manager_wm.get_code_list()
                    # 更新每支股票的数据
                    for tk_item in ticker_list:
                        code = tk_item["code"]
                        tk_result = self.db_manager_tk.find_by_key(
                            {"code": code})[0]
                        tk_details = sorted(tk_result["price_list"],
                                            key=lambda x: cmp_datatime_02(x),
                                            reverse=False)
                        # 从数据库中获取这个时间段内的数据
                        cur_tk_details = [
                            x for x in tk_details
                            if x["cur_timer"] in cur_date_list
                        ]
                        open_price_list = [
                            float(x["cur_open_price"]) for x in cur_tk_details
                        ]
                        max_price_list = [
                            float(x["cur_max_price"]) for x in cur_tk_details
                        ]
                        min_price_list = [
                            float(x["cur_min_price"]) for x in cur_tk_details
                        ]
                        close_price_list = [
                            float(x["cur_close_price"]) for x in cur_tk_details
                        ]
                        total_volume_list = [
                            int(x["cur_total_volume"].replace(",", ""))
                            for x in cur_tk_details
                        ]
                        total_money_list = [
                            int(x["cur_total_money"].replace(",", ""))
                            for x in cur_tk_details
                        ]
                        if cur_tk_details:
                            wmacd_item = {
                                "frist_date": cur_date_list[0],
                                "date_list": cur_date_list,
                                "open_price": open_price_list[0],
                                "max_price": max(max_price_list),
                                "min_price": min(min_price_list),
                                "close_price": close_price_list[-1],
                                "total_volume": sum(total_volume_list),
                                "total_money": sum(total_money_list),
                            }
                            # 修改数据库中的数据
                            self.db_manager_wm.update_wm_price_list(
                                code, wmacd_item["frist_date"], wmacd_item)
예제 #3
0
class TsStrategy5f(object):
    def __init__(self):
        self.db_manager_wm = DBManager("fcr_w_details")

    def get_result(self, ticker):
        if isinstance(ticker,
                      tkWMacdBean) and len(ticker.get_wmacd_list()) > 30:
            if ticker.get_wmacd_list()[-1] > 0 >= ticker.get_wmacd_list()[-2]:
                if 0.1 > ticker.get_diff_list()[-1] > 0:
                    if np.mean(ticker.get_tur_list()
                               [-5:-1]) < ticker.get_tur_list()[-1]:
                        return 1
        return -1

    # 排除一些虚假信号
    def get_result2(self, ticker):
        if isinstance(ticker,
                      tkWMacdBean) and len(ticker.get_wmacd_list()) > 30:
            if ticker.get_wmacd_list()[-1] > 0 >= ticker.get_wmacd_list()[-2]:
                if 0.1 > ticker.get_diff_list()[-1] > 0:
                    if np.mean(ticker.get_tur_list()
                               [-5:-1]) < ticker.get_tur_list()[-1]:
                        if ticker.get_wmacd_list(
                        )[-3] < 0 and ticker.get_wmacd_list(
                        )[-4] < 0 and ticker.get_wmacd_list()[-5] < 0:
                            return 1
        return -1

    def get_result3(self, ticker):
        if isinstance(ticker,
                      tkWMacdBean) and len(ticker.get_wmacd_list()) > 30:
            if ticker.get_wmacd_list()[-1] > 0 >= ticker.get_wmacd_list()[-2]:
                if 0.1 > ticker.get_diff_list()[-1] > 0:
                    if np.mean(ticker.get_tur_list()
                               [-5:-1]) < ticker.get_tur_list()[-1]:
                        if ticker.get_wmacd_list(
                        )[-3] < 0 and ticker.get_wmacd_list(
                        )[-4] < 0 and ticker.get_wmacd_list()[-5] < 0:
                            return 1
        return -1

    def get_buy_list(self, date):
        code_list = self.db_manager_wm.get_code_list()
        buy_list = list()
        for item in code_list:
            try:
                code = item["code"]
                price_list = list()
                tur_list = list()
                highest_list = list()
                open_list = list()
                date_list = list()
                # 获取wmacd数据
                tk_details = self.db_manager_wm.find_by_key({"code": code})[0]
                for tk_item in [
                        x for x in tk_details["price_list"] if x["close"] != 0
                ]:
                    if time_cmp(str(date), tk_item["date"]):
                        date_list.append(tk_item["date"])
                        price_list.append(float(tk_item["close"]))
                        tur_list.append(float(tk_item["volume"]))
                        highest_list.append(float(tk_item["high"]))
                        open_list.append(float(tk_item["open"]))
                wmacd_list, diff_list, dea_list = self.get_w_macd(
                    price_list[:])
                # 创建wmacd实体
                tk_bean = tkWMacdBean(code, price_list, wmacd_list, diff_list,
                                      dea_list, tur_list, highest_list,
                                      open_list)
                if self.get_result2(tk_bean) == 1:
                    buy_list.append(code)
            except Exception as e:
                continue
        return buy_list

    # 计算当前时间段的wmacd值
    def get_w_macd(self, price_list):
        ema_12_list = list()
        for index in range(len(price_list)):
            if index == 0:
                ema_12_list.append(price_list[0])
            else:
                ema_12_list.append(
                    round(
                        ema_12_list[index - 1] * 11 / 13 +
                        price_list[index] * 2 / 13, 4))
        ema_26_list = list()
        for index in range(len(price_list)):
            if index == 0:
                ema_26_list.append(price_list[0])
            else:
                ema_26_list.append(
                    round(
                        ema_26_list[index - 1] * 25 / 27 +
                        price_list[index] * 2 / 27, 4))
        diff_list = list()
        for index in range(len(ema_12_list)):
            diff = ema_12_list[index] - ema_26_list[index]
            diff_list.append(diff)
        dea_list = list()
        for index in range(len(diff_list)):
            if index == 0:
                dea_list.append(diff_list[0])
            else:
                dea_list.append(
                    round(dea_list[index - 1] * 0.8 + diff_list[index] * 0.2,
                          4))
        wmacd_list = list()
        for index in range(len(dea_list)):
            bar = (diff_list[index] - dea_list[index]) * 3
            wmacd_list.append(bar)
        return wmacd_list, diff_list, dea_list
class TsStrategyExpma:
    def __init__(self):
        self.db_manager_wm = DBManager("wm_details")

    def get_result(self, ema_12_list, ema_26_list):
        if len(ema_12_list) > 30 and ema_12_list[-1] > ema_26_list[
                -1] and ema_12_list[-2] < ema_26_list[-2]:
            return 1
        return -1

    def get_buy_list(self, date):
        code_list = self.db_manager_wm.get_code_list()
        buy_list = list()
        for item in code_list:
            try:
                code = item["code"]
                price_list = list()
                expma_list = list()
                # 获取ema数据
                tk_details = self.db_manager_wm.find_by_key({"code": code})[0]
                for tk_item in [
                        x for x in tk_details["price_list"]
                        if x["close_price"] != 0
                ]:
                    if time_cmp(str(date), tk_item["date_list"][-2]):
                        price_list.append(tk_item["close_price"])
                ema_12_list, ema_26_list = self.get_w_expma_2(price_list[:])
                if self.get_result(ema_12_list, ema_26_list) == 1:
                    buy_list.append(code)
            except Exception as e:
                continue
        return buy_list

    # 将策略结果同步到redis
    def update_redis(self, date):
        buy_list = self.get_buy_list(date)
        rm = RedisManager()
        rm.set_data("ema_" + str(date), buy_list)

    # 计算当前时间段ema值
    def get_w_expma(self, price_list):
        ema_12_list = list()
        for index in range(len(price_list)):
            if index == 0:
                ema_12_list.append(price_list[0])
            else:
                ema_12_list.append(
                    round(
                        ema_12_list[index - 1] * 11 / 13 +
                        price_list[index] * 2 / 13, 4))
        ema_26_list = list()
        for index in range(len(price_list)):
            if index == 0:
                ema_26_list.append(price_list[0])
            else:
                ema_26_list.append(
                    round(
                        ema_26_list[index - 1] * 25 / 27 +
                        price_list[index] * 2 / 27, 4))
        return ema_12_list, ema_26_list

    # 计算当前时间段ema值
    def get_w_expma_2(self, price_list):
        ema_12_list = list()
        for index in range(len(price_list)):
            if index == 0:
                ema_12_list.append(price_list[0])
            else:
                ema_12_list.append(
                    round(
                        ema_12_list[index - 1] * 4 / 6 +
                        price_list[index] * 2 / 6, 4))
        ema_26_list = list()
        for index in range(len(price_list)):
            if index == 0:
                ema_26_list.append(price_list[0])
            else:
                ema_26_list.append(
                    round(
                        ema_26_list[index - 1] * 11 / 13 +
                        price_list[index] * 2 / 13, 4))
        return ema_12_list, ema_26_list
예제 #5
0
class AccountBean(object):
    def __init__(self, capital_base=1000000):
        self.capital_base = capital_base  # 起始资金
        self.capital_available = capital_base  # 可用资金
        self.position_list = list()  # 当前持仓
        self.history_order_list = list()  # 订单记录
        self.db_manager_tk = DBManager("fcr_details")

    def fun_buy(self, order):
        if isinstance(order, OrderBean):
            open_price = self.get_cur_values(order.ticker, order.date, "open")
            if open_price <= 0 or open_price * order.amount > self.capital_available or self.get_cur_weekday(
                    order.date) >= 5:
                return
            if self.get_position_by_ticker(order.ticker):  # 加仓
                item_position = self.get_position_by_ticker(order.ticker)
                surplus_value = ((item_position.price * item_position.amount) +
                                 (open_price * order.amount)) / (
                                     item_position.amount + order.amount)
                item_position.price = surplus_value  # 修改剩余价值
                item_position.amount = order.amount + item_position.amount
                self.capital_available -= open_price * order.amount
            else:  # 开仓
                item_position = PositionBean(order.ticker, open_price,
                                             order.amount, order.date)
                self.position_list.append(item_position)
                self.capital_available -= open_price * order.amount
            self.history_order_list.append(order)

    def fun_sell(self, order):
        if isinstance(order, OrderBean):
            item_position = self.get_position_by_ticker(order.ticker)
            if self.get_cur_weekday(order.date) < 5 and item_position:
                close_price = self.get_cur_values(order.ticker, order.date,
                                                  "close")
                if self.get_date_diff(item_position.date,
                                      order.date) > 0 and close_price > 0:
                    if order.amount < item_position.amount:  # 减仓
                        surplus_value = (
                            (item_position.price * item_position.amount) -
                            (close_price * order.amount)) / (
                                item_position.amount - order.amount)
                        item_position.price = surplus_value  # 修改剩余价值
                        item_position.amount = item_position.amount - order.amount  # 修改剩余持仓
                        self.capital_available += close_price * order.amount
                    else:  # 平仓
                        print(self.capital_available)
                        self.capital_available += close_price * item_position.amount
                        print(self.capital_available, close_price,
                              item_position.price)
                        self.position_list.remove(item_position)

    def get_totla_capital(self):
        totla_capital = self.capital_available
        for position in self.position_list:
            totla_capital += position.price * position.amount
        return totla_capital

    def get_position_by_ticker(self, ticker):
        for item in self.position_list:
            if isinstance(item, PositionBean) and item.ticker == ticker:
                return item
        return []

    def get_cur_values(self, code, date, key):
        result = [
            x[key] for x in self.db_manager_tk.find_by_key({'code': code})[0]
            ["price_list"] if x["date"] == date
        ]
        if result:
            return round(float(result[0]), 2)
        return 0

    def get_cur_weekday(self, date):
        return datetime.datetime.strptime(date, "%Y-%m-%d").weekday()

    def get_date_diff(self, start, end, format="%Y-%m-%d"):
        strptime, strftime = datetime.datetime.strptime, datetime.datetime.strftime
        days = (strptime(end, format) - strptime(start, format)).days
        return int(days)

    @property
    def capital_base(self):
        return self._capital_base

    @property
    def capital_available(self):
        return self._capital_available

    @property
    def position_list(self):
        return self._position_list

    @property
    def history_order_list(self):
        return self._history_order_list

    @capital_base.setter
    def capital_base(self, value):
        self._capital_base = value

    @capital_available.setter
    def capital_available(self, value):
        self._capital_available = value

    @position_list.setter
    def position_list(self, value):
        self._position_list = value

    @history_order_list.setter
    def history_order_list(self, value):
        self._history_order_list = value
예제 #6
0
    plt.bar(lable_x, list1, color="r", width=1.0)
    plt.bar(lable_x, list2, color="g", width=1.0)
    plt.xlim(lable_x.min(), lable_x.max() * 1.1)
    plt.ylim(min(list2) * 1.1, max(list1) * 1.1)
    plt.grid(True)
    plt.show()


if __name__ == '__main__':
    db_manager = DBManager("wm_details")
    code_list = db_manager.get_code_list()
    result_list = list()
    for item in code_list:
        code = item["code"]
        print(code)
        tk_data = db_manager.find_by_key({"code": code})[0]
        close_list = [
            float(x["cur_close_price"]) for x in tk_data["price_list"]
            if x["cur_close_price"] != 0
        ]
        len_list = len(close_list)
        if close_list:
            for index in range(expect, len(close_list)):
                rate_1 = (close_list[index - period] -
                          close_list[index]) / close_list[index - period]
                if rate_1 > rate_fail and len_list > index + expect:
                    exp_top = max(close_list[index + 1:index + expect])
                    exp_low = min(close_list[index + 1:index + expect])
                    rate_2 = (exp_top - close_list[index]) / close_list[index]
                    rate_3 = (exp_low - close_list[index]) / close_list[index]
                    result_list.append({