def big_deal_loader(self, code, vol=400): today = date.today() today_str = datetime.strftime(today, '%Y-%m-%d') try: df = ts.get_sina_dd(code, date=today_str, vol=vol) assert df is not None result_set = df.to_json(orient='records') result_set = json.loads(result_set) for record in result_set: volume = record['volume'] price = record['price'] amount = volume * price if amount < 300000: continue deal_time = record.pop('time') time_str = '{} {}'.format(today_str, deal_time) date_time = datetime.strptime(time_str, '%Y-%m-%d %H:%M:%S') record['date'] = date_time self.db.deal_record.insert(record) self.logger.info('{} has been inserted successfully at {}'.format( code, today_str)) except Exception, e: self.logger.error( '{} has failed to insert beacuse of {} at {}'.format( code, e, today_str))
def dd(stock_code, date_val=date.today(), vol=0): if vol == 0: vol = get_default_vol(stock_code) result = ts.get_sina_dd(stock_code, date_val, vol) return result
def getBigVol(stockCode='000748', vols=400): today = time.strftime('%Y-%m-%d') #.decode('utf-8') dtype = [u'中性盘', u'买盘', u'卖盘'] color = ['b', 'r', 'g'] df = ts.get_sina_dd(stockCode, date=today, vol=vols) #默认400手 df = df.sort_values(by='time') df['amount'] = df.volume * df.price df.index = range(df.shape[0]) gp = df.groupby(['type']) sumt = gp.sum() sumt['avgPrice'] = sumt.amount / sumt.volume print(sumt) plt.figure(figsize=(15, 4)) # plt.rcParams['font.sans-serif'] = ['Microsoft YaHei'] #用来正常显示中文标签 # plt.rcParams['font.sans-serif'] = ['SimHei'] #用来正常显示中文标签 # plt.rcParams['axes.unicode_minus'] = False #用来正常显示负号 pdf = df.tail(100) for i in range(3): plt.bar(pdf[pdf['type'] == dtype[i]].index, pdf[pdf['type'] == dtype[i]].volume, alpha=0.7, color=color[i]) plt.grid(True) plt.title(stockCode + u"大单设置为:" + str(vols)) #plt.xticks(range(df.shape[0]),range(df.shape[0])) plt.margins(0) plt.show() return df, sumt #
def dd_buy(code, trade_days_dict): result = True for day in trade_days_dict.keys(): dd = ts.get_sina_dd(code, date=day, vol=500) if dd is None: result = False break buy_vol = dd[dd.type == '买盘']['volume'].to_dict() sel_vol = dd[dd.type == '卖盘']['volume'].to_dict() total_buy = 0 total_sel = 0 for k in buy_vol.keys(): total_buy += buy_vol[k] for k in sel_vol.keys(): total_sel += sel_vol[k] if total_buy - total_sel < 0: result = False break return result
def ddjy(index, date=getYesterday(), vol=2000): df = ts.get_sina_dd(index, date=str(getYesterday()), vol=2000) # ============================================================================= # df['date'] = datetime.date.today() # ============================================================================= return df
def trade_block_worker(engine, codes, sdate, edate): pid = os.getpid() tsl.log("pid %i start with %i codes..." % (pid, len(codes))) df = pd.DataFrame() cdate = sdate temp = {} while cdate <= edate: if not tsu.is_holiday(cdate): for code in codes: try: newdf = ts.get_sina_dd(code, cdate, vol=10000) if newdf is not None: newdf['date'] = cdate df = df.append(newdf, ignore_index=True) except BaseException, e: if 'timed out' in str(e) or 'urlopen error' in str(e): temp.setdefault(cdate, []) temp[cdate].append(code) pass else: print e tsl.log("pid %i error for %s on %s" % (pid, code, str(cdate))) if len(df) != 0: df = df.set_index('code', drop='true') df.to_sql('trade_block', engine, if_exists='append') cdate += datetime.timedelta(days=1)
def getSinaDd(self, code, date, vol=400, bseq=True): data = tushare.get_sina_dd(code, date, vol, 60, 0.5) it = data.iterrows() l = [] for row in it: ll = [date] # 如果数据是NaN或者是'--'则改为0 ll.extend([ e if (isinstance(e, str) or not math.isnan(e)) and e != '--' else 0 for e in row[1].values ]) l.append(ll) sql = 'insert into sina_dd values(' sql += 'null' if bseq else '' # date参数位置 sql += ',%s' for e in data.columns.values: sql += ',%s' sql += ')' conn = self.getDBConn() cursor = conn.cursor() cursor.executemany(sql, l) conn.commit() conn.close() print(code + ' ' + date + ' inserted')
def get_dadan_data(self): ''' 参数说明: code:股票代码,即6位数字代码 date:日期,格式YYYY-MM-DD vol:手数,默认为400手,输入数值型参数 retry_count : int, 默认3,如遇网络等问题重复执行的次数 pause : int, 默认 0,重复请求数据过程中暂停的秒数,防止请求间隔时间太短出现的问题 返回值说明: code:代码 name:名称 time:时间 price:当前价格 volume:成交手 preprice :上一笔价格 type:买卖类型【买盘、卖盘、中性盘】 ''' dadan_data = ts.get_sina_dd(code=self.code, date='2018-08-08', vol=400, retry_count=self.retry_count, pause=self.pause) print(dadan_data) return dadan_data
def get_tick_sina(_stock_id, _trade_date): # sina base base_vol = 200 # should >= 400 df = None try: df = ts.get_sina_dd(_stock_id, date=_trade_date, vol=base_vol) except Exception: log_error("warn: %s get sina exception!", _stock_id) time.sleep(5) return None if df is None : log_error("warn: stock %s, %s is None, next", _stock_id, _trade_date) return None if df.empty: log_error("warn: stock %s, %s is empty, next", _stock_id, _trade_date) return None if len(df) <= 5: log_error("warn: stock %s, %s is short %d, next", _stock_id, _trade_date, len(df)) return None # sina data: convert to 手 df['volume'] = df['volume'] / 100 df = df.set_index('time').sort_index() return df
def download_dd_data(start=None): ''' 获取大单数据 ''' conn = db.get_dd_data_db() start = start if start is None: start = utils.today_last_year(1) for code in get_all_stock_code(): end = datetime.today().date() while start < end: date = end.strftime('%Y-%m-%d') df = ts.get_sina_dd(code=code, date=date, vol=500) if df is not None: df.insert(0, 'code', code) try: sql_df = df.loc[:, :] sql.to_sql(sql_df, name='dd_data', con=conn, index=True, if_exists='append') log.info('%s,%s dd data download ok.' % (code, start)) except Exception as e: log.error('download error:%s,%s' % (code, date)) pass start = start + timedelta(days=1)
def get_bid(index): if index in codes: return hist = ts.get_sina_dd(index, date=date, vol=500) if hist is not None: # print(index, hist['volume'].sum()) hist = hist.iloc[::-1] r = redis.Redis(connection_pool=redis_pool) rise_stop = r.get('quant.{}.rise_stop'.format(index)) rise_stop = float(rise_stop) super_dd = False for i in range(len(hist)): if hist['volume'][i] > 2000 * 100: super_dd = True if hist['price'][i] == rise_stop: # 涨停 if hist['preprice'][i] != 0: # 开盘涨停 # if hist['preprice'][i] != hist['price'][i] or super_dd: # 大单打到涨停 if hist['preprice'][i] != hist['price'][i]: # 大单打到涨停 mu.acquire() codes.append(index) mu.release() print( 'time: {}, rise stop: {}, preprice: {}, price: {}'. format(hist['time'][i], rise_stop, hist['preprice'][i], hist['price'][i])) print('https://xueqiu.com/S/{}{}'.format( get_stock_type(index).upper(), index)) break
def get_index_data(code): stock_basic = get_basic(code).ix[0] subtitle = [stock_basic[1],code] max_date = get_max_date(code)#dt.datetime.now() max_date = dt.datetime.strptime(max_date,"%Y-%m-%d") start = max_date + dt.timedelta(days=-90) stock_90 = get_one_stock_all(code,start=start.strftime('%Y-%m-%d'),end=max_date.strftime('%Y-%m-%d')) stock_data = stock_90.tail(2).sort_values(by="date",ascending=False) sotck_sh_90 = get_one_stock_all('sh',start=start.strftime('%Y-%m-%d'),end=max_date.strftime('%Y-%m-%d'))#ts.get_hist_data('sh',start=start.strftime('%Y-%m-%d'),end=max_date.strftime('%Y-%m-%d')) x_data = stock_90.date.values.tolist() y_data0 = stock_90.p_change.values.tolist() y_data1 = sotck_sh_90.p_change.values.tolist() #大手买卖 tf = ts.get_sina_dd(code, stock_data.iloc[0]['date']) if isinstance(tf,pd.DataFrame): gf = tf.groupby('type') s = gf.volume.sum() pie_l = {x:y for x,y in s.items()} pie_data = generate_pie_data(pie_l) category = list(pie_l.keys()) else: category = ["没有数据",] pie_data = "[{name:'没有数据',value:1}]" return stock_basic,subtitle,stock_data,x_data,y_data0,y_data1,category,pie_data#,cloud_text
def updatestockbigchange(): conn = ms.connect(host='localhost', port=3306, user='******', passwd='123456', db='investment', charset="utf8") cur = conn.cursor() cur.execute('select code from mainlist') stus = cur.fetchall() now = time.strftime("%Y-%m-%d", time.localtime(time.time())) code = '' for stu in stus: code = stu[0] stockbigchangelist = ts.get_sina_dd(code, now) stockbigchange = pd.DataFrame(stockbigchangelist) if stockbigchange.size != 0: cur.execute("delete from stockbigchange_" + code) values = [] for index, row in stockbigchange.iterrows(): values.append((row['time'], row['price'], row['volume'], row['preprice'], row['type'])) insertstr = 'insert into stockbigchange_' + code + ' (time,price,volume,preprice,type) values(%s,%s,%s,%s,%s)' cur.executemany(insertstr, values) conn.commit() cur.close() conn.close()
def get_sina_dd(self, code, date='', vol=400): """ 大单交易数据 """ if date == '': date = date_time.get_today_str() d = ts.get_sina_dd(code, date=date, vol=vol) return d
def exportDadan(fromDate, toDate, code): df = ts.get_sina_dd(code, toDate) engine = create_engine(sql_str) table_name = "dadan_" + code + "_" + toDate.replace("-", "", 3) # 存入数据库 df.to_sql('dadan', engine, if_exists='append') Log.logger.info("dadan数据导入完成。") return True
def get_sina_dd(stock_code, **params): try: logger.info('get sina DD data starting...') df = ts.get_sina_dd(stock_code, **params) logger.info('get sina DD data end...') except: logger.exception('some errors between get sina DD data end...') return df
def GetStockData(self, code=None, startTime=None): if code is None: return None try: return ts.get_sina_dd(code=code, date=startTime) except: print(sys.exc_info()[0]) return None
def get_large_order(code=None, date=None, vol=400, retry_count=3, pause=0.001, data_source='tushare'): if is_tushare(data_source): return ts.get_sina_dd(code, date, vol, retry_count, pause)
def get_dd_df_thru_code(code, vol=400, date=None): ''' 获得某个股票的大单df ''' if date is None: date = datetime.datetime.now().strftime("%Y-%m-%d") df = ts.get_sina_dd(code, date=date, vol=vol) return df
def get_sina_dd(self, code, timeStr): df = ts.get_sina_dd(code, date=timeStr) ndf = pandas.DataFrame() if df is not None: ndf = df.groupby('type').sum().reset_index() if len(ndf) > 2: return ndf[1:].reset_index() return ndf
def big_deal(self, codes, vol, check_time): ''' for code in codes: df=ts.get_sina_dd(code,vol,check_time) print df ''' df = ts.get_sina_dd('603918', '2017-04-22') print df
def get_dd(index): hist = ts.get_sina_dd(index, date=date, vol=500) print(hist) print(hist['volume'].sum()) buy = hist[hist['type'].str.contains('买盘')]['volume'].sum() sell = hist[hist['type'].str.contains('卖盘')]['volume'].sum() print('买盘', buy) print('卖盘', sell) print(round(buy / sell, 2))
def download_Large_amount(time,code,vols): data_L=ts.get_sina_dd(code , date=time ,vol=vols) files_path = '../report/Daily_database/big_amu'+code if os.path.exists(files_path) == False: # 判断文件是不是存在 os.mkdir(files_path) # 创建目录5 data_L.to_csv(files_path+'/%s_LGamount%s_csv.csv'%(time,vols),encoding='gbk') with pd.ExcelWriter(files_path+'/%s_LGamount%s_xlx.xlsx'%(time,vols)) as writer: data_L.to_excel(writer, sheet_name='Sheet1') print('\n%s 大单 have been saved'%time)
def bigs(): """大于400手的大单 参数说明: code:股票代码,即6位数字代码 date:日期,格式YYYY-MM-DD vol:手数,默认为400手,输入数值型参数 retry_count : int, 默认3,如遇网络等问题重复执行的次数 pause : int, 默认 0,重复请求数据过程中暂停的秒数,防止请求间隔时间太短出现的问题""" df = ts.get_sina_dd('600848', date='2020-10-09', vol=500) # 默认400手 print(df)
def realtime(self, id): # all_stock=ts.get_today_all() # print all_stock df = ts.get_realtime_quotes(id) # print df[['time','name','price','bid','ask','volume']] # print df.head() price_change = ts.get_today_ticks(id) print price_change[['time', 'change', 'type', 'volume']] big_share = ts.get_sina_dd(id, date='2016-04-01') print big_share[['time', 'code', 'price', 'preprice', 'volume', 'type']]
def realtime(self, id): # all_stock=ts.get_today_all() # print(all_stock) df = ts.get_realtime_quotes(id) # print(df[['time','name','price','bid','ask','volume']]) # print(df.head()) price_change = ts.get_today_ticks(id) print(price_change[['time', 'change', 'type', 'volume']]) big_share = ts.get_sina_dd(id, date='2016-04-01') print(big_share[['time', 'code', 'price', 'preprice', 'volume', 'type']])
def realtime(self, id): # all_stock=ts.get_today_all() # print(all_stock) df = ts.get_realtime_quotes(id) # print(df[['time','name','price','bid','ask','volume']]) # print(df.head()) # price_change = ts.get_today_ticks(id) # print(price_change[['time','change','type','volume']]) big_share = ts.get_sina_dd(id, date=self.date) print(big_share[['time', 'code', 'price', 'preprice', 'volume', 'type']]) print(big_share.sort(columns='volume'))
def get_sina_dd(): df = ts.get_sina_dd('600848', date='2016-12-09') grouped = df.groupby(df['type']) groupedSum = grouped.sum() a = groupedSum.iloc[0:1,:] buy = groupedSum[1,2] if groupedSum[1,1]=='买盘' else groupedSum[2,2] print df print grouped print groupedSum print a print buy return a
def anyl_dadan_zhanbi(self): matchs = [] j = 0 for code in self._codes: # if code.startswith('300'): # continue # code_zhanbi=() # code_zhanbi['code']=code j += 1 df = ts.get_realtime_quotes(code) # keys=numpy.array(df.volume.keys).tolist() # if not days[i] in keys: # continue if 0 not in df.volume: continue chengjiaoliang = 0 if self._maxval <= 0: chengjiaoliang = float(df.volume[0]) self._maxval = int(chengjiaoliang * 0.00005) print self._maxval print '-----------------------' # print ('%d/%d current=%s' %(j,len(self._codes),code)) ds = ts.get_sina_dd(code=code, date=self._time_start, vol=self._maxval) # ds['total']=ds.price*ds.volume # maipantol=ds[ds.type=='买盘'].total.sum() # maipan1tol=ds[ds.type=='卖盘'].total.sum() if ds is None: continue maipantol = ds[ds.type == '买盘'].volume.sum() maipan1tol = ds[ds.type == '卖盘'].volume.sum() jinliuru = maipantol - maipan1tol # df= ts.get_hist_data(code=code,start=self._time_start,end=self._time_start) time.sleep(1) if jinliuru == 0.0 or chengjiaoliang == 0.0: continue liuruzhanbi = jinliuru / chengjiaoliang # p_change=(df.price-df.open[0])/df.open[0] # if liuruzhanbi<self._zhanbi: # print liuruzhanbi # continue # code_zhanbi['zhanbi']=liuruzhanbi matchs.append( (code, Decimal.from_float(liuruzhanbi), self._time_start)) # print ('date=%s,code=%s,liuruzhanbi=%s' %(str(self._time_start),str(code),str(liuruzhanbi))) # print ('date=%s,code=%s,zhangfu=%s,jinliuru=%s' %(str(days[i]),str(self._codes[0]),str(secdayzhangfu),str(jinliuru))) # print matchs return matchs
def get_plenty_of_order(): # 笔数阀值 vol = 1000 for index, date in enumerate(dates): for i in range(len(stock_list)): target_stock = stock_list.loc[i] if i <= 2: target_date_stock = ts.get_sina_dd(stock_code_format( target_stock['ts_code']), date=date, vol=vol) if target_date_stock is not None: print(target_date_stock, date)
def realtime(self, id): # all_stock=ts.get_today_all() # print all_stock df = ts.get_realtime_quotes(id) # print df[['time','name','price','bid','ask','volume']] # print df.head() # price_change = ts.get_today_ticks(id) # print price_change[['time','change','type','volume']] big_share = ts.get_sina_dd(id, date=self.date) print big_share[[ 'time', 'code', 'price', 'preprice', 'volume', 'type' ]] print big_share.sort(columns='volume')
def rt_dadan_rank_one(_stock_id, _dd_date, _db): buy = 0 sell = 0 mid = 0 # 100手起 base_vol = 100 df = None try: df = ts.get_sina_dd(_stock_id, date=_dd_date, vol=base_vol) except Exception: log_error("warn: %s get_sina_dd exception!", _stock_id) time.sleep(5) return -4,-4,-4 if df is None : # log_error("warn: stock %s is None, next", _stock_id) return -1,-1,-1 if df.empty: # log_error("warn: stock %s is empty, next", _stock_id) return -2,-2,-2 if len(df) <= 5: # log_error("warn: stock %s is short, next", _stock_id) return -3,-3,-3 # convert to 手 df['volume'] = df['volume'] / 100 log_debug("ranking: %s, size: %d", _stock_id, len(df)) # 保持 升序 df = df.sort_index(ascending=False) # rank 2016/8/28 rank, content = get_df_rank(df) # if rank >= 300 or (rank >= 209 and rank % 100 == 9): # if rank >= 100 or (rank >= 209 and rank % 100 == 9): if rank >= 309: subject = "rank: %d 净流入 %s (%.2f%%)" % (rank, _stock_id, get_chg_rate(_stock_id)) content += get_basic_info_all(_stock_id, _db) log_info("nice: %s, %s", subject, content) # saimail_dev(subject, content) buy, sell, mid = get_buy_sell_sum2(df) return buy, sell, mid
def get_fangliang_time(self, code, timeStr=None): if not timeStr: timeStr = time.strftime("%Y-%m-%d", time.localtime()) df = ts.get_sina_dd(code, date=timeStr) if len(df) < 20: return None index = len(df) - 20 while index > 0: ndf = df[index:].groupby(['type']).sum().reset_index() if (ndf['volume'][1] > ndf['volume'][2] * 1.5): print(ndf) return df['time'][index] index -= 1 return None
def getBigVol(code): # 获取当天的分笔 # today_vol=ts.get_today_ticks(code) # hist_vol=ts.get_tick_data(code,date='2016-11-28') # print(today_vol.head(10)) # print(hist_vol) hist_big_deal = ts.get_sina_dd(code, date='2016-12-01', vol=500) if hist_big_deal is None: print("No Big Deal") else: print(hist_big_deal)
def bigTrade(code, dat=str(date.today())): df = ts.get_sina_dd(code=code, date=dat, vol=600) # 默认400手 conn = connMysql().createconn() conn.autocommit(True) cur = conn.cursor() sql = 'delete from stk_dadan_detail where code=' + '\'' + code + '\'' + ' and date=' + '\'' + dat + '\';' cur.execute(sql) try: if (type(df) == pandas.core.frame.DataFrame): df['date'] = dat #print() df.to_sql('stk_dadan_detail', engine, if_exists='append') except: print(code + ': 获取大单数据失败')
def updatestockbigchange(): conn= ms.connect(host='localhost',port = 3306,user='******', passwd='123456',db ='investment',charset="utf8") cur = conn.cursor() cur.execute('select code from mainlist') stus = cur.fetchall() now=time.strftime("%Y-%m-%d",time.localtime(time.time())) code='' for stu in stus: code=stu[0] stockbigchangelist=ts.get_sina_dd(code,now) stockbigchange=pd.DataFrame(stockbigchangelist) if stockbigchange.size!=0: cur.execute("delete from stockbigchange_"+code) values=[] for index,row in stockbigchange.iterrows(): values.append((row['time'],row['price'],row['volume'],row['preprice'],row['type'])) insertstr='insert into stockbigchange_'+code+' (time,price,volume,preprice,type) values(%s,%s,%s,%s,%s)' cur.executemany(insertstr,values) conn.commit() cur.close() conn.close()
def get_sina_big_deal(engine, stock_code): df = ts.get_sina_dd(stock_code, date='2016-03-09') # 默认400手 df.to_sql('sina_bigdeal', engine, if_exists='append')
def getSinaDD(code, date, vol, retry_count, pause): return ts.get_sina_dd(code, date, vol, retry_count, pause)
if __name__=="__main__": sailog_set("sairank.log") base_vol = 100 stock_id = "000736" dd_date = "2016-08-17" stock_id = "002694" stock_id = "600187" stock_id = "600084" stock_id = "000002" dd_date = "2016-08-19" dd_date = "2016-08-17" stock_id = "002154" stock_id = "002694" # df = ts.get_tick_data(stock_id, date=dd_date) df = ts.get_sina_dd(stock_id, date=dd_date, vol=base_vol) if df is None: log_debug("None") else: df['volume'] = df['volume'] / 100 # sina df = df.sort_values(by='time') # df = df[df.volume >= 200] log_debug("size: %d", len(df)) # df = df.head(580) log_debug("data:\n%s", df) rank, rs, ns, mess = check_df_rates(df) log_debug("rank: %d", rank) # end
def rt_dadan_one(_stock_id, _dd_date, _db): global g_has_noticed global g_has_noticed2 global g_has_noticed3 global g_good_list global g_good_list2 global g_good_list3 # today data base_vol = 200 df = None try: df = ts.get_sina_dd(_stock_id, date=_dd_date, vol=base_vol) except Exception: log_error("warn: %s get_sina_dd exception!", _stock_id) time.sleep(5) return -4 if df is None : # log_error("warn: stock %s is None, next", _stock_id) return -1 if df.empty: log_error("warn: stock %s is empty, next", _stock_id) return -2 log_debug("processing: %s", _stock_id) # convert to 手 df['volume'] = df['volume'] / 100 global g_check_count g_check_count += 1 df = df.sort_index(ascending=False) # buy vol_base = 3000 df_buy = df[df.volume >= vol_base] # check df vol = 3000 cnt = 20 pri = 7.00 if g_has_noticed.has_key(_stock_id): pass else: good = rt_dadan_check_buy(_stock_id, df_buy, _db, vol, cnt, g_has_noticed, g_good_list, pri) vol = 10000 cnt = 12 pri = 6.00 if g_has_noticed2.has_key(_stock_id): pass else: good = rt_dadan_check_buy(_stock_id, df_buy, _db, vol, cnt, g_has_noticed2, g_good_list2, pri) vol = 120000 cnt = 1 pri = 6.00 g_good_list3 = [] # 2017-4-3 if g_has_noticed3.has_key(_stock_id): pass else: good = rt_dadan_check_buy(_stock_id, df_buy, _db, vol, cnt, g_has_noticed3, g_good_list3, pri) return
# download_dir = setup_download_dir() # links = [l for l in get_links(client_id) if l.endswith('.jpg')] # Create a queue to communicate with the worker threads queue = Queue.Queue() if len(codes) >=8: thread_n=8 else: thread_n=len(codes) # Create 8 worker threads print "thread is ::::",thread_n print "" for x in range(thread_n): worker = DownloadWorker(queue) # Setting daemon to True will let the main thread exit even though the workers are blocking worker.daemon = True worker.start() # Put the tasks into the queue as a tuple for code in codes: logger.info('Queueing {}'.format(code)) queue.put((code)) # Causes the main thread to wait for the queue to finish processing all the tasks queue.join() logging.info('Took %s', time() - ts) # print('Took %s', time() - ts) if __name__ == '__main__': # codes=mp.get_all_top() # main(codes) df = ts.get_sina_dd('601198','2015-11-20') print df