Esempio n. 1
0
    def on_close(self, event):
        print('进入主GUI的关闭响应函数!')

        # 保存操作日志
        if opt_lock.acquire():
            try:
                file_dir = data_dir + 'Opt_Record/'

                if not os.path.exists(file_dir):
                    os.makedirs(file_dir)
                with open(file_dir + get_current_date_str() + 'record.json',
                          'w') as f:
                    json.dump(opt_record, f)
                print('主GUI:记录保存成功!')
            finally:
                opt_lock.release()

        event.Skip()
Esempio n. 2
0
    def save_opt_info(self):
        """
		将操作日志打印到json
		:return:
		"""

        if opt_lock.acquire():
            try:
                opt_record.append({
                    'stk_code': self.stk_code,
                    'p_now': self.current_price,
                    'sale_reseau': self.thh_sale,
                    'buy_reseau': self.thh_buy,
                    'p_last': self.last_p,
                    # 'opt': opt,
                    'date_time': get_current_datetime_str()
                })

            except Exception as e:
                print('函数 JudgeSingleStk:写入操作记录失败!原因:\n' + str(e))

            finally:
                opt_lock.release()
Esempio n. 3
0
# encoding=utf-8
Esempio n. 4
0
def judge_single_stk(stk_code, stk_amount_last, qq, debug=False, gui=False):
	"""

	:param stk_code:
	:param stk_amount_last:
	:param qq:
	:param debug:
	:param gui:
	:return:
	"""
	
	money_each_opt = 5000
	
	""" 变量声明 """
	str_gui = {
		'note': '',
		'msg': ''
	}
	
	""" 'n':无操作,'b', 's' """
	opt_now = 'n'
	
	""" ==== 获取该stk的实时价格,如果是大盘指数,使用聚宽数据,否则有限使用tushare ==== """
	try:
		current_price = get_current_price_JQ(stk_code)
	except:
		str_gui = myPrint(str_gui, stk_code + '获取实时price失败!', method={True: 'gm', False: 'n'}[gui])
		return str_gui
	
	debug_print_txt('stk_judge', stk_code, 'realtime_p:' + str(current_price) + '\n', debug)
	
	""" ==================== 获取上次price ==================== """
	opt_json_stk = read_opt_json(stk_code, opt_record_file_url)
	
	# 如果没有相应的json文件,不进行判断,直接返回
	if opt_json_stk == {}:
		debug_print_txt('stk_judge', stk_code, '函数 judge_single_stk:' + code2name(stk_code) + '没有历史操作记录,不进行阈值判断!\n',
		                debug)
		
		# print('函数 judge_single_stk:' + code2name(stk_code) + '没有历史操作记录,不进行阈值判断!')
		return str_gui
	
	if len(opt_json_stk['b_opt']) == 0:
		debug_print_txt('stk_judge', stk_code, '函数 judge_single_stk:' + code2name(stk_code) + '没有历史操作记录,不进行阈值判断!\n',
		                debug)
		
		# print('函数 judge_single_stk:' + code2name(stk_code) + '没有历史操作记录,不进行阈值判断!')
		return str_gui
	
	# 读取上次p和b操作中的最小p,备用
	last_p = opt_json_stk['p_last']
	b_p_min = np.min([x['p'] for x in opt_json_stk['b_opt']])
	
	debug_print_txt('stk_judge', stk_code,
	                '\n上次p:' + str(last_p) +
	                '\n最小p:' + str(b_p_min) +
	                '\nb_change_ratio:' + '%0.2f' % (100.0 * (current_price - last_p) / last_p) + '%' +
	                '\ns_change_ratio:' + '%0.2f' % (100.0 * (current_price - b_p_min) / b_p_min) + '%',
	                debug)
	
	""" =========== 实时计算价差,用于“波动提示”和“最小网格限制” ======== """
	if debug:
		str_gui = myPrint(
			str_gui,
			'\n\n' + stk_code + ':\np_now:' + str(current_price) + '\np_last:' + str(
				last_p) + '\np_change_ratio:' + '',
			method={True: 'gm', False: 'n'}[gui])
	
	""" ========== 排除获取的价格为0的情况,此种情况可能是stop或者时间未到 ========== """
	if current_price == 0.0:
		str_gui = myPrint(
			str_gui,
			stk_code + 'price==0.0! 返回!',
			method={True: 'gm', False: 'n'}[gui])
		
		return str_gui
	
	buy_amount = math.floor((money_each_opt / current_price) / 100) * 100
	
	""" 实时计算网格大小 """
	earn_threshold_unit = get_single_stk_reseau(stk_code)
	
	""" 调节 buy 和 sale 的threshold """
	if stk_code in RSV_Record.keys():
		thh_sale = earn_threshold_unit * 2 * RSV_Record[stk_code]
		thh_buy = earn_threshold_unit * 2 * (1 - RSV_Record[stk_code])
		
		debug_print_txt('stk_judge', stk_code, '', debug)
	else:
		RSV_Record[stk_code] = cal_rsv_rank(stk_code, 5)
		thh_sale = earn_threshold_unit * 2 * RSV_Record[stk_code]
		thh_buy = earn_threshold_unit * 2 * (1 - RSV_Record[stk_code])
	
	debug_print_txt('stk_judge', stk_code, 'thh_sale:' + str(thh_sale) + ' thh_buy:' + str(thh_buy),
	                debug)
	
	""" 将操作日志保存到全局变量中 """
	if opt_lock.acquire():
		try:
			opt_record.append({
				'stk_code': stk_code,
				'p_now': current_price,
				'sale_reseau': thh_sale,
				'buy_reseau': thh_buy,
				'p_last': last_p,
				# 'opt': opt,
				'date_time': get_current_datetime_str()
			})
		
		except Exception as e:
			print('函数 JudgeSingleStk:写入操作记录失败!原因:\n' + str(e))
		
		finally:
			opt_lock.release()
	
	if debug:
		# print('函数 JudgeSingleStk:' + stk_code + '定时处理完成后,操作记录为:\n')
		# pprint(opt_record)
		pass
	
	""" ==================== 判断波动是否满足“最小网格限制” ================= """
	"""
	config_json = read_config()
	if not ('minReseau' in config_json.keys()):
		write_config('minReseau', 0.02)
		min_reseau = 0.02
	else:
		min_reseau = config_json['minReseau']

	if (min_reseau > math.fabs(thh_sale/last_p)) | (min_reseau > math.fabs(thh_buy/last_p)):

		str_tmp = stk_code + ' ' + code2name(stk_code) + ':\n'\
			+ 'buy相对宽度:%0.3f \n' % (thh_buy/last_p) +\
			  'sale相对宽度:%0.3f\n' % (thh_sale/last_p) +\
			  '设定波动阈值:%0.3f\n' % min_reseau +\
			  '波动未达到最小网格宽度,返回!'

		str_gui = myPrint(
			str_gui,
			str_tmp,
			method={True: 'gm', False: 'n'}[gui])
		return str_gui

	if debug:

		str_gui = myPrint(
			str_gui,
			stk_code +
			':\np_change:' + str(price_diff * stk_amount_last) +
			'\nthreshold:' + str(earn_threshold_unit) +
			'\nthh_sale:' + str(thh_sale) +
			'\nthh_buy:' + str(thh_buy),
			method={True: 'gm', False: 'n'}[gui])
	"""
	
	""" ============================= 判断是否超过阈值,进行bs操作 ============================== """
	pcr = read_config()['pcr']
	if (current_price - b_p_min > thh_sale) & ((current_price - b_p_min) / b_p_min >= pcr):
		
		str_temp = "触发卖出网格!可以考虑卖出! " + stk_code + code2name(stk_code) + \
		           '\nAmount:' + str(stk_amount_last) + \
		           '\n当前价格:' + str(current_price) + \
		           '\n上次买入价格:' + str(b_p_min) + \
		           '\n买入网格大小:' + '%0.3f' % thh_buy + \
		           '\n卖出网格大小:' + '%0.3f' % thh_sale + \
		           '\n最小操作幅度:' + '%0.3f' % pcr
		
		debug_print_txt('stk_judge', stk_code, str_temp,
		                debug)
		
		str_gui = myPrint(
			str_gui,
			str_temp,
			method={True: 'gn', False: 'qq'}[gui],
			towho=qq)
		
		if not gui:
			# sendHourMACDToQQ(stk_code, qq, source='jq')
			pass
	
	elif (current_price - last_p < -thh_buy) & ((current_price - last_p) / b_p_min <= -pcr):
		
		str_temp = "触发买入网格!可以考虑买入!" + stk_code + code2name(stk_code) + \
		           '\nAmount:' + str(buy_amount) + \
		           '\n当前价格:' + str(current_price) + \
		           '\n上次价格:' + str(last_p) + \
		           '\n买入网格大小:' + '%0.2f' % thh_buy + \
		           '\n卖出网格大小:' + '%0.2f' % thh_sale + \
		           '\n最小操作幅度:' + '%0.3f' % pcr
		
		debug_print_txt('stk_judge', stk_code, str_temp,
		                debug)
		
		str_gui = myPrint(
			str_gui,
			str_temp,
			method={True: 'gn', False: 'qq'}[gui],
			towho=qq)
		
		# if not gui:
			# sendHourMACDToQQ(stk_code, qq, source='jq')
	
	else:
		str_gui = myPrint(
			str_gui,
			stk_code + ':未触发任何警戒线!',
			method={True: 'gm', False: 'n'}[gui])
		
		debug_print_txt('stk_judge', stk_code, stk_code + ':未触发任何警戒线!',
		                debug)
	
	""" ========================== 波动检测 =========================== """
	# change_flag, str_gui = judge_p_change_ratio(stk_code, (current_price-last_p)/last_p, str_gui=str_gui, gui=gui)
	# if change_flag:
	#
	# 	str_temp = "波动推送! " + stk_code + code2name(stk_code) +\
	# 			'\nAmount:' + str(buy_amount) +\
	# 			'\n当前价格:' + str(current_price) +\
	# 			'\n上次价格:' + str(last_p) +\
	# 			'\n买入网格大小:' + '%0.2f' % thh_buy +\
	# 			'\n卖出网格大小:' + '%0.2f' % thh_sale
	#
	# 	str_gui = myPrint(
	# 		str_gui,
	# 		str_temp,
	# 		method={True: 'gn', False: 'qq'}[gui],
	# 		towho=qq)
	
	# if not gui:
	# 	sendHourMACDToQQ(stk_code, qq, source='jq')
	
	return str_gui
Esempio n. 5
0
def judge_single_stk(stk_code, stk_amount_last, debug=False, gui=False):
	"""

	:param stk_code:
	:param stk_amount_last:
	:param debug:
	:param gui:
	:return:
	"""
	
	""" 变量声明 """
	str_gui = {
		'note': '',
		'msg': ''
	}
	str_ = '\n\n\n=============================================\n' + code2name(stk_code) + ':开始进入本周期判断!\n'
	debug_print_txt('stk_judge', stk_code, str_, debug)
	str_gui['msg'] = str_gui['msg'] + str_
	
	money_each_opt = 5000
	
	""" ==== 获取该stk的实时价格,如果是大盘指数,使用聚宽数据,否则有限使用tushare ==== """
	try:
		current_price = get_current_price_JQ(stk_code)
	except:
		str_gui['msg'] = str_gui['msg'] + stk_code + '获取实时price失败!\n'
		return str_gui
	
	str_ = '实时价格:' + str(current_price) + '\n'
	debug_print_txt('stk_judge', stk_code, str_, debug)
	str_gui['msg'] = str_gui['msg'] + str_
	
	""" ==================== 获取上次price ==================== """
	opt_json_stk = read_opt_json(stk_code, opt_record_file_url)
	
	# 如果没有相应的json文件,不进行判断,直接返回
	if opt_json_stk == {}:
		str_ = code2name(stk_code) + '没有历史操作记录,不进行阈值判断!\n'
		debug_print_txt('stk_judge', stk_code, '函数 judge_single_stk:' + str_,
		                debug)
		str_gui['msg'] = str_gui['msg'] + str_
		return str_gui
	
	if len(opt_json_stk['b_opt']) == 0:
		str_ = code2name(stk_code) + '没有历史操作记录,不进行阈值判断!\n'
		debug_print_txt('stk_judge', stk_code, '函数 judge_single_stk:' + str_,
		                debug)
		str_gui['msg'] = str_gui['msg'] + str_
		return str_gui
	
	# 读取
	threshold_satisfied_flag = opt_json_stk['threshold_satisfied_flag']
	
	str_gui['msg'] = str_gui['msg'] + '已经进行过闪动提示?:' + {False: '是', True: '否'}.get(threshold_satisfied_flag, '未知') + '\n'
	
	# 读取上次p和b操作中的最小p,备用
	last_p = opt_json_stk['p_last']
	b_p_min = np.min([x['p'] for x in opt_json_stk['b_opt']])
	
	str_ = '\n上次操作价格:' + str(last_p) + \
	       '\n最小买入价格:' + str(b_p_min) + \
           '\n买入-波动率:' + '%0.3f' % (100.0 * (current_price - last_p) / last_p) + '%' + \
           '\n卖出-波动率:' + '%0.3f' % (100.0 * (current_price - b_p_min) / b_p_min) + '%' + \
			'\n买入-波动:' + '%0.2f' % (current_price - last_p) + \
			'\n卖出-波动:' + '%0.2f' % (current_price - b_p_min)
	
	debug_print_txt('stk_judge', stk_code, str_, debug)
	str_gui['msg'] = str_gui['msg'] + str_ + '\n'
	
	""" =========== 实时计算价差,用于“波动提示”和“最小网格限制” ======== """
	
	""" ========== 排除获取的价格为0的情况,此种情况可能是stop或者时间未到 ========== """
	if current_price == 0.0:
		str_gui = myPrint(
			str_gui,
			stk_code + 'price==0.0! 返回!',
			method={True: 'gm', False: 'n'}[gui])
		
		return str_gui
	
	buy_amount = math.floor((money_each_opt / current_price) / 100) * 100
	
	""" 实时计算网格大小 """
	earn_threshold_unit = get_single_stk_reseau(stk_code)
	
	""" 调节 buy 和 sale 的threshold """
	if stk_code in RSV_Record.keys():
		thh_sale = earn_threshold_unit * 2 * RSV_Record[stk_code]
		thh_buy = earn_threshold_unit * 2 * (1 - RSV_Record[stk_code])
		
		debug_print_txt('stk_judge', stk_code, '', debug)
	else:
		RSV_Record[stk_code] = cal_rsv_rank(stk_code, 5)
		thh_sale = earn_threshold_unit * 2 * RSV_Record[stk_code]
		thh_buy = earn_threshold_unit * 2 * (1 - RSV_Record[stk_code])
		
	str_ = '\n卖出网格大小:' + '%0.3f' % thh_sale + '\n买入网格大小:' + '%0.3f' % thh_buy + '\n'
	debug_print_txt('stk_judge', stk_code, str_, debug)
	str_gui['msg'] = str_gui['msg'] + str_
	
	""" 将操作日志保存到全局变量中 """
	if opt_lock.acquire():
		try:
			opt_record.append({
				'stk_code': stk_code,
				'p_now': current_price,
				'sale_reseau': thh_sale,
				'buy_reseau': thh_buy,
				'p_last': last_p,
				# 'opt': opt,
				'date_time': get_current_datetime_str()
			})
		
		except Exception as e:
			print('函数 JudgeSingleStk:写入操作记录失败!原因:\n' + str(e))
		
		finally:
			opt_lock.release()
	
	""" ============================= 判断是否超过阈值,进行bs操作 ============================== """
	pcr = read_config()['pcr']/100.0
	
	# 打印日志
	debug_print_txt('stk_judge', stk_code, '读取的最小波动率:' + str(pcr) + '\n', debug)
	
	debug_print_txt('stk_judge', stk_code, '允许阈值判断标志位(True为允许):' + str(threshold_satisfied_flag) + '\n', debug)
	debug_print_txt('stk_judge', stk_code, '判断是否可以卖出:\ncurrent_price - b_p_min > thh_sale:' + str(current_price - b_p_min > thh_sale) + '\n', debug)
	debug_print_txt('stk_judge', stk_code,
	                '(current_price - b_p_min) / b_p_min >= pcr:' + str((current_price - b_p_min) / b_p_min >= pcr) + '\n', debug)
	
	debug_print_txt('stk_judge', stk_code,
	                '判断是否可以买入:\ncurrent_price - last_p < -thh_buy:' + str(current_price - last_p < -thh_buy) + '\n',
	                debug)
	debug_print_txt('stk_judge', stk_code,
	                '(current_price - last_p) / b_p_min <= -pcr:' + str((current_price - last_p) / b_p_min <= -pcr) + '\n', debug)

	if (current_price - b_p_min > thh_sale) & ((current_price - b_p_min) / b_p_min >= pcr):
		
		str_temp = "触发卖出网格!可以考虑卖出! " + stk_code + code2name(stk_code) + \
		           '\nAmount:' + str(stk_amount_last) + \
		           '\n当前价格:' + str(current_price) + \
		           '\n上次买入价格:' + str(b_p_min) + \
		           '\n买入网格大小:' + '%0.3f' % thh_buy + \
		           '\n卖出网格大小:' + '%0.3f' % thh_sale + \
		           '\n最小操作幅度:' + '%0.3f' % pcr
		
		debug_print_txt('stk_judge', stk_code, str_temp + '\n在灯神进行bs操作之前,此stk不再进行阈值判断\n',
		                debug)
		
		if threshold_satisfied_flag:
			str_gui['note'] = str_gui['note'] + str_temp
			
			# 除非有bs操作,否则不再提示
			set_opt_json_threshold_satisfied_flag(opt_record_file_url, stk_code, value=False)
			
		else:
			str_gui['msg'] = str_gui['msg'] + str_temp
			
	elif (current_price - last_p < -thh_buy) & ((current_price - last_p) / b_p_min <= -pcr):
		
		str_temp = "触发买入网格!可以考虑买入!" + stk_code + code2name(stk_code) + \
		           '\nAmount:' + str(buy_amount) + \
		           '\n当前价格:' + str(current_price) + \
		           '\n上次价格:' + str(last_p) + \
		           '\n买入网格大小:' + '%0.2f' % thh_buy + \
		           '\n卖出网格大小:' + '%0.2f' % thh_sale + \
		           '\n最小操作幅度:' + '%0.3f' % pcr
		
		debug_print_txt('stk_judge', stk_code, str_temp + '\n在灯神进行bs操作之前,此stk不再进行阈值判断\n',
		                debug)
		if threshold_satisfied_flag:
			str_gui['note'] = str_gui['note'] + str_temp
			
			# 除非有bs操作,否则不再提示
			set_opt_json_threshold_satisfied_flag(opt_record_file_url, stk_code, value=False)
		else:
			str_gui['msg'] = str_gui['msg'] + str_temp
		
		
	
	else:
		str_ = stk_code + ':未触发任何警戒线!\n'
		str_gui['msg'] = str_gui['msg'] + str_
		debug_print_txt('stk_judge', stk_code, str_, debug)
		
	return str_gui