def calc_money_today(self, code):
        self.fi.get_last_result("../result/intelligent_fixed_investment_result")
        fetcher = stock_fetcher()
        self.fi.net = fetcher.get_present_price(code).close_price
        print "net:" + str(self.fi.net)

        self.fi.code = code
        self.fi.no += 1
        self.fi.date = datetime.now().strftime("%Y-%m-%d")

        new_sum = self.fi.sum_month_money * (1 + EXPE_RATE) + GAP
        #new_sum = self.fi.no * GAP * (1 + EXPE_RATE)
        print "new_sum:",new_sum

        now_sum = self.fi.share * self.fi.net
        print "now_sum:", now_sum

        self.fi.month_money = round(new_sum - now_sum, 6)
        self.fi.month_money = self.fi.month_money if self.fi.month_money < MAX_GAP else MAX_GAP
        self.fi.month_money = self.fi.month_money if self.fi.month_money > (MAX_GAP * -1) else (MAX_GAP * -1)

        print "month_money:" + str(self.fi.month_money)

        share = self.fi.month_money * (1 - DEAL_RATE) / self.fi.net
        self.fi.month_share = round(share, 6)
        self.fi.share += self.fi.month_share
    def calc_money_today(self, code):
        self.fi.get_last_result(
            "../result/intelligent_fixed_investment_result")
        self.fi.last_deal_date = self.fi.date
        fetcher = stock_fetcher()
        self.fi.net = fetcher.get_present_price(code).close_price
        print "net:" + str(self.fi.net)

        self.fi.code = code
        self.fi.no += 1
        self.fi.date = datetime.now().strftime("%Y-%m-%d")

        new_sum = self.fi.sum_month_money * (1 + EXPE_RATE) + GAP
        #new_sum = self.fi.no * GAP * (1 + EXPE_RATE)
        print "new_sum:", new_sum

        now_sum = self.fi.share * self.fi.net
        print "now_sum:", now_sum

        self.fi.month_money = round(new_sum - now_sum, 6)
        self.fi.month_money = self.fi.month_money if self.fi.month_money < MAX_GAP else MAX_GAP
        self.fi.month_money = self.fi.month_money if self.fi.month_money > (
            MAX_GAP * -1) else (MAX_GAP * -1)

        print "month_money:" + str(self.fi.month_money)

        share = self.fi.month_money * (1 - DEAL_RATE) / self.fi.net
        self.fi.month_share = round(share, 6)
        self.fi.share += self.fi.month_share
    def calc_money(self, code):
        self.fi.get_last_result(
            "../result/intelligent_fixed_investment_result")
        end_date = datetime.now().strftime("%Y-%m-%d")
        start_date = datetime.strptime(self.fi.date,
                                       "%Y-%m-%d") + timedelta(days=5)
        start_date = start_date.strftime("%Y-%m-%d")
        fetcher = stock_fetcher()

        time_arr = start_date.split("-")
        last_key = time_arr[0] + time_arr[1]

        new_dataset = fetcher.get_his_day_k(code, start_date, end_date)
        for item in new_dataset.data:
            time_arr = item.date.split("-")
            key = time_arr[0] + time_arr[1]
            # print "last_key: ", last_key, " key: ", key

            if (key == last_key):
                last_item = item
                continue

            self.fi.code = code
            last_key = key

            self.fi.no += 1
            self.fi.date = last_item.date
            # print "date: ", self.fi.date

            self.fi.net = round(last_item.close_price, 2)
            # print "net: ", self.fi.net

            new_sum = self.fi.sum * (1 + EXPE_RATE) + GAP
            now_sum = self.fi.share * self.fi.net
            self.fi.month_money = round(new_sum - now_sum, 6)
            self.fi.month_money = self.fi.month_money if self.fi.month_money < MAX_GAP else MAX_GAP
            # print "month_money: ", self.fi.month_money

            self.fi.sum_month_money += self.fi.month_money
            self.fi.sum_month_money = round(self.fi.sum_month_money, 6)

            share = self.fi.month_money * (1 - DEAL_RATE) / self.fi.net
            self.fi.month_share = round(share, 6)
            self.fi.share = round(self.fi.share + share, 6)
            # print "share: ", self.fi.share

            self.fi.sum = round(self.fi.net * self.fi.share, 6)

            self.fi.profit = self.fi.sum - self.fi.sum_month_money
            self.fi.profit = round(self.fi.profit, 6)
            # print "profit: ", self.fi.profit

            self.fi.profit_rate = (self.fi.profit /
                                   self.fi.sum_month_money) * 100
            self.fi.profit_rate = round(self.fi.profit_rate, 2)
            # print "profit_rate: ", self.fi.profit_rate
            self.recode()
    def calc_money(self, code):
        self.fi.get_last_result("../result/intelligent_fixed_investment_result")
        end_date = datetime.now().strftime("%Y-%m-%d")
        start_date = datetime.strptime(self.fi.date, "%Y-%m-%d") + timedelta(days=5)
        start_date = start_date.strftime("%Y-%m-%d")
        fetcher = stock_fetcher()

        time_arr = start_date.split("-")
        last_key = time_arr[0] + time_arr[1]

        new_dataset = fetcher.get_his_day_k(code, start_date, end_date)
        for item in new_dataset.data:
            time_arr = item.date.split("-")
            key = time_arr[0] + time_arr[1]
            # print "last_key: ", last_key, " key: ", key

            if(key == last_key):
                last_item = item
                continue

            self.fi.code = code
            last_key = key

            self.fi.no += 1
            self.fi.date = last_item.date
            # print "date: ", self.fi.date

            self.fi.net = round(last_item.close_price, 2)
            # print "net: ", self.fi.net

            new_sum = self.fi.sum * (1 + EXPE_RATE) + GAP
            now_sum = self.fi.share * self.fi.net
            self.fi.month_money = round(new_sum - now_sum, 6)
            self.fi.month_money = self.fi.month_money if self.fi.month_money < MAX_GAP else MAX_GAP
            # print "month_money: ", self.fi.month_money

            self.fi.sum_month_money += self.fi.month_money
            self.fi.sum_month_money = round(self.fi.sum_month_money, 6)


            share = self.fi.month_money * (1 - DEAL_RATE) / self.fi.net
            self.fi.month_share = round(share, 6)
            self.fi.share = round(self.fi.share + share, 6)
            # print "share: ", self.fi.share

            self.fi.sum = round(self.fi.net * self.fi.share, 6)

            self.fi.profit = self.fi.sum - self.fi.sum_month_money
            self.fi.profit = round(self.fi.profit, 6)
            # print "profit: ", self.fi.profit

            self.fi.profit_rate = (self.fi.profit / self.fi.sum_month_money) * 100
            self.fi.profit_rate = round(self.fi.profit_rate, 2)
            # print "profit_rate: ", self.fi.profit_rate
            self.recode()
    def refresh_his_day_file(self, stockid, days_num, file_name):
        fetcher = stock_fetcher()
        old_dataset = stock_dataset()
        new_data_list = []

        old_dataset.load_from_file(stockid, file_name)
        end_date = datetime.now().strftime("%Y%m%d")
        start_date = (datetime.now() - timedelta(days=days_num)).strftime("%Y%m%d")

        new_dataset = fetcher.get_his_day_k(stockid, start_date, end_date)

        for item in new_dataset.data:
            if old_dataset.get_data(item.date) == None:
                new_data_list.append(item)

        old_dataset.append_to_file(file_name, new_data_list)

        return
    def refresh_his_day_file(self, stockid, days_num, file_name):
        fetcher = stock_fetcher()
        old_dataset = stock_dataset()
        new_data_list = []

        old_dataset.load_from_file(stockid, file_name)
        end_date = datetime.now().strftime("%Y%m%d")
        start_date = (datetime.now() -
                      timedelta(days=days_num)).strftime("%Y%m%d")

        new_dataset = fetcher.get_his_day_k(stockid, start_date, end_date)

        for item in new_dataset.data:
            if old_dataset.get_data(item.date) == None:
                new_data_list.append(item)

        old_dataset.append_to_file(file_name, new_data_list)

        return
Beispiel #7
0
    def monitor(self, code):
        data_set = stock_dataset()
        data_set.load_from_file(code, "../data/" + code)

        #把今天的数据加入到data_set里
        fetcher = stock_fetcher()
        net = fetcher.get_present_price(code).close_price
        stock = stock_data()
        stock.close_price = net
        # today = datetime.now() - timedelta(days = 2)  #周末测试时使用
        today = datetime.now()
        today = today.strftime("%Y-%m-%d")
        stock.date = today
        data_set.data.append(stock)

        turtle_plan3 = turtle()
        result = turtle_plan3.get_trading_plan3(data_set, today)
        result["code"] = code

        f = open("../result/turtle3_result", "r+")
        line = f.readline()
        f.close()
        if line.startswith("#"):
            result["share"] = 0
            result["cost"] = 0
            return result
        arr = line.strip().split("|")
        result["share"] = arr[5]
        result["cost"] = arr[6]
        # if(result["choise"] == 4):  #buy
        #     result["share"] = round((money * (1 - DEAL_RATE) / result["close_price"]), 3)
        #     result["money"] = round(money * (1 - DEAL_RATE), 3)
        #     result["profit"] = profit
        #     self.append_result_to_file(filename, result, delim)
        # elif(result["choise"] == 2): #sell
        #     result["share"] = share
        #     result["money"] = round(result["close_price"] * share * (1 - DEAL_RATE), 3)
        #     result["profit"] = result["money"] - money
        #     self.append_result_to_file(filename, result, delim)
        return result
    def monitor(self, days_gap):

        sh000300_his = stock_dataset()
        sh000300_his.load_from_file("sh000300", "../data/sh000300")

        sh000905_his = stock_dataset()
        sh000905_his.load_from_file("sh000905", "../data/sh000905")

        sz399008_his = stock_dataset()
        sz399008_his.load_from_file("sz399008", "../data/sz399008")

        fetcher = stock_fetcher()

        sh000300_now = fetcher.get_present_price("sh000300")
        sh000905_now = fetcher.get_present_price("sh000905")
        sz399008_now = fetcher.get_present_price("sz399008")

        if sh000300_now == None or sh000905_now == None or sz399008_now == None:
            print "get now price error"
            return None

        if sh000300_now.date != sh000905_now.date or sh000300_now.date != sz399008_now.date:
            print "different date"
            return None

        last_valid_date = datetime.strptime(sh000300_his.data[0].date, "%Y-%m-%d")
        now_str = sh000300_now.date
        now = datetime.strptime(now_str, "%Y-%m-%d")
        last = now - timedelta(days=days_gap)

        while True:
            last_str = last.strftime("%Y-%m-%d")

            sh000300_last = sh000300_his.get_data(last_str)
            sh000905_last = sh000905_his.get_data(last_str)
            sz399008_last = sz399008_his.get_data(last_str)

            if sh000300_last != None and sh000905_last != None and sz399008_last:
                break
            else:
                last = last - timedelta(days=1)
                if last < last_valid_date:
                    return None

        sh000300_up = (sh000300_now.close_price - sh000300_last.close_price) / sh000300_last.close_price
        sh000905_up = (sh000905_now.close_price - sh000905_last.close_price) / sh000905_last.close_price
        sz399008_up = (sz399008_now.close_price - sz399008_last.close_price) / sz399008_last.close_price

        result = {}

        result["now_date"] = now_str
        result["last_date"] = last_str
        result["sh000300"] = [sh000300_last.close_price, sh000300_now.close_price, sh000300_up]
        result["sh000905"] = [sh000905_last.close_price, sh000905_now.close_price, sh000905_up]
        result["sz399008"] = [sz399008_last.close_price, sz399008_now.close_price, sz399008_up]

        if sh000300_up < 0 and sh000905_up < 0 and sz399008_up < 0:
            result["choise"] = "sell all"
            return result

        if sh000300_up > sh000905_up and sh000300_up > sz399008_up:
            result["choise"] = "sh000300"
        elif sh000905_up > sh000300_up and sh000905_up > sz399008_up:
            result["choise"] = "sh000905"
        elif sz399008_up > sh000300_up and sz399008_up > sh000905_up:
            result["choise"] = "sz399008"

        return result