Пример #1
0
    def run(self):
        count = 0
        years = {}
        for year in range(self._hstart[0], self._hend[0] + 1):
            print "get index from ", year
            start = (year, self._rstart[1], self._rstart[2])
            end = (year + self._rend[0], self._rend[1], self._rend[2])
            #for every stock
            ss = Globals.get_instance().stocks()
            count2 = 0
            thisyear = {}
            for s in ss:
                S = ss[s]
                ids = {}
                for index in self._indexs:
                    idx = index  #()
                    idx.setup(start, end, S)
                    v = idx.value()
                    ids[idx.name()] = v
                thisyear[s] = ids
                count2 += 1
                if count2 > 7 and self.DEBUG:
                    break
#                    pass
            years[year] = thisyear
            count += 1
            if count > 2 and self.DEBUG:
                break


#        print years
        json.dump(years, open("output/temp/years", 'w'))
        pass
Пример #2
0
    def run(self, args, opts):
        gbls = Globals.get_instance()
        ohr1 = gbls.oneholdedrecord("input/holded/holded-20181110-0930")
        ohr2 = gbls.oneholdedrecord("input/holded/holded-20181120-1330")

        for s in ohr2.stocks():
            if ohr2.gettotal(s) < 1:
                continue
            if not s in ohr1.stocks():
                continue

            if not ohr1.gettotalholded(s) == ohr2.gettotalholded(s):
                continue

            print s, ohr1.gettotal(s), ohr1.getprice(s), ohr1.gettotalholded(s)
            print s, ohr2.gettotal(s), ohr2.getprice(s), ohr2.gettotalholded(s)
            stock = gbls.getstock(s)
            io = InvestOne(stock)
            io.buyshare(ohr1.gettotalholded(s), (2018, 11, 12))
            hps1 = stock.historypricesshare((2018, 11, 12))
            hps2 = stock.historypricesshare((2018, 11, 21))
            print s, hps1[0], hps2[0]

            money = io.sell((2018, 11, 21))
            delta1 = (ohr2.gettotal(s) - ohr1.gettotal(s)) / ohr1.gettotal(s)
            delta2 = io.added()
            print s, delta1, delta2
            print ""
Пример #3
0
    def run(self, args, opts):
        gbls = Globals.get_instance()
        count = 0
        self._stats = {"total":0, "nodata":0, "none": 0, "zero":0, "same": 0, "minidelta":0, "sendingshare":0, "addshare":0, "left": 0,}
        for s in gbls.stocks():
            S = gbls.stocks()[s]
            self.verifytotalshares(S)
            count += 1
            if count > 10:
                pass
#                break

        total = self._stats["total"]
        print "nodata: number of stocks without price data \t", self._stats["nodata"]
        print ""

        self.printstat("none", "price or marketvalue are None, 比如节假日休市没有数据\t", total)
        self.printstat("zero", "price or marketvalue are zero, 比如停牌\t\t", total)
        self.printstat("same", "totalshare is not changed\t\t\t", total)
        self.printstat("minidelta", "totalshare 变更较小\t\t\t", total)
        self.printstat("sendingshare", "送股行为\t\t\t\t", total)
        self.printstat("addshare", "增资扩股\t\t\t\t", total)

        self.printstat("left", "left cases\t\t\t", total)

        print "Left: ", self._stats["left"]
Пример #4
0
    def setup(self, start, end):
        try:
            startdate = datetime.date(start[0], start[1], start[2])
        except:
            return

        tmptime = time.mktime(startdate.timetuple())
        endstr = "%04d-%02d-%02d" % (end[0], end[1], end[2])
        init = prev = self._db["%04d-%02d-%02d" % start]
        while True:
            tmptime += (3600 * 24)
            dt = datetime.date.fromtimestamp(tmptime)
            if str(dt) > endstr:
                break
            share = {}
            for idx in init:
                S = Stock(Globals.get_instance(), idx)
                ps = S.historypricesshare((dt.year, dt.month, dt.day))
                if ps == None:
                    share[idx] = (prev[idx][0], prev[idx][1])
                else:
                    if ps[1] == 1:
                        share[idx] = (prev[idx][0], ps[0])
                    elif ps[1] == 0:
                        share[idx] = (prev[idx][0], ps[0])
                    else:
                        share[idx] = (prev[idx][0] * ps[1], ps[0])
                prev = share
            self._db[str(dt)] = share
        pass
Пример #5
0
    def run(self, args, opts):
        gbls = Globals.get_instance()
        fails = 0
        for s in gbls.stocks():
            S = gbls.stocks()[s]
            recent = S.price((2018, 11, 30))
            history = S.price((2018, 11, 29))
            if self.zero(recent) and self.zero(history):
                continue

            if self.zero(recent) or self.zero(history):
                print s, recent, history
                fails += 1
                continue

            delta = (recent - history) / history
            if abs(delta) > 0.10:
                print s, delta
                fails += 1
                continue

        delta = (fails * 1.0) / len(gbls.stocks())
        print "Mismatch ratio: ", delta * 100, "%"
        if delta < 0.1:
            self._result = True
        else:
            self._result = False
Пример #6
0
 def caculateaddings(self, starty, endy, func):
     self._ss = Globals.get_instance().stocks()
     print "total samples:", (endy - starty) * len(self._ss) / 2
     ys = {}
     cnt = 0
     for y in range(starty, endy):
         yp = self.yearprocess(y)
         ys[y] = yp
         if cnt > 1 and self.DEBUG:
             break
         cnt += 1
     func(ys)
Пример #7
0
    def run(self):
        ids = Globals.get_instance().stocks().keys()

        os.chdir("./output/historyprices/")
        # for each stock
        for idx in ids:
            self.downloadstock(idx)
        # download index
        # SZSE.399001 深证成指, SZSE.399006 创业板指, SHSE.000001 上证综指, SZSE.399300 沪深300

        for idx in ("1399001", "1399006", "0000001", "0000300"):
            self.downloadindex(idx)
        os.chdir("../../")
        pass
Пример #8
0
    def select(self, ids):
        no = 1
        for i in ids:
            print no, i, Globals.get_instance().getstockidnamemapping(
            ).getname(i)
            no += 1

        while True:
            choice = raw_input("Please select:")
            try:
                k = int(choice)
                if k < no and k > 0:
                    return ids[k - 1]
            except:
                pass
Пример #9
0
    def __init__(self, ss, start, end):
        share = {}
        for idx in ss:
            S = Stock(Globals.get_instance(), idx)
            ps = S.historypricesshare(start)
            #use 20000 to buy it in 100 shares
            if ps == None:
                share[idx] = (0, 0)
            else:
                share[idx] = (int(20000 / ps[0]), ps[0])
        self._db = {}
        self._db["%04d-%02d-%02d" % start] = share

        self.setup(start, end)
        pass
Пример #10
0
    def test4(self):
        self._totalsample = 0
        self._mismatchsample = 0

        ss = Globals.get_instance().getff10jqka().allstocks()
        count = 0
        for s in ss:
            self.compare(s, "sales_adding")
            count += 1
#            if count > 10:
#                break

        ratio = (self._mismatchsample * 1.0 / self._totalsample)
        print "mismatch ratio: ", ratio
        if abs(ratio) < 0.06:
            print "Test 4 Pass\n"

        pass
Пример #11
0
    def run(self):
        allstocks = Globals.get_instance().stocks().keys()

        count = 0
        mylist = ""
        self.plist = {}
        for item in allstocks:
            mylist += StockCommon().stockidlocation(item) + ","
            count += 1
            if count == 3:
                self.request(mylist)
                count = 0
                mylist = ""
        if count > 0:
            self.request(mylist)
        n = datetime.datetime.now()
        name = Config.PRICES_PATH + "/%04d%02d%02d-%02d%02d" % (
            n.year, n.month, n.day, n.hour, n.minute)
        json.dump(self.plist, open(name, 'w'))
Пример #12
0
    def __init__(
        self,
        stockid,
        source=None
    ):  # consturct from FFNetease, if not suitable, consturct from 10jqka
        if source == None:
            source = self.NETEASE

        self._ff = None
        if source == self.TONGHS:
            from moneyseav3.globals import Globals
            path = Globals.get_instance().getff10jqka().getpath(
                stockid) + "/finance"
            self._ff = FF10jqka(path)
            self._ff.doparse()
        else:  #netease
            self._ff = FFNetEase(Config.STOCKS_PATH_NETEASE + "/zycwzb_" +
                                 stockid + ".html?type=report")
            self._ff.doparse()
            pass
        pass
Пример #13
0
    def run(self, args, opts):
        if len(args) < 1:
            print "Please specific the stock in id or pinyin"
            return

        try:
            int(args[0])
            idx = args[0]
        except:
            #it is pinyin
            ids = Globals.get_instance().getinputstocks().getids(args[0])
            if len(ids) == 1:
                idx = ids[0]
            elif len(ids) > 1:
                idx = self.select(ids)
            else:
                print "Please specific a correct stock"
                return

        stock = Stock(idx, DefaultAdding(idx))

        print stock.id()
        print stock.name()
        print stock.types()
        print "industry:", stock.industry()
        print stock.n(), stock.p()

        if not stock.ffvalid()[0]:
            print "No valid financial data"
            return

        print "addings: ", stock.addings()

        print "select adding:", stock.a()
        print "pershareearning:", stock.e()
        print "price:", stock.price()
        print "e/q:", stock.eq()
        print "q(reasonable price):", stock.q()
        print "dratio(price adding predict):", stock.dratio()
Пример #14
0
    def run1(self, args, opts):
        gbls = Globals.get_instance()
        sid = "300230"
        S = gbls.stocks()[sid]

        startstr = "2010-01-01"
        endstr = "2018-11-30"
        items = startstr.split("-")
        try:
            startdate = datetime.date(int(items[0]), int(items[1]), int(items[2]))
        except:
            #without correct history prices
            return
        tmptime = time.mktime(startdate.timetuple())

        while True:
            dt = datetime.date.fromtimestamp(tmptime)
            if str(dt) > endstr:
                break

            p = S.historyprice(self.date(dt))
            ps = S.historypricesshare(self.date(dt))
            if (p == None or p < 0.001) and ps == None:
                tmptime += (3600*24)
                continue

            if p == None or ps == None:
                print "Warning: not match", p, ps
                tmptime += (3600*24)
                continue

            delta = (ps[0] - p)/p
            if delta < 0.00001:
                tmptime += (3600*24)
                continue
            print "Warning: mismatch"

            tmptime += (3600*24)
Пример #15
0
        eqpa = EQPrevAdding(self._tag, self._range, Settings.REF_PRICE_DATE)
        eqpa.setup(Settings.PLAN_BUY_DATE, None, S, reportseason = Settings.REPORT_SEASON)
        if eqpa.value() == None:
            return (False, "fail")
        return (True, eqpa.value())

    def name(self):         # name of this filter
        return "eqpa_" + self._tag + str(self._range)

    def selectratio(self, ratio):    # the ratio we use to select stocks per this property
        return (0.5)


if __name__ == "__main__":
    from moneyseav3.globals import Globals

    s = Globals.get_instance().getstock("300230")

    eqrap = EQRA_Prop("profit", -1)
    print eqrap.name()
    print eqrap.run(s)

    print "---"

    eqpap = EQPA_Prop("profit", -1)
    print eqpap.name()
    print eqpap.run(s)
 

    pass
Пример #16
0
    def __init__(self, start, end):
        BaseStat.__init__(self, start, end)

        idx = "1399001"
        self._S = Stock(Globals.get_instance(), idx)
        pass
Пример #17
0
    def run(self, args, opts):
        self._db = {}
        self._ss = Globals.get_instance().stocks()
        for year in range(self.STARTYEAR, self.ENDYEAR + 1):
            ydb = {}
            sdt = self.startdate(year)
            dt = datetime.date(year, sdt[1], sdt[2])
            tmptime = time.mktime(dt.timetuple())
            ydb["starttime"] = tmptime
            self._db[year] = ydb
        #for every date
        cnt1 = 0
        ax = []
        ay = []
        for i in range(0, self.DAYS):
            cnt2 = 0
            total = 0
            for year in range(self.STARTYEAR, self.ENDYEAR):
                ydb = self._db[year]
                stime = ydb["starttime"]
                ttime = stime + (i * 3600 * 24)

                dt = datetime.date.fromtimestamp(ttime)
                ytotal = self.yeartotal(ydb, dt)
#                print dt, ydb, ytotal
#                print i, dt, ytotal
                total += ytotal
                cnt2 += 1
                if cnt2 > 1 and self.DEBUG:
                    break

            # corresponding to new year
            ttime = self._db[self.ENDYEAR]["starttime"] + (i * 3600 * 24)
            dt = datetime.date.fromtimestamp(ttime)
            if i == 0:
                inittotal = total
            delta = (total - inittotal)*100/total
            print i, dt, ":(%6.2f, %6.2f)"%(total, delta), "%"
            ax.append(i)
            ay.append(delta)
 
            cnt1 += 1
            if cnt1 > 1 and self.DEBUG:
                break

        plt.plot(ax, ay, color="red", linewidth=2)
        #make a reference
        ax = []
        ay = []
        ax.append(365)
        ay.append(20)
        ax.append(365)
        ay.append(-20)
        plt.plot(ax, ay, color="black", linewidth=2)
        ax = []
        ay = []
        ax.append(0)
        ay.append(0)
        ax.append(self.DAYS)
        ay.append(0)
        plt.plot(ax, ay, color="black", linewidth=2)
        #index 28, Feb 1
        ax = []
        ay = []
        ax.append(28)
        ay.append(-5)
        ax.append(28)
        ay.append(5)
        plt.plot(ax, ay, color="blue", linewidth=2)
        #index 97, April 10
        ax = []
        ay = []
        ax.append(97)
        ay.append(-5)
        ax.append(97)
        ay.append(5)
        plt.plot(ax, ay, color="blue", linewidth=2)
        #index 160, June 12
        ax = []
        ay = []
        ax.append(160)
        ay.append(-5)
        ax.append(160)
        ay.append(5)
        plt.plot(ax, ay, color="blue", linewidth=2)

        #index 250, Sep 15
        ax = []
        ay = []
        ax.append(250)
        ay.append(-5)
        ax.append(250)
        ay.append(5)
        plt.plot(ax, ay, color="blue", linewidth=2)

        plt.show()
        return
Пример #18
0
        dt = datetime.date(date[0], date[1], date[2])
        start = time.mktime(dt.timetuple())
        for i in range(0, rng):
            tmptime = start + i * (3600*24)
            ndt = datetime.date.fromtimestamp(tmptime)
            hps = self._gbls.historypricesshare(self._sid, (ndt.year, ndt.month, ndt.day))
            if hps == None:
                continue
            return hps
        return None

    def gethpssimplelist(self):
        return self._gbls.gethpssimplelist(self._sid)

    def fd(self):
        if self._fd == None:
            self._fd = FaData(self._sid)
        return self._fd



if __name__ == "__main__":
    from moneyseav3.globals import Globals

    idx = "0000001" #"300230"
    gbls = Globals.get_instance()
    s = Stock(gbls, idx)
    print s.id()
    print s.name()
    print s.price((2018, 12, 27))
Пример #19
0
    def getprices(self):
        return self._mydict

    def range(self):
        return self._range

    def validdate(self, dstr):
        items = dstr.split("-")
        if len(items) != 3:
            return False
        return True

if __name__ == "__main__2":
    from moneyseav3.globals import Globals

    allstocks = Globals.get_instance().stocks().keys()
    for idx in allstocks:
        hp = HistoryPrices(idx)
        dct = hp.getprices()
        print dct
        print idx, len(dct)
        print hp.range()
        break

if __name__ == "__main__":
    idx = "0000001"
    hp = HistoryPrices(idx)
    dct = hp.getprices()
    print dct
    print idx, len(dct)
    print hp.range()
Пример #20
0
    def run(self, args, opts):
        normal_idxs = (GainIndex(), PriceIndex(), MarketValueIndex(),
                       NegativeMarketValueIndex(), PseYieldIndex(),
                       PriceDeltaIndex())
        report_idxs = self.report_idxs()
        eqratio_idxs = self.eqratio_idxs()
        rangeadding_idxs = self.rangeadding_idxs()
        eqra_idxs = self.eqra_idxs()
        eqpa_idxs = self.eqpa_idxs()

        if self.DEBUG:
            self.HISTORY_DATA_INVEST_START = (2017, 1, 1)
            self.RANGE_STEP = (8 * 30)
            self.INVEST_RANGE = (16 * 30)
            self._ss = {
                "300230": Globals.get_instance().getstock("300230"),
                "002061": Globals.get_instance().getstock("002061"),
                "600276": Globals.get_instance().getstock("600276"),
                "600518": Globals.get_instance().getstock("600518"),
                "600801": Globals.get_instance().getstock("600801"),
                "600585": Globals.get_instance().getstock("600585"),
            }
            self._indexes = (GainIndex(), EQRatio("profit_adding"),
                             EQRatio("profit2_adding"),
                             EQRatio("sales_adding"))

            aa = []
            aa.extend(normal_idxs)
            #            aa.extend(report_idxs)
            #            aa.extend(eqratio_idxs)
            aa.extend(eqra_idxs)
            self._indexes = aa
        else:
            self._ss = Globals.get_instance().stocks()
            aa = []
            aa.extend(normal_idxs)
            aa.extend(report_idxs)
            aa.extend(eqratio_idxs)
            aa.extend(rangeadding_idxs)
            aa.extend(eqra_idxs)
            aa.extend(eqpa_idxs)
            self._indexes = aa

        if len(args) < 1:
            print "please specify the step"
            return

        self._automix = None
        if args[0] == "gen":
            for days in range(self.RANGE_STEP,
                              self.INVEST_RANGE + self.RANGE_STEP,
                              self.RANGE_STEP):
                self.everyrange(days)
        elif args[0] == "parse":
            self.parse(self.statistic, self.OUTPUTDIR2)
        elif args[0] == "output":
            self._relatedll = []
            self.output("output/indexesstat/")
            print self._relatedll
        elif args[0] == "outputmix":
            self.output("output/mixstat/")
        elif args[0] == "test":
            self.test()
        elif args[0] == "mixparse":
            self.parse(self.mixstatistic, self.OUTPUTDIR3)
        elif args[0] == "automixparse":
            self.parse(self.automixstatistic, self.OUTPUTDIR4)
        elif args[0] == "outputautomix":
            self.outputautomix()
        pass