Ejemplo n.º 1
0
    def _position(self, update=None, context=None):
        position_table = PrettyTable(['', 'Long', 'Short'])
        if 'long' in self._bot.position:
            long_position = self._bot.position['long']
            shrt_position = self._bot.position['shrt']

            position_table.add_row([f'Size', round_dynamic(long_position['size'], 3),
                                    round_dynamic(shrt_position['size'], 3)])
            position_table.add_row(['Price', round_dynamic(long_position['price'], 3),
                                    round_dynamic(shrt_position['price'], 3)])
            position_table.add_row(['Curr.price', round_dynamic(self._bot.price, 3),
                                    round_dynamic(self._bot.price, 3)])
            position_table.add_row(['Leverage', compress_float(long_position['leverage'], 3),
                                     compress_float(shrt_position['leverage'], 3)])
            position_table.add_row(['Liq.price', round_dynamic(long_position['liquidation_price'], 3),
                 round_dynamic(shrt_position['liquidation_price'], 3)])
            position_table.add_row(['Liq.diff.%', round_dynamic(float(long_position['liq_diff']) * 100, 3),
                 round_dynamic(float(shrt_position['liq_diff']) * 100, 3)])
            position_table.add_row([f'UPNL {self._bot.margin_coin}', round_dynamic(float(long_position['upnl']), 3),
                                    round_dynamic(float(shrt_position['upnl']), 3)])

            table_msg = position_table.get_string(border=True, padding_width=1,
                                                  junction_char=' ', vertical_char=' ',
                                                  hrules=HEADER)
            self.send_msg(f'<pre>{table_msg}</pre>')
        else:
            self.send_msg("Position not initialized yet, please try again later")
Ejemplo n.º 2
0
            async def send_daily_async():
                nr_of_days = 7
                today = datetime.utcnow().replace(hour=0,
                                                  minute=0,
                                                  second=0,
                                                  microsecond=0)
                daily = {}
                position = await self._bot.fetch_position()
                wallet_balance = position['wallet_balance']
                for idx, item in enumerate(range(0, nr_of_days)):
                    start_of_day = today - timedelta(days=idx)
                    end_of_day = start_of_day + timedelta(days=1)
                    start_time = int(start_of_day.timestamp()) * 1000
                    end_time = int(end_of_day.timestamp()) * 1000
                    daily_trades = await self._bot.fetch_fills(
                        start_time=start_time, end_time=end_time)
                    pln_summary = 0
                    for trade in daily_trades:
                        pln_summary += float(trade['realized_pnl'])
                    daily[idx] = {}
                    daily[idx]['date'] = start_of_day.strftime('%d-%m')
                    daily[idx]['pnl'] = pln_summary

                table = PrettyTable(['Date', 'PNL', 'Daily %'])
                pnl_sum = 0.0
                for item in daily.keys():
                    day_profit = daily[item]['pnl']
                    pnl_sum += day_profit
                    previous_day_close_wallet_balance = wallet_balance - day_profit
                    profit_pct = ((wallet_balance / previous_day_close_wallet_balance) - 1) * 100 \
                        if previous_day_close_wallet_balance > 0.0 else 0.0
                    wallet_balance = previous_day_close_wallet_balance
                    table.add_row([
                        daily[item]['date'],
                        compress_float(day_profit, 3),
                        round_(profit_pct, 0.01)
                    ])

                pct_sum = ((position['wallet_balance'] + pnl_sum) / position['wallet_balance'] - 1) * 100 \
                    if position['wallet_balance'] > 0.0 else 0.0
                table.add_row(['-------', '------', '---------'])
                table.add_row([
                    'Total',
                    compress_float(pnl_sum, 3),
                    round_(pct_sum, 0.01)
                ])

                msg = f'<pre>{table.get_string(border=True, padding_width=1, junction_char=" ", vertical_char=" ", hrules=HEADER)}</pre>'
                self.send_msg(msg)
Ejemplo n.º 3
0
            async def send_closed_trades_async():
                tradess = await self._bot.fetch_fills(limit=100)
                closed_trades = [t for t in tradess if t['realized_pnl'] > 0.0]
                closed_trades.sort(key=lambda x: x['timestamp'], reverse=True)

                table = PrettyTable(['Date', 'Pos.', 'Price', f'Pnl {self._bot.quot}'])

                for trade in closed_trades[:self.n_trades]:
                    trade_date = datetime.fromtimestamp(trade['timestamp'] / 1000)
                    table.add_row(
                        [trade_date.strftime('%m-%d %H:%M'), trade['position_side'], round_(trade['price'], self._bot.price_step),
                         compress_float(trade['realized_pnl'], 3)])

                msg = f'Closed trades:\n' \
                      f'<pre>{table.get_string(border=True, padding_width=1, junction_char=" ", vertical_char=" ", hrules=HEADER)}</pre>'
                self.send_msg(msg)