def update_tables(tns): for tn in tns: last_date_updated = CandleTable.get_last_date(tn) target_curr = CandleTable.get_target_currency(tn) period = CandleTable.get_period(tn) CandleFetcher.fetch_candles_after_date( target_curr, (last_date_updated - 10 * int(period)), period)
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 __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 update_tables_imperative(tns): ##CandleFetcher.update_tables(tns) ##perform normal update of any legit candles(replacing fake ones) for i, tn in enumerate(tns): sec_now = time.time() last_candle_date = CandleTable.get_last_date(tn) target_curr = CandleTable.get_target_currency(tn) period = int(CandleTable.get_period(tn)) curr_pair = "USDT_" + target_curr last_candle_date += period while (last_candle_date < sec_now): ##(top_bid, bottom_ask) = OrderMaker.get_spread(curr_pair) ##if curr_avail[target_curr]: ##means it is true, means it is available to be sold ##close = bottom_ask ##else: ##close = top_bid close = OrderMaker.get_last_trade_rate(curr_pair, last_candle_date) c = Candle(tn, last_candle_date, 0, 0, 0, close, 0, 0, 0) c.save() last_candle_date += period dbm = DBManager.get_instance() dbm.save_and_close()
def get_candle_data(curr_target, date_start, date_end, period): polo = Poloniex.get_instance() ##print "Adding ", curr_target, " candle data between: ", timestamp_to_date(date_start), " ---- ", timestamp_to_date(date_end) ##configuration curr_ref = "USDT" ##curr_target = "BTC" ##start = 1451606400 ## Jan 01 2016 ##end = 1459468800## Apr 1 2016 ##start = 1459468800## Apr 1 2016 ##end = 1467331200 ## july 01 2016 ##start = 1467331200 ## july 01 2016 ##end = 1475280000## oct 01 2016 ##start = 1475280000 ## aug 8 2016 ##end = 9999999999 ## present ##period = 14400 ## in seconds ##table_name = CandleTable.calc_table_name(curr_ref, curr_target, start, end, period) table_name = "CANDLE_" + curr_ref + "_" + curr_target + "_" + str( period) if not DBManager.exists_table(table_name): ct = CandleTable(curr_ref, curr_target, date_start, date_end, period, table_name) ct.save() print("Populating table: " + table_name + " ...") curr_pair = curr_ref + "_" + curr_target data = polo.api_query( "returnChartData", { 'currencyPair': curr_pair, 'start': date_start, 'end': date_end, 'period': period }) cp = CandleParser(table_name, data)
def update_all(): for tn in table_names.complete_tables: last_date_updated = CandleTable.get_last_date(tn) target_curr = CandleTable.get_target_currency(tn) period = CandleTable.get_period(tn) CandleFetcher.fetch_candles_after_date(target_curr, last_date_updated, period)
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 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 __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 cut_table(orig_table_name, date_start, date_end=9999999999): ##print "Cutting table: ", orig_table_name, " candle data between: ", timestamp_to_date(date_start), " ---- ", timestamp_to_date(date_end) ##create new table curr_ref = CandleTable.get_ref_currency(orig_table_name) curr_target = CandleTable.get_target_currency(orig_table_name) period = CandleTable.get_period(orig_table_name) new_table = CandleTable(curr_ref, curr_target, date_start, date_end, period) new_table_name = new_table.table_name if DBManager.exists_table(new_table_name): DBManager.drop_table(new_table_name) new_table.save() ##populate new table with candles from orig_table that lie between the 2 dates candle_array = CandleTable.get_candle_array_by_date( orig_table_name, date_start, date_end) for c in candle_array: new_c = Candle(new_table_name, c.date, c.high, c.low, c.open, c.close, c.volume, c.quoteVolume, c.weightedAverage) new_c.save() dbm = DBManager.get_instance() return new_table_name
def grab_new_signals(self): period = float(CandleTable.get_period(table_names.short_term_tables[0])) last_time = CandleTable.get_last_date(table_names.short_term_tables[0]) cur_time = time.time() if(cur_time-last_time) > (period+1): print("***********************************SIGNALS*********************************************") self.signaler.update(self.order_updater.sym_infos) new_signals_array = self.signaler.new_signals_array self.handle_new_currency_signals(new_signals_array) sym_infos = self.order_updater.sym_infos for key, value in sym_infos.items(): print("last operation was: " , value.is_owned) return Task.CONTINUE
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 run(self): ## first iterate over candle index most_candles = self.candles_array[self.most_candles_index()] num_most_candles = len(most_candles) for i in range(num_most_candles): ##then iterate over candle table for j in range(self.num_currencies): period = CandleTable.get_period(self.table_name_array[j]) self.bit_sec += self.bits_array[j] * int(period) ##offset adjusts for different tables having different num of candles offset = num_most_candles - len(self.candles_array[j]) if offset <= i: operation = self.strategy_array[j].decide( i - offset, self.bits_array[j], self.balance) self.process_operation(j, operation, self.candles_array[j][i - offset]) else: ##too early to trade these candles pass self.finalize_balance() if self.to_log: self.save_all_trades() if self.to_print: self.print_results()
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 setup(self): if self.POINT in self.table_name: self.input_points = PointTable.to_point_array(self.table_name) elif self.TREND in self.table_name: self.input_points = TrendTable.to_point_array(self.table_name) else: self.input_points = CandleTable.to_point_array(self.table_name, "close") self.output_table_name = self.table_name.replace(self.CANDLE, self.POINT)
def set_defaults(self): if CandleTable.get_period(self.table_name) == "14400": self.bb_factor = 2.5 self.stddev_adjust = False self.avg_period = 40 self.num_past_buy = 0 self.num_past_sell = 3 self.one_op = False self.to_carry = True self.wait_better = True if CandleTable.get_period(self.table_name) == "7200": self.bb_factor = 2 self.stddev_adjust = False self.avg_period = 80 self.num_past_buy = 0 self.num_past_sell = 6 self.one_op = False self.to_carry = False self.wait_better = False
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 calc_vol_tables(self): if self.calc_stats: self.strat_stat_array = [] self.ss_tn = StratStatTable.calc_name(self.table_name, self.NAME) if DBManager.exists_table(self.ss_tn): DBManager.drop_table(self.ss_tn) sst = StratStatTable(self.ss_tn) sst.save() pt_name = CandleTable.to_point_table(self.table_name, "volume") pt_close = CandleTable.to_point_table(self.table_name, "close") self.points = PointTable.get_point_array(pt_name) self.points_close = PointTable.get_point_array(pt_close) pt_name_1 = "TEMP1" mv1 = MovingAverage(pt_name_1, self.points) pt_name_2 = "TEMP2" mv2 = MovingAverage(pt_name_2, self.points) self.vol_pts_short = mv1.simple(self.VOL_PERIOD_SHORT) self.vol_pts_long = mv2.simple(self.VOL_PERIOD_LONG) ##pp = PointPopulator(self.table_name) ##self.stddev_pts_short = pp.create_stddev(self.STDDEV_PERIOD_SHORT) ##self.stddev_pts_long = pp.create_stddev(self.STDDEV_PERIOD_LONG) DBManager.drop_matching_tables("TEMP")
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 __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 calc_name(candle_table_name, strategy_name): return "TRADE_" + strategy_name + "_" + CandleTable.get_target_currency( candle_table_name) + "_" + CandleTable.get_period( candle_table_name)
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, 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))