def __init__(self): cf = configparser.ConfigParser() cf.read('/home/lgy/PycharmProjects/Stock/stock.init') #cf.read('stock.init') usr = cf.get('db','db_user') pwd = cf.get('db', 'db_pass') # self.engine = create_engine('mysql://*****:*****@127.0.0.1/gupiao?charset=utf8') # self.sqlconn = pymysql.connect(host='localhost', port=3306,user='******',passwd='root',db='mysql',charset='UTF8') self.engine = create_engine('mysql://'+usr+':'+pwd+'@127.0.0.1/gupiao?charset=utf8') self.sqlconn = pymysql.connect(host='localhost', port=3306,user=usr,passwd=pwd,db='mysql',charset='UTF8') self.cur = self.sqlconn.cursor() self.cur.execute("use gupiao;") self.db = m_db2()
def get_canBuy_code(type): db = m_db2() if type == 'YES': db.cur.execute( "select code,outstanding from t_all_stickcode where outstanding < 50000 \ and (substr(code,1,1)='0' or substr(code,1,1)='6'); ") else: db.cur.execute("select code,outstanding from t_all_stickcode ;") sqlrs = db.cur.fetchall() for code in sqlrs: # print('pre_data:',code[0]) df = pre_data(code[0], 'W') control(df) return
def pre_data(stick_code, ktype='D', today=''): # ktype in ('D','W','M') #today='2010-01-01' if '' == today: today = datetime.date.today().strftime('%Y-%m-%d') # begindate = datetime.date.today() - datetime.timedelta(days=13) global df db = m_db2() try: if ktype == 'D': df = db.get_data( "select * from t_stick_data_d where code = '" + stick_code + "' and date > '2015-09-01' and date <='" + today + "' order by date asc;") #and date>'2015-05-01' elif ktype == 'W': df = db.get_data("select * from t_stick_data_w where code = '" + stick_code + "' ;") #and date>'2015-05-01' elif ktype == 'M': df = db.get_data("select * from t_stick_data_m where code = '" + stick_code + "' ;") # and date>'2015-05-01' except Exception as e: #print('ERR:',e) return df['cci'] = ta.CCI(df['high'].values.astype('double'), df['low'].values.astype('double'), df['close'].values.astype('double')) df['diff'], df['dea'], df['macd'] = ta.MACD( df['close'].values.astype('double'), fastperiod=12, slowperiod=26, signalperiod=9) df['obv'] = ta.OBV(df['close'].values.astype('double'), df['vol'].values.astype('double')) df['volma5'] = ta.MA(df['vol'].values.astype('double'), 5) df['volma13'] = ta.MA(df['vol'].values.astype('double'), 13) df['volma20'] = ta.MA(df['vol'].values.astype('double'), 20) df['volma34'] = ta.MA(df['vol'].values.astype('double'), 34) df['MA20'] = ta.MA(df['close'].values.astype('double'), 20) df['MA60'] = ta.MA(df['close'].values.astype('double'), 60) df['MA5'] = ta.MA(df['close'].values.astype('double'), 5) df['MA13'] = ta.MA(df['close'].values.astype('double'), 13) df['MA34'] = ta.MA(df['close'].values.astype('double'), 34) df['MA89'] = ta.MA(df['close'].values.astype('double'), 89) df['MA144'] = ta.MA(df['close'].values.astype('double'), 144) df['cwbili'] = 0 df['pricebili'] = 0 return df
def drawDayWeek(code, date, duadays, ktype='D'): # code=xxxx date=YYYY-MM-DD db = m_db2() beginday = datetime.datetime.strptime(date, '%Y-%m-%d') endday = beginday + datetime.timedelta(days=duadays) beforday = beginday - datetime.timedelta(days=9) befordayW = beginday - datetime.timedelta(days=400) df = db.pre_data(code, beginday=beforday.strftime('%Y-%m-%d'), endday=endday.strftime('%Y-%m-%d'), ktype=ktype) dfw = db.pre_data(code, beginday=befordayW.strftime('%Y-%m-%d'), endday=beforday.strftime('%Y-%m-%d'), ktype='W') saveimg(df, dfw, date) return
def pre_data(stick_code, ktype='D'): # ktype in ('D','W','M') global df db = m_db2() try: if ktype == 'D': df = db.get_data( "select * from t_stick_data_d where code = '" + stick_code + "' and date > '2015-09-01';") #and date>'2015-05-01' elif ktype == 'W': df = db.get_data("select * from t_stick_data_w where code = '" + stick_code + "' ;") #and date>'2015-05-01' elif ktype == 'M': df = db.get_data("select * from t_stick_data_m where code = '" + stick_code + "' ;") # and date>'2015-05-01' except Exception as e: print('ERR:', e) return df['cci'] = ta.CCI(df['high'].values.astype('double'), df['low'].values.astype('double'), df['close'].values.astype('double')) df['diff'], df['dea'], df['macd'] = ta.MACD( df['close'].values.astype('double'), fastperiod=12, slowperiod=26, signalperiod=9) df['obv'] = ta.OBV(df['close'].values.astype('double'), df['vol'].values.astype('double')) df['volma5'] = ta.MA(df['vol'].values.astype('double'), 5) df['volma20'] = ta.MA(df['vol'].values.astype('double'), 20) df['MA20'] = ta.MA(df['close'].values.astype('double'), 5) #print(df) #i= ta.CDLCONCEALBABYSWALL(df['open'].values.astype('double'),df['high'].values.astype('double'), # df['low'].values.astype('double'),df['close'].values.astype('double'),) #print(i) return df
def control(df): db = m_db2() if df is None: print('is none') return try: lendf = len(df) if lendf > 2 and float(df.loc[lendf-1]['vol'])>1.4*float(df.loc[lendf-2]['vol'])\ and float(df.loc[lendf - 2]['open']) > float(df.loc[lendf-1]['close']) \ and float(df.loc[lendf-1]['close']) > float(df.loc[lendf-1]['open']) \ and float(df.loc[lendf-1]['open']) > float(df.loc[lendf - 2]['close']): print('can buy:', df.loc[lendf - 1]['code']) try: print("insert ;;;;;;;;;;;;;;;;;;;;;;;;;;", df.loc[lendf - 1]['date'], df.loc[lendf - 1]['code']) db.insert_can_buy_code(df.loc[lendf - 1]['date'], df.loc[lendf - 1]['code'], '1') db.commit() except Exception as e: print('ERR:', e) except Exception as e: pre_data('err:', e) return
return def runtoday(): currentday = datetime.date.today() strcurrentday = currentday.strftime('%Y-%m-%d') print(time.asctime(time.localtime(time.time())), 'currentday:', currentday) coderslt = db.getXiaoShiZhiStock() for code in coderslt['code']: # run(code,strcurrentday) run2(code, strcurrentday) return ################################ db = m_db2() ld = load() def m_run(): #补全历史数据 day #ld.data_complete(beginday='2015-06-01',endday='2017-02-14',ktype='D') enddate = datetime.date.today() begindate = datetime.date.today() - datetime.timedelta(days=7) ld.data_complete(beginday=begindate.strftime('%Y-%m-%d'), endday=enddate.strftime('%Y-%m-%d'), ktype='D') #m_draw.drawDayWeek('000672','2016-12-30',10,ktype='D') #huiCeMoniDay()
def show_canbuy_code(date): db = m_db2() return db.get_can_buy_code(date)
def test(): #ld = m_load_update_data.load() enddate = datetime.date.today() begindate = datetime.date.today() - datetime.timedelta(days=14) db = m_db2() db.delete_date('t_stick_data_w',enddate.strftime('%Y-%m-%d'),'603900')