def __init__(self, instrument, base_path, config_name, base_time): super(TrendReverseAlgo, self).__init__(instrument, base_path, config_name, base_time) self.base_price = 0 self.setPrice(base_time) self.debug_logger = getLogger("debug") self.result_logger = getLogger("result") self.mysql_connector = MysqlConnector() self.first_flag = self.config_data["first_trail_mode"] self.second_flag = self.config_data["second_trail_mode"] self.most_high_price = 0 self.most_low_price = 0 self.mode = "" self.buy_count = 0 self.buy_count_price = 0 self.sell_count = 0 self.sell_count_price = 0 self.original_stoploss_rate = 0 self.week_start_price = 0 self.count_threshold = 1 self.stoploss_flag = False self.algorithm = "" self.log_max_price = 0 self.log_min_price = 0 self.first_trade_flag = "pass" self.first_trade_time = base_time self.stl_first_flag = False self.buy_flag = False self.sell_flag = False self.stl_logic = "none" self.perfect_order_buycount = 0 self.perfect_order_sellcount = 0 self.setReverseIndicator(base_time)
def __init__(self, instrument, base_path, config_name, base_time): super(MultiAlgo, self).__init__(instrument, base_path, config_name, base_time) self.base_price = 0 self.setPrice(base_time) self.debug_logger = getLogger("debug") self.result_logger = getLogger("result") self.mysql_connector = MysqlConnector() self.first_flag = self.config_data["first_trail_mode"] self.second_flag = self.config_data["second_trail_mode"] self.most_high_price = 0 self.most_low_price = 0 self.mode = "" self.buy_count = 0 self.buy_count_price = 0 self.sell_count = 0 self.sell_count_price = 0 self.original_stoploss_rate = 0 self.week_start_price = 0 self.count_threshold = 1 self.stoploss_flag = False self.algorithm = "" self.log_max_price = 0 self.log_min_price = 0 self.first_flag = "pass" self.second_flag = "pass" self.first_flag_time = None self.second_flag_time = None self.setExpantionIndicator(base_time) #self.setVolatilityIndicator(base_time) self.setDailyIndicator(base_time)
def __init__(self, instrument, base_path, config_name, base_time): super(Scalping, self).__init__(instrument, base_path, config_name, base_time) self.setPrice(base_time) self.debug_logger = getLogger("debug") self.result_logger = getLogger("result") self.mysql_connector = MysqlConnector() self.first_flag = self.config_data["first_trail_mode"] self.second_flag = self.config_data["second_trail_mode"] self.most_high_price = 0 self.most_low_price = 0 self.mode = "" self.algorithm = "" self.log_max_price = 0 self.log_min_price = 0 self.stl_logic = "none" self.output_max_price = 0 self.output_min_price = 0 self.first_trade_flag = "" self.first_trade_time = None self.log_object = {} self.current_path = os.path.abspath(os.path.dirname(__file__)) self.model_1h = self.load_model(model_filename="multi_model_1h.json", weights_filename="multi_model_1h.hdf5") self.model_5m = self.load_model(model_filename="multi_model_5m.json", weights_filename="multi_model_5m.hdf5") self.model_1m = self.load_model(model_filename="multi_model_1m.json", weights_filename="multi_model_1m.hdf5")
def __init__(self, instrument, base_path, config_name, base_time): super(DaytimeAlgo, self).__init__(instrument, base_path, config_name, base_time) self.base_price = 0 self.setPrice(base_time) self.debug_logger = getLogger("debug") self.result_logger = getLogger("result") self.mysql_connector = MysqlConnector() self.first_flag = self.config_data["first_trail_mode"] self.second_flag = self.config_data["second_trail_mode"] self.most_high_price = 0 self.most_low_price = 0 self.mode = "" self.buy_count = 0 self.buy_count_price = 0 self.sell_count = 0 self.sell_count_price = 0 self.original_stoploss_rate = 0 self.stoploss_flag = False self.algorithm = "" self.high_price = 0 self.low_price = 500 self.entry_buy_count = 0 self.entry_sell_count = 0 self.setDaytimeIndicator(base_time) self.update_price_flag = False
def __init__(self, instrument, time_width, base_time): self.instrument = instrument self.time_width = time_width self.indicator_object = IndicatorObject() self.old_base_time = base_time self.base_time = base_time self.mysql_connector = MysqlConnector() self.setPrice(base_time)
def __init__(self, instrument, base_path, config_name, indicator_object, base_time): super(ComputePriceThread, self).__init__() self.instrument = instrument self.config_data = instrument_init(instrument, base_path, config_name) self.indicator_object = indicator_object self.old_base_time = base_time self.base_time = base_time self.mysql_connector = MysqlConnector() self.setPrice(base_time) self.setIndicator(base_time)
def __init__(self, base_time): super(TestThread, self).__init__() self.config_data = instrument_init(self.instrument, self.base_path) self.base_time = base_time self.ask_price_list = [] self.bid_price_list = [] self.insert_time_list = [] self.mysqlConnector = MysqlConnector() sql = self.getInitialSql(self.base_time) response = self.mysqlConnector.select_sql(sql) self.setResponse(response)
def __init__(self, instruments, base_time, time_width): super(SetPriceThread, self).__init__() self.ask_price_list = [] self.bid_price_list = [] self.insert_time_list = [] self.instruments = instruments self.time_width = time_width self.start_time = base_time - timedelta(seconds=self.time_width) self.end_time = base_time self.base_time = base_time self.con = MysqlConnector() self.setPrice()
def train_save_model_wrapper(base_time, table_layout, output_train_index, learning_span, window_size, model_filename, weight_filename, normalization_filename, figure_filename): connector = MysqlConnector() train_base_time = change_to_ptime(base_time) original_dataset, value_dataset = getDataset(train_base_time, connector, window_size, learning_span, output_train_index, table_layout) max_price = max(original_dataset["end"]) min_price = min(original_dataset["end"]) value_dataset, normalization_model = change_to_normalization(value_dataset) input_train_data, output_train_data, time_list = createTrainDataset( value_dataset, original_dataset, window_size, learning_span, output_train_index) np.random.seed(202) model = build_model(input_train_data, output_size=1, neurons=20) history = model.fit(input_train_data, output_train_data, epochs=50, batch_size=1, verbose=2, shuffle=True) # モデルの保存 json_string = model.to_json() open(model_filename, "w").write(json_string) model.save_weights(weight_filename) # 正規化モデルの保存 save_to_normalization_model(normalization_model, normalization_filename) ## 訓練データで予測 train_predict = model.predict(input_train_data) paint_predict = [] paint_right = [] # 正規化戻し train_predict = normalization_model.inverse_transform(train_predict) output_train_data = normalization_model.inverse_transform( output_train_data) for i in range(len(train_predict)): print(time_list[i]) paint_predict.append(train_predict[i]) paint_right.append(output_train_data[i]) # paint_predict.append((train_predict[i]*(max_price-min_price))+min_price) # paint_right.append((output_train_data[i]*(max_price-min_price))+min_price) ### paint predict train data fig, ax1 = plt.subplots(1, 1) ax1.plot(time_list, paint_predict, label="Predict", color="blue") ax1.plot(time_list, paint_right, label="Actual", color="red") plt.savefig(figure_filename)
def __init__(self, instrument, base_path, config_name, base_time): super(ExpantionAlgo, self).__init__(instrument, base_path, config_name, base_time) self.base_price = 0 self.setPrice(base_time) self.debug_logger = getLogger("debug") self.result_logger = getLogger("result") self.slope = 0 self.mysql_connector = MysqlConnector() self.first_flag = self.config_data["first_trail_mode"] self.second_flag = self.config_data["second_trail_mode"] self.most_high_price = 0 self.most_low_price = 0 self.mode = "" self.buy_count = 0 self.buy_count_price = 0 self.sell_count = 0 self.sell_count_price = 0 self.original_stoploss_rate = 0 self.week_start_price = 0 self.setIndicator(base_time) self.high_price, self.low_price = getHighlowPriceWrapper( instrument=self.instrument, base_time=base_time, span=24, slide_span=0, connector=self.mysql_connector) self.daily_slope = self.getDailySlope(self.instrument, base_time, span=10, connector=self.mysql_connector) self.stoploss_flag = False self.algorithm = ""
def __init__(self, instrument, base_path, config_name, base_time): super(LstmAlgo, self).__init__(instrument, base_path, config_name, base_time) self.base_price = 0 self.setPrice(base_time) self.debug_logger = getLogger("debug") self.result_logger = getLogger("result") self.mysql_connector = MysqlConnector() self.first_flag = self.config_data["first_trail_mode"] self.second_flag = self.config_data["second_trail_mode"] self.trail_third_flag = False self.most_high_price = 0 self.most_low_price = 0 self.mode = "" self.algorithm = "" self.log_max_price = 0 self.log_min_price = 0 self.stl_logic = "none" self.output_max_price = 0 self.output_min_price = 0 self.first_trade_flag = "" self.first_trade_time = None self.current_path = os.path.abspath(os.path.dirname(__file__)) self.usdjpy_current_price = None self.usdjpy1h = None self.usdjpy3h = None self.usdjpyday = None self.eurusd_current_price = None self.eurusd1h = None self.eurusd3h = None self.eurusdday = None self.eurjpy_current_price = None self.eurjpy1h = None self.eurjpy3h = None self.eurjpyday = None self.usdjpy_sma = None self.eurusd_sma = None self.eurjpy_sma = None self.eurjpy_1hmodel = self.load_model(model_filename="multi_model_EUR_JPY_1h.json", weights_filename="multi_model_EUR_JPY_1h.hdf5") self.eurjpy_3hmodel = self.load_model(model_filename="multi_model_EUR_JPY_3h.json", weights_filename="multi_model_EUR_JPY_3h.hdf5") self.eurjpy_daymodel = self.load_model(model_filename="multi_model_EUR_JPY_day.json", weights_filename="multi_model_EUR_JPY_day.hdf5") self.eurjpy1h = self.multi_predict(table_type="1h", target_time=base_time) self.eurjpy3h = self.multi_predict(table_type="3h", target_time=base_time) self.eurjpyday = self.multi_predict(table_type="day", target_time=base_time)
def __init__(self, instrument, base_path, config_name, base_time): super(MultiEvolvAlgo, self).__init__(instrument, base_path, config_name, base_time) self.base_price = 0 self.setPrice(base_time) self.debug_logger = getLogger("debug") self.result_logger = getLogger("result") self.mysql_connector = MysqlConnector() self.first_flag = self.config_data["first_trail_mode"] self.second_flag = self.config_data["second_trail_mode"] self.most_high_price = 0 self.most_low_price = 0 self.mode = "" self.buy_count = 0 self.buy_count_price = 0 self.sell_count = 0 self.sell_count_price = 0 self.original_stoploss_rate = 0 self.week_start_price = 0 self.count_threshold = 1 self.stoploss_flag = False self.algorithm = "" self.setExpantionIndicator(base_time) self.setVolatilityIndicator(base_time) self.setReverseIndicator(base_time) self.high_price, self.low_price = getHighlowPriceWrapper( instrument=self.instrument, base_time=base_time, span=1, table_type="day", connector=self.mysql_connector) self.daily_slope = self.getDailySlope(self.instrument, base_time, span=10, connector=self.mysql_connector) self.setDailyStartPrice(base_time) self.reverse_sell_flag = False self.reverse_buy_flag = False self.reverse_sell_count = 0 self.reverse_buy_count = 0
def __init__(self, instrument, base_path, config_name, base_time): self.base_path = base_path self.instrument = instrument self.config_data = instrument_init(self.instrument, self.base_path, config_name) self.ask_price = 0 self.bid_price = 0 self.insert_time = "" self.order_price = 0 self.stl_price = 0 self.stoploss_rate = 0 self.takeprofit_rate = 0 self.order_flag = False self.trade_id = 0 self.order_kind = "" self.mysql_connector = MysqlConnector() self.trail_flag = False self.trail_second_flag = False self.trail_price = 0 self.order_history = "pass" self.profit_history = "pass" self.count_threshold = 1
def __init__(self, instrument, base_path, config_name, base_time): super(ReverseAlgo, self).__init__(instrument, base_path, config_name, base_time) self.base_price = 0 self.setPrice(base_time) self.setIndicator(base_time) self.setHighlowPrice(base_time, 24) self.debug_logger = getLogger("debug") self.result_logger = getLogger("result") self.slope = 0 self.mysql_connector = MysqlConnector() self.first_flag = self.config_data["first_trail_mode"] self.second_flag = self.config_data["second_trail_mode"] self.most_high_price = 0 self.most_low_price = 0 self.buy_flag = False self.sell_flag = False self.first_trade_flag = False
class DBWrapper: def __init__(self): self.con = MysqlConnector() def getPrice(self, instrument, time_width, now): time_width = int(time_width) base_time = now - timedelta(seconds=time_width) base_time = base_time.strftime("%Y-%m-%d %H:%M:%S") end_time = now.strftime("%Y-%m-%d %H:%M:%S") sql = "select ask_price, bid_price, insert_time from %s_TABLE where insert_time > \'%s\' and insert_time < \'%s\' order by insert_time" % ( instrument, base_time, end_time) print sql logging.info(sql) response = self.con.select_sql(sql) return response def getStartEndPrice(self, instrument, time_width, now): time_width = int(time_width) base_time = now - timedelta(seconds=time_width) base_time = base_time.strftime("%Y-%m-%d %H:%M:00") end_time = now.strftime("%Y-%m-%d %H:%M:00") sql = "select ask_price, bid_price, insert_time from %s_TABLE where insert_time > \'%s\' and insert_time < \'%s\' order by insert_time" % ( instrument, base_time, end_time) print sql logging.info(sql) response = self.con.select_sql(sql) return response def getTimeFormat(self, dtime): return dtime.strftime("%Y-%m-%d %H:%M:%S") def insertOrderHistory(self): sql = u"insert into ORDER_HISTORY_TABLE(trade_id, instrument, order_time, order_price, trade_flag, mode) values(%s, %s, %s, %s, %s, %s)" % ( trade_id, instrument, order_time, order_price, trade_flag, mode) self.con.insert_sql(sql) def updateOrderHistory(self): sql = u"update ORDER_HISTORY_TABLE set stl_flag = 0, stl_time = \'%s\', stl_price = %s where trade_id = %s" % ( stl_time, stl_price) self.con.insert_sql(sql)
import numpy as np from logging import getLogger, FileHandler, DEBUG from send_mail import SendMail mode = sys.argv[1] filename = sys.argv[0].split(".")[0] print(filename) debug_logfilename = "%s-%s-%s.log" % (mode, filename, datetime.now().strftime("%Y%m%d%H%M%S")) debug_logger = getLogger("debug") debug_fh = FileHandler(debug_logfilename, "a+") debug_logger.addHandler(debug_fh) debug_logger.setLevel(DEBUG) con = MysqlConnector() instrument_list = [ "EUR_GBP", "EUR_USD", "EUR_JPY", "GBP_USD", "GBP_JPY", "USD_JPY" ] instrument_list = [ "GBP_JPY", "EUR_JPY", "AUD_JPY", "GBP_USD", "EUR_USD", "AUD_USD", "USD_JPY" ] insert_time = '2019-04-01 07:00:00' #insert_time = '2019-07-01 07:00:00' insert_time = datetime.strptime(insert_time, "%Y-%m-%d %H:%M:%S") now = datetime.now() end_time = datetime.strptime('2019-07-06 00:00:00', "%Y-%m-%d %H:%M:%S") def decide_season(base_time):
class TrendReverseAlgo(SuperAlgo): def __init__(self, instrument, base_path, config_name, base_time): super(TrendReverseAlgo, self).__init__(instrument, base_path, config_name, base_time) self.base_price = 0 self.setPrice(base_time) self.debug_logger = getLogger("debug") self.result_logger = getLogger("result") self.mysql_connector = MysqlConnector() self.first_flag = self.config_data["first_trail_mode"] self.second_flag = self.config_data["second_trail_mode"] self.trail_third_flag = False self.most_high_price = 0 self.most_low_price = 0 self.mode = "" self.buy_count = 0 self.buy_count_price = 0 self.sell_count = 0 self.sell_count_price = 0 self.original_stoploss_rate = 0 self.week_start_price = 0 self.count_threshold = 1 self.stoploss_flag = False self.algorithm = "" self.log_max_price = 0 self.log_min_price = 0 self.first_trade_flag = "pass" self.second_trade_flag = False self.first_trade_time = base_time self.second_trade_time = base_time self.third_trade_flag = "pass" self.third_trade_time = base_time self.stl_first_flag = False self.buy_flag = False self.sell_flag = False self.stl_logic = "none" self.perfect_order_buycount = 0 self.perfect_order_sellcount = 0 self.setReverse5mIndicator(base_time) self.setReverse1hIndicator(base_time) self.setReverseDailyIndicator(base_time) # decide trade entry timing def decideTrade(self, base_time): trade_flag = "pass" try: # if self.order_flag: # pass # else: if 1 == 1: weekday = base_time.weekday() hour = base_time.hour minutes = base_time.minute seconds = base_time.second current_price = self.getCurrentPrice() if seconds < 10 and minutes % 5 == 0: self.setReverse5mIndicator(base_time) if seconds < 10 and minutes == 0: self.setReverse1hIndicator(base_time) if seconds < 10 and hour == 7: self.setReverseDailyIndicator(base_time) # if weekday == Saturday, we will have no entry. if weekday == 5 and hour >= 5: trade_flag = "pass" self.buy_count = 0 self.sell_count = 0 else: # if spread rate is greater than 0.5, we will have no entry if (self.ask_price - self.bid_price) >= 0.05: pass else: trade_flag = self.decideReverseTrade( trade_flag, current_price, base_time) if trade_flag != "pass" and self.order_flag: if trade_flag == "buy" and self.order_kind == "buy": trade_flag = "pass" elif trade_flag == "sell" and self.order_kind == "sell": trade_flag = "pass" else: self.stl_logic = "allovertheworld settlement" self.algorithm = self.algorithm + " by allovertheworld" return trade_flag except: raise # settlement logic def decideStl(self, base_time): try: stl_flag = False ex_stlmode = self.config_data["ex_stlmode"] if self.order_flag: if ex_stlmode == "on": weekday = base_time.weekday() hour = base_time.hour minutes = base_time.minute seconds = base_time.second current_price = self.getCurrentPrice() self.updatePrice(current_price) # if weekday == Saturday, we will settle one's position. if weekday == 5 and hour >= 5: self.result_logger.info("# weekend stl logic") stl_flag = True else: pass stl_flag = self.decideReverseStl(stl_flag, base_time) stl_flag = self.decideTrailLogic( stl_flag, self.ask_price, self.bid_price, base_time) else: pass # self.writeDebugLog(base_time, mode="stl") return stl_flag except: raise def decideReverseStl(self, stl_flag, base_time): if self.order_flag: hour = base_time.hour minutes = base_time.minute seconds = base_time.second current_price = (self.ask_price + self.bid_price) / 2 if self.algorithm == "perfect_order": if self.order_kind == "buy" and current_price > self.upper_sigma_1h1 and self.stl_first_flag == False: self.stl_first_flag = True self.writeDebugStlLog(base_time, stl_flag) elif self.order_kind == "sell" and current_price < self.lower_sigma_1h1 and self.stl_first_flag == False: self.stl_first_flag = True self.writeDebugStlLog(base_time, stl_flag) if self.order_kind == "buy" and self.stl_first_flag and current_price < self.sma1h40: stl_flag = True self.writeDebugStlLog(base_time, stl_flag) elif self.order_kind == "sell" and self.stl_first_flag and current_price > self.sma1h40: stl_flag = True self.writeDebugStlLog(base_time, stl_flag) elif self.algorithm == "reverse_order": reverse_stoploss_rate = 0.3 if self.order_kind == "buy" and current_price > self.upper_sigma_1h3: stl_flag = True self.writeDebugStlLog(base_time, stl_flag) elif self.order_kind == "sell" and current_price < self.lower_sigma_1h3: stl_flag = True self.writeDebugStlLog(base_time, stl_flag) if self.order_kind == "buy" and current_price < ( self.order_price - reverse_stoploss_rate): stl_flag = True self.writeDebugStlLog(base_time, stl_flag) elif self.order_kind == "sell" and current_price > ( self.order_price + reverse_stoploss_rate): stl_flag = True self.writeDebugStlLog(base_time, stl_flag) return stl_flag def decideBollingerCrossOver(self, flag): state = False if flag == "upper": for i in range(0, len(self.upper_sigma_5m2_list)): if self.max_price_5m_list[i] > self.upper_sigma_5m2_list[i]: state = True elif flag == "lower": for i in range(0, len(self.lower_sigma_5m2_list)): if self.min_price_5m_list[i] < self.lower_sigma_5m2_list[i]: state = True else: raise return state def decidePerfectOrder(self, span, slope_flag): direct = "pass" if span == "5m": if (self.sma5m20 > self.sma5m40 > self.sma5m80): if slope_flag: if (self.sma5m20_slope > 0 and self.sma5m40_slope > 0 and self.sma5m80_slope > 0): direct = "buy" else: direct = "buy" elif (self.sma5m20 < self.sma5m40 < self.sma5m80): if slope_flag: if (self.sma5m20_slope < 0 and self.sma5m40_slope < 0 and self.sma5m80_slope < 0): direct = "sell" else: direct = "sell" elif span == "1h": if (self.sma1h20 > self.sma1h40 > self.sma1h80): if slope_flag: if (self.sma1h20_slope > 0 and self.sma1h40_slope > 0 and self.sma1h80_slope > 0): direct = "buy" else: direct = "buy" elif (self.sma1h20 < self.sma1h40 < self.sma1h80): if slope_flag: if (self.sma1h20_slope < 0 and self.sma1h40_slope < 0 and self.sma1h80_slope < 0): direct = "sell" else: direct = "sell" return direct def decideSma(self, sma_value, current_price): direct = "pass" if current_price > sma_value: direct = "buy" elif current_price < sma_value: direct = "sell" return direct def decideEwma(self): flag = True if self.start_price_1d < self.end_price_1d: if self.start_price_1d < self.sma20_1d < self.end_price_1d: flag = False else: if self.end_price_1d < self.sma20_1d < self.start_price_1d: flag = False return flag def decideReverseTrade(self, trade_flag, current_price, base_time): if trade_flag == "pass": hour = base_time.hour minutes = base_time.minute seconds = base_time.second if 1 == 0: #if ((self.min_price < self.sma1d20 < self.max_price) == True and (self.thisday_min < self.sma1d20 < self.thisday_max) == True) or (40 < self.rsi_value < 70 and (self.upper_sigma_1d3 - self.lower_sigma_1d3) < 10): # if self.order_flag: if 1 == 1: pass else: if current_price < self.lower_sigma_1h25 and self.first_trade_flag != "buy" and self.order_kind != "buy": self.first_trade_flag = "buy" self.first_trade_time = base_time self.first_trade_price = current_price self.second_trade_flag = False self.third_trade_flag = "pass" self.writeDebugTradeLog(base_time, trade_flag) elif current_price > self.upper_sigma_1h25 and self.first_trade_flag != "sell" and self.order_kind != "sell": self.first_trade_flag = "sell" self.first_trade_time = base_time self.first_trade_price = current_price self.second_trade_flag = False self.third_trade_flag = "pass" self.writeDebugTradeLog(base_time, trade_flag) if self.first_trade_flag == "buy" and self.decidePerfectOrder( "5m", slope_flag=True) == "buy": trade_flag = "buy" self.algorithm = "reverse_order" self.writeDebugTradeLog(base_time, trade_flag) self.first_trade_flag = "pass" self.second_trade_flag = False self.third_trade_flag = "pass" elif self.first_trade_flag == "sell" and self.decidePerfectOrder( "5m", slope_flag=True) == "sell": trade_flag = "sell" self.algorithm = "reverse_order" self.writeDebugTradeLog(base_time, trade_flag) self.first_trade_flag = "pass" self.second_trade_flag = False self.third_trade_flag = "pass" else: if self.decidePerfectOrder( "1h", slope_flag=True ) == "buy" and self.first_trade_flag != "buy" and self.order_kind != "buy": self.first_trade_flag = "buy" self.perfect_order_buycount = self.perfect_order_buycount + 1 self.first_trade_time = base_time self.first_trade_price = current_price self.second_trade_flag = False self.third_trade_flag = "pass" self.writeDebugTradeLog(base_time, trade_flag) elif self.decidePerfectOrder( "1h", slope_flag=True ) == "sell" and self.first_trade_flag != "sell" and self.order_kind != "sell": self.perfect_order_sellcount = self.perfect_order_sellcount + 1 self.first_trade_flag = "sell" self.first_trade_time = base_time self.first_trade_price = current_price self.second_trade_flag = False self.third_trade_flag = "pass" self.writeDebugTradeLog(base_time, trade_flag) if self.first_trade_flag == "buy": if self.decidePerfectOrder( "5m", slope_flag=False ) == "sell" and self.second_trade_flag == False: self.second_trade_time = base_time self.second_trade_flag = True self.second_trade_price = current_price self.writeDebugTradeLog(base_time, trade_flag) elif self.first_trade_flag == "sell": if self.decidePerfectOrder( "5m", slope_flag=False ) == "buy" and self.second_trade_flag == False: self.second_trade_time = base_time self.second_trade_flag = True self.second_trade_price = current_price self.writeDebugTradeLog(base_time, trade_flag) if self.first_trade_flag == "buy" and self.second_trade_flag and self.sma5m40 < current_price and self.decidePerfectOrder( "1h", slope_flag=False) == "buy": self.third_trade_flag = "buy" self.third_trade_time = base_time self.third_trade_price = current_price self.writeDebugTradeLog(base_time, trade_flag) elif self.first_trade_flag == "sell" and self.second_trade_flag and self.sma5m40 > current_price and self.decidePerfectOrder( "1h", slope_flag=False) == "sell": self.third_trade_flag = "sell" self.third_trade_time = base_time self.third_trade_price = current_price self.writeDebugTradeLog(base_time, trade_flag) if self.first_trade_flag == "buy" and self.second_trade_flag and self.third_trade_flag == "buy": trade_flag = "buy" self.first_trade_flag = "pass" self.second_trade_flag = False self.third_trade_flag = "pass" self.algorithm = "perfect_order" self.writeDebugTradeLog(base_time, trade_flag) elif self.first_trade_flag == "sell" and self.second_trade_flag and self.third_trade_flag == "sell": trade_flag = "sell" self.first_trade_flag = "pass" self.second_trade_flag = False self.third_trade_flag = "pass" self.algorithm = "perfect_order" self.writeDebugTradeLog(base_time, trade_flag) return trade_flag def updatePrice(self, current_price): if self.log_max_price == 0: self.log_max_price = current_price elif self.log_max_price < current_price: self.log_max_price = current_price if self.log_min_price == 0: self.log_min_price = current_price elif self.log_min_price > current_price: self.log_min_price = current_price # reset flag and valiables function after settlement def resetFlag(self): if self.order_kind == "buy": self.buy_count = 0 elif self.order_kind == "sell": self.sell_count = 0 self.mode = "" self.most_high_price = 0 self.most_low_price = 0 self.stoploss_flag = False #self.algorithm = "" self.log_max_price = 0 self.log_min_price = 0 #self.first_trade_flag = "pass" #self.second_trade_flag = False self.stl_first_flag = False self.buy_flag = False self.sell_flag = False self.perfect_order_buycount = 0 self.perfect_order_sellcount = 0 self.trail_third_flag = False self.stl_logic = "none" super(TrendReverseAlgo, self).resetFlag() def setReverse5mIndicator(self, base_time): try: ### get 5m dataset target_time = base_time - timedelta(minutes=5) dataset = getBollingerWrapper(target_time, self.instrument, table_type="5m", window_size=21, connector=self.mysql_connector, sigma_valiable=2.5, length=0) self.upper_sigma_5m25 = dataset["upper_sigmas"][-1] self.lower_sigma_5m25 = dataset["lower_sigmas"][-1] dataset = getBollingerWrapper(target_time, self.instrument, table_type="5m", window_size=20, connector=self.mysql_connector, sigma_valiable=2, length=1) self.sma5m20 = dataset["base_lines"][-1] self.sma5m20_before = dataset["base_lines"][-2] dataset = getBollingerWrapper(target_time, self.instrument, table_type="5m", window_size=40, connector=self.mysql_connector, sigma_valiable=2, length=1) self.sma5m40 = dataset["base_lines"][-1] self.sma5m40_before = dataset["base_lines"][-2] dataset = getBollingerWrapper(target_time, self.instrument, table_type="5m", window_size=80, connector=self.mysql_connector, sigma_valiable=2, length=1) self.sma5m80 = dataset["base_lines"][-1] self.sma5m80_before = dataset["base_lines"][-2] self.sma5m20_slope = getSlope([self.sma5m20_before, self.sma5m20]) self.sma5m40_slope = getSlope([self.sma5m40_before, self.sma5m40]) self.sma5m80_slope = getSlope([self.sma5m80_before, self.sma5m80]) except Exception as e: message = traceback.format_exc() self.debug_logger.info("# %s" % base_time) self.debug_logger.info("# %s" % message) def getStartTime(self, base_time): hour = base_time.hour week = base_time.weekday() base_ftime = base_time.strftime("%Y-%m-%d 07:00:00") base_time = datetime.strptime(base_ftime, "%Y-%m-%d %H:%M:%S") if hour < 9: for i in range(1, 5): tmp_time = base_time - timedelta(days=i) if decideMarket(tmp_time): break base_time = tmp_time else: pass return base_time def setReverse1hIndicator(self, base_time): try: ### get 1h dataset target_time = base_time - timedelta(hours=1) dataset = getBollingerWrapper(target_time, self.instrument, table_type="1h", window_size=21, connector=self.mysql_connector, sigma_valiable=1, length=0) self.upper_sigma_1h1 = dataset["upper_sigmas"][-1] self.lower_sigma_1h1 = dataset["lower_sigmas"][-1] dataset = getBollingerWrapper(target_time, self.instrument, table_type="1h", window_size=21, connector=self.mysql_connector, sigma_valiable=2.5, length=0) self.upper_sigma_1h25 = dataset["upper_sigmas"][-1] self.lower_sigma_1h25 = dataset["lower_sigmas"][-1] dataset = getBollingerWrapper(target_time, self.instrument, table_type="1h", window_size=21, connector=self.mysql_connector, sigma_valiable=3, length=0) self.upper_sigma_1h3 = dataset["upper_sigmas"][-1] self.lower_sigma_1h3 = dataset["lower_sigmas"][-1] dataset = getBollingerWrapper(target_time, self.instrument, table_type="1h", window_size=100, connector=self.mysql_connector, sigma_valiable=2, length=0) self.sma1h100 = dataset["base_lines"][-1] dataset = getBollingerWrapper(target_time, self.instrument, table_type="1h", window_size=20, connector=self.mysql_connector, sigma_valiable=2, length=1) self.sma1h20 = dataset["base_lines"][-1] self.sma1h20_before = dataset["base_lines"][-2] self.sma1h20_slope = getSlope([self.sma1h20_before, self.sma1h20]) dataset = getBollingerWrapper(target_time, self.instrument, table_type="1h", window_size=40, connector=self.mysql_connector, sigma_valiable=2, length=1) self.sma1h40 = dataset["base_lines"][-1] self.sma1h40_before = dataset["base_lines"][-2] self.sma1h40_slope = getSlope([self.sma1h40_before, self.sma1h40]) dataset = getBollingerWrapper(target_time, self.instrument, table_type="1h", window_size=80, connector=self.mysql_connector, sigma_valiable=2, length=1) self.sma1h80 = dataset["base_lines"][-1] self.sma1h80_before = dataset["base_lines"][-2] self.sma1h80_slope = getSlope([self.sma1h80_before, self.sma1h80]) start_time = self.getStartTime(base_time) sql = "select max_price, min_price from %s_%s_TABLE where insert_time > \'%s\' and insert_time < \'%s\'" % ( self.instrument, "1h", start_time, base_time) response = self.mysql_connector.select_sql(sql) tmp_max = [] tmp_min = [] for res in response: tmp_max.append(res[0]) tmp_min.append(res[1]) self.thisday_max = max(tmp_max) self.thisday_min = min(tmp_min) except Exception as e: message = traceback.format_exc() self.debug_logger.info("# %s" % base_time) self.debug_logger.info("# %s" % message) def setReverseDailyIndicator(self, base_time): try: ### get daily dataset target_time = base_time - timedelta(days=1) sql = "select max_price, min_price, start_price, end_price from %s_%s_TABLE where insert_time < \'%s\' order by insert_time desc limit 1" % ( self.instrument, "day", target_time) response = self.mysql_connector.select_sql(sql) self.max_price_1d = response[0][0] self.min_price_1d = response[0][1] self.start_price_1d = response[0][2] self.end_price_1d = response[0][3] dataset = getBollingerWrapper(target_time, self.instrument, table_type="day", window_size=21, connector=self.mysql_connector, sigma_valiable=3, length=0) self.upper_sigma_1d3 = dataset["upper_sigmas"][-1] self.lower_sigma_1d3 = dataset["lower_sigmas"][-1] dataset = getBollingerWrapper(target_time, self.instrument, table_type="day", window_size=21, connector=self.mysql_connector, sigma_valiable=1, length=0) self.upper_sigma_1d1 = dataset["upper_sigmas"][-1] self.lower_sigma_1d1 = dataset["lower_sigmas"][-1] dataset = getBollingerWrapper(target_time, self.instrument, table_type="day", window_size=20, connector=self.mysql_connector, sigma_valiable=2, length=0) self.sma1d20 = dataset["base_lines"][-1] sql = "select max_price, min_price from %s_%s_TABLE where insert_time < '%s' order by insert_time desc limit 1" % ( self.instrument, "day", target_time) response = self.mysql_connector.select_sql(sql) self.max_price = response[0][0] self.min_price = response[0][1] self.rsi_value = getRsiWrapper(target_time, self.instrument, "day", self.mysql_connector, 14) except Exception as e: message = traceback.format_exc() self.debug_logger.info("# %s" % base_time) self.debug_logger.info("# %s" % message) def decideTrailLogic(self, stl_flag, current_ask_price, current_bid_price, base_time): minutes = base_time.minute seconds = base_time.second if minutes % 5 == 0 and seconds < 10: order_price = self.getOrderPrice() first_take_profit = 0.5 second_take_profit = 1.0 third_take_profit = 2.0 # update the most high and low price if self.most_high_price == 0 and self.most_low_price == 0: self.most_high_price = order_price self.most_low_price = order_price if self.most_high_price < current_bid_price: self.most_high_price = current_bid_price if self.most_low_price > current_ask_price: self.most_low_price = current_ask_price # first trailing stop logic if self.order_kind == "buy": if (current_bid_price - order_price) > first_take_profit: self.trail_flag = True elif self.order_kind == "sell": if (order_price - current_ask_price) > first_take_profit: self.trail_flag = True if self.trail_flag == True and self.order_kind == "buy": if self.order_price > current_bid_price: self.result_logger.info("# Execute FirstTrail Stop") stl_flag = True elif self.trail_flag == True and self.order_kind == "sell": if self.order_price < current_ask_price: self.result_logger.info("# Execute FirstTrail Stop") stl_flag = True # # second trailing stop logic # if self.order_kind == "buy": # if (current_bid_price - order_price) > second_take_profit: # self.trail_second_flag = True # elif self.order_kind == "sell": # if (order_price - current_ask_price) > second_take_profit: # self.trail_second_flag = True # # if self.trail_second_flag == True and self.order_kind == "buy": # if (self.order_price + 0.5) > current_bid_price: # self.result_logger.info("# Execute SecondTrail Stop") # stl_flag = True # elif self.trail_second_flag == True and self.order_kind == "sell": # if (self.order_price - 0.5) < current_ask_price : # self.result_logger.info("# Execute SecondTrail Stop") # stl_flag = True # # # third trailing stop logic # if self.order_kind == "buy": # if (current_bid_price - order_price) > third_take_profit: # self.trail_third_flag = True # elif self.order_kind == "sell": # if (order_price - current_ask_price) > third_take_profit: # self.trail_third_flag = True # # if self.trail_third_flag == True and self.order_kind == "buy": # if (self.order_price + 1.0) > current_bid_price: # self.result_logger.info("# Execute SecondTrail Stop") # stl_flag = True # elif self.trail_third_flag == True and self.order_kind == "sell": # if (self.order_price - 1.0) < current_ask_price : # self.result_logger.info("# Execute SecondTrail Stop") # stl_flag = True return stl_flag # write log function def writeDebugTradeLog(self, base_time, trade_flag): self.debug_logger.info("# %s " % base_time) self.debug_logger.info("# self.first_trade_flag= %s" % self.first_trade_flag) self.debug_logger.info("# self.second_trade_flag=%s" % self.second_trade_flag) self.debug_logger.info("# self.third_trade_flag= %s" % self.third_trade_flag) self.debug_logger.info("# trade_flag= %s" % trade_flag) self.debug_logger.info("#############################################") def writeDebugStlLog(self, base_time, stl_flag): self.debug_logger.info("# %s " % base_time) self.debug_logger.info("# self.stl_first_flag=%s" % self.stl_first_flag) self.debug_logger.info("# stl_flag= %s" % stl_flag) self.debug_logger.info("#############################################") def entryLogWrite(self, base_time): self.result_logger.info( "#######################################################") self.result_logger.info("# in %s Algorithm" % self.algorithm) self.result_logger.info("# first_trade_time=%s" % self.first_trade_time) self.result_logger.info("# second_trade_time= %s" % self.second_trade_time) self.result_logger.info("# third_trade_time= %s" % self.third_trade_time) self.result_logger.info("# EXECUTE ORDER at %s" % base_time) self.result_logger.info("# first_trade_price=%s" % self.first_trade_price) self.result_logger.info("# second_trade_price= %s" % self.second_trade_price) self.result_logger.info("# third_trade_price= %s" % self.third_trade_price) self.result_logger.info("# ORDER_PRICE=%s, TRADE_FLAG=%s" % (self.order_price, self.order_kind)) self.result_logger.info("# self.upper_sigma_5m25=%s" % self.upper_sigma_5m25) self.result_logger.info("# self.lower_sigma_5m25=%s" % self.lower_sigma_5m25) self.result_logger.info("# self.upper_sigma_1h3=%s" % self.upper_sigma_1h3) self.result_logger.info("# self.lower_sigma_1h3=%s" % self.lower_sigma_1h3) self.result_logger.info("# self.sma5m20=%s" % self.sma5m20) self.result_logger.info("# self.sma5m40=%s" % self.sma5m40) self.result_logger.info("# self.sma5m80=%s" % self.sma5m80) # self.result_logger.info("# self.sma5m20_slope=%s" % self.sma5m20_slope) # self.result_logger.info("# self.sma5m40_slope=%s" % self.sma5m40_slope) # self.result_logger.info("# self.sma5m80_slope=%s" % self.sma5m80_slope) self.result_logger.info("# self.sma1h100=%s" % self.sma1h100) self.result_logger.info("# self.sma1h20=%s" % self.sma1h20) self.result_logger.info("# self.sma1h40=%s" % self.sma1h40) self.result_logger.info("# self.sma1h80=%s" % self.sma1h80) # self.result_logger.info("# self.sma1h20_slope=%s" % self.sma1h20_slope) # self.result_logger.info("# self.sma1h40_slope=%s" % self.sma1h40_slope) # self.result_logger.info("# self.sma1h80_slope=%s" % self.sma1h80_slope) self.result_logger.info("# self.before_max=%s" % self.max_price) self.result_logger.info("# self.before_min=%s" % self.min_price) self.result_logger.info("# self.thisday_max=%s" % self.thisday_max) self.result_logger.info("# self.thisday_min=%s" % self.thisday_min) self.result_logger.info("# self.sma1d20=%s" % self.sma1d20) self.result_logger.info("# self.upper_sigma_1d3=%s" % self.upper_sigma_1d3) self.result_logger.info("# self.lower_sigma_1d3=%s" % self.lower_sigma_1d3) self.result_logger.info("# self.upper_sigma_1d1=%s" % self.upper_sigma_1d1) self.result_logger.info("# self.lower_sigma_1d1=%s" % self.lower_sigma_1d1) self.result_logger.info("# self.rsi_value=%s" % self.rsi_value) if ((self.min_price < self.sma1d20 < self.max_price) == True and (self.thisday_min < self.sma1d20 < self.thisday_max) == True): self.result_logger.info("self.sma1d20_logic=TRUE") else: self.result_logger.info("self.sma1d20_logic=FALSE") if (40 < self.rsi_value < 70 and (self.upper_sigma_1d3 - self.lower_sigma_1d3) < 10): self.result_logger.info("self.rsi_bollinger_logic=TRUE") else: self.result_logger.info("self.rsi_bollinger_logic=FALSE") def settlementLogWrite(self, profit, base_time, stl_price, stl_method): self.result_logger.info("# %s at %s" % (stl_method, base_time)) self.result_logger.info("# self.ask_price=%s" % self.ask_price) self.result_logger.info("# self.bid_price=%s" % self.bid_price) self.result_logger.info("# self.log_max_price=%s" % self.log_max_price) self.result_logger.info("# self.log_min_price=%s" % self.log_min_price) self.result_logger.info("# self.stl_logic=%s" % self.stl_logic) self.result_logger.info("# STL_PRICE=%s" % stl_price) self.result_logger.info("# PROFIT=%s" % profit)
class Scalping(SuperAlgo): def __init__(self, instrument, base_path, config_name, base_time): super(Scalping, self).__init__(instrument, base_path, config_name, base_time) self.setPrice(base_time) self.debug_logger = getLogger("debug") self.result_logger = getLogger("result") self.mysql_connector = MysqlConnector() self.first_flag = self.config_data["first_trail_mode"] self.second_flag = self.config_data["second_trail_mode"] self.most_high_price = 0 self.most_low_price = 0 self.mode = "" self.algorithm = "" self.log_max_price = 0 self.log_min_price = 0 self.stl_logic = "none" self.output_max_price = 0 self.output_min_price = 0 self.first_trade_flag = "" self.first_trade_time = None self.log_object = {} self.current_path = os.path.abspath(os.path.dirname(__file__)) self.model_1h = self.load_model(model_filename="multi_model_1h.json", weights_filename="multi_model_1h.hdf5") self.model_5m = self.load_model(model_filename="multi_model_5m.json", weights_filename="multi_model_5m.hdf5") self.model_1m = self.load_model(model_filename="multi_model_1m.json", weights_filename="multi_model_1m.hdf5") # decide trade entry timing def decideTrade(self, base_time): trade_flag = "pass" try: weekday = base_time.weekday() hour = base_time.hour minutes = base_time.minute seconds = base_time.second current_price = self.getCurrentPrice() # if weekday == Saturday, we will have no entry. if weekday == 4 and hour >= 22: trade_flag = "pass" if weekday == 5: trade_flag = "pass" else: # if spread rate is greater than 0.5, we will have no entry if (self.ask_price - self.bid_price) >= 0.05: pass else: trade_flag = self.decideReverseTrade(trade_flag, current_price, base_time) #trade_flag = "buy" return trade_flag except: raise # settlement logic def decideStl(self, base_time): try: stl_flag = False ex_stlmode = self.config_data["ex_stlmode"] if self.order_flag: if ex_stlmode == "on": weekday = base_time.weekday() hour = base_time.hour minutes = base_time.minute seconds = base_time.second current_price = self.getCurrentPrice() self.updatePrice(current_price) # if weekday == Saturday, we will settle one's position. if weekday == 5 and hour >= 5: self.result_logger.info("# weekend stl logic") stl_flag = True else: stl_flag = self.decideReverseStl(stl_flag, base_time) #pass else: pass return stl_flag except: raise def decideReverseStl(self, stl_flag, base_time): return stl_flag def get_current_price(self, target_time): table_type = "1m" instruments = "EUR_JPY" sql = "select close_ask, close_bid from %s_%s_TABLE where insert_time < \'%s\' order by insert_time desc limit 1" % (instruments, table_type, target_time - timedelta(minutes=1)) response = self.mysql_connector.select_sql(sql) return ((response[0][0] + response[0][1]) / 2) def decideReverseTrade(self, trade_flag, current_price, base_time): if self.order_flag == False: minutes = base_time.minute seconds = base_time.second # if 15 < seconds < 30: # target_time = base_time # self.set_current_price(target_time) if 0 < seconds <= 10: target_time = base_time right_string = "EUR_JPY" instruments = "EUR_JPY" window_size = 20 output_train_index = 60 table_type = "1m" predict_price_1m = predict_value(target_time, self.model_1m, window_size=window_size, table_type=table_type, output_train_index=output_train_index, instruments=instruments, right_string=right_string) window_size = 20 output_train_index = 12 table_type = "5m" predict_price_5m = predict_value(target_time, self.model_5m, window_size=window_size, table_type=table_type, output_train_index=output_train_index, instruments=instruments, right_string=right_string) window_size = 20 output_train_index = 1 table_type = "1h" predict_price_1h = predict_value(target_time, self.model_1h, window_size=window_size, table_type=table_type, output_train_index=output_train_index, instruments=instruments, right_string=right_string) current_price = self.get_current_price(base_time) if current_price < predict_price_1m and current_price < predict_price_5m and current_price < predict_price_1h: self.first_trade_flag = "buy" self.first_trade_time = base_time self.setLogObject("first_trade_time", base_time) self.setLogObject("first_trade_flag", self.first_trade_flag) self.setLogObject("predict_price_1m", predict_price_1m) self.setLogObject("predict_price_5m", predict_price_5m) self.setLogObject("predict_price_1h", predict_price_1h) self.setLogObject("first_trade_price", current_price) elif current_price > predict_price_1m and current_price > predict_price_5m and current_price > predict_price_1h: self.first_trade_flag = "sell" self.first_trade_time = base_time self.setLogObject("first_trade_time", base_time) self.setLogObject("first_trade_flag", self.first_trade_flag) self.setLogObject("predict_price_1m", predict_price_1m) self.setLogObject("predict_price_5m", predict_price_5m) self.setLogObject("predict_price_1h", predict_price_1h) self.setLogObject("first_trade_price", current_price) if self.first_trade_flag != "" and 5 < seconds < 15: current_price = self.get_current_price(base_time) eurjpy_sma = get_sma(instrument="EUR_JPY", base_time=base_time, table_type="1m", length=20, con=self.mysql_connector) if self.first_trade_flag == "buy": if current_price > eurjpy_sma: trade_flag = "buy" self.setLogObject("second_trade_time", base_time) self.setLogObject("second_trade_price", current_price) self.setLogObject("eurjpy_sma", eurjpy_sma) elif self.first_trade_flag == "sell": if current_price < eurjpy_sma: trade_flag = "sell" self.setLogObject("second_trade_time", base_time) self.setLogObject("second_trade_price", current_price) self.setLogObject("eurjpy_sma", eurjpy_sma) else: raise return trade_flag def updatePrice(self, current_price): if self.log_max_price == 0: self.log_max_price = current_price elif self.log_max_price < current_price: self.log_max_price = current_price if self.log_min_price == 0: self.log_min_price = current_price elif self.log_min_price > current_price: self.log_min_price = current_price # reset flag and valiables function after settlement def resetFlag(self): self.first_trade_flag = "" self.mode = "" self.most_high_price = 0 self.most_low_price = 0 self.log_max_price = 0 self.log_min_price = 0 self.stl_logic = "none" super(Scalping, self).resetFlag() def setLogObject(self, key, value): self.log_object[key] = value # write log function def writeLog(self, logger): key_list = self.log_object.keys() logger.info("####################################") for key in key_list: logger.info("%s=%s" % (key, self.log_object[key])) self.log_object = {} def entryLogWrite(self, base_time): self.setLogObject("EXECUTE_ORDER", base_time) self.writeLog(self.result_logger) def settlementLogWrite(self, profit, base_time, stl_price, stl_method): self.setLogObject("STL_TIME", base_time) self.setLogObject("PROFIT", profit) self.setLogObject("LOG_MAX_PRICE", self.log_max_price) self.setLogObject("LOG_MIN_PRICE", self.log_min_price) self.writeLog(self.result_logger) def load_model(self, model_filename, weights_filename): model_filename = "%s/../model/master/%s" % (self.current_path, model_filename) weights_filename = "%s/../model/master/%s" % (self.current_path, weights_filename) print(model_filename) print(weights_filename) json_string = open(model_filename).read() learning_model = model_from_json(json_string) learning_model.load_weights(weights_filename) return learning_model
class ComputeIndicator: def __init__(self, instrument, time_width, base_time): self.instrument = instrument self.time_width = time_width self.indicator_object = IndicatorObject() self.old_base_time = base_time self.base_time = base_time self.mysql_connector = MysqlConnector() self.setPrice(base_time) def getHiLowPrice(self, base_time): # 直近2時間前〜1時間前の高値、安値を入れる # 一日で取りたい場合はSQLでフォローする start_time = base_time - timedelta(hours=2) end_time = base_time - timedelta(hours=1) sql = "select max(ask_price), max(bid_price) from %s_TABLE where insert_time > \'%s\' and insert_time < \'%s\'" % ( self.instrument, start_time, end_time) response = self.mysql_connector.select_sql(sql) for res in response: ask_price = res[0] bid_price = res[1] hi_price = (ask_price + bid_price) / 2 sql = "select min(ask_price), min(bid_price) from %s_TABLE where insert_time > \'%s\' and insert_time < \'%s\'" % ( self.instrument, start_time, end_time) response = self.mysql_connector.select_sql(sql) for res in response: ask_price = res[0] bid_price = res[1] min_price = (ask_price + bid_price) / 2 return hi_price, min_price def setBaseTime(self, base_time): self.base_time = base_time def getBaseTime(self): return self.base_time def setInitialPrice(self, base_time): logging.info("setInitialPrice Start") end_time = base_time.strftime("%Y-%m-%d %H:%M:%S") self.old_base_time = base_time sql = "select ask_price, bid_price, insert_time from %s_TABLE where insert_time < \'%s\' ORDER BY insert_time DESC limit %s" % ( self.instrument, end_time, self.time_width) response = self.mysql_connector.select_sql(sql) self.indicator_object.setPriceList(response) logging.info("setInitialPrice End") def addPrice(self, base_time): logging.info("addPrice Start") start_time = self.old_base_time.strftime("%Y-%m-%d %H:%M:%S") end_time = base_time.strftime("%Y-%m-%d %H:%M:%S") sql = "select ask_price, bid_price, insert_time from %s_TABLE where insert_time >= \'%s\' and insert_time < \'%s\'" % ( self.instrument, start_time, end_time) self.old_base_time = base_time logging.info(sql) logging.info("select SQL Start") response = self.mysql_connector.select_sql(sql) logging.info("select SQL END") self.indicator_object.addPriceList(response) logging.info("addPrice End") def setPrice(self, base_time): logging.info("setPrice Start") if len(self.indicator_object.getAskPriceList()) == 0: self.setInitialPrice(base_time) else: self.addPrice(base_time) logging.info("setPrice End") def calculatePollingTime(self, base_time, response, polling_time): flag = False if len(response) > 0: get_time = response[0][0] if base_time >= (get_time + timedelta(seconds=polling_time)): flag = True else: pass else: flag = True return flag def set1hIndicator(self, base_time): logging.info("set1hIndicator base_time = %s" % base_time) ask_price_list = self.indicator_object.getAskPriceList() bid_price_list = self.indicator_object.getBidPriceList() try: sql = "select insert_time from INDICATOR_TABLE where insert_time <= \'%s\' and type = \'bollinger1h3\' order by insert_time DESC limit 1" % base_time response = self.mysql_connector.select_sql(sql) polling_time = 3600 if self.calculatePollingTime(base_time, response, polling_time): # 1時間置きに実行 ind_type = "highlow" # 前日高値、安値の計算 if decideMarket(base_time - timedelta(hours=2)): high_price, low_price = self.getHiLowPrice(base_time) # instrument, type, high_price, low_price, insert_time sql = "insert into INDICATOR_TABLE(instrument, type, high_price, low_price, insert_time) values(\'%s\', \'%s\', %s, %s, \'%s\')" % ( self.instrument, ind_type, high_price, low_price, base_time) self.mysql_connector.insert_sql(sql) logging.info(sql) ind_type = "ewma1h200" wma_length = 200 candle_width = 3600 # 移動平均の取得(WMA200 1h) ewma200_1h = getEWMA(ask_price_list, bid_price_list, wma_length, candle_width) # instrument, type, ewma_value, insert_time sql = "insert into INDICATOR_TABLE(instrument, type, ewma_value, insert_time) values(\'%s\', \'%s\', %s, \'%s\')" % ( self.instrument, ind_type, ewma200_1h[-1], base_time) self.mysql_connector.insert_sql(sql) logging.info(sql) ind_type = "bollinger1h1" window_size = 28 candle_width = 3600 sigma_valiable = 1 data_set = getBollingerDataSet(ask_price_list, bid_price_list, window_size, sigma_valiable, candle_width) sql = "insert into INDICATOR_TABLE(instrument, type, upper_sigma, lower_sigma, base_line, insert_time) values(\'%s\', \'%s\', %s, %s, %s, \'%s\')" % ( self.instrument, ind_type, data_set["upper_sigmas"][-1], data_set["lower_sigmas"][-1], data_set["base_lines"][-1], base_time) self.mysql_connector.insert_sql(sql) logging.info(sql) ind_type = "bollinger1h2" window_size = 28 candle_width = 3600 sigma_valiable = 2 data_set = getBollingerDataSet(ask_price_list, bid_price_list, window_size, sigma_valiable, candle_width) sql = "insert into INDICATOR_TABLE(instrument, type, upper_sigma, lower_sigma, base_line, insert_time) values(\'%s\', \'%s\', %s, %s, %s, \'%s\')" % ( self.instrument, ind_type, data_set["upper_sigmas"][-1], data_set["lower_sigmas"][-1], data_set["base_lines"][-1], base_time) self.mysql_connector.insert_sql(sql) logging.info(sql) ind_type = "bollinger1h2.5" window_size = 28 candle_width = 3600 sigma_valiable = 2.5 data_set = getBollingerDataSet(ask_price_list, bid_price_list, window_size, sigma_valiable, candle_width) sql = "insert into INDICATOR_TABLE(instrument, type, upper_sigma, lower_sigma, base_line, insert_time) values(\'%s\', \'%s\', %s, %s, %s, \'%s\')" % ( self.instrument, ind_type, data_set["upper_sigmas"][-1], data_set["lower_sigmas"][-1], data_set["base_lines"][-1], base_time) self.mysql_connector.insert_sql(sql) logging.info(sql) ind_type = "bollinger1h3" window_size = 28 candle_width = 3600 sigma_valiable = 3 data_set = getBollingerDataSet(ask_price_list, bid_price_list, window_size, sigma_valiable, candle_width) sql = "insert into INDICATOR_TABLE(instrument, type, upper_sigma, lower_sigma, base_line, insert_time) values(\'%s\', \'%s\', %s, %s, %s, \'%s\')" % ( self.instrument, ind_type, data_set["upper_sigmas"][-1], data_set["lower_sigmas"][-1], data_set["base_lines"][-1], base_time) self.mysql_connector.insert_sql(sql) logging.info(sql) except Exception as e: logging.info(traceback.format_exc()) def set5mIndicator(self, base_time): ask_price_list = self.indicator_object.getAskPriceList() bid_price_list = self.indicator_object.getBidPriceList() try: logging.info("set5mIndicator base_time = %s" % base_time) # 1シグマボリンジャーバンドを取得する window_size = 28 candle_width = 300 sigma_valiable = 1 data_set = getBollingerDataSet(ask_price_list, bid_price_list, window_size, sigma_valiable, candle_width) # instrument, type, upper_sigma, lower_sigma, base_line, insert_time ind_type = "bollinger5m1" sql = "insert into INDICATOR_TABLE(instrument, type, upper_sigma, lower_sigma, base_line, insert_time) values(\'%s\', \'%s\', %s, %s, %s, \'%s\')" % ( self.instrument, ind_type, data_set["upper_sigmas"][-1], data_set["lower_sigmas"][-1], data_set["base_lines"][-1], base_time) self.mysql_connector.insert_sql(sql) logging.info(sql) # 2シグマボリンジャーバンドを取得する window_size = 28 candle_width = 300 sigma_valiable = 2 data_set = getBollingerDataSet(ask_price_list, bid_price_list, window_size, sigma_valiable, candle_width) # instrument, type, upper_sigma, lower_sigma, base_line, insert_time ind_type = "bollinger5m2" sql = "insert into INDICATOR_TABLE(instrument, type, upper_sigma, lower_sigma, base_line, insert_time) values(\'%s\', \'%s\', %s, %s, %s, \'%s\')" % ( self.instrument, ind_type, data_set["upper_sigmas"][-1], data_set["lower_sigmas"][-1], data_set["base_lines"][-1], base_time) self.mysql_connector.insert_sql(sql) logging.info(sql) # 2.5シグマボリンジャーバンドを取得する window_size = 28 candle_width = 300 sigma_valiable = 2.5 data_set = getBollingerDataSet(ask_price_list, bid_price_list, window_size, sigma_valiable, candle_width) # instrument, type, upper_sigma, lower_sigma, base_line, insert_time ind_type = "bollinger5m2.5" sql = "insert into INDICATOR_TABLE(instrument, type, upper_sigma, lower_sigma, base_line, insert_time) values(\'%s\', \'%s\', %s, %s, %s, \'%s\')" % ( self.instrument, ind_type, data_set["upper_sigmas"][-1], data_set["lower_sigmas"][-1], data_set["base_lines"][-1], base_time) self.mysql_connector.insert_sql(sql) logging.info(sql) # 3シグマボリンジャーバンドを取得する window_size = 28 candle_width = 300 sigma_valiable = 3 data_set = getBollingerDataSet(ask_price_list, bid_price_list, window_size, sigma_valiable, candle_width) # instrument, type, upper_sigma, lower_sigma, base_line, insert_time ind_type = "bollinger5m3" sql = "insert into INDICATOR_TABLE(instrument, type, upper_sigma, lower_sigma, base_line, insert_time) values(\'%s\', \'%s\', %s, %s, %s, \'%s\')" % ( self.instrument, ind_type, data_set["upper_sigmas"][-1], data_set["lower_sigmas"][-1], data_set["base_lines"][-1], base_time) self.mysql_connector.insert_sql(sql) logging.info(sql) # 移動平均の取得(WMA50 5m) wma_length = 50 candle_width = 300 ewma50 = getEWMA(ask_price_list, bid_price_list, wma_length, candle_width) # 短期トレンドの取得 slope_length = (10 * candle_width) * -1 slope_list = ewma50[slope_length:] slope = getSlope(slope_list) # instrument, type, ewma_value, insert_time ind_type = "ewma5m50" sql = "insert into INDICATOR_TABLE(instrument, type, ewma_value, slope, insert_time) values(\'%s\', \'%s\', %s, %s, \'%s\')" % ( self.instrument, ind_type, ewma50[-1], slope, base_time) self.mysql_connector.insert_sql(sql) logging.info(sql) # 移動平均の取得(WMA200 5m) wma_length = 200 candle_width = 300 ewma200 = getEWMA(ask_price_list, bid_price_list, wma_length, candle_width) # instrument, type, ewma_value, insert_time ind_type = "ewma5m200" sql = "insert into INDICATOR_TABLE(instrument, type, ewma_value, insert_time) values(\'%s\', \'%s\', %s, \'%s\')" % ( self.instrument, ind_type, ewma200[-1], base_time) self.mysql_connector.insert_sql(sql) logging.info(sql) except Exception as e: logging.info(traceback.format_exc()) def set1mIndicator(self, base_time): ask_price_list = self.indicator_object.getAskPriceList() bid_price_list = self.indicator_object.getBidPriceList() try: logging.info("set5mIndicator base_time = %s" % base_time) # 1シグマボリンジャーバンドを取得する window_size = 28 candle_width = 60 sigma_valiable = 1 data_set = getBollingerDataSet(ask_price_list, bid_price_list, window_size, sigma_valiable, candle_width) # instrument, type, upper_sigma, lower_sigma, base_line, insert_time ind_type = "bollinger1m1" sql = "insert into INDICATOR_TABLE(instrument, type, upper_sigma, lower_sigma, base_line, insert_time) values(\'%s\', \'%s\', %s, %s, %s, \'%s\')" % ( self.instrument, ind_type, data_set["upper_sigmas"][-1], data_set["lower_sigmas"][-1], data_set["base_lines"][-1], base_time) self.mysql_connector.insert_sql(sql) logging.info(sql) # 2シグマボリンジャーバンドを取得する window_size = 28 candle_width = 60 sigma_valiable = 2 data_set = getBollingerDataSet(ask_price_list, bid_price_list, window_size, sigma_valiable, candle_width) # instrument, type, upper_sigma, lower_sigma, base_line, insert_time ind_type = "bollinger1m2" sql = "insert into INDICATOR_TABLE(instrument, type, upper_sigma, lower_sigma, base_line, insert_time) values(\'%s\', \'%s\', %s, %s, %s, \'%s\')" % ( self.instrument, ind_type, data_set["upper_sigmas"][-1], data_set["lower_sigmas"][-1], data_set["base_lines"][-1], base_time) self.mysql_connector.insert_sql(sql) logging.info(sql) # 2.5シグマボリンジャーバンドを取得する window_size = 28 candle_width = 60 sigma_valiable = 2.5 data_set = getBollingerDataSet(ask_price_list, bid_price_list, window_size, sigma_valiable, candle_width) # instrument, type, upper_sigma, lower_sigma, base_line, insert_time ind_type = "bollinger1m2.5" sql = "insert into INDICATOR_TABLE(instrument, type, upper_sigma, lower_sigma, base_line, insert_time) values(\'%s\', \'%s\', %s, %s, %s, \'%s\')" % ( self.instrument, ind_type, data_set["upper_sigmas"][-1], data_set["lower_sigmas"][-1], data_set["base_lines"][-1], base_time) self.mysql_connector.insert_sql(sql) logging.info(sql) # 3シグマボリンジャーバンドを取得する window_size = 28 candle_width = 60 sigma_valiable = 3 data_set = getBollingerDataSet(ask_price_list, bid_price_list, window_size, sigma_valiable, candle_width) # instrument, type, upper_sigma, lower_sigma, base_line, insert_time ind_type = "bollinger1m3" sql = "insert into INDICATOR_TABLE(instrument, type, upper_sigma, lower_sigma, base_line, insert_time) values(\'%s\', \'%s\', %s, %s, %s, \'%s\')" % ( self.instrument, ind_type, data_set["upper_sigmas"][-1], data_set["lower_sigmas"][-1], data_set["base_lines"][-1], base_time) self.mysql_connector.insert_sql(sql) logging.info(sql) # 移動平均の取得(WMA50 5m) wma_length = 50 candle_width = 60 ewma50 = getEWMA(ask_price_list, bid_price_list, wma_length, candle_width) # 短期トレンドの取得 slope_length = (10 * candle_width) * -1 slope_list = ewma50[slope_length:] slope = getSlope(slope_list) # instrument, type, ewma_value, insert_time ind_type = "ewma1m50" sql = "insert into INDICATOR_TABLE(instrument, type, ewma_value, slope, insert_time) values(\'%s\', \'%s\', %s, %s, \'%s\')" % ( self.instrument, ind_type, ewma50[-1], slope, base_time) self.mysql_connector.insert_sql(sql) logging.info(sql) # 移動平均の取得(WMA200 5m) wma_length = 200 candle_width = 60 ewma200 = getEWMA(ask_price_list, bid_price_list, wma_length, candle_width) # instrument, type, ewma_value, insert_time ind_type = "ewma1m200" sql = "insert into INDICATOR_TABLE(instrument, type, ewma_value, insert_time) values(\'%s\', \'%s\', %s, \'%s\')" % ( self.instrument, ind_type, ewma200[-1], base_time) self.mysql_connector.insert_sql(sql) logging.info(sql) except Exception as e: logging.info(traceback.format_exc()) def computeInsertIndicator(self, base_time, span): if decideMarket(base_time): self.setPrice(base_time) try: if span == "1h": self.set1hIndicator(base_time) elif span == "5m": self.set5mIndicator(base_time) elif span == "1m": self.set1mIndicator(base_time) else: pass except Exception as e: logging.info(traceback.format_exc()) else: pass
class TrendReverseAlgo(SuperAlgo): def __init__(self, instrument, base_path, config_name, base_time): super(TrendReverseAlgo, self).__init__(instrument, base_path, config_name, base_time) self.base_price = 0 self.setPrice(base_time) self.debug_logger = getLogger("debug") self.result_logger = getLogger("result") self.mysql_connector = MysqlConnector() self.first_flag = self.config_data["first_trail_mode"] self.second_flag = self.config_data["second_trail_mode"] self.most_high_price = 0 self.most_low_price = 0 self.mode = "" self.buy_count = 0 self.buy_count_price = 0 self.sell_count = 0 self.sell_count_price = 0 self.original_stoploss_rate = 0 self.week_start_price = 0 self.count_threshold = 1 self.stoploss_flag = False self.algorithm = "" self.log_max_price = 0 self.log_min_price = 0 self.first_trade_flag = "pass" self.first_trade_time = base_time self.stl_first_flag = False self.buy_flag = False self.sell_flag = False self.stl_logic = "none" self.perfect_order_buycount = 0 self.perfect_order_sellcount = 0 self.setReverseIndicator(base_time) # decide trade entry timing def decideTrade(self, base_time): self.debug_logger.info("decide Trade Logic Start at %s" % base_time.strftime("%Y-%m-%d %H:%M:%S")) trade_flag = "pass" try: # if self.order_flag: # pass # else: if 1 == 1: weekday = base_time.weekday() hour = base_time.hour minutes = base_time.minute seconds = base_time.second current_price = self.getCurrentPrice() # if weekday == Saturday, we will have no entry. if weekday == 5 and hour >= 5: trade_flag = "pass" self.buy_count = 0 self.sell_count = 0 else: # if spread rate is greater than 0.5, we will have no entry if (self.ask_price - self.bid_price) >= 0.05: self.debug_logger.info( "spread logic: NG, ask=%s, bid=%s" % (self.ask_price, self.bid_price)) else: self.debug_logger.info( "spread logic: OK, ask=%s, bid=%s" % (self.ask_price, self.bid_price)) trade_flag = self.decideReverseTrade( trade_flag, current_price, base_time) if trade_flag != "pass" and self.order_flag: if trade_flag == "buy" and self.order_kind == "buy": trade_flag = "pass" elif trade_flag == "sell" and self.order_kind == "sell": trade_flag = "pass" else: self.stl_logic = "allovertheworld settlement" self.algorithm = self.algorithm + " by allovertheworld" return trade_flag except: raise # settlement logic def decideStl(self, base_time): self.debug_logger.info("decide Settle Logic Start at %s" % base_time.strftime("%Y-%m-%d %H:%M:%S")) try: stl_flag = False ex_stlmode = self.config_data["ex_stlmode"] if self.order_flag: if ex_stlmode == "on": weekday = base_time.weekday() hour = base_time.hour minutes = base_time.minute seconds = base_time.second current_price = self.getCurrentPrice() self.updatePrice(current_price) # if weekday == Saturday, we will settle one's position. if weekday == 5 and hour >= 5: self.result_logger.info("# weekend stl logic") stl_flag = True else: stl_flag = self.decideReverseStl(stl_flag, base_time) #stl_flag = self.decideTrailLogic(stl_flag, self.ask_price, self.bid_price, base_time) else: pass # self.writeDebugLog(base_time, mode="stl") return stl_flag except: raise def decideReverseStl(self, stl_flag, base_time): if self.order_flag: hour = base_time.hour minutes = base_time.minute seconds = base_time.second current_price = (self.ask_price + self.bid_price) / 2 self.setReverseIndicator(base_time) # if seconds < 10: # original_stoploss = 0.5 # #original_stoploss = 0.1 # if self.order_kind == "buy": # if (self.order_price - self.end_price_1m) > original_stoploss: # stl_flag = True # elif self.order_kind == "sell": # if (self.end_price_1m - self.order_price) > original_stoploss: # stl_flag = True # # if self.order_kind == "buy" and self.decidePerfectOrder() == "sell": # stl_flag = True # self.stl_logic = "reverse_perfect_order" # elif self.order_kind == "sell" and self.decidePerfectOrder() == "buy": # stl_flag = True # self.stl_logic = "reverse_perfect_order" # # if self.order_kind == "buy" and current_price > self.upper_sigma_1m25: # stl_flag = True # elif self.order_kind == "sell" and current_price < self.lower_sigma_1m25: # stl_flag = True return stl_flag def decideBollingerCrossOver(self, flag): state = False if flag == "upper": for i in range(0, len(self.upper_sigma_5m2_list)): if self.max_price_5m_list[i] > self.upper_sigma_5m2_list[i]: state = True elif flag == "lower": for i in range(0, len(self.lower_sigma_5m2_list)): if self.min_price_5m_list[i] < self.lower_sigma_5m2_list[i]: state = True else: raise return state def decidePerfectOrder(self): direct = "pass" if (self.sma5m20 > self.sma5m40 > self.sma5m80): self.debug_logger.info("perfect order logic: buy") direct = "buy" elif (self.sma5m20 < self.sma5m40 < self.sma5m80): self.debug_logger.info("perfect order logic: sell") direct = "sell" else: self.debug_logger.info("perfect order logic: NG") return direct def decideSma(self, sma_value, current_price): direct = "pass" if current_price > sma_value: self.debug_logger.info("sma logic: buy") direct = "buy" elif current_price < sma_value: self.debug_logger.info("sma logic: sell") direct = "sell" return direct def decideReverseTrade(self, trade_flag, current_price, base_time): # if trade_flag == "pass" and self.order_flag != True: # if trade_flag == "pass" and self.order_flag == False: if trade_flag == "pass": hour = base_time.hour minutes = base_time.minute seconds = base_time.second if seconds < 10 and hour != 6: self.setReverseIndicator(base_time) if 1 == 1: if 1 == 1: if 1 == 1: #if self.decidePerfectOrder() == self.decideSma(self.sma5m40, current_price) == "buy": if self.decidePerfectOrder() == "buy": self.debug_logger.info("first trade flag: buy") self.first_trade_flag = "buy" self.perfect_order_buycount = self.perfect_order_buycount + 1 self.first_trade_time = base_time #elif self.decidePerfectOrder() == self.decideSma(self.sma5m40, current_price) == "sell": elif self.decidePerfectOrder() == "sell": self.debug_logger.info("first trade flag: sell") self.perfect_order_sellcount = self.perfect_order_sellcount + 1 self.first_trade_flag = "sell" self.first_trade_time = base_time else: self.debug_logger.info("first trade flag: reset") self.first_trade_flag = "pass" self.sell_flag = False self.buy_flag = False self.perfect_order_buycount = 0 self.perfect_order_sellcount = 0 if 1 == 1: if self.first_trade_flag == "buy" and self.buy_flag == False and self.sell_flag == False: if current_price < self.lower_sigma_1m25: self.debug_logger.info( "current_price touch bollinger: lower") self.buy_flag = True elif self.first_trade_flag == "sell" and self.buy_flag == False and self.sell_flag == False: if current_price > self.upper_sigma_1m25: self.debug_logger.info( "current_price touch bollinger: upper") self.sell_flag = True if self.buy_flag and current_price > self.ewma40_1mvalue: self.debug_logger.info( "current_price touch bollinger: lower") trade_flag = "buy" elif self.sell_flag and current_price < self.ewma40_1mvalue: trade_flag = "sell" return trade_flag def updatePrice(self, current_price): if self.log_max_price == 0: self.log_max_price = current_price elif self.log_max_price < current_price: self.log_max_price = current_price if self.log_min_price == 0: self.log_min_price = current_price elif self.log_min_price > current_price: self.log_min_price = current_price # reset flag and valiables function after settlement def resetFlag(self): if self.order_kind == "buy": self.buy_count = 0 elif self.order_kind == "sell": self.sell_count = 0 self.mode = "" self.most_high_price = 0 self.most_low_price = 0 self.stoploss_flag = False #self.algorithm = "" self.log_max_price = 0 self.log_min_price = 0 self.first_trade_flag = "pass" self.stl_first_flag = False self.buy_flag = False self.sell_flag = False self.perfect_order_buycount = 0 self.perfect_order_sellcount = 0 self.stl_logic = "none" super(TrendReverseAlgo, self).resetFlag() def setReverseIndicator(self, base_time): target_time = base_time - timedelta(minutes=1) ### get 1m dataset # dataset = getBollingerWrapper(target_time, self.instrument, table_type="1m", window_size=28, connector=self.mysql_connector, sigma_valiable=2, length=0) # self.upper_sigma_1m2 = dataset["upper_sigmas"][-1] # self.lower_sigma_1m2 = dataset["lower_sigmas"][-1] # self.base_line_1m2 = dataset["base_lines"][-1] dataset = getBollingerWrapper(target_time, self.instrument, table_type="1m", window_size=28, connector=self.mysql_connector, sigma_valiable=2.5, length=0) self.upper_sigma_1m25 = dataset["upper_sigmas"][-1] self.lower_sigma_1m25 = dataset["lower_sigmas"][-1] self.base_line_1m25 = dataset["base_lines"][-1] # sql = "select start_price, end_price, max_price, min_price from %s_%s_TABLE where insert_time < \'%s\' order by insert_time desc limit 1" % (self.instrument, "1m", target_time) # response = self.mysql_connector.select_sql(sql) # self.start_price_1m = response[0][0] # self.end_price_1m = response[0][1] # self.max_price_1m = response[0][2] # self.min_price_1m = response[0][3] # dataset = getBollingerWrapper(target_time, self.instrument, table_type="1m", window_size=28, connector=self.mysql_connector, sigma_valiable=2, length=9) # base_line_1m2_list = dataset["base_lines"][-10:] # self.slope_1m = getSlope(base_line_1m2_list) target_time = base_time width = 60 * 40 sql = "select ask_price, bid_price from %s_TABLE where insert_time < \'%s\' order by insert_time desc limit %s" % ( self.instrument, target_time, width) response = self.mysql_connector.select_sql(sql) tmp = [] for res in response: tmp.append((res[0] + res[1]) / 2) tmp.reverse() ewma40_rawdata = tmp[-1 * 60 * 40:] self.ewma40_1mvalue = getEWMA(ewma40_rawdata, len(ewma40_rawdata))[-1] ### get 5m dataset target_time = base_time - timedelta(minutes=5) # width = 20 # sql = "select end_price from %s_%s_TABLE where insert_time < \'%s\' order by insert_time desc limit %s" % (self.instrument, "5m", target_time, width) # response = self.mysql_connector.select_sql(sql) # tmp = [] # for res in response: # tmp.append(res[0]) # tmp.reverse() # self.ewma20_5mvalue = getEWMA(tmp, len(tmp))[-1] # # dataset = getBollingerWrapper(target_time, self.instrument, table_type="5m", window_size=28, connector=self.mysql_connector, sigma_valiable=2, length=0) # self.base_line_5m2 = dataset["base_lines"][-1] # self.upper_sigma_5m2 = dataset["upper_sigmas"][-1] # self.lower_sigma_5m2 = dataset["lower_sigmas"][-1] dataset = getBollingerWrapper(target_time, self.instrument, table_type="5m", window_size=20, connector=self.mysql_connector, sigma_valiable=2, length=5) self.sma5m20 = dataset["base_lines"][-1] self.sma5m20_before = dataset["base_lines"][-5] dataset = getBollingerWrapper(target_time, self.instrument, table_type="5m", window_size=40, connector=self.mysql_connector, sigma_valiable=2, length=5) self.sma5m40 = dataset["base_lines"][-1] self.sma5m40_before = dataset["base_lines"][-5] dataset = getBollingerWrapper(target_time, self.instrument, table_type="5m", window_size=80, connector=self.mysql_connector, sigma_valiable=2, length=5) self.sma5m80 = dataset["base_lines"][-1] self.sma5m80_before = dataset["base_lines"][-5] self.sma5m20_slope = getSlope([self.sma5m20_before, self.sma5m20]) self.sma5m40_slope = getSlope([self.sma5m40_before, self.sma5m40]) self.sma5m80_slope = getSlope([self.sma5m80_before, self.sma5m80]) target_time = base_time - timedelta(hours=1) dataset = getBollingerWrapper(target_time, self.instrument, table_type="1h", window_size=80, connector=self.mysql_connector, sigma_valiable=2, length=0) self.sma1h80 = dataset["base_lines"][-1] def decideTrailLogic(self, stl_flag, current_ask_price, current_bid_price, base_time): minutes = base_time.minute seconds = base_time.second if minutes % 5 == 0 and seconds < 10: order_price = self.getOrderPrice() first_take_profit = 0.05 second_take_profit = 0.05 # update the most high and low price if self.most_high_price == 0 and self.most_low_price == 0: self.most_high_price = order_price self.most_low_price = order_price if self.most_high_price < current_bid_price: self.most_high_price = current_bid_price if self.most_low_price > current_ask_price: self.most_low_price = current_ask_price # first trailing stop logic if self.order_kind == "buy": if (current_bid_price - order_price) > first_take_profit: self.trail_flag = True elif self.order_kind == "sell": if (order_price - current_ask_price) > first_take_profit: self.trail_flag = True if self.trail_flag == True and self.order_kind == "buy": if (self.most_high_price - 0.02) > current_bid_price: self.result_logger.info("# Execute FirstTrail Stop") stl_flag = True elif self.trail_flag == True and self.order_kind == "sell": if (self.most_low_price + 0.02) < current_ask_price: self.result_logger.info("# Execute FirstTrail Stop") stl_flag = True # second trailing stop logic if self.order_kind == "buy": if (current_bid_price - order_price) > second_take_profit: self.trail_second_flag = True elif self.order_kind == "sell": if (order_price - current_ask_price) > second_take_profit: self.trail_second_flag = True if self.trail_second_flag == True and self.order_kind == "buy": if (self.most_high_price - 0.02) > current_bid_price: self.result_logger.info("# Execute SecondTrail Stop") stl_flag = True elif self.trail_second_flag == True and self.order_kind == "sell": if (self.most_low_price + 0.02) < current_ask_price: self.result_logger.info("# Execute SecondTrail Stop") stl_flag = True return stl_flag # write log function def writeDebugLog(self, base_time, mode): self.debug_logger.info("%s: %s Logic START" % (base_time, mode)) self.debug_logger.info("# self.start_price_1m=%s" % self.start_price_1m) self.debug_logger.info("# self.end_price_1m=%s" % self.end_price_1m) self.debug_logger.info("# self.slope_1m=%s" % self.slope_1m) self.debug_logger.info("#############################################") def entryLogWrite(self, base_time): self.result_logger.info( "#######################################################") self.result_logger.info("# in %s Algorithm" % self.algorithm) self.result_logger.info("# First trade time at %s" % self.first_trade_time) self.result_logger.info("# EXECUTE ORDER at %s" % base_time) self.result_logger.info("# ORDER_PRICE=%s, TRADE_FLAG=%s" % (self.order_price, self.order_kind)) self.result_logger.info("# self.upper_sigma_1m25=%s" % self.upper_sigma_1m25) self.result_logger.info("# self.sma5m20_slope=%s" % self.sma5m20_slope) self.result_logger.info("# self.sma5m40_slope=%s" % self.sma5m40_slope) self.result_logger.info("# self.sma5m80_slope=%s" % self.sma5m80_slope) self.result_logger.info("# self.sma5m20=%s" % self.sma5m20) self.result_logger.info("# self.sma5m40=%s" % self.sma5m40) self.result_logger.info("# self.sma5m80=%s" % self.sma5m80) self.result_logger.info("# self.sma1h80=%s" % self.sma1h80) self.result_logger.info("# self.perfect_order_buycount=%s" % self.perfect_order_buycount) self.result_logger.info("# self.perfect_order_sellcount=%s" % self.perfect_order_sellcount) def settlementLogWrite(self, profit, base_time, stl_price, stl_method): self.result_logger.info("# %s at %s" % (stl_method, base_time)) self.result_logger.info("# self.ask_price=%s" % self.ask_price) self.result_logger.info("# self.bid_price=%s" % self.bid_price) self.result_logger.info("# self.log_max_price=%s" % self.log_max_price) self.result_logger.info("# self.log_min_price=%s" % self.log_min_price) self.result_logger.info("# self.stl_logic=%s" % self.stl_logic) self.result_logger.info("# STL_PRICE=%s" % stl_price) self.result_logger.info("# PROFIT=%s" % profit)
# 実行スクリプトのパスを取得して、追加 current_path = os.path.abspath(os.path.dirname(__file__)) #current_path = "/.." sys.path.append(current_path) sys.path.append(current_path + "/trade_algorithm") sys.path.append(current_path + "/obj") sys.path.append(current_path + "/lib") from mysql_connector import MysqlConnector from common import decideMarket from common import getSlope target_time = "2017-09-01 00:00:00" target_time = datetime.strptime(target_time, "%Y-%m-%d %H:%M:%S") mysqlConnector = MysqlConnector() output_file = open("regression_test.txt", "w") while True: if decideMarket(target_time): trend_time_width = 5 instrument = "GBP_JPY" sql = "select ask_price from %s_TABLE where insert_time <= \'%s\' and insert_time like \'%%59:59\' order by insert_time desc limit 10" % ( instrument, target_time) response = mysqlConnector.select_sql(sql) base_line_list = [] for res in response: base_line_list.append(res[0]) base_line_list.reverse()
from db_wrapper import DBWrapper from oanda_wrapper import OandaWrapper from send_mail import SendMail from oandapy import oandapy import time # python extra_record_sql.py GBP_JPY[instrument] 2018-02-16[start_day] 00:00:00[start_time] args = sys.argv instrument = args[1] account_id = 4093685 token = 'e93bdc312be2c3e0a4a18f5718db237a-32ca3b9b94401fca447d4049ab046fad' env = 'live' mysql_connector = MysqlConnector() now = datetime.now() start_time = "2017-01-04 07:00:00" end_time = "2018-05-19 00:00:00" end_time = datetime.strptime(end_time, "%Y-%m-%d %H:%M:%S") start_time = datetime.strptime(start_time, "%Y-%m-%d %H:%M:%S") sql_file = open("%s_record.sql" % instrument, "w") while start_time < end_time: if decideMarket(start_time): start_ftime = start_time - timedelta(hours=9) start_ftime = start_ftime.strftime("%Y-%m-%dT%H:%M:%S") oanda = oandapy.API(environment=env, access_token=token)
def __init__(self): self.con = MysqlConnector()
class TrendReverseAlgo(SuperAlgo): def __init__(self, instrument, base_path, config_name, base_time): super(TrendReverseAlgo, self).__init__(instrument, base_path, config_name, base_time) self.base_price = 0 self.setPrice(base_time) self.debug_logger = getLogger("debug") self.result_logger = getLogger("result") self.mysql_connector = MysqlConnector() self.first_flag = self.config_data["first_trail_mode"] self.second_flag = self.config_data["second_trail_mode"] self.trail_third_flag = False self.most_high_price = 0 self.most_low_price = 0 self.mode = "" self.buy_count = 0 self.buy_count_price = 0 self.sell_count = 0 self.sell_count_price = 0 self.original_stoploss_rate = 0 self.week_start_price = 0 self.count_threshold = 1 self.stoploss_flag = False self.algorithm = "" self.log_max_price = 0 self.log_min_price = 0 self.first_trade_flag = "pass" self.second_trade_flag = False self.first_trade_time = base_time self.second_trade_time = base_time self.third_trade_flag = "pass" self.third_trade_time = base_time self.stl_first_flag = False self.buy_flag = False self.sell_flag = False self.stl_logic = "none" self.perfect_order_buycount = 0 self.perfect_order_sellcount = 0 self.setReverse5mIndicator(base_time) self.setReverse1hIndicator(base_time) self.setReverseDailyIndicator(base_time) # decide trade entry timing def decideTrade(self, base_time): self.debug_logger.info("decide Trade Logic Start at %s" % base_time.strftime("%Y-%m-%d %H:%M:%S")) trade_flag = "pass" try: # if self.order_flag: # pass # else: if 1 == 1: weekday = base_time.weekday() hour = base_time.hour minutes = base_time.minute seconds = base_time.second current_price = self.getCurrentPrice() # # if weekday == Saturday, we will have no entry. # if weekday == 5 and hour >= 5: # trade_flag = "pass" # self.buy_count = 0 # self.sell_count = 0 # # else: if 1 == 1: # if spread rate is greater than 0.5, we will have no entry if (self.ask_price - self.bid_price) >= 0.05: self.debug_logger.info( "spread logic: NG, ask=%s, bid=%s" % (self.ask_price, self.bid_price)) else: self.debug_logger.info( "spread logic: OK, ask=%s, bid=%s" % (self.ask_price, self.bid_price)) trade_flag = self.decideReverseTrade( trade_flag, current_price, base_time) if trade_flag != "pass" and self.order_flag: if trade_flag == "buy" and self.order_kind == "buy": trade_flag = "pass" elif trade_flag == "sell" and self.order_kind == "sell": trade_flag = "pass" else: self.stl_logic = "allovertheworld settlement" self.algorithm = self.algorithm + " by allovertheworld" return trade_flag except: raise # settlement logic def decideStl(self, base_time): self.debug_logger.info("decide Settle Logic Start at %s" % base_time.strftime("%Y-%m-%d %H:%M:%S")) try: stl_flag = False ex_stlmode = self.config_data["ex_stlmode"] if self.order_flag: if ex_stlmode == "on": weekday = base_time.weekday() hour = base_time.hour minutes = base_time.minute seconds = base_time.second current_price = self.getCurrentPrice() self.updatePrice(current_price) # if weekday == Saturday, we will settle one's position. # if weekday == 5 and hour >= 5: # self.result_logger.info("# weekend stl logic") # stl_flag = True # # else: if 1 == 1: # stl_flag = self.decideReverseStl(stl_flag, base_time) stl_flag = self.decideTrailLogic( stl_flag, self.ask_price, self.bid_price, base_time) else: pass # self.writeDebugLog(base_time, mode="stl") return stl_flag except: raise def decideReverseStl(self, stl_flag, base_time): if self.order_flag: hour = base_time.hour minutes = base_time.minute seconds = base_time.second current_price = (self.ask_price + self.bid_price) / 2 if seconds < 10 and minutes == 0: # self.setReverseIndicator(base_time) if self.order_kind == "buy" and self.sma1h20 < self.sma1h100: stl_flag = True elif self.order_kind == "sell" and self.sma1h20 > self.sma1h100: stl_flag = True current_price = (self.ask_price + self.bid_price) / 2 if self.order_kind == "buy" and current_price > self.upper_sigma_1h3: stl_flag = True elif self.order_kind == "sell" and current_price < self.lower_sigma_1h3: stl_flag = True # if seconds < 10: # original_stoploss = 0.5 # #original_stoploss = 0.1 # if self.order_kind == "buy": # if (self.order_price - self.end_price_1m) > original_stoploss: # stl_flag = True # elif self.order_kind == "sell": # if (self.end_price_1m - self.order_price) > original_stoploss: # stl_flag = True # # if self.order_kind == "buy" and self.decidePerfectOrder() == "sell": # stl_flag = True # self.stl_logic = "reverse_perfect_order" # elif self.order_kind == "sell" and self.decidePerfectOrder() == "buy": # stl_flag = True # self.stl_logic = "reverse_perfect_order" # # if self.order_kind == "buy" and current_price > self.upper_sigma_1m25: # stl_flag = True # elif self.order_kind == "sell" and current_price < self.lower_sigma_1m25: # stl_flag = True return stl_flag def decideBollingerCrossOver(self, flag): state = False if flag == "upper": for i in range(0, len(self.upper_sigma_5m2_list)): if self.max_price_5m_list[i] > self.upper_sigma_5m2_list[i]: state = True elif flag == "lower": for i in range(0, len(self.lower_sigma_5m2_list)): if self.min_price_5m_list[i] < self.lower_sigma_5m2_list[i]: state = True else: raise return state def decidePerfectOrder(self, span): direct = "pass" if span == "5m": if ( self.sma5m20 > self.sma5m40 > self.sma5m80 ) and self.sma5m20_slope > 0 and self.sma5m40_slope > 0 and self.sma5m80_slope > 0: direct = "buy" elif ( self.sma5m20 < self.sma5m40 < self.sma5m80 ) and self.sma5m20_slope < 0 and self.sma5m40_slope < 0 and self.sma5m80_slope < 0: direct = "sell" elif span == "1h": if ( self.sma1h20 > self.sma1h40 > self.sma1h80 ) and self.sma1h20_slope > 0 and self.sma1h40_slope > 0 and self.sma1h80_slope > 0: direct = "buy" elif ( self.sma1h20 < self.sma1h40 < self.sma1h80 ) and self.sma1h20_slope < 0 and self.sma1h40_slope < 0 and self.sma1h80_slope < 0: self.debug_logger.info("1h sell perfect_order") direct = "sell" return direct def decideSma(self, sma_value, current_price): direct = "pass" if current_price > sma_value: self.debug_logger.info("sma logic: buy") direct = "buy" elif current_price < sma_value: self.debug_logger.info("sma logic: sell") direct = "sell" return direct def decideEwma(self): flag = True if self.start_price_1d < self.end_price_1d: if self.start_price_1d < self.sma20_1d < self.end_price_1d: flag = False else: if self.end_price_1d < self.sma20_1d < self.start_price_1d: flag = False return flag def decideReverseTrade(self, trade_flag, current_price, base_time): if trade_flag == "pass": hour = base_time.hour minutes = base_time.minute seconds = base_time.second if seconds < 10 and minutes % 5 == 0: self.setReverse5mIndicator(base_time) if seconds < 10 and minutes == 0: self.setReverse1hIndicator(base_time) if seconds < 10 and hour == 7: self.setReverseDailyIndicator(base_time) if 1 == 1: if 1 == 1: if 1 == 1: if self.sma1h100 < self.sma1h20 and self.decidePerfectOrder( "1h" ) == "buy" and self.first_trade_flag != "buy" and self.order_kind != "buy": self.debug_logger.info( "first trade flag: buy at %s" % base_time) self.first_trade_flag = "buy" self.perfect_order_buycount = self.perfect_order_buycount + 1 self.first_trade_time = base_time self.first_trade_price = current_price self.second_trade_flag = False self.third_trade_flag = "pass" elif self.sma1h100 > self.sma1h20 and self.decidePerfectOrder( "1h" ) == "sell" and self.first_trade_flag != "sell" and self.order_kind != "sell": self.debug_logger.info( "first trade flag: sell at %s" % base_time) self.perfect_order_sellcount = self.perfect_order_sellcount + 1 self.first_trade_flag = "sell" self.first_trade_time = base_time self.first_trade_price = current_price self.second_trade_flag = False self.third_trade_flag = "pass" if 1 == 1: if self.first_trade_flag == "buy": if current_price < self.sma1h20 and self.second_trade_flag == False: self.debug_logger.info( "second trade flag: True at %s" % base_time) self.second_trade_time = base_time self.second_trade_flag = True self.second_trade_price = current_price elif self.first_trade_flag == "sell": if current_price > self.sma1h20 and self.second_trade_flag == False: self.debug_logger.info( "second trade flag: True at %s" % base_time) self.second_trade_time = base_time self.second_trade_flag = True self.second_trade_price = current_price if self.first_trade_flag == "buy" and self.second_trade_flag and self.decidePerfectOrder( "5m") == "buy": self.third_trade_flag = "buy" self.third_trade_time = base_time self.third_trade_price = current_price elif self.first_trade_flag == "sell" and self.second_trade_flag and self.decidePerfectOrder( "5m") == "sell": self.third_trade_flag = "sell" self.third_trade_time = base_time self.third_trade_price = current_price #if self.first_trade_flag == "buy" and self.second_trade_flag and self.third_trade_flag == "buy" and current_price < self.lower_sigma_5m25: if self.first_trade_flag == "buy" and self.second_trade_flag and self.third_trade_flag == "buy": self.first_trade_flag = "pass" self.second_trade_flag = False self.third_trade_flag = "pass" trade_flag = "buy" #elif self.first_trade_flag == "sell" and self.second_trade_flag and self.third_trade_flag == "sell" and current_price > self.upper_sigma_5m25: elif self.first_trade_flag == "sell" and self.second_trade_flag and self.third_trade_flag == "sell": self.first_trade_flag = "pass" self.second_trade_flag = False self.third_trade_flag = "pass" trade_flag = "sell" return trade_flag def updatePrice(self, current_price): if self.log_max_price == 0: self.log_max_price = current_price elif self.log_max_price < current_price: self.log_max_price = current_price if self.log_min_price == 0: self.log_min_price = current_price elif self.log_min_price > current_price: self.log_min_price = current_price # reset flag and valiables function after settlement def resetFlag(self): if self.order_kind == "buy": self.buy_count = 0 elif self.order_kind == "sell": self.sell_count = 0 self.mode = "" self.most_high_price = 0 self.most_low_price = 0 self.stoploss_flag = False #self.algorithm = "" self.log_max_price = 0 self.log_min_price = 0 #self.first_trade_flag = "pass" #self.second_trade_flag = False self.stl_first_flag = False self.buy_flag = False self.sell_flag = False self.perfect_order_buycount = 0 self.perfect_order_sellcount = 0 self.trail_third_flag = False self.stl_logic = "none" super(TrendReverseAlgo, self).resetFlag() def setReverse5mIndicator(self, base_time): ### get 5m dataset target_time = base_time - timedelta(minutes=5) dataset = getBollingerWrapper(target_time, self.instrument, table_type="5m", window_size=28, connector=self.mysql_connector, sigma_valiable=2.5, length=0) self.upper_sigma_5m25 = dataset["upper_sigmas"][-1] self.lower_sigma_5m25 = dataset["lower_sigmas"][-1] dataset = getBollingerWrapper(target_time, self.instrument, table_type="5m", window_size=20, connector=self.mysql_connector, sigma_valiable=2, length=5) self.sma5m20 = dataset["base_lines"][-1] self.sma5m20_before = dataset["base_lines"][-2] dataset = getBollingerWrapper(target_time, self.instrument, table_type="5m", window_size=40, connector=self.mysql_connector, sigma_valiable=2, length=5) self.sma5m40 = dataset["base_lines"][-1] self.sma5m40_before = dataset["base_lines"][-2] dataset = getBollingerWrapper(target_time, self.instrument, table_type="5m", window_size=80, connector=self.mysql_connector, sigma_valiable=2, length=5) self.sma5m80 = dataset["base_lines"][-1] self.sma5m80_before = dataset["base_lines"][-2] self.sma5m20_slope = getSlope([self.sma5m20_before, self.sma5m20]) self.sma5m40_slope = getSlope([self.sma5m40_before, self.sma5m40]) self.sma5m80_slope = getSlope([self.sma5m80_before, self.sma5m80]) def setReverse1hIndicator(self, base_time): ### get 1h dataset target_time = base_time - timedelta(hours=1) dataset = getBollingerWrapper(target_time, self.instrument, table_type="1h", window_size=28, connector=self.mysql_connector, sigma_valiable=3, length=0) self.upper_sigma_1h3 = dataset["upper_sigmas"][-1] self.lower_sigma_1h3 = dataset["lower_sigmas"][-1] dataset = getBollingerWrapper(target_time, self.instrument, table_type="1h", window_size=100, connector=self.mysql_connector, sigma_valiable=2, length=0) self.sma1h100 = dataset["base_lines"][-1] dataset = getBollingerWrapper(target_time, self.instrument, table_type="1h", window_size=20, connector=self.mysql_connector, sigma_valiable=2, length=1) self.sma1h20 = dataset["base_lines"][-1] self.sma1h20_before = dataset["base_lines"][-2] self.sma1h20_slope = getSlope([self.sma1h20_before, self.sma1h20]) dataset = getBollingerWrapper(target_time, self.instrument, table_type="1h", window_size=40, connector=self.mysql_connector, sigma_valiable=2, length=1) self.sma1h40 = dataset["base_lines"][-1] self.sma1h40_before = dataset["base_lines"][-2] self.sma1h40_slope = getSlope([self.sma1h40_before, self.sma1h40]) dataset = getBollingerWrapper(target_time, self.instrument, table_type="1h", window_size=80, connector=self.mysql_connector, sigma_valiable=2, length=1) self.sma1h80 = dataset["base_lines"][-1] self.sma1h80_before = dataset["base_lines"][-2] self.sma1h80_slope = getSlope([self.sma1h80_before, self.sma1h80]) def setReverseDailyIndicator(self, base_time): ### get daily dataset target_time = base_time - timedelta(days=1) sql = "select max_price, min_price, start_price, end_price from %s_%s_TABLE where insert_time < \'%s\' order by insert_time desc limit 1" % ( self.instrument, "day", target_time) response = self.mysql_connector.select_sql(sql) self.max_price_1d = response[0][0] self.min_price_1d = response[0][1] self.start_price_1d = response[0][2] self.end_price_1d = response[0][3] dataset = getBollingerWrapper(target_time, self.instrument, table_type="day", window_size=28, connector=self.mysql_connector, sigma_valiable=2, length=0) self.upper_sigma_1d2 = dataset["upper_sigmas"][-1] self.lower_sigma_1d2 = dataset["lower_sigmas"][-1] dataset = getBollingerWrapper(target_time, self.instrument, table_type="day", window_size=20, connector=self.mysql_connector, sigma_valiable=2, length=0) self.sma20_1d = dataset["base_lines"][-1] def decideTrailLogic(self, stl_flag, current_ask_price, current_bid_price, base_time): minutes = base_time.minute seconds = base_time.second if minutes % 5 == 0 and seconds < 10: order_price = self.getOrderPrice() first_take_profit = 1.0 second_take_profit = 2.0 third_take_profit = 3.0 # update the most high and low price if self.most_high_price == 0 and self.most_low_price == 0: self.most_high_price = order_price self.most_low_price = order_price if self.most_high_price < current_bid_price: self.most_high_price = current_bid_price if self.most_low_price > current_ask_price: self.most_low_price = current_ask_price # first trailing stop logic if self.order_kind == "buy": if (current_bid_price - order_price) > first_take_profit: self.trail_flag = True elif self.order_kind == "sell": if (order_price - current_ask_price) > first_take_profit: self.trail_flag = True if self.trail_flag == True and self.order_kind == "buy": if self.order_price > current_bid_price: self.result_logger.info("# Execute FirstTrail Stop") stl_flag = True elif self.trail_flag == True and self.order_kind == "sell": if self.order_price < current_ask_price: self.result_logger.info("# Execute FirstTrail Stop") stl_flag = True # second trailing stop logic if self.order_kind == "buy": if (current_bid_price - order_price) > second_take_profit: self.trail_second_flag = True elif self.order_kind == "sell": if (order_price - current_ask_price) > second_take_profit: self.trail_second_flag = True if self.trail_second_flag == True and self.order_kind == "buy": if (self.order_price + 1.0) > current_bid_price: self.result_logger.info("# Execute SecondTrail Stop") stl_flag = True elif self.trail_second_flag == True and self.order_kind == "sell": if (self.order_price + 1.0) < current_ask_price: self.result_logger.info("# Execute SecondTrail Stop") stl_flag = True # third trailing stop logic if self.order_kind == "buy": if (current_bid_price - order_price) > third_take_profit: self.trail_third_flag = True elif self.order_kind == "sell": if (order_price - current_ask_price) > third_take_profit: self.trail_third_flag = True if self.trail_third_flag == True and self.order_kind == "buy": if (self.order_price + 2.0) > current_bid_price: self.result_logger.info("# Execute SecondTrail Stop") stl_flag = True elif self.trail_third_flag == True and self.order_kind == "sell": if (self.order_price + 2.0) < current_ask_price: self.result_logger.info("# Execute SecondTrail Stop") stl_flag = True return stl_flag # write log function def writeDebugLog(self, base_time, mode): self.debug_logger.info("%s: %s Logic START" % (base_time, mode)) self.debug_logger.info("# self.start_price_1m=%s" % self.start_price_1m) self.debug_logger.info("# self.end_price_1m=%s" % self.end_price_1m) self.debug_logger.info("# self.slope_1m=%s" % self.slope_1m) self.debug_logger.info("#############################################") def entryLogWrite(self, base_time): self.result_logger.info( "#######################################################") self.result_logger.info("# in %s Algorithm" % self.algorithm) self.result_logger.info("# first_trade_time=%s" % self.first_trade_time) self.result_logger.info("# second_trade_time= %s" % self.second_trade_time) self.result_logger.info("# third_trade_time= %s" % self.third_trade_time) self.result_logger.info("# EXECUTE ORDER at %s" % base_time) self.result_logger.info("# first_trade_price=%s" % self.first_trade_price) self.result_logger.info("# second_trade_price= %s" % self.second_trade_price) self.result_logger.info("# third_trade_price= %s" % self.third_trade_price) self.result_logger.info("# ORDER_PRICE=%s, TRADE_FLAG=%s" % (self.order_price, self.order_kind)) self.result_logger.info("# self.upper_sigma_5m25=%s" % self.upper_sigma_5m25) self.result_logger.info("# self.lower_sigma_5m25=%s" % self.lower_sigma_5m25) self.result_logger.info("# self.upper_sigma_1h3=%s" % self.upper_sigma_1h3) self.result_logger.info("# self.lower_sigma_1h3=%s" % self.lower_sigma_1h3) self.result_logger.info("# self.sma5m20=%s" % self.sma5m20) self.result_logger.info("# self.sma5m40=%s" % self.sma5m40) self.result_logger.info("# self.sma5m80=%s" % self.sma5m80) self.result_logger.info("# self.sma5m20_slope=%s" % self.sma5m20_slope) self.result_logger.info("# self.sma5m40_slope=%s" % self.sma5m40_slope) self.result_logger.info("# self.sma5m80_slope=%s" % self.sma5m80_slope) self.result_logger.info("# self.sma1h100=%s" % self.sma1h100) self.result_logger.info("# self.sma1h20=%s" % self.sma1h20) self.result_logger.info("# self.sma1h40=%s" % self.sma1h40) self.result_logger.info("# self.sma1h80=%s" % self.sma1h80) self.result_logger.info("# self.sma1h20_slope=%s" % self.sma1h20_slope) self.result_logger.info("# self.sma1h40_slope=%s" % self.sma1h40_slope) self.result_logger.info("# self.sma1h80_slope=%s" % self.sma1h80_slope) def settlementLogWrite(self, profit, base_time, stl_price, stl_method): self.result_logger.info("# %s at %s" % (stl_method, base_time)) self.result_logger.info("# self.ask_price=%s" % self.ask_price) self.result_logger.info("# self.bid_price=%s" % self.bid_price) self.result_logger.info("# self.log_max_price=%s" % self.log_max_price) self.result_logger.info("# self.log_min_price=%s" % self.log_min_price) self.result_logger.info("# self.stl_logic=%s" % self.stl_logic) self.result_logger.info("# STL_PRICE=%s" % stl_price) self.result_logger.info("# PROFIT=%s" % profit)
class TrendReverseAlgo(SuperAlgo): def __init__(self, instrument, base_path, config_name, base_time): super(TrendReverseAlgo, self).__init__(instrument, base_path, config_name, base_time) self.base_price = 0 self.setPrice(base_time) self.debug_logger = getLogger("debug") self.result_logger = getLogger("result") self.mysql_connector = MysqlConnector() self.first_flag = self.config_data["first_trail_mode"] self.second_flag = self.config_data["second_trail_mode"] self.most_high_price = 0 self.most_low_price = 0 self.mode = "" self.buy_count = 0 self.buy_count_price = 0 self.sell_count = 0 self.sell_count_price = 0 self.original_stoploss_rate = 0 self.week_start_price = 0 self.count_threshold = 1 self.stoploss_flag = False self.algorithm = "" self.log_max_price = 0 self.log_min_price = 0 self.first_trade_flag = "pass" self.first_trade_time = base_time self.stl_first_flag = False self.buy_flag = False self.sell_flag = False self.setReverseIndicator(base_time) # decide trade entry timing def decideTrade(self, base_time): trade_flag = "pass" try: # if self.order_flag: # pass # else: if 1==1: weekday = base_time.weekday() hour = base_time.hour minutes = base_time.minute seconds = base_time.second current_price = self.getCurrentPrice() # if weekday == Saturday, we will have no entry. if weekday == 5 and hour >= 5: trade_flag = "pass" self.buy_count = 0 self.sell_count = 0 else: # if spread rate is greater than 0.5, we will have no entry if (self.ask_price - self.bid_price) >= 0.05: pass else: trade_flag = self.decideReverseTrade(trade_flag, current_price, base_time) self.debug_logger.info("# self.order_flag=%s" % self.order_flag) self.debug_logger.info("# trade_flag=%s" % trade_flag) if trade_flag != "pass" and self.order_flag: self.debug_logger.info("# allovertheworld logic") self.debug_logger.info("# trade_flag=%s" % trade_flag) self.debug_logger.info("# self.order_flag=%s" % self.order_flag) if trade_flag == "buy" and self.order_kind == "buy": trade_flag = "pass" elif trade_flag == "sell" and self.order_kind == "sell": trade_flag = "pass" else: self.algorithm = self.algorithm + " by allovertheworld" #self.result_logger.info("# execute all over the world at TrendReverseAlgo") self.writeDebugLog(base_time, mode="trade") return trade_flag except: raise # settlement logic def decideStl(self, base_time): try: stl_flag = False ex_stlmode = self.config_data["ex_stlmode"] if self.order_flag: if ex_stlmode == "on": weekday = base_time.weekday() hour = base_time.hour minutes = base_time.minute seconds = base_time.second current_price = self.getCurrentPrice() self.updatePrice(current_price) # if weekday == Saturday, we will settle one's position. if weekday == 5 and hour >= 5: self.result_logger.info("# weekend stl logic") stl_flag = True else: stl_flag = self.decideReverseStl(stl_flag, base_time) stl_flag = self.decideTrailLogic(stl_flag, self.ask_price, self.bid_price, base_time) else: pass self.writeDebugLog(base_time, mode="stl") return stl_flag except: raise def decideReverseStl(self, stl_flag, base_time): if self.order_flag: hour = base_time.hour minutes = base_time.minute seconds = base_time.second current_price = (self.ask_price + self.bid_price) / 2 self.setReverseIndicator(base_time) if seconds < 10: original_stoploss = 0.04 if self.order_kind == "buy": if (self.order_price - self.end_price_1m) > original_stoploss: stl_flag = True elif self.order_kind == "sell": if (self.end_price_1m - self.order_price) > original_stoploss: stl_flag = True # if self.order_kind == "buy" and self.max_price_1m > self.upper_sigma_1m2: # self.stl_first_flag = True # # elif self.order_kind == "sell" and self.min_price_1m < self.lower_sigma_1m2: # self.stl_first_flag = True # # if self.order_kind == "buy" and self.stl_first_flag: # #if self.end_price_1m < self.upper_sigma_1m2: # if self.end_price_1m < self.upper_sigma_1m15: # stl_flag = True # elif self.order_kind == "sell" and self.stl_first_flag: # #if self.end_price_1m > self.lower_sigma_1m2: # if self.end_price_1m > self.lower_sigma_1m15: # stl_flag = True return stl_flag def decideBollingerCrossOver(self, flag): state = False if flag == "upper": for i in range(0, len(self.upper_sigma_5m2_list)): if self.max_price_5m_list[i] > self.upper_sigma_5m2_list[i]: state = True elif flag == "lower": for i in range(0, len(self.lower_sigma_5m2_list)): if self.min_price_5m_list[i] < self.lower_sigma_5m2_list[i]: state = True else: raise return state def decideReverseTrade(self, trade_flag, current_price, base_time): # if trade_flag == "pass" and self.order_flag != True: if trade_flag == "pass" and self.order_flag == False: hour = base_time.hour minutes = base_time.minute seconds = base_time.second if seconds < 10 and hour != 6: self.setReverseIndicator(base_time) if 1 == 1: if 1 == 1: if self.first_trade_flag == "pass": if self.slope_1m > 0 and current_price > self.ewma20_5mvalue and current_price < self.ewma50_1mvalue and (self.ask_price - self.bid_price) < 0.01 and (self.upper_sigma_5m2 - self.lower_sigma_5m2) > 0.1: self.first_trade_flag = "buy" self.first_trade_time = base_time elif self.slope_1m < 0 and current_price < self.ewma20_5mvalue and current_price > self.ewma50_1mvalue and (self.ask_price - self.bid_price) < 0.01 and (self.upper_sigma_5m2 - self.lower_sigma_5m2) > 0.1: self.first_trade_flag = "sell" self.first_trade_time = base_time if seconds < 10: #if self.first_trade_flag == "buy" and self.first_trade_time + timedelta(minutes=1) < base_time: if self.first_trade_flag == "buy": if self.end_price_1m > self.ewma30_1mvalue: trade_flag = "buy" self.algorithm = "crossover_base_line" #elif self.first_trade_flag == "sell" and self.first_trade_time + timedelta(minutes=1) < base_time: elif self.first_trade_flag == "sell": if self.end_price_1m < self.ewma30_1mvalue: trade_flag = "sell" self.algorithm = "crossover_base_line" if seconds < 10: if self.end_price_5m_list[0] < self.ewma20_5mvalue and self.end_price_5m_list[-1] > self.ewma20_5mvalue: trade_flag = "buy" self.algorithm = "ewma5m_break" elif self.end_price_5m_list[0] > self.ewma20_5mvalue and self.end_price_5m_list[-1] < self.ewma20_5mvalue: trade_flag = "sell" self.algorithm = "ewma5m_break" if self.first_trade_time + timedelta(minutes=10) < base_time and self.first_trade_flag != "pass": self.debug_logger.info("reset first trade time: %s" % self.first_trade_time) self.debug_logger.info("reset first trade flag: %s" % self.first_trade_flag) self.debug_logger.info("reset first trade: %s" % base_time) self.resetFlag() return trade_flag def updatePrice(self, current_price): if self.log_max_price == 0: self.log_max_price = current_price elif self.log_max_price < current_price: self.log_max_price = current_price if self.log_min_price == 0: self.log_min_price = current_price elif self.log_min_price > current_price: self.log_min_price = current_price # reset flag and valiables function after settlement def resetFlag(self): if self.order_kind == "buy": self.buy_count = 0 elif self.order_kind == "sell": self.sell_count = 0 self.mode = "" self.most_high_price = 0 self.most_low_price = 0 self.stoploss_flag = False #self.algorithm = "" self.log_max_price = 0 self.log_min_price = 0 self.first_trade_flag = "pass" self.stl_first_flag = False self.buy_flag = False self.sell_flag = False super(TrendReverseAlgo, self).resetFlag() def setReverseIndicator(self, base_time): target_time = base_time - timedelta(minutes=1) ### get 1m dataset dataset = getBollingerWrapper(target_time, self.instrument, table_type="1m", window_size=28, connector=self.mysql_connector, sigma_valiable=2, length=0) self.upper_sigma_1m2 = dataset["upper_sigmas"][-1] self.lower_sigma_1m2 = dataset["lower_sigmas"][-1] self.base_line_1m2 = dataset["base_lines"][-1] sql = "select start_price, end_price, max_price, min_price from %s_%s_TABLE where insert_time < \'%s\' order by insert_time desc limit 2" % (self.instrument, "1m", target_time) response = self.mysql_connector.select_sql(sql) self.start_price_1m = response[0][0] self.end_price_1m = response[0][1] self.max_price_1m = response[0][2] self.min_price_1m = response[0][3] dataset = getBollingerWrapper(target_time, self.instrument, table_type="1m", window_size=28, connector=self.mysql_connector, sigma_valiable=2, length=9) base_line_1m2_list = dataset["base_lines"][-10:] self.slope_1m = getSlope(base_line_1m2_list) target_time = base_time width = 60*30 sql = "select ask_price, bid_price from %s_TABLE where insert_time < \'%s\' order by insert_time desc limit %s" % (self.instrument, target_time, width) response = self.mysql_connector.select_sql(sql) tmp = [] for res in response: tmp.append((res[0]+res[1])/2) tmp.reverse() ewma30_rawdata = tmp[-1*width:] self.ewma30_1mvalue = getEWMA(ewma30_rawdata, len(ewma30_rawdata))[-1] width = 60*50 sql = "select ask_price, bid_price from %s_TABLE where insert_time < \'%s\' order by insert_time desc limit %s" % (self.instrument, target_time, width) response = self.mysql_connector.select_sql(sql) tmp = [] for res in response: tmp.append((res[0]+res[1])/2) tmp.reverse() ewma50_rawdata = tmp[-1*width:] self.ewma50_1mvalue = getEWMA(ewma50_rawdata, len(ewma50_rawdata))[-1] ### get 5m dataset target_time = base_time - timedelta(minutes=5) sql = "select start_price, end_price, max_price, min_price from %s_%s_TABLE where insert_time < \'%s\' order by insert_time desc limit 2" % (self.instrument, "5m", target_time) response = self.mysql_connector.select_sql(sql) self.start_price_5m_list = [response[1][0], response[0][0]] self.end_price_5m_list = [response[1][1], response[0][1]] self.max_price_5m_list = [response[1][2], response[0][2]] self.min_price_5m_list = [response[1][3], response[0][3]] width = 20 sql = "select end_price from %s_%s_TABLE where insert_time < \'%s\' order by insert_time desc limit %s" % (self.instrument, "5m", target_time, width) response = self.mysql_connector.select_sql(sql) tmp = [] for res in response: tmp.append(res[0]) tmp.reverse() self.ewma20_5mvalue = getEWMA(tmp, len(tmp))[-1] dataset = getBollingerWrapper(target_time, self.instrument, table_type="5m", window_size=28, connector=self.mysql_connector, sigma_valiable=2, length=0) self.base_line_5m2 = dataset["base_lines"][-1] self.upper_sigma_5m2 = dataset["upper_sigmas"][-1] self.lower_sigma_5m2 = dataset["lower_sigmas"][-1] def decideTrailLogic(self, stl_flag, current_ask_price, current_bid_price, base_time): minutes = base_time.minute seconds = base_time.second if minutes % 5 == 0 and seconds < 10: order_price = self.getOrderPrice() first_take_profit = 0.03 second_take_profit = 1.0 # update the most high and low price if self.most_high_price == 0 and self.most_low_price == 0: self.most_high_price = order_price self.most_low_price = order_price if self.most_high_price < current_bid_price: self.most_high_price = current_bid_price if self.most_low_price > current_ask_price: self.most_low_price = current_ask_price # first trailing stop logic if self.order_kind == "buy": if (current_bid_price - order_price) > first_take_profit: self.trail_flag = True elif self.order_kind == "sell": if (order_price - current_ask_price) > first_take_profit: self.trail_flag = True if self.trail_flag == True and self.order_kind == "buy": if (self.most_high_price - 0.03) > current_bid_price: self.result_logger.info("# Execute FirstTrail Stop") stl_flag = True elif self.trail_flag == True and self.order_kind == "sell": if (self.most_low_price + 0.03) < current_ask_price : self.result_logger.info("# Execute FirstTrail Stop") stl_flag = True # second trailing stop logic if self.order_kind == "buy": if (current_bid_price - order_price) > second_take_profit: self.trail_second_flag = True elif self.order_kind == "sell": if (order_price - current_ask_price) > second_take_profit: self.trail_second_flag = True if self.trail_second_flag == True and self.order_kind == "buy": if (self.most_high_price - 0.3) > current_bid_price: self.result_logger.info("# Execute SecondTrail Stop") stl_flag = True elif self.trail_second_flag == True and self.order_kind == "sell": if (self.most_low_price + 0.3) < current_ask_price : self.result_logger.info("# Execute SecondTrail Stop") stl_flag = True return stl_flag # write log function def writeDebugLog(self, base_time, mode): self.debug_logger.info("%s: %s Logic START" % (base_time, mode)) self.debug_logger.info("# self.start_price_1m=%s" % self.start_price_1m) self.debug_logger.info("# self.end_price_1m=%s" % self.end_price_1m) self.debug_logger.info("# self.slope_1m=%s" % self.slope_1m) self.debug_logger.info("#############################################") def entryLogWrite(self, base_time): self.result_logger.info("#######################################################") self.result_logger.info("# in %s Algorithm" % self.algorithm) self.result_logger.info("# First trade time at %s" % self.first_trade_time) self.result_logger.info("# EXECUTE ORDER at %s" % base_time) self.result_logger.info("# ORDER_PRICE=%s, TRADE_FLAG=%s" % (self.order_price, self.order_kind)) self.result_logger.info("# self.slope_1m=%s" % self.slope_1m) self.result_logger.info("# self.upper_sigma_1m2=%s" % self.upper_sigma_1m2) self.result_logger.info("# self.lower_sigma_1m2=%s" % self.lower_sigma_1m2) self.result_logger.info("# self.start_price_1m=%s" % self.start_price_1m) self.result_logger.info("# self.end_price_1m=%s" % self.end_price_1m) self.result_logger.info("# self.max_price_1m=%s" % self.max_price_1m) self.result_logger.info("# self.min_price_1m=%s" % self.min_price_1m) self.result_logger.info("# self.ewma20_1mvalue=%s" % self.ewma20_1mvalue) def settlementLogWrite(self, profit, base_time, stl_price, stl_method): self.result_logger.info("# %s at %s" % (stl_method, base_time)) self.result_logger.info("# self.ask_price=%s" % self.ask_price) self.result_logger.info("# self.bid_price=%s" % self.bid_price) self.result_logger.info("# self.log_max_price=%s" % self.log_max_price) self.result_logger.info("# self.log_min_price=%s" % self.log_min_price) self.result_logger.info("# STL_PRICE=%s" % stl_price) self.result_logger.info("# PROFIT=%s" % profit)
class MultiAlgo(SuperAlgo): def __init__(self, instrument, base_path, config_name, base_time): super(MultiAlgo, self).__init__(instrument, base_path, config_name, base_time) self.base_price = 0 self.setPrice(base_time) self.debug_logger = getLogger("debug") self.result_logger = getLogger("result") self.mysql_connector = MysqlConnector() self.first_flag = self.config_data["first_trail_mode"] self.second_flag = self.config_data["second_trail_mode"] self.most_high_price = 0 self.most_low_price = 0 self.mode = "" self.buy_count = 0 self.buy_count_price = 0 self.sell_count = 0 self.sell_count_price = 0 self.original_stoploss_rate = 0 self.week_start_price = 0 self.count_threshold = 1 self.stoploss_flag = False self.algorithm = "" self.log_max_price = 0 self.log_min_price = 0 self.first_flag = "pass" self.second_flag = "pass" self.first_flag_time = None self.second_flag_time = None self.setExpantionIndicator(base_time) #self.setVolatilityIndicator(base_time) self.setDailyIndicator(base_time) # decide trade entry timing def decideTrade(self, base_time): trade_flag = "pass" try: if self.order_flag: pass else: # if 1 == 1: weekday = base_time.weekday() hour = base_time.hour minutes = base_time.minute seconds = base_time.second current_price = self.getCurrentPrice() if hour == 7 and minutes == 0 and seconds < 10: self.setDailyIndicator(base_time) # if weekday == Saturday, we will have no entry. if weekday == 5 and hour >= 5: trade_flag = "pass" self.buy_count = 0 self.sell_count = 0 else: # if spread rate is greater than 0.5, we will have no entry if (self.ask_price - self.bid_price) >= 0.5: pass else: if 1==1: trade_flag = self.decideExpantionTrade(trade_flag, current_price, base_time) if trade_flag != "pass" and self.order_flag: if trade_flag == "buy" and self.order_kind == "buy": trade_flag = "pass" elif trade_flag == "sell" and self.order_kind == "sell": trade_flag = "pass" else: self.result_logger.info("# execute all over the world at MultiAlgo") self.writeDebugLog(base_time, mode="trade") return trade_flag except: raise # settlement logic def decideStl(self, base_time): try: stl_flag = False ex_stlmode = self.config_data["ex_stlmode"] if self.order_flag: if ex_stlmode == "on": weekday = base_time.weekday() hour = base_time.hour minutes = base_time.minute seconds = base_time.second current_price = self.getCurrentPrice() self.updatePrice(current_price) if hour == 7 and minutes == 0 and seconds < 10: self.setDailyIndicator(base_time) # if weekday == Saturday, we will settle one's position. if weekday == 5 and hour >= 5: self.result_logger.info("# weekend stl logic") stl_flag = True else: stl_flag = self.decideCommonStoploss(stl_flag, current_price, base_time) stl_flag = self.decideExpantionStoploss(stl_flag, current_price, base_time) stl_flag = self.decideTrailLogic(stl_flag, self.ask_price, self.bid_price, base_time) else: pass self.writeDebugLog(base_time, mode="stl") return stl_flag except: raise def decideHighLowPrice(self, current_price, exceed_th, surplus_th, high_price, low_price, mode): flag = False if mode == "buy": if current_price > (high_price + exceed_th): flag = True elif current_price < (high_price - surplus_th): flag = True elif mode == "sell": if current_price < (low_price - exceed_th): flag = True elif current_price > (low_price + surplus_th): flag = True return flag # cannot allovertheworld def decideExpantionTrade(self, trade_flag, current_price, base_time): if trade_flag == "pass": hour = base_time.hour minutes = base_time.minute seconds = base_time.second expantion_timelimit = 3 # 3hours #if (hour == 8 or hour == 9 or hour == 10 or hour == 12 or hour == 13 or hour == 14 or hour == 15 or hour == 16 or hour == 17 or hour == 19 or hour == 20 or hour == 21) and (minutes == 59) and seconds < 10: if (hour == 12 or hour == 13 or hour == 14 or hour == 19) and (minutes == 59) and seconds < 10: self.setExpantionIndicator(base_time) if self.sma20_1h > self.sma40_1h > self.sma80_1h: trade_flag = "buy" self.algorithm = "expantion" self.entry_time = base_time elif self.sma20_1h < self.sma40_1h < self.sma80_1h: trade_flag = "sell" self.algorithm = "expantion" self.entry_time = base_time self.setExpantionStoploss(trade_flag) return trade_flag def setExpantionStoploss(self, trade_flag): # if trade_flag != "pass" and self.algorithm == "expantion": if trade_flag != "pass": if trade_flag == "buy" and self.daily_slope > 0: self.original_stoploss_rate = 0.2 elif trade_flag == "buy" and self.daily_slope < 0: self.original_stoploss_rate = 0.2 elif trade_flag == "sell" and self.daily_slope < 0: self.original_stoploss_rate = 0.2 elif trade_flag == "sell" and self.daily_slope > 0: self.original_stoploss_rate = 0.2 def decideCommonStoploss(self, stl_flag, current_price, base_time): if self.algorithm == "expantion" or self.algorithm == "volatility" or self.algorithm == "reverse": minutes = base_time.minute seconds = base_time.second if minutes % 5 == 0 and seconds < 10: if self.order_kind == "buy": if (self.order_price - self.bid_price) > self.original_stoploss_rate: self.result_logger.info("# execute common stoploss") stl_flag = True elif self.order_kind == "sell": if (self.ask_price - self.order_price) > self.original_stoploss_rate: self.result_logger.info("# execute common stoploss") stl_flag = True return stl_flag def decideTouchBollinger(self, mode): flag = False if mode == "upper": for i in range(0, len(self.upper_sigma_5m2_list)): if self.end_price_5m_list[i] > self.upper_sigma_5m2_list[i]: flag = True elif mode == "lower": for i in range(0, len(self.lower_sigma_5m2_list)): if self.end_price_5m_list[i] < self.lower_sigma_5m2_list[i]: flag = True return flag def decideExpantionStoploss(self, stl_flag, current_price, base_time): target_time = self.entry_time + timedelta(minutes=11) base_ftime = base_time.strftime("%Y-%m-%d %H:%M:00") target_ftime = target_time.strftime("%Y-%m-%d %H:%M:00") seconds = base_time.second if target_ftime == base_ftime and seconds < 10: self.debug_logger.info("target_time=%s" % target_time) self.debug_logger.info("base_time=%s" % base_time) self.setExpantionIndicator(base_time) if self.order_kind == "buy" and self.decideTouchBollinger("upper") == False: self.result_logger.info("bollinger stoploss") stl_flag = True elif self.order_kind == "sell" and self.decideTouchBollinger("lower") == False: self.result_logger.info("bollinger stoploss") stl_flag = True # target_time = self.entry_time + timedelta(hours=3) # # if base_time > target_time: # if self.order_kind == "buy" and self.sma20_1h > current_price: # self.result_logger.info("sma20 stoploss") # stl_flag = True # elif self.order_kind == "sell" and self.sma20_1h < current_price: # self.result_logger.info("sma20 stoploss") # stl_flag = True return stl_flag def updatePrice(self, current_price): if self.log_max_price == 0: self.log_max_price = current_price elif self.log_max_price < current_price: self.log_max_price = current_price if self.log_min_price == 0: self.log_min_price = current_price elif self.log_min_price > current_price: self.log_min_price = current_price # trail settlement function def decideTrailLogic(self, stl_flag, current_ask_price, current_bid_price, base_time): minutes = base_time.minute seconds = base_time.second if minutes % 5 == 0 and seconds < 10: order_price = self.getOrderPrice() first_take_profit = 0.5 second_take_profit = 1.0 # update the most high and low price if self.most_high_price == 0 and self.most_low_price == 0: self.most_high_price = order_price self.most_low_price = order_price if self.most_high_price < current_bid_price: self.most_high_price = current_bid_price if self.most_low_price > current_ask_price: self.most_low_price = current_ask_price # first trailing stop logic if self.order_kind == "buy": if (current_bid_price - order_price) > first_take_profit: self.trail_flag = True elif self.order_kind == "sell": if (order_price - current_ask_price) > first_take_profit: self.trail_flag = True if self.trail_flag == True and self.order_kind == "buy": if (self.most_high_price - 0.3) > current_bid_price: self.result_logger.info("# Execute FirstTrail Stop") stl_flag = True elif self.trail_flag == True and self.order_kind == "sell": if (self.most_low_price + 0.3) < current_ask_price : self.result_logger.info("# Execute FirstTrail Stop") stl_flag = True # second trailing stop logic if self.order_kind == "buy": if (current_bid_price - order_price) > second_take_profit: self.trail_second_flag = True elif self.order_kind == "sell": if (order_price - current_ask_price) > second_take_profit: self.trail_second_flag = True if self.trail_second_flag == True and self.order_kind == "buy": if (self.most_high_price - 0.3) > current_bid_price: self.result_logger.info("# Execute SecondTrail Stop") stl_flag = True elif self.trail_second_flag == True and self.order_kind == "sell": if (self.most_low_price + 0.3) < current_ask_price : self.result_logger.info("# Execute SecondTrail Stop") stl_flag = True return stl_flag # reset flag and valiables function after settlement def resetFlag(self): self.most_high_price = 0 self.most_low_price = 0 self.stoploss_flag = False self.algorithm = "" self.log_max_price = 0 self.log_min_price = 0 self.first_flag = "pass" self.second_flag = "pass" self.first_flag_time = None self.second_flag_time = None super(MultiAlgo, self).resetFlag() # set Indicator dataset function def getDailySlope(self, instrument, target_time, span, connector): table_type = "day" sql = "select end_price from %s_%s_TABLE where insert_time < \'%s\' order by insert_time desc limit %s" % (instrument, table_type, target_time, span) response = connector.select_sql(sql) price_list = [] for res in response: price_list.append(res[0]) price_list.reverse() slope = getSlope(price_list) return slope def setExpantionIndicator(self, base_time): # set dataset 5minutes target_time = base_time - timedelta(minutes=5) # set 5m 2sigma bollinger band dataset = getBollingerWrapper(target_time, self.instrument, table_type="5m", window_size=28, connector=self.mysql_connector, sigma_valiable=2, length=1) self.upper_sigma_5m2_list = dataset["upper_sigmas"][-2:] self.lower_sigma_5m2_list = dataset["lower_sigmas"][-2:] self.base_line_5m2_list = dataset["base_lines"][-2:] # set 5m 3sigma bollinger band dataset = getBollingerWrapper(target_time, self.instrument, table_type="5m", window_size=28, connector=self.mysql_connector, sigma_valiable=3, length=1) self.upper_sigma_5m3_list = dataset["upper_sigmas"][-2:] self.lower_sigma_5m3_list = dataset["lower_sigmas"][-2:] self.base_line_5m3_list = dataset["base_lines"][-2:] # set 5m end price list sql = "select end_price from %s_%s_TABLE where insert_time < \'%s\' order by insert_time desc limit 2" % (self.instrument, "5m", target_time) response = self.mysql_connector.select_sql(sql) tmp = [] for res in response: tmp.append(res[0]) tmp.reverse() self.end_price_5m_list = tmp # set dataset 1hour target_time = base_time - timedelta(hours=1) # set 1h end price list sql = "select end_price from %s_%s_TABLE where insert_time < \'%s\' order by insert_time desc limit 80" % (self.instrument, "1h", target_time) response = self.mysql_connector.select_sql(sql) tmp = [] for res in response: tmp.append(res[0]) tmp.reverse() self.end_price_1h_list = tmp self.sma20_1h = sum(self.end_price_1h_list[-20:])/len(self.end_price_1h_list[-20:]) self.sma40_1h = sum(self.end_price_1h_list[-40:])/len(self.end_price_1h_list[-40:]) self.sma80_1h = sum(self.end_price_1h_list[-80:])/len(self.end_price_1h_list[-80:]) def setVolatilityIndicator(self, base_time): target_time = base_time - timedelta(minutes=5) sql = "select start_price, end_price from %s_%s_TABLE where insert_time < \'%s\' order by insert_time desc limit 1" % (self.instrument, "5m", target_time) response = self.mysql_connector.select_sql(sql) self.start_price_1m = response[0][0] self.end_price_1m = response[0][1] def setDailyIndicator(self, base_time): target_time = base_time - timedelta(days=1) self.daily_slope = self.getDailySlope(self.instrument, target_time, span=10, connector=self.mysql_connector) sql = "select max_price, min_price from %s_%s_TABLE where insert_time < \'%s\' order by insert_time desc limit 1" % (self.instrument, "day", target_time) response = self.mysql_connector.select_sql(sql) self.high_price = response[0][0] self.low_price = response[0][1] # write log function def writeDebugLog(self, base_time, mode): pass # self.debug_logger.info("%s: %s Logic START" % (base_time, mode)) # self.debug_logger.info("# self.buy_count=%s" % self.buy_count) # self.debug_logger.info("# self.sell_count=%s" % self.sell_count) # self.debug_logger.info("# self.daily_slope=%s" % self.daily_slope) # self.debug_logger.info("# self.upper_sigma_1h3=%s" % self.upper_sigma_1h3) # self.debug_logger.info("# self.lower_sigma_1h3=%s" % self.lower_sigma_1h3) # self.debug_logger.info("# self.upper_sigma_5m3=%s" % self.upper_sigma_5m3) # self.debug_logger.info("# self.lower_sigma_5m3=%s" % self.lower_sigma_5m3) # self.debug_logger.info("# self.start_price_5m=%s" % self.start_price_5m) # self.debug_logger.info("# self.end_price_5m=%s" % self.end_price_5m) # self.debug_logger.info("# self.start_price_1m=%s" % self.start_price_1m) # self.debug_logger.info("# self.end_price_1m=%s" % self.end_price_1m) # self.debug_logger.info("#############################################") def entryLogWrite(self, base_time): self.result_logger.info("#######################################################") self.result_logger.info("# in %s Algorithm" % self.algorithm) self.result_logger.info("# EXECUTE ORDER at %s" % base_time) self.result_logger.info("# ORDER_PRICE=%s, TRADE_FLAG=%s" % (self.order_price, self.order_kind)) self.result_logger.info("# self.daily_slope=%s" % self.daily_slope) self.result_logger.info("# self.upper_sigma_5m2=%s" % self.upper_sigma_5m2_list[-1]) self.result_logger.info("# self.lower_sigma_5m2=%s" % self.lower_sigma_5m2_list[-1]) self.result_logger.info("# self.sma20_1h=%s" % self.sma20_1h) self.result_logger.info("# self.sma40_1h=%s" % self.sma40_1h) self.result_logger.info("# self.sma80_1h=%s" % self.sma80_1h) def settlementLogWrite(self, profit, base_time, stl_price, stl_method): self.result_logger.info("# self.log_max_price=%s" % self.log_max_price) self.result_logger.info("# self.log_min_price=%s" % self.log_min_price) self.result_logger.info("# EXECUTE SETTLEMENT at %s" % base_time) self.result_logger.info("# STL_PRICE=%s" % stl_price) self.result_logger.info("# PROFIT=%s" % profit)
from db_wrapper import DBWrapper from oanda_wrapper import OandaWrapper from send_mail import SendMail from oandapy import oandapy import time # python extra_record_sql.py GBP_JPY[instrument] 2018-02-16[start_day] 00:00:00[start_time] args = sys.argv instrument = args[1] account_data = account_init("production", current_path) account_id = account_data["account_id"] token = account_data["token"] env = account_data["env"] mysql_connector = MysqlConnector() now = datetime.now() start_time = "2015-01-10 00:00:00" end_time = "2018-08-01 00:00:00" end_time = datetime.strptime(end_time, "%Y-%m-%d %H:%M:%S") start_time = datetime.strptime(start_time, "%Y-%m-%d %H:%M:%S") sql_file = open("%s_record.sql" % instrument, "w") while start_time < end_time: if decideMarket(start_time): start_ftime = start_time - timedelta(hours=9) start_ftime = start_ftime.strftime("%Y-%m-%dT%H:%M:%S")
class SuperAlgo(object): def __init__(self, instrument, base_path, config_name, base_time): self.base_path = base_path self.instrument = instrument self.config_data = instrument_init(self.instrument, self.base_path, config_name) self.ask_price = 0 self.bid_price = 0 self.insert_time = "" self.order_price = 0 self.stl_price = 0 self.stoploss_rate = 0 self.takeprofit_rate = 0 self.order_flag = False self.trade_id = 0 self.order_kind = "" self.mysql_connector = MysqlConnector() self.trail_flag = False self.trail_second_flag = False self.trail_price = 0 self.order_history = "pass" self.profit_history = "pass" self.count_threshold = 1 ################################################ # listは、要素数が大きいほうが古い。 # 小さいほうが新しい ############################################### def resetFlag(self): self.order_flag = False self.order_kind = "" self.trade_id = 0 self.trail_flag = False self.trail_second_flag = False self.trail_price = 0 def setOrderData(self, trade_flag, order_price, order_flag, trade_id): self.order_kind = trade_flag self.order_price = order_price self.order_flag = order_flag self.trade_id = trade_id def setPrice(self, base_time): sql = "select close_ask, close_bid, insert_time from %s_1m_TABLE where insert_time < \'%s\' order by insert_time DESC limit 1" % ( self.instrument, base_time - timedelta(minutes=1)) response = self.mysql_connector.select_sql(sql) self.ask_price = response[0][0] self.bid_price = response[0][1] self.insert_time = response[0][2] def getAskPrice(self): return self.ask_price def getBidPrice(self): return self.bid_price def setTradeId(self, trade_id): self.trade_id = trade_id def getTradeId(self): return self.trade_id def getOrderFlag(self): return self.order_flag def setOrderFlag(self, flag): self.order_flag = flag def setOrderKind(self, kind): self.order_kind = kind def setOrderPrice(self, price): self.order_price = price def getOrderPrice(self): return self.order_price def setStlPrice(self, price): self.stl_price = price def getStlPrice(self): return self.stl_price def getBidPrice(self): return self.bid_price def getAskPrice(self): return self.ask_price def getCurrentPrice(self): price = (self.ask_price + self.bid_price) / 2 return price def getCurrentTime(self): return self.insert_time def getOrderKind(self): return self.order_kind def decideTradeTime(self, base_time, trade_flag): enable_time_mode = self.config_data["enable_time_mode"] if enable_time_mode == "on": enable_times = self.config_data["enable_time"] enable_flag = False cmp_time = base_time.strftime("%Y-%m-%d") for ent in enable_times: ent = "%s %s" % (cmp_time, ent) before_time = datetime.strptime(ent, "%Y-%m-%d %H:%M:%S") after_time = before_time + timedelta(hours=1) if base_time > before_time and base_time < after_time: enable_flag = True else: enable_flag = True if enable_flag: pass else: trade_flag = "pass" return trade_flag def calcThreshold(self, trade_flag): stop_loss = self.config_data["stop_loss"] take_profit = self.config_data["take_profit"] threshold_list = {} if trade_flag == "buy": threshold_list["stoploss"] = self.ask_price - stop_loss threshold_list["takeprofit"] = self.ask_price + take_profit else: threshold_list["stoploss"] = self.bid_price + stop_loss threshold_list["takeprofit"] = self.bid_price - take_profit if take_profit == 0: threshold_list["takeprofit"] = 0 if stop_loss == 0: threshold_list["stoploss"] = 0 self.stoploss_rate = threshold_list["stoploss"] self.takeprofit_rate = threshold_list["takeprofit"] return threshold_list # testmodeでstoploss, takdeprofitに引っかかった場合 def decideReverceStl(self): try: self.takeprofit_rate = float(self.takeprofit_rate) self.stoploss_rate = float(self.stoploss_rate) self.ask_price = float(self.ask_price) self.bid_price = float(self.bid_price) stl_flag = False if self.order_kind == "buy": if self.bid_price > self.takeprofit_rate or self.bid_price < self.stoploss_rate: stl_flag = True elif self.order_kind == "sell": if self.ask_price < self.takeprofit_rate or self.ask_price > self.stoploss_rate: stl_flag = True return stl_flag except: raise def calcProfit(self): if self.order_kind == "buy": stl_price = self.bid_price profit = stl_price - self.order_price else: stl_price = self.ask_price profit = self.order_price - stl_price if profit >= 0: self.profit_history = "v" sleep_time = self.config_data["stl_sleep_vtime"] # self.count_threshold = 0 self.count_threshold = 1 else: self.profit_history = "l" sleep_time = self.config_data["stl_sleep_ltime"] # if self.count_threshold >= 3: if self.count_threshold >= 1: pass else: self.count_threshold = self.count_threshold + 1 self.order_history = self.order_kind self.setStlPrice(stl_price) return profit, sleep_time @abstractmethod def setIndicator(self, base_time): pass @abstractmethod def decideTrade(self, base_time): pass @abstractmethod def decideStl(self, base_time): pass
sys.path.append(current_path) sys.path.append(current_path + '../lib') import matplotlib matplotlib.use('Agg') from matplotlib.ticker import MultipleLocator import matplotlib.pyplot as plt import matplotlib.dates as mdates import mpl_finance from matplotlib import ticker import pandas as pd from mysql_connector import MysqlConnector con = MysqlConnector() sql = "select insert_time, open_ask, open_bid, high_ask, high_bid, low_ask, low_bid, close_ask, close_bid from GBP_JPY_5m_TABLE where insert_time < '2019-07-01 00:00:00' order by insert_time desc limit 1000" #sql = "select insert_time, open_ask, open_bid, high_ask, high_bid, low_ask, low_bid, close_ask, close_bid from GBP_JPY_day_TABLE where insert_time < '2019-07-01 00:00:00' order by insert_time desc limit 100" res = con.select_sql(sql) lst = [] for r in res: print(r) lst.append(list(r)) df = pd.DataFrame(lst) insert_time = df[0] op_price = (df[1] + df[2])/2 hi_price = (df[3] + df[4])/2
class LstmAlgo(SuperAlgo): def __init__(self, instrument, base_path, config_name, base_time): super(LstmAlgo, self).__init__(instrument, base_path, config_name, base_time) self.base_price = 0 self.setPrice(base_time) self.debug_logger = getLogger("debug") self.result_logger = getLogger("result") self.mysql_connector = MysqlConnector() self.first_flag = self.config_data["first_trail_mode"] self.second_flag = self.config_data["second_trail_mode"] self.trail_third_flag = False self.most_high_price = 0 self.most_low_price = 0 self.mode = "" self.algorithm = "" self.log_max_price = 0 self.log_min_price = 0 self.stl_logic = "none" self.output_max_price = 0 self.output_min_price = 0 self.first_trade_flag = "" self.first_trade_time = None self.current_path = os.path.abspath(os.path.dirname(__file__)) self.usdjpy_current_price = None self.usdjpy1h = None self.usdjpy3h = None self.usdjpyday = None self.eurusd_current_price = None self.eurusd1h = None self.eurusd3h = None self.eurusdday = None self.eurjpy_current_price = None self.eurjpy1h = None self.eurjpy3h = None self.eurjpyday = None self.usdjpy_sma = None self.eurusd_sma = None self.eurjpy_sma = None self.eurjpy_1hmodel = self.load_model(model_filename="multi_model_EUR_JPY_1h.json", weights_filename="multi_model_EUR_JPY_1h.hdf5") self.eurjpy_3hmodel = self.load_model(model_filename="multi_model_EUR_JPY_3h.json", weights_filename="multi_model_EUR_JPY_3h.hdf5") self.eurjpy_daymodel = self.load_model(model_filename="multi_model_EUR_JPY_day.json", weights_filename="multi_model_EUR_JPY_day.hdf5") self.eurjpy1h = self.multi_predict(table_type="1h", target_time=base_time) self.eurjpy3h = self.multi_predict(table_type="3h", target_time=base_time) self.eurjpyday = self.multi_predict(table_type="day", target_time=base_time) # decide trade entry timing def decideTrade(self, base_time): trade_flag = "pass" try: weekday = base_time.weekday() hour = base_time.hour minutes = base_time.minute seconds = base_time.second current_price = self.getCurrentPrice() # if weekday == Saturday, we will have no entry. #if weekday == 5 and hour >= 5: if weekday == 4 and hour >= 22: trade_flag = "pass" if weekday == 5: trade_flag = "pass" else: # if spread rate is greater than 0.5, we will have no entry if (self.ask_price - self.bid_price) >= 0.05: pass else: trade_flag = self.decideReverseTrade(trade_flag, current_price, base_time) return trade_flag except: raise # settlement logic def decideStl(self, base_time): try: stl_flag = False ex_stlmode = self.config_data["ex_stlmode"] if self.order_flag: if ex_stlmode == "on": weekday = base_time.weekday() hour = base_time.hour minutes = base_time.minute seconds = base_time.second current_price = self.getCurrentPrice() self.updatePrice(current_price) # if weekday == Saturday, we will settle one's position. if weekday == 5 and hour >= 5: self.result_logger.info("# weekend stl logic") stl_flag = True else: stl_flag = self.decideReverseStl(stl_flag, base_time) #pass else: pass return stl_flag except: raise def decideReverseStl(self, stl_flag, base_time): seconds = base_time.second if 15 < seconds < 30: target_time = base_time self.set_current_price(target_time) self.debug_logger.info("Stl Logic base_time = %s" % base_time) trade_day = self.first_trade_time.day trade_hour = self.first_trade_time.hour current_day = base_time.day current_hour = base_time.hour tmp_flag = False if int(trade_day) + 1 == int(current_day) and 6 < current_hour: tmp_flag = True elif int(trade_day) + 1 < int(current_day): tmp_flag = True if tmp_flag: if self.order_kind == "buy" and self.order_price < self.eurjpy_current_price: stl_flag = True elif self.order_kind == "sell" and self.order_price > self.eurjpy_current_price: stl_flag = True return stl_flag def get_model(self, table_type, instruments): model = None if table_type == "1h" and instruments == "EUR_JPY": model = self.eurjpy_1hmodel elif table_type == "3h" and instruments == "EUR_JPY": model = self.eurjpy_3hmodel elif table_type == "day" and instruments == "EUR_JPY": model = self.eurjpy_daymodel else: raise return model def multi_predict(self, table_type, target_time): right_string = "close_price" window_size = 10 output_train_index = 1 instruments = "EUR_JPY" if table_type == "1h": target_time = target_time - timedelta(hours=1) elif table_type == "3h": target_time = target_time - timedelta(hours=3) elif table_type == "day": target_time = target_time - timedelta(days=1) else: raise eurjpy = predict_value(target_time, self.get_model(table_type, instruments), window_size=window_size, table_type=table_type, output_train_index=output_train_index, instruments=instruments, right_string=right_string) return eurjpy def set_current_price(self, target_time): table_type = "1m" instruments = "EUR_JPY" sql = "select close_ask, close_bid from %s_%s_TABLE where insert_time < \'%s\' order by insert_time desc limit 1" % (instruments, table_type, target_time - timedelta(minutes=1)) response = self.mysql_connector.select_sql(sql) self.eurjpy_current_price = (response[0][0] + response[0][1]) / 2 def decideReverseTrade(self, trade_flag, current_price, base_time): if self.order_flag == False: minutes = base_time.minute seconds = base_time.second hour = base_time.hour target_time = base_time self.set_current_price(target_time) if 15 < seconds < 30: table_type = "1h" self.eurjpy1h = self.multi_predict(table_type, target_time) table_type = "3h" self.eurjpy3h = self.multi_predict(table_type, target_time) table_type = "day" self.eurjpyday = self.multi_predict(table_type, target_time) trade1h_flag = "" trade3h_flag = "" tradeday_flag = "" if self.eurjpy_current_price < self.eurjpy1h: trade1h_flag = "buy" if self.eurjpy_current_price > self.eurjpy1h: trade1h_flag = "sell" if self.eurjpy_current_price < self.eurjpy3h: trade3h_flag = "buy" if self.eurjpy_current_price > self.eurjpy3h: trade3h_flag = "sell" if self.eurjpy_current_price < self.eurjpyday: tradeday_flag = "buy" if self.eurjpy_current_price > self.eurjpyday: tradeday_flag = "sell" if trade1h_flag == tradeday_flag == "buy": self.first_trade_flag = "buy" self.first_trade_time = base_time elif trade1h_flag == tradeday_flag == "sell": self.first_trade_flag = "sell" self.first_trade_time = base_time self.writeDebugTradeLog(base_time, trade_flag) if self.first_trade_flag != "" and minutes % 5 == 0 and 5 < seconds < 15: self.eurjpy_sma = get_sma(instrument="EUR_JPY", base_time=base_time, table_type="5m", length=40, con=self.mysql_connector) if self.first_trade_flag == "buy": if self.eurjpy_current_price > self.eurjpy_sma: trade_flag = "buy" elif self.first_trade_flag == "sell": if self.eurjpy_current_price < self.eurjpy_sma: trade_flag = "sell" else: raise self.writeDebugTradeLog(base_time, trade_flag) return trade_flag def updatePrice(self, current_price): if self.log_max_price == 0: self.log_max_price = current_price elif self.log_max_price < current_price: self.log_max_price = current_price if self.log_min_price == 0: self.log_min_price = current_price elif self.log_min_price > current_price: self.log_min_price = current_price def setBollinger(self, base_time): window_size = 21 table_type = "5m" sigma_valiable = 3 target_time = base_time - timedelta(hours=1) sql = "select close_ask, close_bid from %s_%s_TABLE where insert_time < \'%s\' order by insert_time desc limit %s" % (self.instrument, table_type, target_time, window_size) response = self.mysql_connector.select_sql(sql) close_price = [] for res in response: close_price.append((res[0]+res[1])/2) close_price.reverse() dataset = getBollingerDataSet(close_price, window_size, sigma_valiable) self.upper_sigma1h = dataset["upper_sigmas"][-1] self.lower_sigma1h = dataset["lower_sigmas"][-1] # reset flag and valiables function after settlement def resetFlag(self): self.first_trade_flag = "" self.mode = "" self.most_high_price = 0 self.most_low_price = 0 self.log_max_price = 0 self.log_min_price = 0 self.stl_logic = "none" super(LstmAlgo, self).resetFlag() # write log function def writeDebugTradeLog(self, base_time, trade_flag): self.debug_logger.info("#############################################") self.debug_logger.info("# %s "% base_time) self.debug_logger.info("# trade_flag=%s" % trade_flag) self.debug_logger.info("# first_trade_flag=%s" % self.first_trade_flag) self.debug_logger.info("# first_trade_time=%s" % self.first_trade_time) self.debug_logger.info("# eurjpy=%s" % self.eurjpy_current_price) self.debug_logger.info("# eurjpy1h=%s" % self.eurjpy1h) self.debug_logger.info("# eurjpy3h=%s" % self.eurjpy3h) self.debug_logger.info("# eurjpyday=%s" % self.eurjpyday) self.debug_logger.info("# eurjpy_sma=%s" % self.eurjpy_sma) def entryLogWrite(self, base_time): self.result_logger.info("#######################################################") self.result_logger.info("# in %s Algorithm" % self.algorithm) self.result_logger.info("# first trade time at %s" % self.first_trade_time) self.result_logger.info("# EXECUTE ORDER at %s" % base_time) self.result_logger.info("# trade_flag=%s" % self.order_kind) self.result_logger.info("# ORDER_PRICE=%s" % ((self.ask_price + self.bid_price)/2 )) self.result_logger.info("# eurjpy=%s" % self.eurjpy_current_price) self.result_logger.info("# eurjpy1h=%s" % self.eurjpy1h) self.result_logger.info("# eurjpy3h=%s" % self.eurjpy3h) self.result_logger.info("# eurjpyday=%s" % self.eurjpyday) self.result_logger.info("# eurjpy_sma=%s" % self.eurjpy_sma) def settlementLogWrite(self, profit, base_time, stl_price, stl_method): self.result_logger.info("# %s at %s" % (stl_method, base_time)) self.result_logger.info("# self.ask_price=%s" % self.ask_price) self.result_logger.info("# self.bid_price=%s" % self.bid_price) self.result_logger.info("# self.log_max_price=%s" % self.log_max_price) self.result_logger.info("# self.log_min_price=%s" % self.log_min_price) self.result_logger.info("# self.stl_logic=%s" % self.stl_logic) self.result_logger.info("# STL_PRICE=%s" % stl_price) self.result_logger.info("# PROFIT=%s" % profit) def load_model(self, model_filename, weights_filename): model_filename = "%s/../model/master/%s" % (self.current_path, model_filename) weights_filename = "%s/../model/master/%s" % (self.current_path, weights_filename) print(model_filename) print(weights_filename) json_string = open(model_filename).read() learning_model = model_from_json(json_string) learning_model.load_weights(weights_filename) return learning_model