Exemple #1
0
 def __init__(self,
              user="******",
              password="******",
              zuhe="ZH1008383"):
     self.user = easytrader.use('xq')
     self.user.prepare(user=user, password=password, portfolio_code=zuhe)
     self.buylist = []
     self.selllist = []
     self.limitnum = 20
     self.m = MongoDB()
Exemple #2
0
class dbdata():
    def __init__(self):
        self.m = MongoDB(DB_SERVER, DB_PORT, USER, PWD, AUTHDBNAME)
        self.formatlist = [
            "date", "volume", "close", "high", "low", "open", "pre_close"
        ]
        return

    def dbserver(self):
        return DB_SERVER

    def _getdata(self,trade_date=dt.datetime.today(),collection="000923.SZ",
                 db="ml_security_table",\
                 out=[],isfilt=True,filt={}):
        if not out: out = self.formatlist
        if isfilt and not filt:            filt =  \
{"date":{"$gt": trade_date, "$lt": trade_date+dt.timedelta(days=1)}}
        query = self.m.read_data(db, collection, filt=filt)
        return self.formatquery(query, out)

    def formatquery(self, query, out):
        '''
        query:your source data ,should be a list with dict
        out:the fields you want to convert into dataframe 
        '''
        if not out:
            query = [i for i in query.sort("policy", 1)]
        else:
            query = [{k: i[k] for k in out} for i in query.sort("policy", 1)]
        return query
Exemple #3
0
 def __init__(self, startdate=(2011, 1, 1), enddate=[]):
     self.startdate = datetime.datetime(*startdate, 0, 0, 0, 0)
     self.enddate = enddate
     self.m = MongoDB(DB_SERVER, DB_PORT, USER, PWD, AUTHDBNAME)
     self.formatlist = [
         "date", "volume", "close", "high", "low", "open", "pre_close"
     ]
     self.savevols = [
         "stock", "buy_date", "sell_date", "holddays", "profit", "features"
     ]
     self.looplist = []
     self.trading_records = []
     self.holding_records = []
     self.datalst = []
     self.collection = None
     self.tempstatus = []
     self.lateststatus = []
Exemple #4
0
 def connetdb(self):
     self.m = MongoDB(DB_SERVER, DB_PORT, USER, PWD, AUTHDBNAME)
     return self.m
Exemple #5
0
 def __init__(self, stock="600455.SH"):
     self.m = MongoDB(DB_SERVER, DB_PORT, USER, PWD, AUTHDBNAME)
Exemple #6
0
class strategy1(object):
    '''
    trading volume is the lowest in 60 days
    '''
    def __init__(self, stock="600455.SH"):
        self.m = MongoDB(DB_SERVER, DB_PORT, USER, PWD, AUTHDBNAME)

    def _getdata(self, db="ml_security_table", collection="600455.SH"):
        query = self.m.read_data(
            db,
            collection,
            filt={"date": {
                "$gt": datetime.datetime(2013, 1, 1, 0, 0, 0, 0)
            }})
        return self.m.format2dataframe(query)

    # 获取股票列表
    def import_stocklist(self, stocklistname):
        # df = pd.read_csv(str(stocklistname) + '.csv', parse_dates=['date'])
        df = pd.read_csv(str(stocklistname) + '.csv',
                         parse_dates=['startdate'])
        return df

    def mean_volume(self, data):
        m_vol = sum(data) / len(data)
        return m_vol

    def buy_condition1(self,
                       dat,
                       vol,
                       vol_data,
                       close,
                       last_high,
                       maxprice,
                       minprice,
                       count,
                       vol_weight=1.2):
        if self.condition1(vol, vol_data, vol_weight) and \
                self.condition2(close, last_high) and \
                self.condition3(close, maxprice) and \
                self.condition4(close, minprice):
            return True
        return False

    def buy_condition(self,
                      dat,
                      vol,
                      vol_data,
                      close,
                      last_high,
                      maxprice,
                      minprice,
                      count,
                      parameter,
                      vol_weight=1.2):
        if self.condition6(dat, parameter[1]) and \
                self.condition7(close, parameter[0]):
            return True
        return False

    def buy_condition2(self,
                       dat,
                       vol,
                       vol_data,
                       close,
                       last_high,
                       maxprice,
                       minprice,
                       count,
                       parameter,
                       vol_weight=1.2):
        if self.condition8(close):
            return True
        return False

    def sell_conditon(self,
                      buy_price,
                      currentday_high,
                      currentday_low,
                      hold_days,
                      gain_grads=0.1,
                      loss_grads=-0.05,
                      dayout=10):
        if self.stopgain_condition(buy_price, currentday_high, gain_grads):
            return True, "stopgain"
        elif self.stoploss_condition(buy_price, currentday_low, loss_grads):
            return True, "stoploss"
        elif self.holdingtime_condition(hold_days, dayout):
            return True, "holdtime"
        return False, None

    def sell_conditon1(self,
                       buy_price,
                       currentday_high,
                       currentday_low,
                       hold_days,
                       gain_grads=0.1,
                       loss_grads=-0.05,
                       dayout=10):
        if self.condition9(close):
            return True, "Sold"
        return False, None

    def stopgain_condition(self, buy_price, current_price, grads=0.1):
        if (current_price - buy_price) / buy_price >= grads:
            return True
        return False

    def stoploss_condition(self, buy_price, current_price, grads=-0.05):
        if (current_price - buy_price) / buy_price <= grads:
            return True
        return False

    def holdingtime_condition(self, hold_days, dayout=10):
        if hold_days >= dayout:
            return True
        return False

    def condition1(self, vol, vol_data, vol_weight=1.2):
        if vol >= vol_weight * self.mean_volume(vol_data):
            return True
        return False

    def condition2(self, close, last_high):
        if close >= last_high:
            return True
        return False

    def condition3(self, close, high, grads=0.2):
        if (high - close) / high >= grads:
            return True
        return False

    def condition4(self, close, low, grads=0.05):
        if (close - low) / low <= grads:
            return True
        return False

    def condition5(self, close, low, grads=0.05):
        if close <= 100:
            return True
        return False

    def condition6(self, dat, startdate):
        newdat = pd.to_datetime(dat)
        newdat = newdat.strftime('%Y-%m-%d')
        newstartdate = pd.to_datetime(startdate)
        newstartdate = newstartdate.strftime('%Y-%m-%d')
        # print(newdat)
        # print(newstartdate)
        if newdat > newstartdate:
            return True
        return False

    def condition7(self, close, cashprice):
        if close < cashprice:
            return True
        return False

    def condition8(self, close):
        if close < 110:
            return True
        return False

    def condition9(self, close):
        if close > 120:
            return True
        return False

    def formatdata(self, stock, source="mongodb"):
        if source == "mongodb":
            df = self._getdata(collection=stock)
        elif source == "tushare":
            df = ts.get_hist_data(
                stock,
                start='2005-01-01',
                end='2016-11-18',
            )
            df.sort_index(inplace=True)
            df["date"] = df.index
            df["pre_close"] = df["close"] - df["price_change"]
            df.to_csv(stock + '.csv')
        return df

    def histofyreturn(self,
                      parameter,
                      db="ml_security_table",
                      table="",
                      source="mongodb"):
        buy = []
        stopgain = 0.1
        stoploss = -0.1
        vol_day = 10
        price_day = 60
        count = 60
        transaction_record = []
        df = self.formatdata(table, source)
        # df.to_csv(table+'.csv')
        lst = [
            l for l in df[[
                "date", "volume", "close", "high", "low", "open", "pre_close"
            ]].fillna(0).values if l[1] != 0
        ]
        for line in lst[count:]:
            dat = line[0]
            vol = line[1]
            if vol == 0: continue
            close = line[2]
            last_high = lst[count - 1][3]
            vol_data = [i[1] for i in lst[count - vol_day:count]]
            maxprice = max([i[3]] for i in lst[count - price_day:count])[0]
            minprice = min([i[4]] for i in lst[count - price_day:count])[0]

            for b in buy[:]:
                d = b[0]
                c = b[1]
                buy_date = d[0]
                sell_date = line[0]
                hold_days = count - c
                buy_price = d[2]
                currentday_high = line[3]
                currentday_low = line[4]

                is_sell, selltype = self.sell_conditon(buy_price, currentday_high, currentday_low, \
                                                       hold_days, gain_grads=0.1, \
                                                       loss_grads=-0.05, dayout=10)
                if is_sell:
                    #print(buy)
                    #print(b)
                    buy.remove(b)
                    if selltype == "stopgain":
                        profit = stopgain
                    elif selltype == "stopgain":
                        profit = stoploss
                    else:
                        profit = (close - buy_price) / buy_price
                    transaction_record.append(
                        [table, buy_date, sell_date, hold_days, profit])
                    # print (profit)
                    # print(transaction_record)
            if self.buy_condition(dat,
                                  vol,
                                  vol_data,
                                  close,
                                  last_high,
                                  maxprice,
                                  minprice,
                                  count,
                                  parameter,
                                  vol_weight=1.2):
                buy.append((line, count, table))
            count += 1
        # df_buy = pd.DataFrame(transaction_record)
        # df_buy.to_csv("test_tushare_buy_new.csv")
        return transaction_record, buy

    def filter_with_all_stocks(self, stocklist, source="mongodb"):
        error_list = []
        result = []
        buyresult = []
        for i in stocklist:
            print(i[0])
            startdate = i[1]
            cashprice = i[2]
            # try:
            r, buyed = self.histofyreturn(startdate,
                                          cashprice,
                                          table=i[0],
                                          source=source)
            # if r: result.extend(r)
            result.extend(r)
            print(result)
            if buyed: buyresult.extend(buyed)
            # except:
            #   error_list.append(i)
        return result, buyresult, error_list

    def filter_with_all_stocks_new(self, df, source="mongodb"):
        error_list = []
        result = []
        buyresult = []
        par = []
        count = 0
        df_len = len(df.index)
        column_num = len(df.count())
        while (count < df_len):
            columncount = 1
            # print(df.iat[0,1])
            par = []
            while (columncount < column_num):
                par.append(df.iat[count, columncount])
                columncount = columncount + 1
            print(par)
            # try:
            print(count)
            print(df.iat[count, 0])
            stock_name = str(df.iat[count, 0])
            print(stock_name)

            if len(stock_name) == 1:
                stock_name = '00000' + stock_name
            elif len(stock_name) == 2:
                stock_name = '0000' + stock_name
            elif len(stock_name) == 3:
                stock_name = '000' + stock_name
            elif len(stock_name) == 4:
                stock_name = '00' + stock_name
            elif len(stock_name) == 5:
                stock_name = '0' + stock_name
            print(stock_name)
            r, buyed = self.histofyreturn(par, table=stock_name, source=source)
            # if r: result.extend(r)
            result.extend(r)
            # if buyed: buyresult.extend(buyed)
            buyresult.extend(buyed)
            # except:
            error_list.append(stock_name)
            count = count + 1
        return result, buyresult, error_list
Exemple #7
0
class trade():
    def __init__(self,
                 user="******",
                 password="******",
                 zuhe="ZH1008383"):
        self.user = easytrader.use('xq')
        self.user.prepare(user=user, password=password, portfolio_code=zuhe)
        self.buylist = []
        self.selllist = []
        self.limitnum = 20
        self.m = MongoDB()

    def getcurrentdata(self):
        '''code:代码, name:名称 ,changepercent:涨跌幅 , trade:现价 ,open:开盘价 ,high:最高价, low:最低价, settlement:昨日收盘价 ,
           volume:成交量 ,turnoverratio:换手率 ,amount:成交量 ,per:市盈率 ,pb:市净率, mktcap:总市值 ,nmc:流通市值
        '''
        out = ["code", "trade", "open", "high", "low", "settlement"]
        rst = ts.get_today_all()
        return rst[out]

    def getbuylist(self, filename="procedure_records.csv"):
        s = macd()
        self.buylist = s.getprocedure(isdb=True)
        return self.buylist

    def justsellit(self, db="macd", collection="operatequeue", stoploss=False):
        currentdata = {i[0]: i for i in self.getcurrentdata().values}
        rst = self.m.read_data(db, collection, filt={"status": "HOLD"})
        position = self.getposition()
        for i in rst:
            stock = i["stock"]
            for xq in position:
                if stock == xq["stock_code"][2:]:
                    rate = xq["market_value"] / i["buymoney"]
                    print("check stock:{}       rate:{}".format(stock, rate))
                    print(i)
                    print(xq)
                    print("********************")
                    bias = 1
                    if stoploss:
                        bias = currentdata[stock][4] / currentdata[stock][1]
                    self.trysell(stock, rate, stoploss, bias=bias)
                    break

    def trysell(self, stock, rate, stoploss=False, bias=1):
        if stoploss and rate / bias <= 0.9:
            self.sell(stock=stock, number=100, price=0.55)
            self.updateholdlst(stock)
        elif rate >= 1.1:
            self.sell(stock=stock, number=100, price=0.55)
            self.updateholdlst(stock)

    def updateholdlst(self, stock, db="macd", collection="operatequeue"):
        data = {'$set': {'status': 'SELL'}}
        self.m.update_data(data, db, collection, filt={"stock": stock})

    def getholdlst(self, holddate, db="etfgrid", collection="operatequeue"):
        out = ["stock", "buymoney", "date", "startprice", "status"]
        #df = self.m._getdata(collection, db, out=out, filt={"date": holddate})
        #df = self.m.read_data(db, collection, filt={"date": holddate})
        #df = self.m.read_data(db, collection)
        df = self.m.read_data(db, collection, filt={"status": "HOLD"})
        #if isfilt and not filt: filt = {"date": {"$gt": self.startdate}}
        #query = self.m.read_data(db, collection, filt=filt)
        print('hold list:')
        print(df)
        #holdlist = [l for l in df[['date', 'startprice', "buytimes", 'close']].values]
        return
        #data = {'$set': {'status': 'SELL'}}
        #self.m.update_data(data, db, collection, filt={"stock": stock})

    def buyitnow(self):
        localbackup = []
        holdlist = []
        tradedate = datetime.today().strftime('%Y-%m-%d')
        holdlist = self.getholdlst(tradedate)
        print(holdlist)
        balance = self.getbalance()
        lst = self.getposition()
        holdnum = len(lst)
        leftmony = balance[0]["current_balance"]
        couldbynum = self.limitnum - holdnum
        holdlst = [i["stock_code"][2:] for i in lst]
        couldbuylst = [i for i in self.buylist if i not in holdlst]
        triggerbuynum = len(couldbuylst)
        print("could buy list:{}".format(self.buylist))
        if couldbynum > 0:
            permoney = leftmony / couldbynum
            if triggerbuynum > couldbynum:
                buylst = random.sample(couldbuylst, couldbynum)
            else:
                buylst = couldbuylst
        else:
            buylst = []

        for stock in buylst:
            try:
                t.buy(stock=stock, number=permoney, price=1)
                localbackup.append({
                    "stock":
                    stock,
                    "buymoney":
                    permoney,
                    "date":
                    datetime.today().strftime('%Y-%m-%d'),
                    "status":
                    "HOLD"
                })
            except:
                print("buy {} failed!!!!".format(stock))
        return localbackup

    def connetdb(self):
        self.m = MongoDB(DB_SERVER, DB_PORT, USER, PWD, AUTHDBNAME)
        return self.m

    def savebackup2db(self, data, db="etfgrid", collection="operatequeue"):
        if not data: return
        db = eval('self.m.client.{}'.format(db))
        db[collection].insert_many(data)
        return

    def getbalance(self):
        '''output:[{ 'asset_balance': '资产总值','current_balance': '当前余额','enable_balance': '可用金额','market_value': '证券市值',
                    'money_type': '币种','pre_interest': '预计利息' }]
        '''
        return self.user.balance

    def getposition(self):
        '''output:[{'cost_price': '摊薄成本价','current_amount': '当前数量','enable_amount': '可卖数量','income_balance': '摊薄浮动盈亏','keep_cost_price': '保本价',
                    'last_price': '最新价','market_value': '证券市值','position_str': '定位串','stock_code': '证券代码','stock_name': '证券名称'}]
        '''
        return self.user.position

    def getentrust(self):
        '''获取今日委托单
          output:[{'business_amount': '成交数量', 'business_price': '成交价格','entrust_amount': '委托数量','entrust_bs': '买卖方向','entrust_no': '委托编号','entrust_price': '委托价格',
           'entrust_status': '委托状态', 废单 / 已报   'report_time': '申报时间', 'stock_code': '证券代码', 'stock_name': '证券名称'}]
        '''
        return self.user.entrust

    def buy(self, stock="600461", number=10000, price=0):
        self.user.buy(stock, price=price, amount=number)

    def cancel(self, entrust):
        '''entrust:委托单号
        '''
        self.user.cancel_entrust(entrust)

    def cancelall(self):
        entrust = [
            i for i in self.getentrust()
            if i["entrust_status"] == "已报" and i["entrust_bs"] == "买入"
        ]
        for i in entrust:
            flash_entrust = [
                i for i in self.getentrust()
                if i["entrust_status"] == "已报" and i["entrust_bs"] == "买入"
            ]
            entrust_no = flash_entrust[0]["entrust_no"]
            self.cancel(entrust_no)
        return

    def sell(self, stock="600461", number=100, price=0.55):
        self.user.sell(stock, price=price, amount=number)

    def adjust_weight(self, stock='000001', weight=10):
        self.user.adjust_weight(stock, weight)

    def updatebuyoppor(self, buyopporlist, lst, tradetype):
        #buyopporlist = []
        if not lst: return buyopporlist
        for line in lst:
            buyopporlist.append({"stock": line, "tradetype": tradetype})
        return buyopporlist
Exemple #8
0
class macd(object):
    '''
           ���������������������
    '''
    def __init__(self):
        self.db = MongoDB(DB_SERVER, DB_PORT, USER, PWD, AUTHDBNAME)
        self.looplist = []

    def setlooplist(self, lst=[]):
        excludelst = [
            "399001.SZ", "399006.SZ", "000001.SH", "000300.SH", "399005.SZ"
        ]
        if not lst:
            self.looplist = [
                i for i in self.db.getallcollections("stockdatas")
                if i not in excludelst
            ]
        else:
            self.looplist = lst
        return self.looplist

    def buycondition(self, data):
        '''out = ["macd","cross_dist"]'''
        if data["macd"] > 0 and data["cross_dist"] == 0 \
            and data["diff12_26"] < 0:
            return True
        return False

    def enhancebuycondition(self, data):
        if data["macd"] < 0 and data["diff12_26"] < 0:
            return True
        return False

    def sellcondition(self, data):
        pass

    def getlastbuylst(self, ):
        dbname = "stockdatas"
        num = 1
        filt = [{"$sort": {"date": -1}}, {"$limit": num}]
        buylst = []
        enhancebuylst = []
        for i in self.looplist:
            collection = i
            query = self.db.read_data(dbname,
                                      collection,
                                      filt,
                                      is_aggregate=True)
            data = [i for i in query]
            if self.buycondition(data[0]):
                buylst.append(i)
                #                 print ("normal condition buy stock:{}".format(i))
                query = self.db.read_data(dbname,
                                          collection, [{
                                              "$sort": {
                                                  "date": -1
                                              }
                                          }, {
                                              "$match": {
                                                  "cross_dist": 0,
                                                  "macd": {
                                                      "$ne": 0
                                                  }
                                              }
                                          }, {
                                              "$limit": 2
                                          }],
                                          is_aggregate=True)
                newdata = [i for i in query]
                if self.enhancebuycondition(newdata[1]):
                    enhancebuylst.append(i)
                    print("enhance condition buy stock:{}".format(i))
        return buylst, enhancebuylst

    def gethistorybuylst(self, starttime="2011-01-01"):
        dbname = "stockdatas"
        filt = [{
            "$sort": {
                "date": 1
            }
        }, {
            "$match": {
                "date": {
                    "$gt": starttime
                }
            }
        }]
        buylst = []
        enhancebuylst = []
        for i in self.looplist:
            collection = i
            query = self.db.read_data(dbname,
                                      collection,
                                      filt,
                                      is_aggregate=True)
            data = [i for i in query]
            for record in data:
                if self.buycondition(record):
                    buylst.append([i, record["date"], record["close"]])
                    #                     print ([i,record["date"],record["close"]])
                    idx = data.index(record)
                    precross = idx - data[idx - 1]["cross_dist"] - 1
                    if precross >= 0 and self.enhancebuycondition(
                            data[precross]):
                        enhancebuylst.append(i)
                        print([i, record["date"], record["close"]])

        return buylst, enhancebuylst

    def gettraderecord(self, starttime="2011-01-01"):

        pass

    def getcurrentdata(self):
        '''code:代码, name:名称 ,changepercent:涨跌幅 , trade:现价 ,open:开盘价 ,high:最高价, low:最低价, settlement:昨日收盘价 ,
           volume:成交量 ,turnoverratio:换手率 ,amount:成交量 ,per:市盈率 ,pb:市净率, mktcap:总市值 ,nmc:流通市值
        '''
        out = ["code", "trade", "open", "high", "low", "settlement"]
        rst = ts.get_today_all()
        return rst[out].set_index('code')

    def gettradebuylst(self):
        dbname = "stockdatas"
        num = 1
        emaslow = 26
        emafast = 12
        demday = 9
        filt = [{"$sort": {"date": -1}}, {"$limit": num}]
        buylst = []
        currentdata = self.getcurrentdata()
        for i in self.looplist:
            collection = i
            query = self.db.read_data(dbname,
                                      collection,
                                      filt,
                                      is_aggregate=True)
            data = [i for i in query]
            s_ema = data[0]["ema26"]
            f_ema = data[0]["ema12"]
            dem = data[0]["dem9"]
            macd = data[0]["macd"]
            try:
                c_close = currentdata["trade"].ix[collection.split(".")[0]]

                n_s_ema = (s_ema * (emaslow - 1) + 2 * c_close) / (emaslow + 1)
                n_f_ema = (f_ema * (emafast - 1) + 2 * c_close) / (emafast + 1)
                n_diff = n_f_ema - n_s_ema
                n_dem = (dem * (demday - 1) + 2 * n_diff) / (demday + 1)
                n_macd = 2 * (n_diff - n_dem)
                if macd * n_macd < 0 and n_macd > 0:
                    buylst.append(collection)
            except:
                print("error stock:{}".format(collection))
        [print("buylist:{}".format(collection)) for collection in buylst]
        return buylst

    def save2db(self):
        pass

    def save2csv(self):
        pass
Exemple #9
0
 def __init__(self):
     self.db = MongoDB(DB_SERVER, DB_PORT, USER, PWD, AUTHDBNAME)
     self.looplist = []
Exemple #10
0
 def __init__(self):
     self.m = MongoDB(DB_SERVER, DB_PORT, USER, PWD, AUTHDBNAME)
     self.formatlist = [
         "date", "volume", "close", "high", "low", "open", "pre_close"
     ]
     return
Exemple #11
0
 def __init__(self, stock="600455.SH"):
     self.m = MongoDB()
Exemple #12
0
class strategy1(object):
    '''
    trading volume is the lowest in 60 days
    '''
    def __init__(self, stock="600455.SH"):
        self.m = MongoDB()

    def _getdata(self, db="ml_security_table", collection="600455.SH"):
        query = self.m.read_data(
            db,
            collection,
            filt={"date": {
                "$gt": datetime.datetime(2013, 1, 1, 0, 0, 0, 0)
            }})
        return self.m.format2dataframe(query)

    def histofyreturn(self, db="ml_security_table", table=""):
        holding = []
        w = 40
        #         stocklist = self.m.getallcollections(db)
        df = self._getdata(collection=table)
        #         df.to_csv('600455.csv')
        count = 1
        origindata = []
        lst = [
            l for l in df[["date", "volume", "close", "high"]].fillna(0).values
            if l[1] != 0
        ]
        for line in lst:
            if line[1] == 0: continue
            origindata.append(line[1])
            if count == w:
                sorteddata = sorted(origindata)
            elif count > w:
                index = self.getnewindex(sorteddata, line[1], w)
                for h in holding:
                    if (line[3] - h[1]) / h[1] >= 0.1:
                        #                         print (count-h[0])
                        print(0.1)
                        holding.remove(h)
                        HOLDS.append(count - h[0])
                    elif (line[3] - h[1]) / h[1] <= -0.10 or count - h[0] > 20:
                        if count - h[0] > 20:
                            print("{},{}".format((line[3] - h[1]) / h[1],
                                                 line[0]))
                        else:
                            print("{},{}".format(-0.1, line[0]))
                        RESULTS.append((line[3] - h[1]) / h[1])
                        HOLDS.append(count - h[0])
                        holding.remove(h)
                if index == 0:
                    holding.append((count, line[2], line[0]))
                    DATES.append(line[0])
#                     print ("{} {} {}".format(line[0],index,(lst[count+2][2]-lst[count][2])/lst[count][2]))
                sorteddata.insert(index, line[1])
                sorteddata.remove(origindata[0])
                del origindata[0]
            count += 1
        print(table)
#         print (holding)

    def getnewindex(self, datas, newdata, l, base=0):
        '''datas:should be sorted from min to max,
           newdats: the data need to check the position in the sorted datas
           l:len of data
           base:first position of the datas in origin datas
        '''
        index = int(l / 2)
        if datas[index] > newdata:
            if l == 1: return base
            d = datas[:index]
            nl = index
            return self.getnewindex(d, newdata, nl, base)
        elif datas[index] < newdata:
            if l <= 2: return base + l
            d = datas[index + 1:]
            nl = int((l - 1) / 2)
            return self.getnewindex(d, newdata, nl, base + index + 1)
        elif datas[index] == newdata:
            return base + index
Exemple #13
0
            c_preclose = line[6]
            if c_preclose != close:
                reh = [[i[0], i[1] * c_preclose / close] for i in reh]
                reh.append([c, c_preclose / close])
            close = line[2]
            c += 1
        result = []
        sc = 0
        ec = 0
        for idx in range(len(reh)):
            weight = reh[idx][1]
            ec = reh[idx][0]
            piece = self.recount(lst, sc, ec, weight)
            result.extend(piece)
            sc = ec
        result.extend(lst[sc:])
        return result

    def recount(self, lst, sc, ec, weight):
        rst = []
        for line in lst[sc:ec]:
            rst.append([line[0], line[1], *[i * weight for i in line[2:]]])
        return rst


if __name__ == '__main__':
    db = MongoDB(DB_SERVER, DB_PORT, USER, PWD, AUTHDBNAME)
    s = StockIndicator(mdb=db)
    s.setlooplist()
    s.updateallstocks2db()
Exemple #14
0
class strategy1(object):
    '''
    trading volume is the lowest in 60 days
    '''
    def __init__(self, stock="600455.SH"):
        self.m = MongoDB(DB_SERVER, DB_PORT, USER, PWD, AUTHDBNAME)

    def _getdata(self, db="ml_security_table", collection="600455.SH"):
        query = self.m.read_data(
            db,
            collection,
            filt={"date": {
                "$gt": datetime.datetime(2011, 1, 1, 0, 0, 0, 0)
            }})
        return self.m.format2dataframe(query)

    # 获取股票列表
    def import_stocklist(self, stocklistname):
        #df = pd.read_csv(str(stocklistname) + '.csv', parse_dates=['date'])
        df = pd.read_csv(str(stocklistname) + '.csv', parse_dates=['code'])
        return df

    def mean_volume(self, data):
        m_vol = sum(data) / len(data)
        return m_vol

    def buy_condition(self,
                      vol,
                      vol_data,
                      close,
                      last_high,
                      maxprice,
                      minprice,
                      count,
                      highindex,
                      vol_weight=1.2):
        if  self.condition1(vol,vol_data,vol_weight) and \
            self.condition2(close,last_high) and \
            self.condition3(close,maxprice) and \
            self.condition4(close,minprice) and \
            self.condition5(count,highindex):
            return True
        return False

    def sell_conditon(self,
                      buy_price,
                      currentday_high,
                      currentday_low,
                      hold_days,
                      gain_grads=0.1,
                      loss_grads=-0.05,
                      dayout=10):
        if self.stopgain_condition(buy_price, currentday_high, gain_grads):
            return True, "stopgain"
        elif self.stoploss_condition(buy_price, currentday_low, loss_grads):
            return True, "stoploss"
        elif self.holdingtime_condition(hold_days, dayout):
            return True, "holdtime"
        return False, None

    def stopgain_condition(self, buy_price, current_price, grads=0.1):
        if (current_price - buy_price) / buy_price >= grads:
            return True
        return False

    def stoploss_condition(self, buy_price, current_price, grads=-0.05):
        if (current_price - buy_price) / buy_price <= grads:
            return True
        return False

    def holdingtime_condition(self, hold_days, dayout=10):
        if hold_days >= dayout:
            return True
        return False

    def condition1(self, vol, vol_data, vol_weight=1.2):
        if vol >= vol_weight * self.mean_volume(vol_data):
            return True
        return False

    def condition2(self, close, last_high):
        if close >= last_high:
            return True
        return False

    def condition3(self, close, high, grads=0.2):
        if (high - close) / high >= grads:
            return True
        return False

    def condition4(self, close, low, grads=0.05):
        if (close - low) / low <= grads:
            return True
        return False

    def condition5(self, currentday, highday, grads=60):
        if currentday - highday >= grads:
            return True
        return False

    def formatdata(self, stock, source="mongodb"):
        if source == "mongodb":
            df = self._getdata(collection=stock)
        elif source == "tushare":
            df = ts.get_hist_data(
                stock,
                start='2016-08-01',
                end='2016-11-18',
            )
            df.sort_index(inplace=True)
            df["date"] = df.index
            df["pre_close"] = df["close"] - df["price_change"]
        return df

    def is_ex_right(self, currentprice, lastprice):
        if currentprice != lastprice:
            return True, currentprice / lastprice
        else:
            return False, 1.0

    def histofyreturn(self,
                      db="ml_security_table",
                      table="",
                      source="mongodb"):
        buy = []
        stopgain = 0.1
        stoploss = -0.5
        vol_day = 30
        price_day = 90
        count = 90
        transaction_record = []
        df = self.formatdata(table, source)
        lst = [
            l for l in df[[
                "date", "volume", "close", "high", "low", "open", "pre_close"
            ]].fillna(0).values if l[1] != 0
        ]

        for line in lst[count:]:
            vol = line[1]
            if vol == 0: continue
            close = line[2]
            last_high = lst[count - 1][3]
            vol_data = [i[1] for i in lst[count - vol_day:count]]
            maxprice = max([i[3]] for i in lst[count - price_day:count])[0]
            maxindex = [
                i for i in range(count - price_day, count)
                if lst[i][3] == maxprice
            ][0]
            minprice = min([i[4]] for i in lst[count - price_day:count])[0]
            ex_right, ex_right_rate = self.is_ex_right(line[6],
                                                       lst[count - 1][2])

            for b in buy[:]:
                d = b[0]
                c = b[1]
                buy_date = d[0]
                sell_date = line[0]
                hold_days = count - c
                buy_price = d[2]
                currentday_high = line[3]
                currentday_low = line[4]

                is_sell,selltype = self.sell_conditon(buy_price,currentday_high,currentday_low,\
                                      hold_days,gain_grads=0.1,\
                                      loss_grads=-0.05,dayout=10)
                if is_sell:
                    buy.remove(b)

                    if selltype == "stopgain":
                        profit = stopgain
                    elif selltype == "stoploss":
                        #                         profit = stoploss
                        profit = (close - buy_price) / buy_price
                    else:
                        profit = (close - buy_price) / buy_price
                    transaction_record.append(
                        [table, buy_date, sell_date, hold_days, profit])
                    print(profit)

            if self.buy_condition(vol,
                                  vol_data,
                                  close,
                                  last_high,
                                  maxprice,
                                  minprice,
                                  count,
                                  maxindex,
                                  vol_weight=1.2):
                buy.append((line, count, table))
                print(line)
            count += 1
        return transaction_record, buy

    def filter_with_all_stocks(self, stocklist, source="mongodb"):
        error_list = []
        result = []
        buyresult = []
        for i in stocklist:
            print(i)
            try:
                r, buyed = self.histofyreturn(table=i, source=source)
                if r: result.extend(r)
                if buyed: buyresult.extend(buyed)
            except:
                error_list.append(i)
        return result, buyresult, error_list
Exemple #15
0
 def import_data(self,stock, start, end ):
     query = MongoDB()
     df = query.format2dataframe(query.read_data("ml_fund_table",stock))
     df['change'] = (df['close'] - df['close'].shift(1))/df['close'].shift(1)
     df['code'] = stock
     return df
Exemple #16
0
class basestrategy(object):
    '''
    trading volume is the lowest in 60 days
    '''
    def __init__(self, startdate=(2011, 1, 1), enddate=[]):
        self.startdate = datetime.datetime(*startdate, 0, 0, 0, 0)
        self.enddate = enddate
        self.m = MongoDB(DB_SERVER, DB_PORT, USER, PWD, AUTHDBNAME)
        self.formatlist = [
            "date", "volume", "close", "high", "low", "open", "pre_close"
        ]
        self.savevols = [
            "stock", "buy_date", "sell_date", "holddays", "profit", "features"
        ]
        self.looplist = []
        self.trading_records = []
        self.holding_records = []
        self.datalst = []
        self.collection = None
        self.tempstatus = []
        self.lateststatus = []

    def setlooplist(self, lst=[]):
        if not lst:
            self.looplist = self.m.getallcollections("ml_security_table")
        else:
            self.looplist = lst
        return self.looplist

    def _getdata(self,
                 collection="600455.SH",
                 db="ml_security_table",
                 out=[],
                 isfilt=True,
                 filt={}):
        if not out: out = self.formatlist
        if isfilt and not filt: filt = {"date": {"$gt": self.startdate}}
        query = self.m.read_data(db, collection, filt=filt)
        return self.formatquery(query, out)

    def formatquery(self, query, out):
        '''
        query:your source data ,should be a list with dict
        out:the fields you want to convert into dataframe 
        '''
        if not out:
            query = [i for i in query.sort("date", 1)]
        else:
            query = [{k: i[k] for k in out} for i in query.sort("date", 1)]
        return pd.DataFrame(query)

    def buy(self, line, count):
        return False

    def sell(self, line, count, holding_record):
        traderecord = []
        return False, traderecord

    def setenv(self, collection):
        self.collection = collection
        data = self._getdata(collection)
        self.datalst = [
            l for l in data[self.formatlist].fillna(0).values if l[1] != 0
        ]

        self.datalst = self.rehabilitation(self.datalst)
        return

    def rehabilitation(self, lst):
        close = lst[0][2]
        c = 1
        reh = []
        for line in lst[1:]:
            c_preclose = line[6]
            if c_preclose != close:
                reh = [[i[0], i[1] * c_preclose / close] for i in reh]
                reh.append([c, c_preclose / close])
            close = line[2]
            c += 1
        result = []
        sc = 0
        for idx in range(len(reh)):
            weight = reh[idx][1]
            ec = reh[idx][0]
            piece = self.recount(lst, sc, ec, weight)
            result.extend(piece)
            sc = ec
        result.extend(self.recount(lst, ec, -1, 1))
        return result

    def recount(self, lst, sc, ec, weight):
        rst = []
        for line in lst[sc:ec]:
            rst.append([line[0], line[1], *[i * weight for i in line[2:]]])
        return rst

    def getfeatures(self, count):
        return []

    def historyreturn(self, collection):
        self.setenv(collection)
        trading_record = []
        holding_record = []
        count = 0
        lst = self.datalst
        for line in lst[:]:
            isbuy = self.buy(lst, count)

            for b in holding_record[:]:
                issell, traderecord = self.sell(lst, count, b)
                if issell:
                    holding_record.remove(b)
                    trading_record.append(traderecord)
                    print(traderecord)
            if isbuy:
                feature = self.getfeatures(lst, count)
                holding_record.append(
                    ([i for i in line], count, collection, feature))
            count += 1


#         "date","volume","close","high","low","open","pre_close"
        holdresult = [[
            collection,
            self.timestamp2date(i[0][0]), i[0][2], i[3]
        ] for i in holding_record]
        return trading_record, holdresult

    def looplist_historyreturn(self):
        for collection in self.looplist:
            try:
                tr, hr = self.historyreturn(collection)
                self.lateststatus.append(self.tempstatus)
                self.trading_records.extend(tr)
                self.holding_records.extend(hr)
            except:
                print("error: {}".format(collection))
        return self.trading_records, self.holding_records

    def timestamp2date(self, timestamp):
        try:
            return str(timestamp).split(" ")[0]
        except:
            return timestamp

    def savetrading2csv(self, filename="trading_records.csv"):
        df = pd.DataFrame(self.trading_records, columns=self.savevols)
        df.to_csv(filename)
        return

    def savetrading2db(self,
                       db="regressiontraderesult",
                       strategyname="strategytradersult"):
        db = eval("self.m.client.{}".format(db))
        bulk = db[strategyname].initialize_ordered_bulk_op()
        for line in self.trading_records:
            bulk.find({'buy_date': line[1]}).upsert().update( \
                {'$set': {'stock': line[0], \
                          'buy_date': line[1], \
                          'sell_date': line[2], \
                          'holddays': line[3], \
                          'profit': line[4], \
                          'features': line[5], \
                          }})
        bulk.execute()
        return

    def saveholding2csv(self, filename="holding_records.csv"):
        df = pd.DataFrame(self.holding_records,
                          columns=["stock", "date", "buy_data", "features"])
        df.to_csv(filename)
        return