def notify_realtime_earnings(self): # 实时持仓盈亏检测通知 while True: df_position = DataFrame() if os.path.exists(self.__file_path_position): try: df_position = Utils.read_data(self.__file_path_position) except: # 可能在修改文件,等一分钟 sleep(60) continue else: ERROR('file {0} not exists.'.format(self.__file_path_position)) return cur_time = Utils.cur_time() hour = int(cur_time.split(':')[0]) minute = int(cur_time.split(':')[1]) if hour < 9 or (hour == 9 and minute < 30): LOG( 'notify_realtime_earnings: morning\n{0} hours {1} minutes later market open'\ .format( int( Utils.now2market_morning_time() / 3600 ), int( Utils.now2market_morning_time() % 3600 / 60 ) ) ) sleep(Utils.now2market_morning_time()) elif (hour == 11 and minute >= 30) or hour == 12: LOG('notify_realtime_earnings: nooning\n{0} hours {1} minutes later market open' .format(int(Utils.now2market_nooning_time() / 3600), int(Utils.now2market_nooning_time() % 3600 / 60))) sleep(Utils.now2market_nooning_time()) elif hour >= 15: LOG('notify_realtime_earnings: market close') break content_notify = '' content_notify += '{0}\n'.format(cur_time) total_earn = 0 for index in df_position.index: code = '%06d' % df_position.loc[index]['code'] name = df_position.loc[index]['name'] try: df_realtime_quotes = Data().get_realtime_quotes(code) buy_price = float(df_position.loc[index]['buy_price']) cur_price = float(df_realtime_quotes['price']) position = df_position.loc[index]['position'] earn = (cur_price - buy_price) * position total_earn += earn content_notify += '-{0} {1} cur:{2:.2f} cost:{3:.2f} sell:{4:.2f} position:{5} earn:{6:.2f}\n'\ .format( code, name, cur_price, buy_price, float( df_position.loc[ index ][ 'sell_price' ] ), position, earn) except: pass content_notify += 'total_earn:{0:.2f}'.format(total_earn) if SEND_EMAIL: Utils.send_email(content_notify, 'position notification') sleep(60 * 10) else: LOG(content_notify) sleep(60 * 10)
except: pass if SEND_EMAIL: # 如果发送邮件,10分钟发一次 Utils.send_email(content_notify, 'opportunity notification') sleep(10 * 60) else: LOG('*********************************') LOG(content_notify) LOG('*********************************') sleep(60 * 5) if __name__ == '__main__': if int(Utils.cur_time().split(':')[0]) < 18: # 18点之前,采用前一个交易日的分析结果 analyse_class = model.spill_wave.Analyse() list_process = [] list_process.append( Process(target=Notify().notify_investment_opportunity)) # list_process.append( Process( target = Notify().notify_realtime_earnings ) ) list_process.append(Process(target=Notify().serve_query_request)) for process in list_process: process.start() for process in list_process: process.join() else: # 当日18点之后方可更新数据
def notify_investment_opportunity(self): df_spill_wave_stock = Utils.read_data( model.spill_wave.Analyse().spill_wave_stock_file) df_model_basics = Basics().get_basics().set_index('code') while True: cur_time = Utils.cur_time() hour = int(cur_time.split(':')[0]) minute = int(cur_time.split(':')[1]) if hour < 9 or (hour == 9 and minute < 30): LOG( 'notify_investment_opportunity: morning\n{0} hours {1} minutes later market open'\ .format( int( Utils.now2market_morning_time() / 3600 ), int( Utils.now2market_morning_time() % 3600 / 60 ) ) ) sleep(Utils.now2market_morning_time()) elif (hour == 11 and minute >= 30) or hour == 12: LOG('notify_investment_opportunity: nooning\n{0} hours {1} minutes later market open' .format(int(Utils.now2market_nooning_time() / 3600), int(Utils.now2market_nooning_time() % 3600 / 60))) sleep(Utils.now2market_nooning_time()) elif hour >= 15: LOG('notify_investment_opportunity: market close') break content_notify = '' content_notify += '{0}\n'.format(cur_time) for index in df_spill_wave_stock.index: code = '%06d' % df_spill_wave_stock.loc[index]['code'] name = df_spill_wave_stock.loc[index]['name'] try: df_realtime_quotes = Data().get_realtime_quotes(code) if float(df_realtime_quotes['price']) >= (float( df_spill_wave_stock.loc[index]['buy_price']) * 0.99): content_notify += '-{0} {1} cur price:{2:.2f} buy price:{3:.2f} sell price:{4:.2f} expect earn:{5:.2f}\n'\ .format( code, name, float( df_realtime_quotes[ 'price' ] ), \ float( df_spill_wave_stock.loc[ index ][ 'buy_price' ] ), \ float( df_spill_wave_stock.loc[ index ][ 'sell_price' ] ), \ float( df_spill_wave_stock.loc[ index ][ 'expect_earn_rate' ] ), \ float( df_spill_wave_stock.loc[ index ][ 'min_earn_rate' ] ) ) content_notify += '\tprofit rank:{0}\n \tindustry:{1} pe rank:{2}\n'.format( df_model_basics[ 'rank_profit_grow' ][ int( code ) ],\ df_model_basics[ 'industry' ][ int( code ) ], df_model_basics[ 'rank_pe' ][ int( code ) ] ) id_concept = 1 id_rank = 1 name_concept = '_'.join(['concept', str(id_concept)]) name_rank = '_'.join(['rank_pe', str(id_rank)]) while df_model_basics[name_concept][int( code)] is not np.nan: content_notify += '\tconcept:{0} pe rank:{1}\n'.format( df_model_basics[ name_concept ][ int( code ) ], \ df_model_basics[ name_rank ][ int( code ) ] ) id_concept += 1 id_rank += 1 if id_concept > 20: break name_concept = '_'.join( ['concept', str(id_concept)]) name_rank = '_'.join(['rank_pe', str(id_rank)]) content_notify += '\n' except: pass if SEND_EMAIL: # 如果发送邮件,10分钟发一次 Utils.send_email(content_notify, 'opportunity notification') sleep(10 * 60) else: LOG('*********************************') LOG(content_notify) LOG('*********************************') sleep(60 * 5)
def do_realtime_trade( self ): pd.options.mode.chained_assignment = None # 不显示warn信息 default='warn' df_model_basics = Basics().get_basics().set_index( 'code' ) if os.path.exists( self.__file_path_trade ): self.df_trade = Utils.read_data( self.__file_path_trade ) if self.df_trade.index.size >= 1: self.remain_money = self.df_trade.loc[ self.df_trade.index.size - 1 ][ 'remain' ] if os.path.exists( self.__file_path_position ): self.df_position = Utils.read_data( self.__file_path_position ) while True: cur_time = Utils.cur_time() hour = int( cur_time.split( ':' )[ 0 ] ) minute = int( cur_time.split( ':' )[ 1 ] ) if hour < 9 or ( hour == 9 and minute < 30 ): LOG( 'Trade_Simulator: morning\n{0} hours {1} minutes later market open'\ .format( int( Utils.now2market_morning_time() / 3600 ), int( Utils.now2market_morning_time() % 3600 / 60 ) ) ) sleep( Utils.now2market_morning_time() ) elif ( hour == 11 and minute >= 30 ) or hour == 12: LOG( 'Trade_Simulator: nooning\n{0} hours {1} minutes later market open' .format( int( Utils.now2market_nooning_time() / 3600 ), int( Utils.now2market_nooning_time() % 3600 / 60 ) ) ) sleep( Utils.now2market_nooning_time() ) elif hour >= 15: LOG( 'Trade_Simulator: market close' ) break # sell for index in self.df_position.index: code = '%06d' % int( self.df_position.loc[ index ][ 'code' ] ) df_realtime_quotes = Data().get_realtime_quotes( code ) # T + 1 交易 if self.df_position.loc[ index ][ 'buy_date' ] < Utils.cur_date(): name = self.df_position.loc[ index ][ 'name' ] if float( df_realtime_quotes[ 'price' ] ) >= self.df_position.loc[ index ][ 'sell_price' ]: self.remain_money += float( df_realtime_quotes[ 'price' ] ) * float( self.df_position.loc[ index ][ 'position' ] ) total_earn = '{0:.2f}'.format( ( float( df_realtime_quotes[ 'price' ] ) - float( self.df_position.loc[ index ][ 'cost_price' ] ) ) \ * float( self.df_position.loc[ index ][ 'position' ] ) + float( self.df_trade.loc[ self.df_trade.index.size - 1 ][ 'total_earn' ] ) ) self.df_trade.loc[ self.df_trade.index.size ] = [ ' '.join( [ Utils.cur_date(), Utils.cur_time() ] ), code, name, 'sell', \ self.df_position.loc[ index ][ 'position' ], total_earn, self.remain_money ] self.df_position = self.df_position.drop( index ) continue self.df_position[ 'earn' ][ index ] = '{0:.2f}'.format( ( float( df_realtime_quotes[ 'price' ] ) - float( self.df_position.loc[ index ][ 'cost_price' ] ) ) \ * float( self.df_position.loc[ index ][ 'position' ] ) ) sleep( 1 ) # buy for index in self.df_model_stock.index: code = '%06d' % int( self.df_model_stock.loc[ index ][ 'code' ] ) name = self.df_model_stock.loc[ index ][ 'name' ] # 每只在仓股不再买入 if int( code ) in self.df_position[ 'code' ].to_dict().values() or \ code in self.df_position[ 'code' ].to_dict().values(): continue try: df_realtime_quotes = Data().get_realtime_quotes( code ) if float( df_realtime_quotes[ 'price' ] ) >= ( float( self.df_model_stock.loc[ index ][ 'buy_price' ] ) * 0.99 ) and \ float( df_realtime_quotes[ 'price' ] ) <= ( float( self.df_model_stock.loc[ index ][ 'buy_price' ] ) * 1.002 ) and \ float( self.df_model_stock.loc[ index ][ 'expect_earn_rate' ] ) >= 0.2: try: if float( df_model_basics[ 'rank_profit_grow' ][ int( code ) ].split( '/' )[ 0 ] ) / \ float( df_model_basics[ 'rank_profit_grow' ][ int( code ) ].split( '/' )[ 1 ] ) >= 0.5: continue # LOG( '{0} {1}'.format( code, df_model_basics[ 'rank_profit_grow' ][ int( code ) ].split( '/' ) ) ) except:pass buy_hand_num = 0 # 至少剩余买一手的资金 if 100 * float( df_realtime_quotes[ 'price' ] ) <= self.remain_money: buy_hand_num = int( min( self.initial_fund * float( self.df_model_stock.loc[ index ][ 'expect_earn_rate' ] ) * 0.5 / float( df_realtime_quotes[ 'price' ] ) / 100, \ self.max_buy_each_stock / float( df_realtime_quotes[ 'price' ] ) / 100, self.remain_money / float( df_realtime_quotes[ 'price' ] ) / 100 ) ) if self.df_trade.index.size >= 1: total_earn = self.df_trade.loc[ self.df_trade.index.size - 1 ][ 'total_earn' ] else: total_earn = 0 self.remain_money -= buy_hand_num * 100 * float( df_realtime_quotes[ 'price' ] ) self.df_trade.loc[ self.df_trade.index.size ] = [ ' '.join( [ Utils.cur_date(), Utils.cur_time() ] ), code, name, 'buy', \ buy_hand_num * 100, total_earn, self.remain_money ] self.df_position.loc[ self.df_position.index.size ] = [ code, name, Utils.cur_date(), float( df_realtime_quotes[ 'price' ] ), \ float( df_realtime_quotes[ 'price' ] ) * ( 0.2 * float( self.df_model_stock.loc[ index ][ 'expect_earn_rate' ] ) + 1.0 ), \ buy_hand_num * 100, 0 ] sleep( 1 ) except: pass self.save_data( self.df_trade, self.__file_path_trade ) self.save_data( self.df_position, self.__file_path_position )