def create_cut_table(self): self.first_date = CandleTable.get_first_date(self.candle_table_name) self.last_date = CandleTable.get_last_date(self.candle_table_name) self.cut_trend_table_name = self.get_trend_table_name() if DBManager.exists_table(self.cut_trend_table_name): DBManager.drop_table(self.cut_trend_table_name) tt = TrendTable(self.cut_trend_table_name) tt.save() candles = CandleTable.get_candle_array(self.candle_table_name) for c in candles: date = c.date hits = TrendTable.get_most_recent_hits(self.trend_table_name, date) trend = Trend(dbm, self.cut_trend_table_name, date, hits) trend.save() ##cursor = TrendTable.get_section(self.trend_table_name, self.first_date, self.last_date) ##trend_tuples = cursor.fetchall() ##for t in trend_tuples: ##date = t[0] ##hits = t[1] ##trend = Trend(dbm, self.cut_trend_table_name, date, hits) ##trend.save() ##dbm.save_and_close() return tt
def main(): db = MongoClient().test_database n = 0.0 QUERY = read_in() first = Trend() first.setName(QUERY) for tweet in db.tweets.find({ "entities.hashtags.text": re.compile(QUERY, re.IGNORECASE) }).sort([("_id", pymongo.DESCENDING)]): trend_in_tweet = False for hashtag in tweet["entities"]["hashtags"]: if hashtag['text'].encode("utf-8").lower() != QUERY.lower(): first.addToRelated(hashtag['text'].encode("utf-8").lower(), 1) n = n + 1 print QUERY.lower(), ": ", str(n) top_10 = sorted(first.getRelated().items(), key=operator.itemgetter(1), reverse=True) for top in top_10: print top[0], ": ", float(top[1]) / n first.clear()
def insert(self): rows = self.data["table"]["rows"] for r in rows: try: date_string = r["c"][0]["v"] print(date_string) date = date_to_timestamp(date_string) hits = r["c"][1]["f"] t = Trend(self.table_name, date, hits) t.save() except: print("reached the end")
def _new_trend(self, name): if not self.__running: raise ENotRunning() """ Return an instance that implements ITrend interface for new trend with no points. """ new_trend = Trend() period = 60 points = [] preferences = {} new_trend.configure( {"parent": self.trends, "name": name, "period": period, "points": points, "preferences": preferences} ) return new_trend
def _new_trend(self, name): if not self.__running: raise ENotRunning() """ Return an instance that implements ITrend interface for new trend with no points. """ new_trend = Trend() period = 60 points = [] preferences = {} new_trend.configure({ 'parent': self.trends, 'name': name, 'period': period, 'points': points, 'preferences': preferences }) return new_trend
def convolution(trend_1, trend_2): trend = Trend() n = len(trend_1.y) m = len(trend_2.y) y = [] for i in range(n): y_k = 0 for j in range(m): coefficient_x = i - j if coefficient_x >= 0: y_m = trend_1.y[coefficient_x] * trend_2.y[j] else: # y_m = trend_2.y[j] y_m = 0 y_k += y_m y.append(y_k) trend.y = np.array(y) trend.x = trend_1.x return trend
def get_trend_array(table_name): ##returns a cursor pointing to all candles linked to the table_name dbm = DBManager.get_instance() cursor = dbm.get_cursor() cursor.execute("SELECT * FROM '{tn}'".format(tn=table_name)) trends = [] ##loop through cursor and add all candles to array row = cursor.fetchone() while row is not None: t = Trend.from_tuple(table_name, row) trends.append(t) row = cursor.fetchone() dbm.save_and_close() return trends
def get_trends(self) -> list: trends = [] avarage_body_size = self.get_average_body_size_of_candles() trend_candles = [] dir = self.candles[0].direction for candle in self.candles: if (candle.is_shot(avarage_body_size) or candle.direction == dir): trend_candles.append(candle) else: trends.append(Trend(trend_candles)) trend_candles = [candle] dir = candle.direction return trends
def main(): db = MongoClient().test_database n = 0.0 QUERY = read_in() first = Trend() first.setName(QUERY) for tweet in db.tweets.find({"entities.hashtags.text": re.compile(QUERY, re.IGNORECASE)}).sort([("_id", pymongo.DESCENDING)]): trend_in_tweet = False for hashtag in tweet["entities"]["hashtags"]: if hashtag['text'].encode("utf-8").lower() != QUERY.lower(): first.addToRelated(hashtag['text'].encode("utf-8").lower(),1) n = n + 1 print QUERY.lower(), ": ", str(n) top_10 = sorted(first.getRelated().items(), key=operator.itemgetter(1), reverse=True) for top in top_10: print top[0], ": ", float(top[1])/n first.clear()
def runbacktest(begin=BEGIN_DATE, codename="600519.SH", dir=GRAPH, end=END_DATE, paint=True, star=False): """ 设置策略环境参数,并运行部分回测策略 :param begin: 回测开始时间 :param codename: 回测股票代码或名称 :param thresh: 日线高低点间隔交易日长度 :param dir: 作图目录地址 :param end: 回测结束时间,默认当前 :param bounceThresh: 严格下跌过程中反弹超过bounceThresh交易日确认上涨,默认为65天 :param paint: bool值,是否画图,默认为True :param star: bool值,最后一个标记星星,默认为False :return: 返回策略环境对象整体和图像地址 """ print(f"BS日线策略:正在回测{codename}...") try: klist = ldd(begin, codename, end) except loaddataError as e: print(f"\t{e}") print(f"\t{codename}获取日K线数{len(klist)}") # 设置策略运行参数环境 hlp_env = HLPoint(klist, codename) hlp_env.init_hl() hlp_env.get_hl() try: trd_env = Trend(hlp_env) trd_env.get_trend() except TrendError as e: print(e) return note = [[1, hlp_env.temp_h, hlp_env.temp_min], [0, hlp_env.temp_l, hlp_env.temp_max]][hlp_env.hl == "l"] locale.setlocale(locale.LC_CTYPE, "chinese") # 设置画图环境参数 graph_obj = BSgraph(hlist=hlp_env.hpi, llist=hlp_env.lpi, data=hlp_env.klist, codename=codename, note=note) strategy_info = graph_obj.strategy_info( max(hlp_env.hpi[-1], hlp_env.lpi[-1]), hlp_env.space_h, hlp_env.space_l) print(f"\t{codename}当前趋势{trd_env.trdnow}") stock_info = [] for hh in hlp_env.hpi: stock_info.append((hlp_env.klist[hh].t, "H", hlp_env.klist[hh].high, hlp_env.klist[hlp_env.klist[hh].hl_confirmed].t)) for ll in hlp_env.lpi: stock_info.append((hlp_env.klist[ll].t, "L", hlp_env.klist[ll].low, hlp_env.klist[hlp_env.klist[ll].hl_confirmed].t)) stock_info.sort() # 该股票在总表中的信息 if paint: # 绘图并获取图像地址 gdir = graph_obj.performance( trdchg=trd_env.trdchg, dir=dir, star=star, ) return strategy_info, stock_info, gdir else: return strategy_info, stock_info
def filtration(self): if self.radioButton_image.isChecked(): filter_trend = Trend() type_of_filter = self.comboBox_3.currentText() m = self.lineEdit_2.text() delta_t = self.lineEdit.text() fc_1 = self.lineEdit_3.text() fc_2 = self.lineEdit_4.text() error_dialog = QtWidgets.QErrorMessage() if m != '': try: m = int(m) filter_trend.m = m except: error_dialog.showMessage( 'Значение m должны быть целочисленными. Использовано значение m по ' 'умолчанию') if delta_t != '': try: delta_t = float(delta_t) filter_trend.delta_t = delta_t except: error_dialog.showMessage( 'Значение delta t должны быть вещественными. Использовано значение delta t по ' 'умолчанию') if fc_1 != '': try: fc_1 = int(fc_1) filter_trend.fc_1 = fc_1 except: error_dialog.showMessage( 'Значение fc_1 должны быть целочисленными. Использовано значение fc_1 по ' 'умолчанию') if fc_2 != '': try: fc_2 = int(fc_2) filter_trend.fc_2 = fc_2 except: error_dialog.showMessage( 'Значение fc_2 должны быть целочисленными. Использовано значение fc_2 по ' 'умолчанию') if type_of_filter == 'Низких частот': filter_trend.generation_trend_filter_potter() elif type_of_filter == 'Высоких частот': filter_trend.generating_trend_high_potter() elif type_of_filter == 'Полосовой': filter_trend.generating_trend_bandpass_filter() elif type_of_filter == 'Режекторный': filter_trend.generating_trend_notch_filter() place_to_show_image: int = int(self.comboBox_2.currentText()) try: position_img: int = int(self.comboBox.currentText()) img_path: str = POSITION_FOR_ANALYSIS.get(position_img) except: error_dialog.showMessage( 'Не найдено изображение для фильтрации') return filtration(img_path, filter_trend, place_to_show_image) else: type_of_filter_with_mask: str = self.comboBox_4.currentText() place_to_show_image: int = int(self.comboBox_2.currentText()) error_dialog: object = QtWidgets.QErrorMessage() try: n_of_maska: int = int(self.lineEdit_5.text()) if n_of_maska % 2 == 0: n_of_maska = n_of_maska + 1 except: n_of_maska: int = 3 try: position_img: int = int(self.comboBox.currentText()) img_path: str = POSITION_FOR_ANALYSIS.get(position_img) except: error_dialog.showMessage( 'Не найдено изображение для фильтрации') return if type_of_filter_with_mask == 'Арифметический': arithmetic_filter_img(img_path, place_to_show_image, n_of_maska) elif type_of_filter_with_mask == 'Медианный': median_img(img_path, place_to_show_image, n_of_maska) self.main_window.show_img(place_to_show_image) self.close_window()
def multi(trend_1, trend_2): trend = Trend() trend.y = trend_1.y * trend_2.y trend.x = trend_1.x return trend
import operator import math import getopt def take(n, iterable): "Return first n items of the iterable as a list" return list(islice(iterable, n)) db = MongoClient().test_database n = 0.0 QUERY = "diabetes" first = Trend() first.setName(QUERY) for tweet in db.tweets.find({"entities.hashtags.text": re.compile(QUERY, re.IGNORECASE)}).sort( [("_id", pymongo.DESCENDING)] ): trend_in_tweet = False for hashtag in tweet["entities"]["hashtags"]: if hashtag["text"].encode("utf-8").lower() != QUERY.lower(): first.addToRelated(hashtag["text"].encode("utf-8").lower(), 1) n = n + 1 print QUERY.lower(), ": ", str(n) top_10 = sorted(first.getRelated().items(), key=operator.itemgetter(1), reverse=True) for top in top_10: print "\t", top[0], ": ", float(top[1]) / n
import operator import math import getopt def take(n, iterable): "Return first n items of the iterable as a list" return list(islice(iterable, n)) db = MongoClient().test_database n = 0.0 QUERY = 'ROOT' first = Trend() first.setName(QUERY) for tweet in db.tweets.find(): for hashtag in tweet["entities"]["hashtags"]: if hashtag['text'].encode("utf-8").lower() != QUERY.lower(): first.addToRelated(hashtag['text'].encode("utf-8").lower(), 1) n = n + 1 print QUERY.lower(), ": ", str(n) top_10 = sorted(first.getRelated().items(), key=operator.itemgetter(1), reverse=True) top_10 = take(20, top_10) for top in top_10: print "\t", top[0], ": ", float(top[1]) / n
def filtration(self, model_for_filtration, choice_filter): trend_filter = Trend() # Тренд фильтра # Фильтр низких частот if choice_filter == 1: trend_filter.generation_trend_filter_potter() # Фильтр высоких частот if choice_filter == 2: trend_filter.generating_trend_high_potter() # Фильтр полосовой if choice_filter == 3: trend_filter.generating_trend_bandpass_filter() # Фильтр режекторный if choice_filter == 4: trend_filter.generating_trend_notch_filter() # Тренд существущей модели trend_model = Trend() trend_model.x = model_for_filtration.x trend_model.y = model_for_filtration.y trend_model.n = model_for_filtration.n trend = convolution(trend_model, trend_filter) self.x = trend.x self.y = trend.y
# -*- coding: utf-8 -*- #Alcamena 10 tendencias en la base de datos.# from twython import Twython, TwythonError from trend import Trend from mongoengine import connect connect('twitter') twitter = Twython("iDGDpOTDVj7mwGDD4pkAMw", "rKOrMHCvsP98lISbyXnHPaR7HFWj44PsAa4Yy7cCQvE", "222814276-KsT3JiNkqUCALLKrfFegVenxi229thKKxjkM2Zpr", "soCtSgfJyzuAEhXvN466LZao7Xc5RcenjFbTmd1TA") try: search_results = twitter.get_place_trends(id = 23424787) for trend in search_results[0].get('trends', []): t = Trend(trend) t.save() except TwythonError as e: print e
def calculation(self): # y(x)=kx+b if self.option == 1: trend = Trend() trend.generating_trend_line() self.y = trend.y # y(x)=-kx+b if self.option == 2: trend = Trend() trend.generating_trend_line() trend.y = np.flip(trend.y) self.y = trend.y # y(x) = beta * exp^(alpha * i) if self.option == 3: trend = Trend() trend.generating_exhibitor() self.y = trend.y # y(x) = beta * exp^(alpha * -i) if self.option == 4: trend = Trend() trend.generating_exhibitor() trend.y = np.flip(trend.y) self.y = trend.y # Встроенный рандом if self.option == 5: trend = Trend() trend.generating_trend_random(self.s_min, self.s_max) self.y = trend.y # Кастомный рандом if self.option == 6: trend = Trend() trend.generating_custom_random() self.y = trend.y # Рандом + сдвиг if self.option == 7: trend1 = Trend() trend1.generating_trend_random(self.s_min, self.s_max) trend = self.shift(trend1) self.y = trend.y # Указали, что не требуется нормализация self.flag_normalisation = 0 # Значения за областью if self.option == 8: trend1 = Trend() trend1.generating_random_spikes(self.s_min, self.s_max) self.y = trend1.y # Указали, что не требуется нормализация self.flag_normalisation = 0 # Адитивная модель №1 if self.option == 9: trend1 = Trend() trend1.generating_trend_line() trend1.y = np.flip(trend1.y) trend2 = Trend() trend2.generating_trend_random(self.s_min, self.s_max) trend = sum_trend(trend1, trend2) self.y = trend.y # Адитивная модель №2 if self.option == 10: trend1 = Trend() trend1.generating_trend_line() trend2 = Trend() trend2.generating_trend_random(self.s_min, self.s_max) trend = sum_trend(trend1, trend2) self.y = trend.y # Мультипликативная модель №1 if self.option == 11: trend1 = Trend() trend1.generating_trend_line() trend1.y = np.flip(trend1.y) trend2 = Trend() trend2.generating_trend_random(self.s_min, self.s_max) trend = multi(trend1, trend2) self.y = trend.y # Мультипликативная модель №2 if self.option == 12: trend1 = Trend() trend1.generating_trend_line() trend2 = Trend() trend2.generating_trend_random(self.s_min, self.s_max) trend = multi(trend1, trend2) self.y = trend.y # График кусочной функции if self.option == 13: trend = Trend() trend.generating_piecewise_function(self.s_min, self.s_max) self.y = trend.y # График гармонический процесс if self.option == 17: trend = Trend() trend.generating_harmonic_process() if self.c != 0: self.y = self.y + self.c else: self.y = trend.y self.flag_normalisation = 0 self.normalisation_axis() # График полигармонического процесса # x(t) = x1(t) + x2(t) = x3(t) # xi(t) = Ai * sin(2piFit) # A1 = 25 f1 = 11 # A2 = 35 f2 = 41 # A3 = 30 f3 = 141 if self.option == 19: trend1 = Trend() trend2 = Trend() trend3 = Trend() trend1.a_0 = 25 trend1.f_0 = 11 trend1.generating_harmonic_process() trend2.a_0 = 35 trend2.f_0 = 41 trend2.generating_harmonic_process() trend3.a_0 = 30 trend3.f_0 = 141 trend3.generating_harmonic_process() trend4 = sum_trend(trend1, trend2) trend = sum_trend(trend4, trend3) self.y = trend.y # График Рандом + спайки if self.option == 20: trend_1 = Trend() trend_1.generating_trend_random(self.s_min, self.s_max) trend_2 = Trend() trend_2.generating_random_spikes(self.s_min, self.s_max) trend = sum_trend(trend_1, trend_2) self.y = trend.y self.flag_normalisation = 0 self.axis_max = np.amax(self.y) * 1.2 self.axis_min = np.amin(self.y) * 1.2 # График Гармонический процесс + trend if self.option == 25: trend_1 = Trend() trend_2 = Trend() trend_1.generating_harmonic_process() trend_2.generating_trend_line() trend = sum_trend(trend_1, trend_2) self.y = trend.y # График Гармонический процесс + спайки if self.option == 26: trend_1 = Trend() trend_2 = Trend() trend_1.generating_harmonic_process() trend_2.generating_random_spikes(self.s_min, self.s_max) trend = sum_trend(trend_1, trend_2) self.y = trend.y self.flag_normalisation = 0 self.normalisation_axis() # График ГП(гармонический процесс) + спайки + рандом + trend if self.option == 27: trend_1 = Trend() trend_2 = Trend() trend_3 = Trend() trend_4 = Trend() trend_1.generating_harmonic_process() trend_2.generating_random_spikes(self.s_min, self.s_max) trend_3.generating_trend_random(self.s_min, self.s_max) trend_4.generating_trend_line() trend = (trend_1, trend_2) trend = (trend, trend_3) trend = (trend, trend_4) self.y = trend.y self.flag_normalisation = 0 self.normalisation_axis() # График из файла if self.option == 28: trend = Trend() trend.generating_trend_from_file() self.y = trend.y self.flag_normalisation = 0 self.normalisation_axis() # График ГП + экспонента (кардиограма) if self.option == 29: self.n = 200 self.x = np.arange(0, self.n) # self.delta_t = 0.005 # self.display_n = self.n self.s_max = 1 trend_1 = Trend() trend_1.n = self.n trend_1.x = self.x trend_1.delta_t = 0.005 trend_1.generating_harmonic_process() trend_2 = Trend() trend_2.n = self.n trend_2.x = self.x trend_2.generating_exhibitor() # Получили тренд сердцебиения trend = multi(trend_1, trend_2) # self.y = trend.y # self.normalization() # trend.y = self.y self.n = 1000 trend_3 = Trend() trend_3.generating_spikes(100) trend = convolution(trend_3, trend) self.y = trend.y self.x = trend.x self.s_max = 100 self.s_min = -self.s_max # Реализация фильтров # Низких частот if self.option == 30: trend = Trend() trend.generation_trend_filter_potter() self.y = trend.y self.x = trend.x self.n = trend.n self.display_n = trend.display_n self.dt = trend.dt # Фильтр высоких частот if self.option == 31: trend = Trend() trend.generating_trend_high_potter() self.y = trend.y self.x = trend.x self.n = trend.n self.display_n = trend.display_n self.dt = trend.dt # Полосовой фильтр if self.option == 32: trend = Trend() trend.generating_trend_bandpass_filter() self.y = trend.y self.x = trend.x self.n = trend.n self.display_n = trend.display_n self.dt = trend.dt # Режекторный фильтр if self.option == 33: trend = Trend() trend.generating_trend_notch_filter() self.y = trend.y self.x = trend.x self.n = trend.n self.display_n = trend.display_n self.dt = trend.dt # Звук ma.wav if self.option == 34: self.name_of_wav_file = "../input files/custom.wav" sound_trend = Sound(self.name_of_wav_file) self.x = sound_trend.x self.y = sound_trend.y * self.c self.rate = sound_trend.rate self.n = len(self.x) self.flag_checking_display_n = 1 # Звук my_voice.wav if self.option == 35: self.name_of_wav_file = "../input files/my_voice.wav" sound_trend = Sound(self.name_of_wav_file) self.x = sound_trend.x self.y = sound_trend.y * self.c self.rate = sound_trend.rate self.n = len(self.x) self.flag_checking_display_n = 1 # Экзамен if self.option == 36: trend = Trend() trend.generating_trend_from_file_exam() self.y = trend.y self.flag_normalisation = 0 self.normalisation_axis() # Модель Input: кардиограма, заполненная нулями с 200 до 1000 if self.option == 37: self.n = 200 self.x = np.arange(0, self.n) self.s_max = 1 trend_1 = Trend() trend_1.n = self.n trend_1.x = self.x trend_1.delta_t = 0.005 trend_1.generating_harmonic_process() trend_2_n = 800 trend_2_y = np.zeros(trend_2_n) self.n = 1000 self.x = np.arange(0, self.n) self.y = np.concatenate([trend_1.y, trend_2_y]) print(self.y) self.s_max = 100 self.s_min = -self.s_max
def sum_trend(trend_1, trend_2): trend = Trend() trend.y = trend_1.y + trend_2.y trend.x = trend_1.x return trend
def daily2(self): if not w.isconnected(): w.start() conn = sqlite3.connect(DATABASE, isolation_level=None, timeout=3) cur = conn.cursor() cur.execute('''select code from Strategy_s''') # 获取数据库中维护的股票代码 codes = (code[0] for code in cur.fetchall()) # 仅保留美股代码 codes = tuple(filter(lambda x: not x[0].isdigit() and x != hsi, codes)) # 获取今日日期时间戳,需要用美国东部时区的时间 today = date.today() # 如果今天是北京时间周一 if not today.weekday(): # 换算成美国东部时间的上周五,减去三天 stamp = today - timedelta(days=3) else: # 换算成美国时间的上个交易日,减去一天 stamp = today - timedelta(days=1) stamp = stamp.strftime("%Y-%m-%d") # 格式化今日时间戳 locale.setlocale(locale.LC_CTYPE, "chinese") stampf = "{}年{}月{}日 tz=US/Eastern".format(*stamp.split("-")) self.chgList = [] high_p = w.wsd(",".join(codes), "high", stamp, stamp).Data[0] low_p = w.wsd(",".join(codes), "low", stamp, stamp).Data[0] # high_p = w.wsd(",".join(codes), "high", '2018-11-27', '2018-11-27').Data[0] # low_p = w.wsd(",".join(codes), "low", "2018-11-27", "2018-11-27").Data[0] for dd in codes: # 提取总表中信息 # 取出今天数据 dd_i = codes.index(dd) high = high_p[dd_i] low = low_p[dd_i] if not high or not low or high == nan or low == nan: continue print("正在更新:" + dd) cur.execute(f'''select * from Strategy_s where code="{dd}"''') dd_dict = dict(zip(Col_stg, cur.fetchone())) stock_tb = code_helper(dd) trd = dd_dict["trend"] hl = dd_dict["next_hl"] temp_hl = dd_dict["temp_hl"] temp_m = dd_dict['temp_m'] from_hl = dd_dict["from_hl"] from_temp = dd_dict["from_temp"] use_space = dd_dict["use_space"] space_h = dd_dict["space_h"] space_l = dd_dict["space_l"] # 取出高点数据 cur.execute( f'''select price from "{stock_tb}" where hl='H' order by date''' ) highs = [h[0] for h in cur.fetchall()] pre_high = highs[-1] pre2_high = highs[-2] # 取出低点数据 cur.execute( f'''select price from "{stock_tb}" where hl='L' order by date''' ) lows = [l[0] for l in cur.fetchall()] pre_low = lows[-1] pre2_low = lows[-2] cur.execute(f'''select hl from "{stock_tb}" order by date''') first_hl = cur.fetchone()[0] if first_hl == "H": n1 = min(len(highs) - 1, len(lows)) n2 = min(len(highs), len(lows)) l2h = [(lows[i], highs[i + 1]) for i in range(n1)] h2l = [(highs[i], lows[i]) for i in range(n2)] else: n1 = min(len(highs), len(lows)) n2 = min(len(highs), len(lows) - 1) l2h = [(lows[i], highs[i]) for i in range(n1)] h2l = [(highs[i], lows[i + 1]) for i in range(n2)] # 新增数据对策略影响 hl_res = HLPoint.step_hl_s(hl, high, low, temp_hl, temp_m, from_hl, from_temp, pre_high, pre_low, use_space, space_h, space_l, l2h, h2l) if hl_res["is_high"]: t = w.tdaysoffset(-(from_temp + 1), stamp).Times[0].strftime("%Y-%m-%d") cur.execute(f"insert into '{stock_tb}' values (?,?,?,?)", (t, "H", temp_hl, stamp)) pre2_high = pre_high pre_high = hl_res["pre_high"] elif hl_res["is_low"]: t = w.tdaysoffset(-(from_temp + 1), stamp).Times[0].strftime("%Y-%m-%d") cur.execute(f"insert into '{stock_tb}' values (?,?,?,?)", (t, "L", temp_hl, stamp)) pre2_low = pre_low pre_low = hl_res["pre_low"] trdmax_res = Trend.step_trdmax_s(hl_res["hl"], low, high, hl_res["from_temp"], hl_res["from_hl"], hl_res["temp_hl"], trd) if trdmax_res[1]: self.chgList.append((dd, trdmax_res[0], trd)) trd = trdmax_res[0] else: trd_res = Trend.step_trd_s(trd, hl_res["hl"], low, high, pre_low, pre_high, pre2_low, pre2_high) if trd_res != trd: self.chgList.append((dd, trd_res, trd)) trd = trd_res try: cur.execute( f'''update Strategy_s set date="{stamp}", trend="{trd}", next_hl={hl_res["hl"]}, temp_hl={hl_res["temp_hl"]}, temp_m={hl_res["temp_m"]}, from_hl={hl_res["from_hl"]}, from_temp={hl_res[ "from_temp"]}, use_space={hl_res["use_space"]}, space_h={hl_res[ "space_h"]}, space_l={hl_res["space_l"]} where code="{dd}"''' ) except sqlite3.OperationalError as e: print(e) print( f'''Sqlite error: {dd} new info: trend:{trd}, next_hl:{hl_res['hl']}, temp_hl:{hl_res['temp_hl']}, temp_m:{hl_res['temp_m']}, from_hl:{hl_res[ 'from_hl']}, from_temp:{hl_res['from_temp']}, use_space:{hl_res[ 'use_space']}, space_h={hl_res['space_h']}, space_l={hl_res['space_l']}''' ) cur.close() conn.close() if len(codes) <= 1: pass else: res = self.sent() if res: mail_obj = SendEmail() mail_obj.buildHTML(res["chg"], res["up"], res["down"], res["consd"], res["img"], stampf) mail_obj.imageHTML(res["chg_img"]) subject = ["趋势策略每日报告", "趋势策略变动报告-美股"][FREQ] mail_obj.setSend(subject, SENDER, RECEIVER, SENDER_KEY) print(stampf + f"邮件是否成功发送?{mail_obj.isSent}")
def daily1(self): if not w.isconnected(): w.start() conn = sqlite3.connect(DATABASE, isolation_level=None, timeout=3) cur = conn.cursor() cur.execute('''select code from Strategy_s''') # 获取数据库中维护的股票代码 codes = (code[0] for code in cur.fetchall()) # 仅保留港股和A股代码 codes = tuple(filter(lambda x: x[0].isdigit() or x == hsi, codes)) # 获取今日日期时间戳 stamp = str(date.today()) # stamp = '2018-10-25' # 格式化今日时间戳 locale.setlocale(locale.LC_CTYPE, "chinese") stampf = date.today().strftime("%Y年%m月%d日") # stampf = '2018年10月19日' self.chgList = [] # 提取全部样本股的价格数据 high_p = w.wsd(",".join(codes), "high", stamp, stamp).Data[0] low_p = w.wsd(",".join(codes), "low", stamp, stamp).Data[0] trade_status = w.wsd(",".join(codes), "trade_status", stamp, stamp).Data[0] # high_p = w.wsd(",".join(codes), "high", '2018-11-23', '2018-11-23').Data[0] # low_p = w.wsd(",".join(codes), "low", '2018-11-23', '2018-11-23').Data[0] # trade_status = w.wsd(",".join(codes), "trade_status", '2018-11-23', '2018-11-23').Data[0] for dd in codes: # 提取总表中信息 # 取出今天数据 print("正在更新:" + dd) dd_i = codes.index(dd) high = high_p[dd_i] low = low_p[dd_i] status = trade_status[dd_i] # 提取总表中信息 if dd not in [sh, hsi] and status == "停牌一天": continue if not high or not low or high == nan or low == nan: continue cur.execute(f'''select * from Strategy_s where code="{dd}"''') dd_dict = dict(zip(Col_stg, cur.fetchone())) stock_tb = code_helper(dd) # dat = dd_dict["date"] # if dat == stamp: # continue trd = dd_dict["trend"] hl = dd_dict["next_hl"] temp_hl = dd_dict["temp_hl"] temp_m = dd_dict["temp_m"] from_hl = dd_dict["from_hl"] from_temp = dd_dict["from_temp"] use_space = dd_dict["use_space"] space_h = dd_dict["space_h"] space_l = dd_dict["space_l"] # 取出高点数据 cur.execute( f'''select price from "{stock_tb}" where hl='H' order by date''' ) #print(cur.fetchall()) highs = [h[0] for h in cur.fetchall()] pre_high = highs[-1] pre2_high = highs[-2] # 取出低点数据 cur.execute( f'''select price from "{stock_tb}" where hl='L' order by date''' ) lows = [l[0] for l in cur.fetchall()] pre_low = lows[-1] pre2_low = lows[-2] cur.execute(f'''select hl from "{stock_tb}" order by date''') first_hl = cur.fetchone()[0] if first_hl == "H": n1 = min(len(highs) - 1, len(lows)) n2 = min(len(highs), len(lows)) l2h = [(lows[i], highs[i + 1]) for i in range(n1)] h2l = [(highs[i], lows[i]) for i in range(n2)] else: n1 = min(len(highs), len(lows)) n2 = min(len(highs), len(lows) - 1) l2h = [(lows[i], highs[i]) for i in range(n1)] h2l = [(highs[i], lows[i + 1]) for i in range(n2)] # 新增数据对策略影响 hl_res = HLPoint.step_hl_s(hl, high, low, temp_hl, temp_m, from_hl, from_temp, pre_high, pre_low, use_space, space_h, space_l, l2h, h2l) if hl_res["is_high"]: t = w.tdaysoffset(-(from_temp + 1), stamp).Times[0].strftime("%Y-%m-%d") cur.execute(f"insert into '{stock_tb}' values (?,?,?,?)", (t, "H", temp_hl, stamp)) pre2_high = pre_high pre_high = hl_res["pre_high"] elif hl_res["is_low"]: t = w.tdaysoffset(-(from_temp + 1), stamp).Times[0].strftime("%Y-%m-%d") cur.execute(f"insert into '{stock_tb}' values (?,?,?,?)", (t, "L", temp_hl, stamp)) pre2_low = pre_low pre_low = hl_res["pre_low"] trdmax_res = Trend.step_trdmax_s(hl_res["hl"], low, high, hl_res["from_temp"], hl_res["from_hl"], hl_res["temp_hl"], trd) if trdmax_res[1]: self.chgList.append((dd, trdmax_res[0], trd)) trd = trdmax_res[0] else: trd_res = Trend.step_trd_s(trd, hl_res["hl"], low, high, pre_low, pre_high, pre2_low, pre2_high) if trd_res != trd: self.chgList.append((dd, trd_res, trd)) trd = trd_res try: cur.execute( f'''update Strategy_s set date="{stamp}", trend="{trd}", next_hl={hl_res["hl"]}, temp_hl={hl_res["temp_hl"]}, temp_m={hl_res["temp_m"]}, from_hl={hl_res["from_hl"]}, from_temp={hl_res[ "from_temp"]}, use_space={hl_res["use_space"]}, space_h={hl_res[ "space_h"]}, space_l={hl_res["space_l"]} where code="{dd}"''' ) except sqlite3.OperationalError as e: print(e) print( f'''Sqlite error: {dd} new info: trend:{trd}, next_hl:{hl_res['hl']}, temp_hl:{hl_res['temp_hl']}, temp_m:{hl_res['temp_m']}, from_hl:{hl_res[ 'from_hl']}, from_temp:{hl_res['from_temp']}, use_space:{hl_res[ 'use_space']}, space_h={hl_res['space_h']}, space_l={hl_res['space_l']}''' ) cur.close() conn.close() if len(codes) > 2: res = self.sent() if res: mail_obj = SendEmail() mail_obj.buildHTML(res["chg"], res["up"], res["down"], res["consd"], res["img"], stampf) mail_obj.imageHTML(res["chg_img"]) subject = ["趋势策略每日报告", "趋势策略变动报告-A股、港股"][FREQ] mail_obj.setSend(subject, SENDER, RECEIVER, SENDER_KEY) print(stampf + f"邮件是否成功发送?{mail_obj.isSent}") self.daily2()
def judge(year, month, day, _year, _month, _day, cycle): string = '' json_result = [] # 打开文件,获取excel文件的workbook(工作簿)对象 workbook = pd.DataFrame(pd.read_excel("data/testData.xlsx")) workbook.columns = ['datetime', 'Open', 'High', 'Low', 'Close'] time1 = pd.Timestamp(year, month, day) time2 = pd.Timestamp(_year, _month, _day) workbook = workbook.loc[workbook['datetime'] >= time1] workbook = workbook.loc[workbook['datetime'] <= time2] if workbook.shape[0] < 2: string = "输入的时间段内数据少于两条,无法判断形态!" return string flag = True # 获取两天开收盘价的高低情况以判断吞没 for i in range(1, workbook.shape[0]): if workbook.iloc[i - 1][1] > workbook.iloc[i - 1][4]: high0 = workbook.iloc[i - 1][1] low0 = workbook.iloc[i - 1][4] else: high0 = workbook.iloc[i - 1][4] low0 = workbook.iloc[i - 1][1] if workbook.iloc[i][1] > workbook.iloc[i][4]: high1 = workbook.iloc[i][1] low1 = workbook.iloc[i][4] else: high1 = workbook.iloc[i][4] low1 = workbook.iloc[i][1] # 如果呈现吞没形态 if high1 > high0 and low1 < low0: # 判断趋势 date = workbook.iloc[i - 1][0] trend = Trend.judgeTrend(date.year, date.month, date.day, cycle) date = workbook.iloc[i][0] if trend == 1: json_result.append( str(date.year) + "/" + str(date.month) + "/" + str(date.day) + " 看跌吞没") string = string + (str(date.year) + "/" + str(date.month) + "/" + str(date.day) + " 看跌吞没\n") flag = False elif trend == 2: json_result.append( str(date.year) + "/" + str(date.month) + "/" + str(date.day) + " 看涨吞没") string = string + (str(date.year) + "/" + str(date.month) + "/" + str(date.day) + " 看涨吞没\n") flag = False # 乌云盖顶 if workbook.iloc[i][1] > workbook.iloc[ i - 1][2] and workbook.iloc[i][4] < ( workbook.iloc[i - 1][1] + workbook.iloc[i - 1][4]) / 2: # 判断趋势 date = workbook.iloc[i - 1][0] if Trend.judgeTrend(date.year, date.month, date.day, cycle) == 1: date = workbook.iloc[i][0] json_result.append( str(date.year) + "/" + str(date.month) + "/" + str(date.day) + " 乌云盖顶") string = string + (str(date.year) + "/" + str(date.month) + "/" + str(date.day) + " 乌云盖顶\n") flag = False if flag: temp = "该段时间内无形态" json_result.append(temp) string = string + "该段时间内无形态\n" while string.count("\n") > 5: string = string[string.find("\n") + 1:] n = string.find("\n") while n >= 0: string = string[:n] + " " + string[n + 1:] n = string.find("\n", n + 1) if n >= 0: string = string[:n] + " " + string[n + 1:] n = string.find("\n", n + 1) if n >= 0: n = string.find("\n", n + 1) return string, json_result
def runbacktest(begin, codename, dir, end, paint=True, star=False): """ 设置策略环境参数,并运行部分回测策略 :param begin: 回测开始时间 :param codename: 回测股票代码或名称 :param dir: 作图目录地址 :param end: 回测结束时间,默认当前 :param paint: bool值,是否画图,默认为True :param star: bool值,最后一个标记星星,默认为False :return: 返回策略环境对象整体和图像地址 """ print(f"BS日线策略:正在回测{codename}...") try: klist = ldd(begin, codename, end) except loaddataError as e: print(f"\t{e}") raise loaddataError print(f"\t{codename}获取日K线数{len(klist)}") # 由于klist数据集是被hlp_env, trd_env和trade_env共享的,指向同一地址 # 因此任意指针均可调用 # 设置策略运行参数环境 hlp_env = HLPoint(klist, codename) hlp_env.init_hl() hlp_env.get_hl() try: trd_env = Trend(hlp_env) trd_env.get_trend() except TrendError as e: print(e) return try: trade_env = Trade(code=codename, klist=hlp_env.klist, hpi=hlp_env.hpi, lpi=hlp_env.lpi) trade_env.get_bs() except Exception as e: print(e) return for k in trade_env.klist: k.t += " 15:00:00" # 统一日线时间与分钟级别时间的表示方法 "yyyy-mm-dd HH:MM:SS" # note = [[1, hlp_env.temp_h, hlp_env.temp_min], # [0, hlp_env.temp_l, hlp_env.temp_max]][hlp_env.hl == "l"] locale.setlocale(locale.LC_CTYPE, "chinese") # 创建回测表现统计操作对象 stat = Stat(hlp_env=hlp_env, trd_env=trd_env, trade_env=trade_env) stat.get_mkt_p() hlp_tb = stat.get_hl_info() bsp_tb = stat.get_bs_info() account_row = stat.get_account_info() strategy_row = stat.get_strategy_info() # 打包回测结果总结 # res_pac = {"hlp": hlp_tb, "bsp": bsp_tb, "account": account_row, "strategy": strategy_row} stat.get_performance() # 创建图像操作对象 graph_obj = BSgraph(trade_env=trade_env, stat=stat) # 向回测全局对象返回结果总结 wrapper_df = graph_obj.wrapper_to_global_backtest() # 该股票在总表中的信息 if paint: # 绘图并获取图像地址 gdir = graph_obj.performance( trdchg=trd_env.trdchg, dir=dir, star=star, ) print(f"回测图像输出到{gdir}") # return res_pac, gdir return wrapper_df else: # return res_pac return wrapper_df
def get_trends_in_place(now, woeid, history=False): ''' Get trends in place, current date and place (woeid) https://developer.twitter.com/en/docs/trends/trends-for-location/api-reference/get-trends-place ''' queryResult = Location.query(Location.woeid == int(woeid)).fetch(1) location = (queryResult[0] if len(queryResult) == 1 else None) # check for history trend_history = None if location is not None: queryResult = TrendHistory.query( ndb.AND(TrendHistory.date == now, TrendHistory.location == location)).fetch(1) trend_history = (queryResult[0] if len(queryResult) == 1 else None) if trend_history is not None: result = [] result.append(trend_history.to_dict()) return result # Only search at history if (history): return "" # If data is not in cache do request if not twitter_api.isAuthenticated(): twitter_api.auth() result = json.loads(twitter_api.get_trends_place(woeid)) if (isinstance(result, list)): # persist result trends = [] for twitter_trend in result[0]['trends']: queryResult = Trend.query( Trend.name == twitter_trend['name']).fetch(1) trend = (queryResult[0] if len(queryResult) == 1 else Trend()) # FIXME(ruben): check keys before assignment trend.name = twitter_trend['name'] trend.promoted_content = twitter_trend['promoted_content'] trend.query_string = twitter_trend['query'] trend.url = twitter_trend['url'] trend.tweet_volume = twitter_trend['tweet_volume'] # increases search count, if trend already exists trend.search_count = (trend.search_count + 1 if len(queryResult) == 1 else 1) trends.append(trend) if len(trends) > 0: ndb.put_multi(trends) if location is None: location = Location() location.name = result[0]['locations'][0]['name'] location.woeid = result[0]['locations'][0]['woeid'] location.put() # create trend history trend_history = TrendHistory() trend_history.date = now trend_history.trends = trends trend_history.location = location trend_history.put() return result else: print("Error:", result) return ""