def check(self, db, trade=None, last_sell=0): if db.is_weekend() and self.weekend_value is not None: self.value = self.weekend_value if self.value is None: return False value = None if self.function == 'trend_of_last_seconds': value = db.get_trend_of_last_seconds(self.param) elif self.function == 'percentage_to_last': value = db.get_percentage_to_last() elif self.function == 'is_weekend': value = db.is_weekend() elif self.function == 'percentage_to_before': value = db.get_percentage_to_before(self.param) elif self.function == 'percentage_to_high': value = db.get_percentage_to_high() elif self.function == 'trade.percentage': value = trade.get_percentage() elif self.function == 'trade.last_percentage': value = trade.get_last_percentage() elif self.function == 'has_change_in_last_ticks': value = db.has_change(self.param) elif self.function == 'milliseconds_to_last_sell': value = (time.time_ns() - last_sell) / 1000000 if value is not None: if self.operator == '>': return value > self.value elif self.operator == '<': return value < self.value elif self.operator == '==': return value == self.value elif self.operator == '!=': return value != self.value elif self.operator == '>=': return value >= self.value elif self.operator == '<=': return value <= self.value elif self.operator == 'range': return self.value_range[0] <= value <= self.value_range[1] else: print('ERROR: ' + self.function + ' not found') return False
def __init__(self, db, tele, name='primary', in_strategy_test=False, color='blue', order=0, muted=False): self.db = db self.tele = tele self.name = name self.can_buy_conditions = [] self.should_buy_conditions = [] self.should_sell_conditions = [] self.cur_trade = trade.Trade(0, self.db, self.tele, False, strategy=self.name) self.in_strategy_test = in_strategy_test self.color = color self.order = order self.last_sell = time.time_ns() self.muted = muted
def create_trading_session(self): db_id = self.get_value('SELECT count(*) FROM sessions')[0] + 1 self.execute('INSERT INTO sessions (ID, starts_at) VALUES (' + str(db_id) + ', ' + str(int(time.time_ns() / 1000000)) + ');') return db_id
def export_balance_image(self): if not self.in_strategy_test: for s in self.strategies: if self.show_full_balance_view: last_balances = self.get_values( 'SELECT * FROM balance WHERE is_test == ' + self.is_test + ' AND strategy == "' + s.get_name() + '" ORDER BY time DESC') else: last_balances = self.get_values( 'SELECT * FROM balance WHERE is_test == ' + self.is_test + ' AND strategy == "' + s.get_name() + '" AND time > ' + str(int(time.time_ns() / 1000000) - 90000000) + ' ORDER BY time DESC') if len(last_balances) > 0: x_axis = [] y_axis = [] x = 0 for b in reversed(last_balances): x = x + 1 timestamp = int(b[0]) / 1000.0 x_axis.insert(0, datetime.fromtimestamp(timestamp)) y_axis.insert(0, b[1]) ax = plt.gca() xfmt = matplotlib.dates.DateFormatter('%H:%M') ax.xaxis.set_major_formatter(xfmt) plt.plot(x_axis, y_axis, color=s.get_color(), zorder=s.get_order()) if self.show_restart_points: if self.show_full_balance_view: sessions = self.get_values( 'SELECT starts_at FROM sessions') else: sessions = self.get_values( 'SELECT starts_at FROM sessions WHERE starts_at > ' + str(int(time.time_ns() / 1000000) - 90000000)) for s in sessions: timestamp = int(s[0]) / 1000.0 if timestamp > 0: date = datetime.fromtimestamp(timestamp) if date: plt.axvline(x=date, alpha=0.8, color="#FF453A", zorder=-2) plt.style.use('dark_background') plt.xticks(rotation=20) plt.xlabel('Time') plt.ylabel('Balance') plt.savefig('graph.png') plt.clf() # bot.send_photo(chat_id=866153882, photo=open('graph.png', 'rb'), caption=caption, # parse_mode=telegram.ParseMode.HTML) return 'graph.png'
def save_balance(self, b, strategy='primary'): self.save_value('INSERT INTO balance VALUES (' + str(int(time.time_ns() / 1000000)) + ', ' + str(b) + ', ' + self.is_test + ', "' + strategy + '");') print('BALANCE SAVED -> ' + str(round(b, 2)) + '€')
#HB.run() #WORKS !! #Typically, ^^ this is how it should be ran without threading. '''Message format testing''' #WORKS ! lo = HB.logOn() #8=FIX.4.2|9=68|35=A|49=qafix67|56=IB|34=30|52=20191219-23:11:38.836563|108=30|98=0|10=239 lf = HB.logOff() #8=FIX.4.2|9=56|35=5|49=qafix67|56=IB|34=30|52=20191219-23:11:38.836563|10=199 hbt = HB.heartBeat() #8=FIX.4.2|9=65|35=0|49=qafix67|56=IB|34=31|52=20191219-23:11:38.836563|1=U01067|10=133 t1 = time.time_ns() buy = HB.createOrder('AMD',1,1) sell = HB.createOrder('AMD',5,1) t2 = time.time_ns() print('Order time in ns: {}'.format((t2-t1))) #0? Not working? #Admin HB.parse(lo) HB.parse(hbt) HB.parse(lf) #OMS HB.parse(buy)
def sell(self): self.last_sell = time.time_ns() self.cur_trade.sell()