def get_average_bond(data): # 参数 偏移百分比 均线价格与当前价格的偏差 单位 百分 float_per = 1 items = data["data"]["item"] current_data = items[-1] #计算5日均线 five_price = calculate_util.calculate_average(items, 5) # 计算5日价格与当前价格的相差百分比 five_diff_rate = calculate_util.calculate_rate(current_data[5], five_price) # 判断 5日均线与当前价格差率是否在范围内 if (five_diff_rate > float_per): return ten_price = calculate_util.calculate_average(items, 10) ten_diff_rate = calculate_util.calculate_rate(current_data[5], ten_price) if (ten_diff_rate > float_per): return twenty_price = calculate_util.calculate_average(items, 20) twenty_diff_rate = calculate_util.calculate_rate(current_data[5], twenty_price) if (twenty_diff_rate > float_per): return """保存信息到策略记录表""" stock_strategy_dao.stock_strategy().save_strategy_choose( current_data, data['data']['symbol'], 5)
def get_up_wave(data): """回溯天数""" call_back_day = 10 """容错天数""" fault_day = 2 """计算5日线回溯天数""" five_average_day = call_back_day + 5 """获取最近5日数据""" item_ = data["data"] items = item_[-five_average_day:] for index in range(call_back_day): """获取当天收盘价 多了5天计算平均值 所有位移5位 """ current_day_close_price = float( items[index + 5].split(",")[crawl_html_url.close_price_index]) """获取计算5日价的 天数""" calculate_five_day = items[index + 1:index + 6] count = 0 for every_day in calculate_five_day: count += float( every_day.split(",")[crawl_html_url.close_price_index]) five_average_value = round(count / 5, 2) """判断收盘价是否低于5日线 低于5日线 容错日减一""" if (current_day_close_price < five_average_value): fault_day -= 1 """最终容错日大于-1 证明该股票符合策略""" if (fault_day > -1): current_day_data = items[-1] # code = str(data['code']) # if code.startswith("0") | code.startswith("3"): # code = "SZ" + code # else: # code = "SH" + code stock_strategy_dao.stock_strategy().save_strategy_choose( current_day_data, data['code'], 4)
def get_biding_info(count=30, max_gain=4, min_gain=2): session = requests.session() session.get(crawl_html_url.snow_ball_main_url, headers=headers) html_data = session.get( crawl_html_url.snow_ball_stock_info_url.format(4000), headers=headers) data = json.loads(html_data.text) stock_list = data["data"]["list"] result = [] sort_array = [] """去除停盘的 停盘的没有交易量 无法比较""" for stock in stock_list: if (stock['volume'] != None): sort_array.append(stock) """根据成交量排序""" sorts_list = sorted(sort_array, key=lambda x: x['volume'], reverse=True) """获取指定排名前面的数据""" before_list = sorts_list[0:count] for stock in before_list: percent = stock["percent"] """判断该股票涨幅是否在 指定区间内""" if (percent > min_gain and percent < max_gain): result.append(stock) #持久化数据到mysql stock_info = [ stock["name"], stock["symbol"], stock["current"], stock["percent"], 1 ] stock_strategy_dao.stock_strategy().save_other_strategy_choose( stock_info) return result
def afternoon_bidding_choose(): """根据主力资金净流入 爬取净流入最大的10条数据""" html_data = requests.get(crawl_html_url.east_money_main_funds.format(20)) data = json.loads(html_data.text)["data"]["diff"] for stock in data: stock_code = complete_stock_code.complete_stock_code(stock["f12"]) stock_info = [stock["f14"], stock_code, stock["f2"], stock["f3"], 8] stock_strategy_dao.stock_strategy().save_other_strategy_choose( stock_info)
def get_stock_position_combination(begin_time=date_time_util.get_date_time(-1), end_time=date_time_util.get_date_time(0), page=1, total=100): begin_time_timestamp = int( time.mktime(time.strptime(begin_time, "%Y-%m-%d %H:%M:%S"))) * 1000 end_time_timestamp = int( time.mktime(time.strptime(end_time, "%Y-%m-%d %H:%M:%S"))) * 1000 """禁止直接访问模拟 浏览器请求""" """使用会话连接 每次访问必须先访问主页""" session = requests.session() session.get(crawl_html_url.snow_ball_main_url, headers=headers) """获取真正有效数据""" html_data = session.get(crawl_html_url.snow_ball_position_url.format( str(page), str(total)), headers=headers) data = json.loads(html_data.text) combinations_list = data['list'] result = {} """遍历投资者""" for conbination in combinations_list: """更新时间在指定时间内的才进行查询仓位操作""" if (conbination["updated_at"] > begin_time_timestamp and conbination["updated_at"] < end_time_timestamp): user_id = str(conbination["last_user_rb_gid"]) """获取仓位数据""" investor_html_data = session.get( crawl_html_url.snow_ball_investor_url.format(user_id), headers=headers) investor_data = json.loads(investor_html_data.text) position_list = investor_data["rebalancing"][ "rebalancing_histories"] """遍历仓位变动数据""" for postion in position_list: """volume表示买进""" if (postion["volume"] > 0): stock_name = postion["stock_name"] stock_code = postion["stock_symbol"] bean = result.get(stock_name) if (None == bean): bean = snow_ball_bean.snowball_stock_info( stock_name, stock_code, 1) result[stock_name] = bean else: bean.count = bean.stock_count + 1 stock_info = [ postion["stock_name"], postion["stock_symbol"], postion["price"], 0, 2 ] stock_strategy_dao.stock_strategy( ).save_other_strategy_choose(stock_info) return result
def call_back_support_stock(data): # 回溯时间 call_back_day = 60 # 排除时间周期 exclude_day = 20 # 相差幅度 float_per = 2 items = data["data"]["item"] # 截取回溯天数数据 items = items[-call_back_day:] """查询结果小于回溯天数 可能是新股 跳过""" if (len(items) < call_back_day): return current_day_data = items[-1] # 获取此时最低价 current_low_price = current_day_data[4] # 获取此时最新报价 current_new_price = current_day_data[5] # 获取此时最低价时间 current_low_day = current_day_data[0] # 获取此时波动幅度 current_percent = current_day_data[7] # 获取上个高点周期数据 last_days = items[:(call_back_day - exclude_day)] # 获取上个周期 最高价排序 sorts_list = sorted(last_days, key=lambda x: x[3], reverse=True) # 获取上个高点价格 last_high_price = sorts_list[0][3] # 获取上个高点时间 last_high_day = sorts_list[0][0] # 计算两个价格之间的相差波动率 percent = (current_low_price - last_high_price) / last_high_price percent = abs(round(percent * 100)) # 判断计算结果是否在指定区间内 if (percent < float_per): stock_strategy_dao.stock_strategy().save_strategy_choose( items[call_back_day - 1], data['data']['symbol'], 3)
def year_average_choose(data): days = 250 float_per = 20 # 获取天数据 item_ = data["data"] # 判断天数据是否低于250 天 低于250 不予处理 if len(item_) < days: return # 获取最近250天数据 items = item_[-days:] current_day_close_price = float( items[-1].split(",")[crawl_html_url.close_price_index]) count = 0 for every_day in items: count += float(every_day.split(",")[crawl_html_url.close_price_index]) year_average_price = round(count / days, 2) diff_per = round(((year_average_price - current_day_close_price) / current_day_close_price) * 100, 2) if (diff_per > 0) & (diff_per < float_per): stock_strategy_dao.stock_strategy().save_strategy_choose( items[-1], data['code'], 9)
def __init__(self): self.redis = redis_pool.RedisPool() self.log_dao = log_dao.log_record() self.stock_strategy_record = stock_strategy_record_dao.stock_strategy_record( ) self.stock_strategy = stock_strategy_dao.stock_strategy()
def __init__(self): self.stock_strategy_dao = stock_strategy_dao.stock_strategy() self.stock_strategy_record_dao = stock_strategy_record_dao.stock_strategy_record( ) pass