Beispiel #1
0
class FuturesSpider:
    def __init__(self):
        self.dm = DBManager("Futures_d_table")

    def init_table(self):
        for item in symbol_list:
            self.dm.add_one({
                "code": item.split("/")[0],
                "symbol": item.split("/")[1],
                "details": []
            })

    def start_crawl(self):
        for symbol in symbol_list:
            url = "http://stock2.finance.sina.com.cn/futures/api/json.php/IndexService.getInnerFuturesDailyKLine?symbol=" + symbol.split(
                "/")[0]
            print(url)

            max_try = 8
            for tries in range(max_try):
                try:
                    content = requests.get(url)
                    self.parse_pager(content.content, symbol.split("/")[0])
                    break
                except Exception:
                    if tries < (max_try - 1):
                        sleep(2)
                        continue
                    else:
                        print(symbol, "fail")

    def parse_pager(self, content, code):
        timer_list = [
            x["date"]
            for x in self.dm.find_one_by_key({"code": code})["details"]
        ]
        data = json.loads(content)
        for item in data:
            __dict = {
                "date": item[0],
                "open": item[1],
                "high": item[2],
                "low": item[3],
                "close": item[4],
                "count": item[5]
            }

            if __dict["date"] not in timer_list:
                self.dm.add_futures_item(code, __dict)
        print(code, "success")
Beispiel #2
0
 def update_data(self):
     print("-redis_start-")
     dm = DBManager("wm_details")
     code_list = dm.get_code_list()
     for item in code_list:
         try:
             code = item["code"][:6]
             _result = dm.find_by_id(item["code"])
             sorted_result = sorted(_result["price_list"],
                                    key=lambda x: cmp_datatime(x),
                                    reverse=True)
             self.set_data(code, sorted_result)
         except Exception:
             print("redis_error:501 " + str(item["code"]))
     print("-redis_close-")
 def update_data(self):
     add_info_logs("redis_start", "-开始同步缓存-")
     dm = DBManager("tk_details")
     code_list = dm.get_code_list()
     for item in code_list:
         try:
             code = item["code"][:6]
             _result = dm.find_by_id(item["code"])
             sorted_result = sorted(_result["price_list"],
                                    key=lambda x: cmp_datatime_02(x),
                                    reverse=True)
             self.set_data(code, sorted_result)
         except Exception:
             add_error_logs("redis_error", "501", item["code"])
             continue
     add_info_logs("redis_close", "-结束同步缓存-")
Beispiel #4
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)
Beispiel #5
0
class DataCrawl:
    def __init__(self):
        self.dm = DBManager("wm_details")
        self.headers = {
            "User-Agent":
            ":Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.115 Safari/537.36"
        }

    def start_crawl(self):
        print("-start up-")
        _year, _season = get_cur_season()
        self.get_url(_year, _season)

    def end_crawl(self):
        self.dm.close_db()

    def get_url(self, year, season):
        code_list = self.dm.get_code_list()
        for item in code_list:
            key = item["code"][:6]
            url = "http://quotes.money.163.com/trade/lsjysj_" + key + ".html?year=" + year + "&season=" + season
            print(url)

            max_try = 8
            for tries in range(max_try):
                try:
                    content = requests.get(url)
                    self.parse_pager(content.content, item["code"])
                    break
                except Exception:
                    if tries < (max_try - 1):
                        sleep(2)
                        continue
                    else:
                        print("ERROR TO DOWNLODE DATE")
        code_list.close()

    def parse_pager(self, content, key):
        try:
            _result = self.dm.find_by_id(key)
            timer_list = [x["cur_timer"] for x in _result["price_list"]]
            soup = bs4.BeautifulSoup(content, "lxml")
            parse_list = soup.select("div.inner_box tr")
            for item in parse_list[1:]:
                data = [x.string for x in item.select("td")]
                price = {
                    "cur_timer": data[0],
                    "cur_open_price": data[1],
                    "cur_max_price": data[2],
                    "cur_min_price": data[3],
                    "cur_close_price": data[4],
                    "cur_price_range": data[6],
                    "cur_total_volume": data[7],
                    "cur_total_money": data[8]
                }
                if price["cur_timer"] not in timer_list:
                    self.dm.add_tk_item(key, price)
            print(key, "success")
        except Exception:
            print(key, "faild")
 def __init__(self):
     self.dm = DBManager("futures_d_table")
Beispiel #7
0
def parse_pager(content, ticker):
    timer_list = [
        x["date"] for x in dm.find_one_by_key({"ticker": ticker})["price_list"]
    ]
    while content.next():
        item_row = content.get_row_data()
        __dict = {
            "date": item_row[0],
            "code": item_row[1],
            "open": item_row[2],
            "high": item_row[3],
            "low": item_row[4],
            "close": item_row[5],
            "preclose": item_row[6],
            "volume": item_row[7],
            "amount": item_row[8],
            "adjustflag": item_row[9],
            "turn": item_row[10],
            "tradestatus": item_row[11],
            "pctChg": item_row[12]
        }
        if __dict["date"] not in timer_list:
            dm.add_tk_item(ticker, __dict)
    print(ticker, "success")


if __name__ == '__main__':
    dm = DBManager("fcr_details")
    init_table()
    start_crawl()
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
Beispiel #9
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)
Beispiel #10
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
Beispiel #11
0
def draw_profit_bar(list1, list2):
    plt.subplot(111)
    lable_x = np.arange(len(list1))
    lable_y = [x * 0 for x in range(len(list1))]

    plt.plot(lable_x, lable_y, color="#404040", linewidth=1.0, linestyle="-")
    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]
    plt.subplot(111)
    lable_x = np.arange(len(list1))
    lable_y = [x * 0 for x in range(len(list1))]
    # 绘制中轴线
    plt.plot(lable_x, lable_y, color="#404040", linewidth=1.0, linestyle="-")
    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_wm = DBManager("fcr_details")
    code_list = db_manager_wm.get_code_list()
    result_list = list()
    for item in code_list:
        code = item["code"]
        print(code)
        tk_data = db_manager_wm.find_by_key({"code": code})[0]
        colse_list = [
            float(x["close"]) for x in tk_data["price_list"] if x["close"] != 0
        ]
        len_list = len(colse_list)
        if colse_list:
            # 执行判断逻辑
            for index in range(expect, len(colse_list)):
                rate_1 = (colse_list[index - period] - colse_list[index]
                          ) / colse_list[index - period]  # 下跌幅度
                        item_position[0],
                        str(round(profit_rate * 100, 2)) + "%", capital_base
                    ]))
                    current_position.remove(item_position)
        # 统计历史数据
        history_capital.append(capital_base)
        f_utils.insert_line("cash->" + str(get_all_capital()))


def start_bp():
    st3 = TsStrategy3()
    history_capital.append(capital_base)
    # 初始化时间轴
    date_list = date_range("2017-01-01", "2017-12-31")
    for index in range(len(date_list)):
        cur_date = date_list[index]
        fun_sell(cur_date)
        if datetime.datetime.strptime(cur_date, "%Y-%m-%d").weekday() == 0:
            buy_list = st3.get_buy_list(cur_date)
            if buy_list:
                fun_buy(buy_list, cur_date)
    net_rate = (history_capital[-1] -
                history_capital[0]) / history_capital[0]  # 计算回测结果
    print(round(net_rate * 100, 2), "%")


if __name__ == "__main__":
    f_utils = FileUtils("bp_result3_1.txt", "w")
    db_manager_tk = DBManager("fcr_details")
    start_bp()
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
Beispiel #15
0
 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 __init__(self):
     self.db_manager_wm = DBManager("fcr_w_details")
 def __init__(self):
     self.dm = DBManager("tk_details")
     self.headers = {
         "User-Agent": ":Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.115 Safari/537.36"
     }
Beispiel #18
0
                ]))
                current_position.remove(item_position)
        # 统计历史数据
        history_capital.append(capital_base)
        f_utils.insert_line("cash->" + str(capital_base))


def start_bp():
    ste = TsStrategyExpma()
    history_capital.append(capital_base)
    # 初始化时间轴
    date_list = date_range("2017-05-08", "2017-09-30")
    for index in range(len(date_list)):
        cur_date = date_list[index]
        print(cur_date)
        if datetime.datetime.strptime(cur_date, "%Y-%m-%d").weekday() == 0:
            buy_list = ste.get_buy_list(cur_date)
            if buy_list:
                fun_buy(buy_list, cur_date)
        else:
            fun_sell(cur_date)
    net_rate = (history_capital[-1] -
                history_capital[0]) / history_capital[0]  # 计算回测结果
    print(round(net_rate * 100, 2), "%")


if __name__ == "__main__":
    f_utils = FileUtils("bp_expma_result_1.txt", "a+")
    db_manager_tk = DBManager("tk_details")
    start_bp()
Beispiel #19
0
import os
import sys
sys.path.append('F:\\PYTHON\\TsSpiderServer\\')
from mongo_db.mongodb_manager import DBManager

base_path = os.path.abspath(os.path.join(os.getcwd(), "..")) + "/bean"

if __name__ == '__main__':
    dm = DBManager("wm_details")
    file_path = base_path + "/data_code.txt"
    _file = open(file_path, 'r', encoding='utf_-8')
    tk_list = list()
    while True:
        line = _file.readline()
        if '' == line:
            break
        str_code = line.split()[0]
        str_title = line.split()[1]
        dm.add_one({"code": str_code, "title": str_title, "price_list": []})
Beispiel #20
0
 def __init__(self):
     self.db_manager_wm = DBManager("wm_details")
     self.db_manager_tk = DBManager("tk_details")
Beispiel #21
0
class EmaManager:
    def __init__(self):
        self.db_manager_tk = DBManager("fcr_w_details")

    def get_buy_list(self, date):
        code_list = [x for x in self.db_manager_tk.get_code_list_02()]
        buy_list = list()
        for code_item in code_list:
            ticker = code_item["ticker"]
            # 获取数据
            close_list = list()
            tk_details = self.db_manager_tk.find_one_by_key({"ticker": ticker})
            for tk_item in [x for x in tk_details["price_list"]]:
                if time_cmp(str(date), tk_item["date"]):
                    close_list.append(float(tk_item["close"]))
            # 执行判断条件
            if len(close_list) > 20:
                ema_20_1 = np.mean(close_list[-20:])
                ema_20_2 = np.mean(close_list[-21:-1])
                if close_list[-1] > ema_20_1 and close_list[-2] < ema_20_2:
                    buy_list.append(ticker)
        return buy_list

    def get_sell_list(self, date):
        code_list = [x for x in self.db_manager_tk.get_code_list_02()]
        sell_list = list()
        for code_item in code_list:
            ticker = code_item["ticker"]
            # 获取数据
            close_list = list()
            tk_details = self.db_manager_tk.find_one_by_key({"ticker": ticker})
            for tk_item in [x for x in tk_details["price_list"]]:
                if time_cmp(str(date), tk_item["date"]):
                    close_list.append(float(tk_item["close"]))
            # 执行判断条件
            if len(close_list) > 20:
                ema_10_1 = np.mean(close_list[-10:])
                ema_10_2 = np.mean(close_list[-11:-1])
                if close_list[-1] < ema_10_1 and close_list[-2] > ema_10_2:
                    sell_list.append(ticker)
        return sell_list

    def fun_01(self):
        code_list = [x for x in self.db_manager_tk.get_code_list_02()]
        result_list = list()
        for code_item in code_list:
            ticker = code_item["ticker"]
            # 获取数据
            tk_details = self.db_manager_tk.find_one_by_key({"ticker": ticker})
            close_list = list()
            open_list = list()
            high_list = list()
            low_list = list()
            volume_list = list()
            for x in tk_details["price_list"]:
                if x["close"] != "":
                    close_list.append(float(x["close"]))
                    open_list.append(float(x["open"]))
                    high_list.append(float(x["high"]))
                    low_list.append(float(x["low"]))
                    volume_list.append(float(x["volume"]))
            ema_list = list()
            for i in range(len(close_list)):
                if i >= 20:
                    ema_list.append(np.mean(close_list[i - 20:i]))
                else:
                    ema_list.append(close_list[i])
            # 执行判断条件
            for date in range(len(close_list) - 1):
                # 均线呈现向上趋势
                if ema_list[date] > ema_list[date - 4]:
                    # K线上穿且连续8周位于均线之下
                    if close_list[date] > ema_list[date]:
                        if close_list[date - 1] < ema_list[
                                date - 1] and close_list[date - 2] < ema_list[
                                    date -
                                    2] and close_list[date - 3] < ema_list[
                                        date - 3] and close_list[
                                            date - 4] < ema_list[date - 4]:
                            profit_rate = (
                                close_list[date + 1] -
                                open_list[date + 1]) / open_list[date + 1]
                            result_list.append(profit_rate)
        print(len(result_list))
        result_list_up = [x for x in result_list if x > 0]
        result_list_down = [x for x in result_list if x < 0]
        print(len(result_list_up), max(result_list_up),
              np.mean(result_list_up))
        print(len(result_list_down), min(result_list_down),
              np.mean(result_list_down))
        # 绘图
        plt.subplot(111)
        lable_x = np.arange(len(result_list))
        lable_y = [x * 0 for x in range(len(result_list))]
        # 绘制中轴线
        plt.plot(lable_x,
                 lable_y,
                 color="#404040",
                 linewidth=1.0,
                 linestyle="-")
        plt.bar(lable_x, result_list, color="g", width=1.0)
        plt.xlim(lable_x.min(), lable_x.max() * 1.1)
        plt.ylim(min(result_list) * 1.1, max(result_list) * 1.1)
        plt.grid(True)
        plt.show()

    def fun_get_strategy(self):
        pass