mirror of
https://github.com/ada-dmitry/etf-gyro.git
synced 2026-09-23 23:20:15 +00:00
63 lines
2.2 KiB
Python
63 lines
2.2 KiB
Python
import os
|
|
from dotenv import load_dotenv
|
|
from t_tech.invest import Client
|
|
from t_tech.invest.utils import quotation_to_decimal as to_dec
|
|
|
|
from app.rebalance import RebalanceBot
|
|
from app.load_config import load_config
|
|
from app.notifier import Notifier
|
|
|
|
load_dotenv()
|
|
|
|
|
|
def main():
|
|
report = "<b>🚀 Запуск бота ребаланса портфеля</b>\n"
|
|
tg = Notifier(token=os.getenv("TELEGRAM_BOT_TOKEN")) # type: ignore
|
|
config, corridor = load_config()
|
|
with Client(token=os.getenv("TINVEST_TOKEN")) as client: # type: ignore
|
|
bot = RebalanceBot(
|
|
client=client,
|
|
account_id=os.getenv("TINVEST_ACCOUNT_ID"), # type: ignore
|
|
target_weights=config, # type: ignore
|
|
corridor=corridor, # type: ignore
|
|
dry_run=os.getenv("DRY_RUN_OVERRIDE", "false").lower() == "true",
|
|
)
|
|
|
|
pos = bot.fetch_portfolio()
|
|
print(pos)
|
|
plan, plan_report = bot.calculate_rebalance(pos, bot.target_weights)
|
|
report += plan_report
|
|
|
|
report += "\n<i>——————————————</i>\n"
|
|
|
|
sales = [trade for trade in plan if trade["action"] == "SELL"]
|
|
buys = [trade for trade in plan if trade["action"] == "BUY"]
|
|
|
|
sales_report = bot.execute_orders(sales, "ПРОДАЖИ")
|
|
report += sales_report
|
|
report += "\n<i>——————————————</i>\n"
|
|
|
|
buys_report = bot.execute_orders(buys, "ПОКУПКИ")
|
|
report += buys_report
|
|
report += "\n<i>——————————————</i>\n"
|
|
|
|
pos_after = bot.fetch_portfolio()
|
|
cash = next(
|
|
(
|
|
p
|
|
for p in pos_after
|
|
if p.instrument_type == "currency" and p.ticker == "RUB000UTSTOM"
|
|
),
|
|
None,
|
|
)
|
|
cash_amount = to_dec(cash.quantity) # type: ignore
|
|
report += f"\n<b>💰 Остаток кэша после ребаланса:</b> <code>{cash_amount:.2f} RUB</code>\n"
|
|
|
|
tg.send_message(user_id=int(os.getenv("TELEGRAM_USER_ID")), text=report) # type: ignore
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|
|
# with Client(token=os.getenv("TINVEST_TOKEN")) as client:
|
|
# print(client.users.get_accounts())
|