#!/usr/bin/env python # coding: utf-8 import datetime import pymongo import sys from datetime import datetime #from datetime import timedelta global stocks global k_ri global list_code global conn_list, conn_k list_code = [] quotes = [] def init_mongodb_list(): global stocks global conn_list # mongodb_link = 'mongodb://127.0.0.1:27017' # mongoClient = MongoClient(mongodb_link) conn_list = pymongo.MongoClient("localhost", 27017) conn_list.stock_list.authenticate("d", "zz") db = conn_list.stock_list stocks = db.stocks def init_mongodb_k(): global k_ri global conn_k conn_k = pymongo.MongoClient("localhost", 27017) conn_k.db_stock.authenticate("d", "zz") db = conn_k.db_stock k_ri = db.k_ri def get_k_ri(code, quotes): print "code",code l = k_ri.find({"code": code, "close": {"$gt": 0}}).sort([('date', 1)]) for i in l: code = i.get("code") date = i.get("date") open = i.get("open") high = i.get("high") low = i.get("low") close = i.get("close") volume = i.get("volume") adj_close = i.get("adj_close") #if (date == "2015-07-21"): # print code, date, open, high, low, close, volume, adj_close quotes.append((date, open, high, low, close)) #print quotes #print code, date,"----------------------------------------" def get_stock_list(): l = stocks.find() for i in l: code = i.get("code") name = i.get("name") list_code.append({"code": code, "name": name}) # print type(name), name, type(code),code #list_code.append({"code": "000001", "name": "上证指数".decode('utf8')}) #list_code.append({"code": "399001", "name": "深证指数".decode('utf8')}) #list_code.append({"code": "399006", "name": "创业板指".decode('utf8')}) def judge_rise(price): #print price if (0 == price): return 0.00 price *= 1.1 price += 0.005 return float('{:.2f}'.format(price)) ''' def plot(quotes): date_old, open_old, high_old, low_old, close_old = 0, 0, 0, 0, 0 flg_rise, high_max, flg_buy, days, cnt_win, cnt_lost = 0, 0, 0, 0, 0, 0 total = 100 total_old = 0 for q in quotes: date, open, high, low, close = q[0], float(q[1]), float(q[2]), float(q[3]), float(q[4]) #if (date == "2015-07-21"): # print date, open, high, low, close, q rise = judge_rise(close_old) #print rise, close, close_old if (0 == flg_buy): if (close == rise): #print "hi", rise, close, close_old, date flg_rise += 1 high_max = high else: if (2 < flg_rise): if (high_max <= high): high_max = high #print "3 dan" else: flg_buy = 1 days = 10 else: flg_rise = 0 elif (1 == flg_buy): if (0 >= days): #print "days die" flg_rise, flg_buy, = 0, 0 if (high_max < high): buy = high_max + 0.01 flg_buy = 2 #print "buy" days -= 1 elif (2 == flg_buy): if (high_max <= high): high_max = high if (0.9 >= close / buy): total_old = total total = total_old * close / buy print buy, high_max, close, total_old, total, date, "lost----------------------------------------------------------" flg_rise, flg_buy, = 0, 0 cnt_lost += 1 else: if (0.9 >= close / high_max or buy - close < 0.5): total_old = total total = total_old * close / buy print buy, high_max, close, total_old, total, date, "win ++++++++++++++++++++++++++++++++++++++++++++++++++++++" flg_rise, flg_buy, = 0, 0 cnt_win += 1 else: print "else " flg_rise, flg_buy, = 0, 0 date_old, open_old, high_old, low_old, close_old = date, open, high, low, close print total, cnt_win, cnt_lost return total #print date, open, high, low, close ''' def judge_rise(price): # print price if (0 == price): return 0.00 price *= 1.1 price += 0.005 return float('{:.2f}'.format(price)) def judge_down(price): # print price if (0 == price): return 0.00 price *= 0.9 price += 0.005 return float('{:.2f}'.format(price)) def plot_buy3(open, high, low, close, close_old, date): global g_flg_rise, g_high_max, g_flg_buy, g_days, g_buy if (g_high_max <= high): g_high_max = high if (0.7 > close / g_high_max): g_buy = open + 0.1 g_flg_rise = 0 print "buy", date return 1 return 0 def plot_buy2(open, high, low, close, close_old, date): global g_flg_rise, g_high_max, g_flg_buy, g_days, g_buy rise = judge_rise(close_old) if (close == rise): g_flg_rise += 1 g_high_max = high else: if (2 < g_flg_rise): if (g_high_max <= high): g_high_max = high g_buy = open + 0.1 g_flg_rise = 0 # print "buy" return 1 else: g_flg_rise = 0 return 0 def plot_buy(open, high, low, close, close_old, date): global g_flg_rise, g_high_max, g_flg_buy, g_days, g_buy if (0 == g_flg_buy): rise = judge_rise(close_old) if (close == rise): g_flg_rise += 1 g_high_max = high else: if (2 < g_flg_rise): if (g_high_max <= high): g_high_max = high # print "read buy" else: g_flg_buy = 1 g_days = 10 else: g_flg_rise = 0 # print g_flg_buy, g_days else: if (0 >= g_days): # print "g_days die" g_flg_rise, g_flg_buy, = 0, 0 if (g_high_max < high): g_buy = g_high_max g_flg_rise, g_flg_buy, = 0, 0 # print "buy" return 1 g_days -= 1 # print g_flg_buy, g_days,"g_buy" return 0 def plot_sale(open, high, low, close, close_old, date): global g_high_max, g_cnt_win, g_cnt_lost, g_buy global g_total if (high == low): return 1 if (g_high_max <= high): g_high_max = high if (0.9 >= close / g_buy): total_old = g_total g_total = total_old * close / g_buy print g_buy, g_high_max, close, total_old, g_total, date, "lost----------------------------------------------------------" g_cnt_lost += 1 return 2 else: gain = g_buy / close if (1.3 > gain): line = 0.8 elif (1.2 > gain): line = 0.85 else: line = 0.9 if (line >= close / g_high_max or 1.05 > gain or 2 > close - g_buy): total_old = g_total g_total = total_old * close / g_buy print g_buy, g_high_max, close, total_old, g_total, date, "win ++++++++++++++++++++++++++++++++++++++++++++++++++++++" g_cnt_win += 1 return 2 return 1 def plot_sale2(open, high, low, close, close_old, date): global g_high_max, g_cnt_win, g_cnt_lost, g_buy global g_total if (1.2 < high / g_buy): total_old = g_total g_total = total_old * close / g_buy print g_buy, g_high_max, close, total_old, g_total, date, "win ++++++++++++++++++++++++++++++++++++++++++++++++++++++" g_cnt_win += 1 return 2 return 1 dict_g_buy_sale_plot = {"plot_buy": plot_buy, "plot_buy2": plot_buy2, "plot_sale": plot_sale, "plot_buy3": plot_buy3, "plot_sale2": plot_sale2} def calculate_g_buy_sale(open, high, low, close, close_old, date, symbol): return dict_g_buy_sale_plot.get(symbol)(open, high, low, close, close_old, date) global g_flg_rise, g_high_max, g_flg_buy, g_days, g_cnt_win, g_cnt_lost, g_buy global g_total def plot(quotes): global g_flg_rise, g_high_max, g_flg_buy, g_days, g_cnt_win, g_cnt_lost, g_buy g_flg_rise, g_high_max, g_flg_buy, g_days, g_cnt_win, g_cnt_lost, g_buy = 0, 0, 0, 0, 0, 0, 0 global g_total g_total = 100 date_old, open_old, high_old, low_old, close_old = 0, 0, 0, 0, 0 flg_buy = 0 for q in quotes: date, open, high, low, close = q[0], float(q[1]), float(q[2]), float(q[3]), float(q[4]) # print flg_buy if (0 == flg_buy): flg_buy = calculate_g_buy_sale(open, high, low, close, close_old, date, "plot_buy") elif (1 == flg_buy): flg_buy = calculate_g_buy_sale(open, high, low, close, close_old, date, "plot_sale") if (2 == flg_buy): g_flg_rise, g_flg_buy, = 0, 0 flg_buy = 0 date_old, open_old, high_old, low_old, close_old = date, open, high, low, close print g_total, g_cnt_win, g_cnt_lost return g_total def plot2(quotes): print "222" calculateDict = {"plot": plot, "plot2": plot2} def calculate(x, symbol): return calculateDict.get(symbol)(x) def main(argv=None): starttime = datetime.now() list_total = [] init_mongodb_list() name = get_stock_list() conn_list.close() # print name init_mongodb_k() print len(list_code) #i = 0 for code in list_code: #if (i > 30): # break #i += 1 quotes = [] get_k_ri(code["code"], quotes) if (0 == len(quotes)): continue total = calculate(quotes, "plot") print total #print total,type(total) list_total.append(total) conn_k.close() list_total.sort() print list_total p = 0 for t in list_total: p += t print p,"total" p /= len(list_total) print p,"pri" endtime = datetime.now() # print (endtime - starttime).microseconds print "cnt", len(list_code), "endtime", (endtime - starttime).seconds print endtime.strftime('%Y-%m-%d %H:%M:%S'), starttime.strftime('%Y-%m-%d %H:%M:%S') if __name__ == '__main__': sys.exit(main())
python 日k 简约策略
最新推荐文章于 2026-06-08 18:32:06 发布
本文介绍了一种利用Python和MongoDB进行股票数据分析的方法。通过定义特定的数据处理函数,如买入和卖出信号判断,实现了对股票市场趋势的自动分析。该策略涉及价格波动分析、买卖时机选择等内容。

1万+

被折叠的 条评论
为什么被折叠?



