コード例 #1
0
ファイル: ma.py プロジェクト: watercolor/stock_analyse
    def update(self, ma_file, datafile, datalist, period='d'):
        first_calc = [False, False, False, False, False, False]
        ma_result = [0.0, 0.0, 0.0, 0.0, 0.0, 0.0]          #this data calc each ma result
        prev_manum_price = [0.0, 0.0, 0.0, 0.0, 0.0, 0.0]   # prev manum data
        queue = [[], [], [], [], [], []]   # each hold queue for hold prev n data
        madata = csvdata(ma_file)
        data = csvdata(datafile)
        end_price_array = data.get_elem_list_last_n('end_val', self.macfg[-1] + len(datalist))
        end_price_array = end_price_array[:-len(datalist)]

        end_price_number = len(end_price_array)
        ma_last_data = madata.read_last()
        new_ma_result_list = []
        # build ma need result, includes last ma_number endprice and last ma_result
        for ma_num in self.macfg:
            idx = self.ma_idx[ma_num]
            data_queue = queue[idx]
            ma_result[idx] = float(ma_last_data[idx + 1]) # +1 for first is date

            if end_price_number < ma_num:
                last_n_data = map(lambda  x: float(x[1]), end_price_array[-end_price_number:])
                prev_manum_price[idx] = None
            else:
                last_n_data = map(lambda  x: float(x[1]), end_price_array[1-ma_num:])
                prev_manum_price[idx] = float(end_price_array[-ma_num][1])
            data_queue.extend(last_n_data)

        for i, data in enumerate(datalist):
            datadate = data[0]
            end_price = float(data[1])
            for ma_num in self.macfg:
                idx = self.ma_idx[ma_num]
                data_queue = queue[idx]
                data_queue.append(end_price)
                length = len(data_queue)

                #add a queue to calc ma price
                if length == ma_num:
                    if first_calc[idx] == False:
                        average_price = sum(data_queue)/length
                        first_calc[idx] = True
                    else:
                        # an accelerate method to calc average, keep precision in calc
                        average_price = ma_result[idx] + (end_price - prev_manum_price[idx]) / ma_num
                    ma_result[idx] = average_price
                    prev_manum_price[idx] = data_queue.pop(0)
            result = [datadate]
            for i in range(self.ma_number):
                try:
                    round_price = ma_result[i]
                    #print "i: %s, price: %s, type:%s"%(i, round_price, type(round_price))
                    round_price = round(round_price, 3)
                    result.append(round_price)
                except:
                    print ma_result[i]
                    print i
            new_ma_result_list.append(result)
        # finally add all calc result to db
        madata.append_data(new_ma_result_list, period)
コード例 #2
0
 def __init__(self, filepath):
     self.filepath = filepath
     self.daydata   = csvdata(os.path.join(filepath, 'day.csv'))
     self.weekdata  = csvdata(os.path.join(filepath, 'week.csv'))
     self.monthdata = csvdata(os.path.join(filepath, 'month.csv'))
     self.daydata.read()
     self.weekdata.read()
     self.monthdata.read()
コード例 #3
0
ファイル: datacheck.py プロジェクト: watercolor/stock_analyse
 def __init__(self, filepath):
     self.filepath = filepath
     self.daydata = csvdata(os.path.join(filepath, "day.csv"))
     self.weekdata = csvdata(os.path.join(filepath, "week.csv"))
     self.monthdata = csvdata(os.path.join(filepath, "month.csv"))
     self.daydata.read()
     self.weekdata.read()
     self.monthdata.read()
コード例 #4
0
    def update(self, macd_file, today_data, period='d'):
        macddata = csvdata(macd_file)
        lastdata = macddata.read_last()
        if lastdata == None:
            self.calc(today_data)
            self.store(macd_file)
            return

        ema_short = float(lastdata[self.IDX_EMA_SHORT])
        ema_long = float(lastdata[self.IDX_EMA_LONG])
        dea = float(lastdata[self.IDX_DEA_IDX2])
        result = []
        for data in today_data:
            end_price = float(data[1])

            ema_short = ema_short * (
                self.short - 2) / self.short + end_price * 2 / self.short
            ema_long = ema_long * (self.long -
                                   2) / self.long + end_price * 2 / self.long
            ema_short = round(ema_short, 4)
            ema_long = round(ema_long, 4)

            diff = ema_short - ema_long
            dea = dea * (self.m - 2) / self.m + diff * 2 / self.m
            dea = round(dea, 4)
            macd = 2 * (diff - dea)
            result.append([
                data[0],
                round(diff, 3),
                round(dea, 3),
                round(macd, 3), ema_short, ema_long, dea
            ])
        macddata.append_data(result, period)
コード例 #5
0
ファイル: macd.py プロジェクト: watercolor/stock_analyse
    def update(self, macd_file, today_data, period='d'):
        macddata = csvdata(macd_file)
        lastdata = macddata.read_last()
        if lastdata == None:
            self.calc(today_data)
            self.store(macd_file)
            return

        ema_short = float(lastdata[self.IDX_EMA_SHORT])
        ema_long = float(lastdata[self.IDX_EMA_LONG])
        dea = float(lastdata[self.IDX_DEA_IDX2])
        result = []
        for data in today_data:
            end_price = float(data[1])

            ema_short = ema_short * (self.short - 2)/self.short + end_price * 2/self.short
            ema_long = ema_long * (self.long - 2)/self.long + end_price * 2/self.long
            ema_short = round(ema_short, 4)
            ema_long = round(ema_long, 4)

            diff = ema_short - ema_long
            dea = dea * (self.m - 2)/self.m + diff * 2/self.m
            dea = round(dea, 4)
            macd = 2*(diff - dea)
            result.append([data[0], round(diff, 3), round(dea, 3), round(macd, 3), ema_short, ema_long, dea])
        macddata.append_data(result, period)
コード例 #6
0
ファイル: sohu_data.py プロジェクト: watercolor/stock_analyse
 def store_csv(self, inputfile='/tmp/stock.csv'):
     if self.data_json != None:
         csv_data = csvdata(inputfile)
         if self.period == 'd':
             csv_data.write(self.data_json['hq'])
         else:
             # judge whether need delete last line to update week or month data
             if csv_data.len() >= 1:
                 last_date = csv_data.read_last_date()
                 recv_first_date = self.data_json['hq'][0][0]
                 #print "recvdate: " + recv_first_date + " last_date: " + last_date
                 deletelast = False
                 if self.period == 'w' and issameweek(last_date, recv_first_date):
                     deletelast = True
                 elif self.period == 'm' and issamemonth(last_date, recv_first_date):
                     deletelast = True
                 if deletelast:
                     csv_data.del_last()
                     csv_data.write(self.data_json['hq'], overwrite= True)
                 else:
                     csv_data.write(self.data_json['hq'])
             else:
                 csv_data.write(self.data_json['hq'])
     else:
         print "Store to " + inputfile + " failed. Data not fetched, maybe code error."
コード例 #7
0
def algodata_check(datafile, fullpath):
    algo_prefix = ["_ma.csv", "_macd.csv"]
    algo_src = []
    datapath = os.path.join(fullpath, datafile)
    datasrc = csvdata(datapath)
    for algo in algo_prefix:
        algofile = datafile[0: datafile.index('.')] + algo
        algofilepath = os.path.join(fullpath, algofile)
        algo_src.append(csvdata(algofilepath))

    for data in datasrc:
        finddate = data[0]
        excep = False
        for algo in algo_src:
            if algo.hasdate(finddate):
                continue
            else:
                excep = True
                print "%s dos not have date %s, break"%(algo.filename(), finddate)
        if excep:
            break
コード例 #8
0
ファイル: datacheck.py プロジェクト: watercolor/stock_analyse
def algodata_check(datafile, fullpath):
    algo_prefix = ["_ma.csv", "_macd.csv"]
    algo_src = []
    datapath = os.path.join(fullpath, datafile)
    datasrc = csvdata(datapath)
    for algo in algo_prefix:
        algofile = datafile[0 : datafile.index(".")] + algo
        algofilepath = os.path.join(fullpath, algofile)
        algo_src.append(csvdata(algofilepath))

    for data in datasrc:
        finddate = data[0]
        excep = False
        for algo in algo_src:
            if algo.hasdate(finddate):
                continue
            else:
                excep = True
                print "%s dos not have date %s, break" % (algo.filename(), finddate)
        if excep:
            break
コード例 #9
0
    def conv_from_csv(self, csvfile):
        csv_data = csvdata(csvfile)
        csv_data.read()
        maxdate = None
        mindate = None
        for data in csv_data.data.values():
            print data
            self.add(data[0], data)
            if data[0] > maxdate or maxdate == None:
                maxdate = data[0]
            if data[0] < mindate or mindate == None:
                mindate = data[0]
        print "MaxDate: " + maxdate + " MinDate: " + mindate
        self.data['range'] = [maxdate, mindate]

        self.data.close()
コード例 #10
0
ファイル: shelfdata.py プロジェクト: fleeaway/stock_analyse
    def conv_from_csv(self, csvfile):
        csv_data = csvdata(csvfile)
        csv_data.read()
        maxdate = None
        mindate = None
        for data in csv_data.data.values():
            print data
            self.add(data[0], data)
            if data[0] > maxdate or maxdate == None:
                maxdate = data[0]
            if data[0] < mindate or mindate == None:
                mindate = data[0]
        print "MaxDate: " + maxdate + " MinDate: " + mindate
        self.data['range'] = [maxdate, mindate]

        self.data.close()
コード例 #11
0
    def check_period(self, daydata, w_m_data, cmp_func, newfile, force = False):
        if daydata.len() == 0:
            return
        first_date = get_elem(daydata.read_index(0), 'date')

        if w_m_data.len() != 0:
            first_date2 = get_elem(w_m_data.read_index(0), 'date')
        if w_m_data.len() != 0 and cmp_func(first_date, first_date2) and force == False:
            return True
        else:
            new_data_list = []
            data_same_period = []
            min_val = 0.0
            max_val = 0.0
            volume = 0.0
            volume_money = 0.0
            diff = 0.0
            diff_ratio = 0.0
            last_date = None
            weekstart = True
            last_end_val = 0.0
            start_val = 0.0
            end_val = 0.0
            for idx, data in enumerate(self.daydata):
                # not same week, record last week data
                if data_same_period != [] and cmp_func(get_elem(data, 'date'), get_elem(data_same_period[-1], 'date')) is False:
                    week_data = [last_date, "%.2f" % start_val, "%.2f" % end_val, diff, diff_ratio,
                                  "%.2f" % min_val, "%.2f" % max_val, volume, "%.2f"%volume_money, 0.0]
                    new_data_list.append(week_data)
                    last_end_val = end_val
                    data_same_period = []
                    weekstart = True
                if weekstart == True:
                    weekstart = False
                    start_val = float(get_elem(data, 'start_val'))
                    min_val = float(get_elem(data, 'low_val'))
                    max_val = float(get_elem(data, 'high_val'))
                    volume = 0.0
                    volume_money = 0.0
                else:
                    min_val = min(min_val, float(get_elem(data, 'low_val')))
                    max_val = max(max_val, float(get_elem(data, 'high_val')))


                data_same_period.append(data)
                volume = volume + float(get_elem(data, 'volume'))
                volume_money = volume_money + float(get_elem(data, 'volume_money'))
                end_val = float(get_elem(data, 'end_val'))
                if last_end_val != 0:
                    diff = '%.2f'%(end_val - last_end_val)
                    diff_ratio = '%.2f%%'%(((end_val-last_end_val)/last_end_val)*100)
                else:
                    diff = "0.00"
                    diff_ratio = "0.00%"
                last_date = get_elem(data, 'date')
            week_data = [last_date, "%.2f" % start_val, "%.2f" % end_val, diff, diff_ratio,
                                  "%.2f" % min_val, "%.2f" % max_val, volume, "%.2f"% volume_money, 0.0]
            new_data_list.append(week_data)
            newdata = csvdata(os.path.join(self.filepath, newfile))
            newdata.write(new_data_list)
            print "Update %s data to %s "%(self.filepath, newfile)
コード例 #12
0
ファイル: ma.py プロジェクト: watercolor/stock_analyse
 def store(self, output_file):
     if self.result_list:
         store = csvdata(output_file)
         store.write(self.result_list)
         self.result_list = []
コード例 #13
0
ファイル: sohu_data.py プロジェクト: fleeaway/stock_analyse
 def store_csv(self, inputfile='/tmp/stock.csv'):
     if self.data_json != None:
         csv_data = csvdata(inputfile)
         csv_data.write(self.data_json['hq'])
     else:
         print "Store to " + inputfile + " failed. Data not fetched, maybe code error."
コード例 #14
0
ファイル: datacheck.py プロジェクト: watercolor/stock_analyse
    def check_period(self, daydata, w_m_data, cmp_func, newfile, force=False):
        if daydata.len() == 0:
            return
        first_date = get_elem(daydata.read_index(0), "date")

        if w_m_data.len() != 0:
            first_date2 = get_elem(w_m_data.read_index(0), "date")
        if w_m_data.len() != 0 and cmp_func(first_date, first_date2) and force == False:
            return True
        else:
            new_data_list = []
            data_same_period = []
            min_val = 0.0
            max_val = 0.0
            volume = 0.0
            volume_money = 0.0
            diff = 0.0
            diff_ratio = 0.0
            last_date = None
            weekstart = True
            last_end_val = 0.0
            start_val = 0.0
            end_val = 0.0
            for idx, data in enumerate(self.daydata):
                # not same week, record last week data
                if (
                    data_same_period != []
                    and cmp_func(get_elem(data, "date"), get_elem(data_same_period[-1], "date")) is False
                ):
                    week_data = [
                        last_date,
                        "%.2f" % start_val,
                        "%.2f" % end_val,
                        diff,
                        diff_ratio,
                        "%.2f" % min_val,
                        "%.2f" % max_val,
                        volume,
                        "%.2f" % volume_money,
                        0.0,
                    ]
                    new_data_list.append(week_data)
                    last_end_val = end_val
                    data_same_period = []
                    weekstart = True
                if weekstart == True:
                    weekstart = False
                    start_val = float(get_elem(data, "start_val"))
                    min_val = float(get_elem(data, "low_val"))
                    max_val = float(get_elem(data, "high_val"))
                    volume = 0.0
                    volume_money = 0.0
                else:
                    min_val = min(min_val, float(get_elem(data, "low_val")))
                    max_val = max(max_val, float(get_elem(data, "high_val")))

                data_same_period.append(data)
                volume = volume + float(get_elem(data, "volume"))
                volume_money = volume_money + float(get_elem(data, "volume_money"))
                end_val = float(get_elem(data, "end_val"))
                if last_end_val != 0:
                    diff = "%.2f" % (end_val - last_end_val)
                    diff_ratio = "%.2f%%" % (((end_val - last_end_val) / last_end_val) * 100)
                else:
                    diff = "0.00"
                    diff_ratio = "0.00%"
                last_date = get_elem(data, "date")
            week_data = [
                last_date,
                "%.2f" % start_val,
                "%.2f" % end_val,
                diff,
                diff_ratio,
                "%.2f" % min_val,
                "%.2f" % max_val,
                volume,
                "%.2f" % volume_money,
                0.0,
            ]
            new_data_list.append(week_data)
            newdata = csvdata(os.path.join(self.filepath, newfile))
            newdata.write(new_data_list)
            print "Update %s data to %s " % (self.filepath, newfile)
コード例 #15
0
    def update(self, ma_file, datafile, datalist, period='d'):
        first_calc = [False, False, False, False, False, False]
        ma_result = [0.0, 0.0, 0.0, 0.0, 0.0,
                     0.0]  #this data calc each ma result
        prev_manum_price = [0.0, 0.0, 0.0, 0.0, 0.0, 0.0]  # prev manum data
        queue = [[], [], [], [], [],
                 []]  # each hold queue for hold prev n data
        madata = csvdata(ma_file)
        data = csvdata(datafile)
        end_price_array = data.get_elem_list_last_n(
            'end_val', self.macfg[-1] + len(datalist))
        end_price_array = end_price_array[:-len(datalist)]

        end_price_number = len(end_price_array)
        ma_last_data = madata.read_last()
        new_ma_result_list = []
        # build ma need result, includes last ma_number endprice and last ma_result
        for ma_num in self.macfg:
            idx = self.ma_idx[ma_num]
            data_queue = queue[idx]
            ma_result[idx] = float(ma_last_data[idx +
                                                1])  # +1 for first is date

            if end_price_number < ma_num:
                last_n_data = map(lambda x: float(x[1]),
                                  end_price_array[-end_price_number:])
                prev_manum_price[idx] = None
            else:
                last_n_data = map(lambda x: float(x[1]),
                                  end_price_array[1 - ma_num:])
                prev_manum_price[idx] = float(end_price_array[-ma_num][1])
            data_queue.extend(last_n_data)

        for i, data in enumerate(datalist):
            datadate = data[0]
            end_price = float(data[1])
            for ma_num in self.macfg:
                idx = self.ma_idx[ma_num]
                data_queue = queue[idx]
                data_queue.append(end_price)
                length = len(data_queue)

                #add a queue to calc ma price
                if length == ma_num:
                    if first_calc[idx] == False:
                        average_price = sum(data_queue) / length
                        first_calc[idx] = True
                    else:
                        # an accelerate method to calc average, keep precision in calc
                        average_price = ma_result[idx] + (
                            end_price - prev_manum_price[idx]) / ma_num
                    ma_result[idx] = average_price
                    prev_manum_price[idx] = data_queue.pop(0)
            result = [datadate]
            for i in range(self.ma_number):
                try:
                    round_price = ma_result[i]
                    #print "i: %s, price: %s, type:%s"%(i, round_price, type(round_price))
                    round_price = round(round_price, 3)
                    result.append(round_price)
                except:
                    print ma_result[i]
                    print i
            new_ma_result_list.append(result)
        # finally add all calc result to db
        madata.append_data(new_ma_result_list, period)
コード例 #16
0
                    prev_manum_price[idx] = data_queue.pop(0)
            result = [datadate]
            for i in range(self.ma_number):
                try:
                    round_price = ma_result[i]
                    #print "i: %s, price: %s, type:%s"%(i, round_price, type(round_price))
                    round_price = round(round_price, 3)
                    result.append(round_price)
                except:
                    print ma_result[i]
                    print i
            new_ma_result_list.append(result)
        # finally add all calc result to db
        madata.append_data(new_ma_result_list, period)

    def store(self, output_file):
        if self.result_list:
            store = csvdata(output_file)
            store.write(self.result_list)
            self.result_list = []


if __name__ == '__main__':
    path = os.path.join(os.getcwd(), 'stockdata', '603999_读者传媒')
    ma_obj = ma()
    data = csvdata(os.path.join(path, 'day.csv'))
    ma_obj.update(os.path.join(path, 'day_ma.csv'),
                  os.path.join(path, 'day.csv'),
                  data.get_elem_list_last_n('end_val', 5))
    #ma_obj.calc(data.get_elem_list('end_val'))
    #ma_obj.store(os.path.join(path, 'day_ma.csv'))
コード例 #17
0
ファイル: ma.py プロジェクト: watercolor/stock_analyse
                        # an accelerate method to calc average, keep precision in calc
                        average_price = ma_result[idx] + (end_price - prev_manum_price[idx]) / ma_num
                    ma_result[idx] = average_price
                    prev_manum_price[idx] = data_queue.pop(0)
            result = [datadate]
            for i in range(self.ma_number):
                try:
                    round_price = ma_result[i]
                    #print "i: %s, price: %s, type:%s"%(i, round_price, type(round_price))
                    round_price = round(round_price, 3)
                    result.append(round_price)
                except:
                    print ma_result[i]
                    print i
            new_ma_result_list.append(result)
        # finally add all calc result to db
        madata.append_data(new_ma_result_list, period)

    def store(self, output_file):
        if self.result_list:
            store = csvdata(output_file)
            store.write(self.result_list)
            self.result_list = []

if __name__ == '__main__':
    path = os.path.join(os.getcwd(), 'stockdata', '603999_读者传媒')
    ma_obj = ma()
    data = csvdata(os.path.join(path, 'day.csv'))
    ma_obj.update(os.path.join(path, 'day_ma.csv'), os.path.join(path, 'day.csv'), data.get_elem_list_last_n('end_val', 5))
    #ma_obj.calc(data.get_elem_list('end_val'))
    #ma_obj.store(os.path.join(path, 'day_ma.csv'))
コード例 #18
0
            ema_short = round(ema_short, 4)
            ema_long = round(ema_long, 4)

            diff = ema_short - ema_long
            dea = dea * (self.m - 2) / self.m + diff * 2 / self.m
            dea = round(dea, 4)
            macd = 2 * (diff - dea)
            result.append([
                data[0],
                round(diff, 3),
                round(dea, 3),
                round(macd, 3), ema_short, ema_long, dea
            ])
        macddata.append_data(result, period)

    def store(self, output_file):
        if self.result_list:
            store = csvdata(output_file)
            store.write(self.result_list)
            self.result_list = []


if __name__ == '__main__':
    path = os.getcwd() + os.sep + 'stockdata' + os.sep + "000002_万科A" + os.sep
    ma = macd()
    data = csvdata(path + 'day.csv')
    ma.update(path + 'day_macd.csv', data.read_last())
    #src = csvdata('/tmp/stock.csv')
    #ma = macd()
    #ma.calc(src.get_elem_list('end_val'))
    #ma.store('/tmp/macd.csv')
コード例 #19
0
 def store(self, output_file):
     if self.result_list:
         store = csvdata(output_file)
         store.write(self.result_list)
         self.result_list = []
コード例 #20
0
ファイル: macd.py プロジェクト: watercolor/stock_analyse
        for data in today_data:
            end_price = float(data[1])

            ema_short = ema_short * (self.short - 2)/self.short + end_price * 2/self.short
            ema_long = ema_long * (self.long - 2)/self.long + end_price * 2/self.long
            ema_short = round(ema_short, 4)
            ema_long = round(ema_long, 4)

            diff = ema_short - ema_long
            dea = dea * (self.m - 2)/self.m + diff * 2/self.m
            dea = round(dea, 4)
            macd = 2*(diff - dea)
            result.append([data[0], round(diff, 3), round(dea, 3), round(macd, 3), ema_short, ema_long, dea])
        macddata.append_data(result, period)

    def store(self, output_file):
        if self.result_list:
            store = csvdata(output_file)
            store.write(self.result_list)
            self.result_list = []


if __name__ == '__main__':
    path = os.getcwd() + os.sep + 'stockdata' + os.sep + "000002_万科A" + os.sep
    ma = macd()
    data = csvdata(path + 'day.csv')
    ma.update(path + 'day_macd.csv', data.read_last())
    #src = csvdata('/tmp/stock.csv')
    #ma = macd()
    #ma.calc(src.get_elem_list('end_val'))
    #ma.store('/tmp/macd.csv')