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
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 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 _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 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 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()
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 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 ""
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 multi(trend_1, trend_2): trend = Trend() trend.y = trend_1.y * trend_2.y trend.x = trend_1.x return trend
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