def edit_account(bot, update, args): bot.send_chat_action(chat_id=update.message.chat_id, action=ChatAction.TYPING) if len(args) < 2 or len(args) > 6: bot.send_message( chat_id=update.message.chat_id, text= "/edit_account <id> name:<name> fee:<fee> risk:<risk> profit:<profit> password:<password>" ) else: try: backup_db(max_backup=int( read_from_config_file(CONFIG_FILE, 'creds', 'num_backup_dbs'))) acc = Account(current_sql_file()) params = dict() for arg in args[1:]: param = arg.split(':') params[param[0]] = param[1] account = acc.update_id(id=args[0], **params) reply_str = str() reply_str += f'ID: *{account[0]}*' + '\n' reply_str += f'NAME: *{account[1]}*' + '\n' reply_str += f'PERCENTAGE: *{account[2]*100}*' + '\n' reply_str += f'FEE: *{account[4]}*' + '\n' reply_str += f'RISK: *{account[5]*100}*' + '\n' reply_str += f'PROFIT: *{account[6]*100}*' + '\n' reply_str += f'TIMESTAMP: {account[3]}' + '\n' bot.send_message(chat_id=update.message.chat_id, text=reply_str, parse_mode=ParseMode.MARKDOWN) except Exception as ex: print(ex)
def set_main(bot, update, args): bot.send_chat_action(chat_id=update.message.chat_id, action=ChatAction.TYPING) if not len(args) == 1: bot.send_message(chat_id=update.message.chat_id, text="/set_main <account_id>") else: try: backup_db(max_backup=int( read_from_config_file(CONFIG_FILE, 'creds', 'num_backup_dbs'))) prt = Portfolio(current_sql_file()) prt_id = read_from_config_file(CONFIG_FILE, 'creds', 'portfolio_id') prt.update_id(prt_id, main_account=int(args[0])) prt = prt.retrieve_id(prt_id) reply_str = str() reply_str += f'ID: *{prt[0]}*' + '\n' reply_str += f'MAIN\_ACCOUNT\_FK: *{prt[1]}*' bot.send_message(chat_id=update.message.chat_id, text=reply_str, parse_mode=ParseMode.MARKDOWN) except Exception as ex: print(ex)
def make_transfer(bot, update, args): bot.send_chat_action(chat_id=update.message.chat_id, action=ChatAction.TYPING) if not len(args) == 3: bot.send_message( chat_id=update.message.chat_id, text= "/make_transfer <sender_name> <reciever_name> <amount(positive)>") else: try: backup_db(max_backup=int( read_from_config_file(CONFIG_FILE, 'creds', 'num_backup_dbs'))) prt_id = read_from_config_file(CONFIG_FILE, 'creds', 'portfolio_id') alg = Algorithm(current_sql_file(), prt_id) alg.make_transfer(sender_name=args[0], reciever_name=args[1], amount=float(args[2])) alg.chart_portfolio_acounts() bot.send_photo(chat_id=update.message.chat_id, photo=open(f'media/portfolio-{prt_id}-accounts.png', 'rb')) except Exception as ex: print(ex)
def chart_portfolio_funds(bot, update): bot.send_chat_action(chat_id=update.message.chat_id, action=ChatAction.TYPING) try: prt = read_from_config_file(CONFIG_FILE, 'creds', 'portfolio_id') alg = Algorithm(current_sql_file(), prt) alg.chart_portfolio_funds() bot.send_photo(chat_id=update.message.chat_id, photo=open(f'media/portfolio-{prt}-funds.png', 'rb')) except Exception as ex: print(ex)
def list_portfolios(bot, update): bot.send_chat_action(chat_id=update.message.chat_id, action=ChatAction.TYPING) try: p = Portfolio(current_sql_file()) prts = p.retrieve_portfolios() reply_str = str() for prt in prts: reply_str += f'ID: *{prt[0]}*' + '\n' reply_str += f'MAIN\_ACCOUNT\_FK: *{prt[1]}*\n\n' bot.send_message(chat_id=update.message.chat_id, text=reply_str, parse_mode=ParseMode.MARKDOWN) except Exception as ex: print(ex)
def pay_fees(bot, update): bot.send_chat_action(chat_id=update.message.chat_id, action=ChatAction.TYPING) try: backup_db(max_backup=int( read_from_config_file(CONFIG_FILE, 'creds', 'num_backup_dbs'))) prt_id = read_from_config_file(CONFIG_FILE, 'creds', 'portfolio_id') alg = Algorithm(current_sql_file(), prt_id) alg.pay_fees() alg.chart_portfolio_acounts() bot.send_photo(chat_id=update.message.chat_id, photo=open(f'media/portfolio-{prt_id}-accounts.png', 'rb')) except Exception as ex: print(ex)
def chart_account_funds(bot, update, args): bot.send_chat_action(chat_id=update.message.chat_id, action=ChatAction.TYPING) if not len(args) == 1: bot.send_message(chat_id=update.message.chat_id, text="/chart_account_funds <name>") else: try: alg = Algorithm( current_sql_file(), read_from_config_file(CONFIG_FILE, 'creds', 'portfolio_id')) alg.chart_account_funds(name=args[0]) bot.send_photo(chat_id=update.message.chat_id, photo=open(f'media/{args[0]}-funds.png', 'rb')) except Exception as ex: print(ex)
def remove_main(bot, update): bot.send_chat_action(chat_id=update.message.chat_id, action=ChatAction.TYPING) try: backup_db(max_backup=int( read_from_config_file(CONFIG_FILE, 'creds', 'num_backup_dbs'))) alg = Algorithm( current_sql_file(), read_from_config_file(CONFIG_FILE, 'creds', 'portfolio_id')) prt = alg.remove_main() reply_str = str() reply_str += f'ID: *{prt[0]}*' + '\n' reply_str += f'MAIN\_ACCOUNT\_FK: *{prt[1]}*' bot.send_message(chat_id=update.message.chat_id, text=reply_str, parse_mode=ParseMode.MARKDOWN) except Exception as ex: print(ex)
def list_account_transactions(bot, update, args): bot.send_chat_action(chat_id=update.message.chat_id, action=ChatAction.TYPING) if not len(args) == 1: bot.send_message(chat_id=update.message.chat_id, text="/list_account_transactions <name>") else: try: alg = Algorithm( current_sql_file(), read_from_config_file(CONFIG_FILE, 'creds', 'portfolio_id')) transaction_list = alg.list_account_transactions(name=args[0]) bot.send_message(chat_id=update.message.chat_id, text=transaction_list, parse_mode=ParseMode.MARKDOWN) backup_db(max_backup=int( read_from_config_file(CONFIG_FILE, 'creds', 'num_backup_dbs'))) except Exception as ex: print(ex)
def add_account(bot, update, args): bot.send_chat_action(chat_id=update.message.chat_id, action=ChatAction.TYPING) if len(args) < 5: bot.send_message( chat_id=update.message.chat_id, text= "/add_account <name> <fund> <fee> <risk> <profit> <is_main=False>") else: try: backup_db(max_backup=int( read_from_config_file(CONFIG_FILE, 'creds', 'num_backup_dbs'))) alg = Algorithm( current_sql_file(), read_from_config_file(CONFIG_FILE, 'creds', 'portfolio_id')) accounts = alg.add_account( name=args[0], fund=float(args[1]), fee=float(args[2]), risk=float(args[3]), profit=float(args[4]), is_main=bool(args[5]) if len(args) == 6 else False) reply_str = str() for account in accounts: if account[1] == args[0]: reply_str += f'ID: *{account[0]}*' + '\n' reply_str += f'NAME: *{account[1]}*' + '\n' reply_str += f'PERCENTAGE: *{account[2]*100}*' + '\n' reply_str += f'FEE: *{account[4]}*' + '\n' reply_str += f'RISK: *{account[5]*100}*' + '\n' reply_str += f'PROFIT: *{account[6]*100}*' + '\n' reply_str += f'TIMESTAMP: {account[3]}' + '\n' bot.send_message(chat_id=update.message.chat_id, text=reply_str, parse_mode=ParseMode.MARKDOWN) except Exception as ex: print(ex)
def list_accounts(bot, update): bot.send_chat_action(chat_id=update.message.chat_id, action=ChatAction.TYPING) try: acc = Account(current_sql_file()) accounts = acc.retrieve_accounts_for_portfolio( read_from_config_file(CONFIG_FILE, 'creds', 'portfolio_id')) reply_str = '*ACCOUNTS*\n\n' for account in accounts: reply_str += f'ID: *{account[0]}*' + '\n' reply_str += f'NAME: *{account[1]}*' + '\n' reply_str += f'PERCENTAGE: *{account[2]*100}*' + '\n' reply_str += f'FEE: *{account[4]}*' + '\n' reply_str += f'RISK: *{account[5]*100}*' + '\n' reply_str += f'PROFIT: *{account[6]*100}*' + '\n' reply_str += f'TIMESTAMP: {account[3]}' + '\n\n' bot.send_message(chat_id=update.message.chat_id, text=reply_str, parse_mode=ParseMode.MARKDOWN) except Exception as ex: print(ex)
def update_total_fund(bot, update, args): bot.send_chat_action(chat_id=update.message.chat_id, action=ChatAction.TYPING) if not len(args) == 1: bot.send_message(chat_id=update.message.chat_id, text="/update_total_fund <current_amount>") else: try: backup_db(max_backup=int( read_from_config_file(CONFIG_FILE, 'creds', 'num_backup_dbs'))) alg = Algorithm( current_sql_file(), read_from_config_file(CONFIG_FILE, 'creds', 'portfolio_id')) alg.update_total_fund(new_fund=float(args[0])) alg.chart_portfolio_funds() prt = read_from_config_file(CONFIG_FILE, 'creds', 'portfolio_id') bot.send_photo(chat_id=update.message.chat_id, photo=open(f'media/portfolio-{prt}-funds.png', 'rb')) except Exception as ex: print(ex)
from investmentalg.algorithm import Algorithm from investmentalg.portfolio import Portfolio from investmentalg.init_db import create_db, current_sql_file, backup_db, undo import os # python -m unittest discover <test_directory> # if os.path.isfile(current_sql_file()): # os.remove(current_sql_file()) p = Portfolio(current_sql_file()) prt = p.create_portfolio() # print('prt: ', prt) # print('--------------------------------------------------------------') # alg = Algorithm(current_sql_file(), prt[0]) # # print('alg.portfolio_total_fund: ', alg.portfolio_total_fund) # # print('--------------------------------------------------------------') # accounts = alg.add_account(name='farhan', fee=0 , risk=0.7 , profit=0.7, fund=0.0) # backup_db(max_backup=10) # alg = Algorithm(current_sql_file(), prt[0]) # # print('alg.portfolio_total_fund: ', alg.portfolio_total_fund) # # print(f'accounts: {accounts}') # # print('--------------------------------------------------------------') # accounts = alg.add_account(name='saeid', fee=0 , risk=0.3 , profit=0.9, fund=100.0) # backup_db(max_backup=10) # alg = Algorithm(current_sql_file(), prt[0]) # # print('alg.portfolio_total_fund: ', alg.portfolio_total_fund) # # print(f'accounts: {accounts}') # # print('--------------------------------------------------------------') # accounts = alg.make_transaction(name='farhan', fund=2000000) # backup_db(max_backup=10) # alg = Algorithm(current_sql_file(), prt[0]) # # print('alg.portfolio_total_fund: ', alg.portfolio_total_fund) # # print(f'accounts: {accounts}')