def get_new_signals(self, tn_index): tn = self.trader_tables[tn_index] signal_tn = self.signal_table_names[tn_index] cur_date = int(time()) last_date = SignalTable.get_last_date(signal_tn) period = float(CandleTable.get_period(tn)) ##cut the candle table to get one of a more manageable size cut_table_name = CandleFetcher.cut_table(tn, int(cur_date - 5*ShortTermStrategy.DATA_PAST*period)) candles = CandleTable.get_candle_array(cut_table_name) ##new_candle_index = self.find_new_candle_index(candles, last_date) new_signals = [] ##run a strategy on the candles and store the resulting operations returned strat = self.strat_array[tn_index] sym = SignalTable.get_sym(self.signal_table_names[tn_index]) strat.update_state(candles) ##i = new_candle_index ##while i < len(candles): i = len(candles) - 1 o = strat.decide(i, self.sym_infos[sym].total_balance, self.sym_infos["USDT"].total_balance) sig = Sig(signal_tn, candles[i].date, SignalTable.get_sym(signal_tn), o.amount, candles[i].close, o.op) new_signals.append(sig) ##i += 1 ##delete created table when done DBManager.drop_table(cut_table_name) return new_signals
def create_cut_table(self): self.first_date = CandleTable.get_first_date(self.candle_table_name) self.last_date = CandleTable.get_last_date(self.candle_table_name) self.cut_trend_table_name = self.get_trend_table_name() if DBManager.exists_table(self.cut_trend_table_name): DBManager.drop_table(self.cut_trend_table_name) tt = TrendTable(self.cut_trend_table_name) tt.save() candles = CandleTable.get_candle_array(self.candle_table_name) for c in candles: date = c.date hits = TrendTable.get_most_recent_hits(self.trend_table_name, date) trend = Trend(dbm, self.cut_trend_table_name, date, hits) trend.save() ##cursor = TrendTable.get_section(self.trend_table_name, self.first_date, self.last_date) ##trend_tuples = cursor.fetchall() ##for t in trend_tuples: ##date = t[0] ##hits = t[1] ##trend = Trend(dbm, self.cut_trend_table_name, date, hits) ##trend.save() ##dbm.save_and_close() return tt
def __init__(self, table_name, is_simul=True, to_print=False, calc_stats=False): self.table_name = table_name self.candles = CandleTable.get_candle_array(table_name) self.to_print = to_print self.calc_stats = calc_stats self.interval_array = None self.is_simul = is_simul self.area_array = [] self.adapter = ShortStratAdapter(is_simul) if is_simul: self.amount = TradeSimulator.get_currency_amount(table_name) else: self.amount = 1 init_candles = self.candles[:self.DATA_PAST] self.ranges = self.create_ranges(init_candles) self.interval_array = self.create_interval_array(self.ranges) ##self.interval_array.pprint() ##print self.interval_array.local_maxes ##print self.interval_array.get_limits(773) ##self.update_levels(self.ranges, self.DATA_PAST) sym = CandleTable.get_target_currency(table_name) if is_simul: self.calc_vol_tables()
def preprocess(self): ##initilize all bits to 0, get symbols for tn in self.table_name_array: self.bits_array.append(0) self.bits_end_array.append(0) self.symbol_array.append(CandleTable.get_target_currency(tn)) self.total_bits_bought_array.append(0) ##get candle arrays self.candles_array = [] for tn in self.table_name_array: candles = CandleTable.get_candle_array(tn) self.candles_array.append(candles) for s in self.strategy_array: if hasattr(s, "adapter") and self.balance_limit is not None: s.adapter.set_limit(self.balance_limit) ##create trade tables if self.to_log: for i, tn in enumerate(self.table_name_array): trade_table_name = TradeTable.calc_name( tn, self.strategy_array[i].get_name()) trade_table = TradeTable(trade_table_name) if DBManager.exists_table(trade_table_name): DBManager.drop_table(trade_table_name) trade_table.save() self.trade_table_name_array.append(trade_table_name) self.strategy_array[i].trade_table_name = trade_table_name self.trades_array.append([])
def cross_validate(self, candle_table_name): ##get candles and use them to calculate outputs candles = CandleTable.get_candle_array(candle_table_name) num_candles = len(candles) ##this splits training set from test set last_candle = int(math.floor(num_candles * (1 - PERCENTAGE_TEST))) self.train_model(candles[:last_candle]) self.test_model(candles[last_candle:])
def __init__(self, table_name, bits, bitsec): self.table_name = table_name self.bits = bits self.bitsec = bitsec self.candles = CandleTable.get_candle_array(table_name) self.period = float(CandleTable.get_period(table_name)) self.buy_amt = self.calc_buy_amt() self.runs = [0]
def __init__(self, ct_name): self.ct_name = ct_name self.candles = CandleTable.get_candle_array(ct_name) self.pt_name_close = CandleTable.to_point_table(ct_name, Candle.CLOSE) self.points_close = PointTable.get_point_array(self.pt_name_close) self.pt_name_volume = CandleTable.to_point_table( ct_name, Candle.VOLUME) self.points_volume = PointTable.get_point_array(self.pt_name_volume)
def __init__(self, table_name, candle_table_name): self.table_name = table_name self.candle_table_name = candle_table_name if dbm.exists_table(table_name): DBManager.drop_table(table_name) self.table = TradeTable(table_name) self.table.save() candles = CandleTable.get_candle_array(candle_table_name) for c in candles: p = Trade(dbm, table_name, c.date, 0, 0, Trade.NONE_TYPE) p.save() dbm = DBManager.get_instance() dbm.save_and_close()
def __init__(self, predicter_table_name, mode): self.cur_traded_candles = None self.predicter_table_name = predicter_table_name self.predicter_candles = CandleTable.get_candle_array( predicter_table_name) self.table_period = CandleTable.get_period(predicter_table_name) self.candles_to_skip = 0 ## if want to trade more often than data available, don't skip anything else skip however many table periods fit into TIME_PERIOD if self.table_period >= self.TIME_PERIOD: self.candles_to_skip = 1 else: self.candles_to_skip = self.TIME_PERIOD / self.table_period ##an array where avg at each time can be looked up self.point_avgs = self.create_table(predicter_table_name, mode)
def __init__(self, table_name, std_amount=False, bb_factor=2.5, to_carry=True, one_op=True, stddev_adjust=True, wait_better=True, avg_period=80, num_past_buy=0, num_past_sell=6, set_default=False, calc_stats=False): ##model parameters self.bb_factor = bb_factor ##number of standard deviations of difference between a bollinger band and the avg self.to_carry = to_carry ##if set, when performing a buy/sell increase amount by the sum of all previous plan_buy/plan_sell self.one_op = one_op ##if set force a delay between operations of the same type self.stddev_adjust = stddev_adjust ## if true adjust amount bought/sold by how much it deviates from the bollinger band, if false use constant amount self.wait_better = wait_better ## if set to true only approve buys or sells if bb_score has improved since last buy or sell self.avg_period = avg_period ## how many candles to use to make avg self.num_past_buy = num_past_buy ## how long to plan buying before actually buying self.num_past_sell = num_past_sell ## how long to plan selling before actually selling self.set_default = set_default ##NOTE: removed extra parameter for stddev_period, currently same as avg_period ##self.stddev_period = stddev_period ##how many past candles to use to compute stddeviation self.table_name = table_name if std_amount: ##used by actual trader that does it's own amount standardization self.amount = self.STANDARD_AMOUNT else: ##used by simulator, adjusts amount based on currency self.amount = TradeSimulator.get_currency_amount(table_name) self.candles = CandleTable.get_candle_array(table_name) self.trade_table = None self.trade_plan_array = [] self.bb_scores = [] self.candle_table_name = table_name if self.set_default: self.set_defaults() self.create_tables() self.cleanup()
def simulate_random_strategy(candle_table_name): candles = CandleTable.get_candle_array(candle_table_name) strat = RandomStrategy(candles) trade_sim = TradeSimulator(candle_table_name, candles, strat) trade_sim.run()
def simulate_manual_attribute_strategy(candle_table_name, attr_name): sim_candles = CandleTable.get_candle_array(candle_table_name) strat = ManualAttributeStrategy(sim_candles, attr_name) trade_sim = TradeSimulator(candle_table_name, sim_candles, strat) trade_sim.run()
def __init__(self, table_name): self.candle_table_name = table_name self.candles = CandleTable.get_candle_array(table_name) self.amount = TradeSimulator.get_currency_amount(table_name) self.create_tables()
def __init__(self, table_name, to_spend): self.candles = CandleTable.get_candle_array(table_name) num_candles = len(self.candles) period = float(CandleTable.get_period(table_name)) self.amount = to_spend / (self.candles[0].close * (1 + TradeSimulator.BUY_FEE))