A while ago I found myself looking for a tool to easily calculate the APR for my delegations which paid something back. I'm not talking about dlease delegations, because their site is well crafted and the APR is in the center of it, both for the delegators and for the delegatees. But you might delegate to other services which also share all or part of the ROI generated from the delegation you offered. So after not finding what I was looking for, I decided to make my own script to calculate the APRs of the delegations. The script is written in Python and I'll share it with you below. You are free to play around with it and adapt it to your needs. ### What can you do with the script? * calculate APRs for the delegations from one account to a number of specified accounts (introduced as a comma-separated list; note: accounts from the list are not tested if they are real Hive accounts, typos will generate a <code>KeyError</code> error) * delegations updates between start date and stop date are tracked and APR is calculated for each distinct period * the algorithm allows different delegatees and paying accounts ### Limitations * something I haven't added in the script although I should have is a distinction between HIVE and HBD for payments received. I wasn't bothered because all my payments were received in HIVE. * it doesn't work for delegations via dlease or any other systems where delegations to different accounts are aggregated when payment is being made * it doesn't take into account the 5 days cooldown period when calculating the APR * it doesn't work if payment is being made to a different account than the one from which the delegations are being made (this can of course be added but I didn't) ### Python Script ``` import sys from beem import Hive from beem.account import Account from beem.nodelist import NodeList import time from datetime import datetime, timedelta from datetime import date if __name__ == "__main__": nodelist = NodeList() nodelist.update_nodes() blockchain_instance = Hive(node=nodelist.get_hive_nodes()) assert blockchain_instance.is_hive your_account_name = input("Enter your account name: ") try: your_account = Account(your_account_name, blockchain_instance=blockchain_instance) except: print('Something went wrong! Does the account exist on Hive? Check the spelling!') sys.exit() delegatee_accounts_names = [] delegatee_accounts_names_str = input("Delegations to calculate APR for (comma-separated list, i.e.: account1, account2, account3): ") delegatee_accounts_names = delegatee_accounts_names_str.strip().split(',') paying_accounts_names = {} for delegatee_account_name in delegatee_accounts_names: delegatee_accounts_names[delegatee_accounts_names.index(delegatee_account_name)] = delegatee_account_name.strip() delegatee_account_name = delegatee_account_name.strip() paying_account_name = input('Enter paying account name for delegation at %s (leave empty if it\'s the same): ' % delegatee_account_name) if paying_account_name == "": paying_account_name = delegatee_account_name paying_accounts_names[delegatee_account_name] = paying_account_name print('Paying accounts: %s' % paying_accounts_names) print('----') print('Make sure the start date is older than the first delegation you included in the analysis!') print('Otherwise your first delegation operation won\'t be found, because the algorithm doesn\'t look in reverse history from the start date.') start_date_str = input('Enter start date (YY-MM-DD): ') start_date = datetime.strptime(start_date_str, '%y-%m-%d') stop_date_str = input('Enter stop date (YY-MM-DD, leave empty for today): ') if stop_date_str == "": stop_date = datetime.combine(date.today(), datetime.min.time()) else: stop_date = datetime.strptime(stop_date_str, '%y-%m-%d') # transactions acc_op = ['transfer', 'delegate_vesting_shares'] current_start_date = {} current_stop_date = {} current_delegated_amount_hp = {} current_total_amount_received = {} for transactions in your_account.history(start_date, stop_date, use_block_num=False, only_ops=acc_op): if transactions['type'] == 'delegate_vesting_shares': if transactions['delegator'] != your_account_name: continue if transactions['delegatee'] not in delegatee_accounts_names: continue delegatee_account_name = transactions['delegatee'] if delegatee_account_name in current_start_date: print('----') print('APR before the delegation update for %s:' % delegatee_account_name) current_stop_date[delegatee_account_name] = datetime.strptime(transactions['timestamp'], '%Y-%m-%dT%H:%M:%S') delta = current_stop_date[delegatee_account_name] - current_start_date[delegatee_account_name] #+ timedelta(days=5) # timedelta = 5 days cooldown if current_delegated_amount_hp[delegatee_account_name] > 0 and delta.days > 0: apr = float(current_total_amount_received[paying_accounts_names[delegatee_account_name]] / current_delegated_amount_hp[delegatee_account_name] * 365 / delta.days * 100) else: apr = -1.0 # if delegation or period is zero print('%s had delegated %.3f HP to %s at %s' % (your_account_name, current_delegated_amount_hp[delegatee_account_name], delegatee_account_name, current_start_date[delegatee_account_name])) print('Delegation date: %s' % current_start_date[delegatee_account_name]) print('Stop date: %s' % current_stop_date[delegatee_account_name]) print('Period (in days): %s' % delta.days) print('Total amount received by %s from %s for delegation to % s, in the given period: %.3f HIVE' % (your_account_name, paying_accounts_names[delegatee_account_name], delegatee_account_name, current_total_amount_received[paying_accounts_names[delegatee_account_name]])) if apr != -1.0: print('APR during the period=%.2f%%' % apr) else: print('APR: Undefined - either the period or the delegation is zero!') current_start_date[delegatee_account_name] = current_stop_date[delegatee_account_name] current_stop_date[delegatee_account_name] = stop_date current_total_amount_received[delegatee_account_name] = 0.0 else: current_start_date[delegatee_account_name] = datetime.strptime(transactions['timestamp'], '%Y-%m-%dT%H:%M:%S') current_stop_date[delegatee_account_name] = stop_date current_delegated_amount_hp[delegatee_account_name] = (float(transactions['vesting_shares']['amount']) / 1000000) * (blockchain_instance.get_hive_per_mvest() / 1000000) if transactions['type'] == 'transfer': if transactions['from'] not in paying_accounts_names.values(): continue if transactions['from'] in current_total_amount_received: current_total_amount_received[transactions['from']] += float(transactions['amount']['amount']) / 1000 else: current_total_amount_received[transactions['from']] = float(transactions['amount']['amount']) / 1000 for delegatee_account_name in delegatee_accounts_names: delta = current_stop_date[delegatee_account_name] - current_start_date[delegatee_account_name] #+ timedelta(days=5) # timedelta = 5 days cooldown if current_delegated_amount_hp[delegatee_account_name] > 0 and delta.days > 0: apr = float(current_total_amount_received[paying_accounts_names[delegatee_account_name]] / current_delegated_amount_hp[delegatee_account_name] * 365 / delta.days * 100) else: apr = -1.0 # if delegation or period is zero print('----') print('%s delegated %.3f HP to %s at %s' % (your_account_name, current_delegated_amount_hp[delegatee_account_name], delegatee_account_name, current_start_date[delegatee_account_name])) print('Delegation date: %s' % current_start_date[delegatee_account_name]) print('Stop date: %s' % current_stop_date[delegatee_account_name]) print('Period (in days): %s' % delta.days) print('Total amount received by %s from %s for delegation to % s, in the given period: %.3f HIVE' % (your_account_name, paying_accounts_names[delegatee_account_name], delegatee_account_name, current_total_amount_received[paying_accounts_names[delegatee_account_name]])) if apr != -1.0: print('APR during the period=%.2f%%' % apr) else: print('APR: Undefined - either the period or the delegation is zero!') ``` *Code EDIT: Removed the unnecessary posting key request and making a few print outputs a little clearer.*
author | gadrian |
---|---|
permlink | calculate-the-apr-of-your-delegations-not-dlease |
category | hive-139531 |
json_metadata | "{"app":"peakd/2020.06.1","format":"markdown","description":"You can calculate the APR of your delegations (other than dlease) using this Python script.","tags":["delegations-apr","python","leofinance"],"image":[]}" |
created | 2020-06-16 12:55:54 |
last_update | 2020-06-17 09:16:33 |
depth | 0 |
children | 7 |
last_payout | 2020-06-23 12:55:54 |
cashout_time | 1969-12-31 23:59:59 |
total_payout_value | 2.871 HBD |
curator_payout_value | 2.688 HBD |
pending_payout_value | 0.000 HBD |
promoted | 0.000 HBD |
body_length | 9,126 |
author_reputation | 643,815,277,325,139 |
root_title | "Calculate the APR of your delegations (not dlease)" |
beneficiaries | [] |
max_accepted_payout | 1,000,000.000 HBD |
percent_hbd | 10,000 |
post_id | 97,999,106 |
net_rshares | 13,623,326,395,133 |
author_curate_reward | "" |
voter | weight | wgt% | rshares | pct | time |
---|---|---|---|---|---|
chitty | 0 | 297,421,920,972 | 75% | ||
diana.catherine | 0 | 6,390,634,345 | 25% | ||
chrispop | 0 | 758,673,482 | 50% | ||
discernente | 0 | 18,745,102,280 | 2.5% | ||
successforall | 0 | 125,385,876,451 | 100% | ||
alexvan | 0 | 406,254,203,377 | 100% | ||
tonyz | 0 | 320,529,675,607 | 60% | ||
pibara | 0 | 176,261,728,795 | 100% | ||
netuoso | 0 | 3,113,190,751,082 | 100% | ||
viorel | 0 | 640,144,336 | 75% | ||
techtek | 0 | 36,559,578,989 | 80% | ||
free999enigma | 0 | 48,638,629,868 | 25% | ||
crokkon | 0 | 69,668,323,438 | 100% | ||
lishu | 0 | 9,489,968,128 | 50% | ||
mytechtrail | 0 | 39,051,602,173 | 15% | ||
sunnyali | 0 | 2,090,611,573 | 50% | ||
b00m | 0 | 34,782,563,647 | 25% | ||
alexandraioana26 | 0 | 18,881,999,894 | 50% | ||
kamchore | 0 | 302,389,127,465 | 100% | ||
fourfourfun | 0 | 800,648,190 | 3.12% | ||
mmmmkkkk311 | 0 | 173,534,845,758 | 2.5% | ||
bala41288 | 0 | 120,028,445,712 | 50% | ||
rbm | 0 | 4,054,228,769 | 75% | ||
holger80 | 0 | 2,064,869,655,761 | 50% | ||
ruth-elise | 0 | 931,097,812 | 50% | ||
alexdory | 0 | 85,422,664,684 | 100% | ||
vargart | 0 | 1,507,785,145 | 25% | ||
erikah | 0 | 284,374,652,423 | 50% | ||
alphasteem | 0 | 11,817,196,651 | 10% | ||
onepercentbetter | 0 | 11,508,149,260 | 5% | ||
photovitamin | 0 | 1,143,000,232 | 30% | ||
sbi2 | 0 | 638,716,305,821 | 53.53% | ||
blainjones | 0 | 1,273,346,593 | 3.75% | ||
acesontop | 0 | 30,975,156,411 | 100% | ||
steemromania | 0 | 119,789,777,883 | 100% | ||
promobot | 0 | 24,692,523,023 | 12.5% | ||
spamfarmer | 0 | 2,055,061,321 | 50% | ||
merlion | 0 | 1,910,753,579 | 5% | ||
digital.mine | 0 | 86,606,498,331 | 0.4% | ||
carolinaelly | 0 | 2,533,201,158 | 100% | ||
ressolid | 0 | 6,039,899,862 | 50% | ||
ro-witness | 0 | 69,683,093,406 | 30% | ||
lux-witness | 0 | 7,939,215,650 | 75% | ||
zuerich | 0 | 752,959,955,168 | 20% | ||
fullnodeupdate | 0 | 11,817,475,916 | 50% | ||
laissez-faire | 0 | 73,941,890 | 100% | ||
sorin.lite | 0 | 38,587,639,132 | 100% | ||
dein-problem | 0 | -16,452,497 | -0.5% | ||
ctime | 0 | 595,929,667,585 | 5% | ||
ga-sm | 0 | 750,665,936 | 100% | ||
hungrybear | 0 | 319,281,699 | 5% | ||
steemstreems | 0 | 2,244,728,715 | 10% | ||
likwid | 0 | 3,240,407,590,941 | 12.5% | ||
ph1102 | 0 | 108,942,235,735 | 25% | ||
imbartley | 0 | 1,570,198,502 | 50% | ||
steem-key | 0 | 901,051,699 | 67% | ||
taskmaster4450le | 0 | 11,635,666,161 | 20% | ||
sbi-tokens | 0 | 1,299,119,968 | 7.74% | ||
bilpcoinbpc | 0 | 643,519,293 | 25% | ||
gitplait | 0 | 61,306,615,261 | 100% | ||
mynima | 0 | 139,230,788 | 12% | ||
peterpanpan | 0 | 181,850,352 | 2% | ||
behiver | 0 | 13,320,046,400 | 100% | ||
photoark | 0 | 974,047,152 | 100% |
I have picked your post for my daily hive voting initiative, Keep it up and Hive On!!
author | chitty |
---|---|
permlink | re-calculate-the-apr-of-your-delegations-not-dlease-20200617t000520 |
category | hive-139531 |
json_metadata | "" |
created | 2020-06-17 00:05:21 |
last_update | 2020-06-17 00:05:21 |
depth | 1 |
children | 1 |
last_payout | 2020-06-24 00:05:21 |
cashout_time | 1969-12-31 23:59:59 |
total_payout_value | 0.000 HBD |
curator_payout_value | 0.000 HBD |
pending_payout_value | 0.000 HBD |
promoted | 0.000 HBD |
body_length | 86 |
author_reputation | 86,901,300,608,582 |
root_title | "Calculate the APR of your delegations (not dlease)" |
beneficiaries | [] |
max_accepted_payout | 1,000,000.000 HBD |
percent_hbd | 10,000 |
post_id | 98,009,329 |
net_rshares | 0 |
Thanks!
author | gadrian |
---|---|
permlink | re-chitty-qc2a01 |
category | hive-139531 |
json_metadata | {"tags":["hive-139531"],"app":"peakd/2020.05.5"} |
created | 2020-06-17 08:24:03 |
last_update | 2020-06-17 08:24:03 |
depth | 2 |
children | 0 |
last_payout | 2020-06-24 08:24:03 |
cashout_time | 1969-12-31 23:59:59 |
total_payout_value | 0.000 HBD |
curator_payout_value | 0.000 HBD |
pending_payout_value | 0.000 HBD |
promoted | 0.000 HBD |
body_length | 7 |
author_reputation | 643,815,277,325,139 |
root_title | "Calculate the APR of your delegations (not dlease)" |
beneficiaries | [] |
max_accepted_payout | 1,000,000.000 HBD |
percent_hbd | 10,000 |
post_id | 98,014,671 |
net_rshares | 0 |
.
author | crokkon |
---|---|
permlink | re-gadrian-qc1aah |
category | hive-139531 |
json_metadata | "{"app": ""}" |
created | 2020-06-16 19:32:33 |
last_update | 2022-08-06 16:02:57 |
depth | 1 |
children | 1 |
last_payout | 2020-06-23 19:32:33 |
cashout_time | 1969-12-31 23:59:59 |
total_payout_value | 0.010 HBD |
curator_payout_value | 0.010 HBD |
pending_payout_value | 0.000 HBD |
promoted | 0.000 HBD |
body_length | 1 |
author_reputation | 81,214,366,861,104 |
root_title | "Calculate the APR of your delegations (not dlease)" |
beneficiaries | [] |
max_accepted_payout | 1,000,000.000 HBD |
percent_hbd | 10,000 |
post_id | 98,005,507 |
net_rshares | 82,839,061,093 |
author_curate_reward | "" |
voter | weight | wgt% | rshares | pct | time |
---|---|---|---|---|---|
gadrian | 0 | 82,839,061,093 | 50% |
Yep, you are right! I reused a script and forgot to take that part out. Thanks for pointing it out.
author | gadrian | ||||||
---|---|---|---|---|---|---|---|
permlink | re-crokkon-2020616t23504537z | ||||||
category | hive-139531 | ||||||
json_metadata | {"tags":["hive-139531"],"app":"esteem/2.2.5-mobile","format":"markdown+html","community":"hive-125125"} | ||||||
created | 2020-06-16 20:50:45 | ||||||
last_update | 2020-06-16 20:50:45 | ||||||
depth | 2 | ||||||
children | 0 | ||||||
last_payout | 2020-06-23 20:50:45 | ||||||
cashout_time | 1969-12-31 23:59:59 | ||||||
total_payout_value | 0.000 HBD | ||||||
curator_payout_value | 0.000 HBD | ||||||
pending_payout_value | 0.000 HBD | ||||||
promoted | 0.000 HBD | ||||||
body_length | 99 | ||||||
author_reputation | 643,815,277,325,139 | ||||||
root_title | "Calculate the APR of your delegations (not dlease)" | ||||||
beneficiaries |
| ||||||
max_accepted_payout | 1,000,000.000 HBD | ||||||
percent_hbd | 10,000 | ||||||
post_id | 98,006,804 | ||||||
net_rshares | 0 |
This is the way to go. Make your own path to move forward. Your efforts are laudable. It will be helpful for the people who are looking for APR Calculations. Keep it up your good work. Gitplait is looking for a user kike you. Your post has been curated with @gitplait community account because this is the kind of publications we like to see in our community. Join our Community on [Hive](https://hive.blog/trending/hive-103590) and Chat with us on [Discord](https://discord.com/invite/CWCj3rw). [Gitplait-Team]
author | gitplait-mod1 |
---|---|
permlink | qc0sm7 |
category | hive-139531 |
json_metadata | {"users":["gitplait"],"links":["https://hive.blog/trending/hive-103590","https://discord.com/invite/CWCj3rw"],"app":"hiveblog/0.1"} |
created | 2020-06-16 13:10:57 |
last_update | 2020-06-16 13:10:57 |
depth | 1 |
children | 1 |
last_payout | 2020-06-23 13:10:57 |
cashout_time | 1969-12-31 23:59:59 |
total_payout_value | 0.014 HBD |
curator_payout_value | 0.014 HBD |
pending_payout_value | 0.000 HBD |
promoted | 0.000 HBD |
body_length | 515 |
author_reputation | 64,455,719,431 |
root_title | "Calculate the APR of your delegations (not dlease)" |
beneficiaries | [] |
max_accepted_payout | 1,000,000.000 HBD |
percent_hbd | 10,000 |
post_id | 97,999,340 |
net_rshares | 111,755,682,235 |
author_curate_reward | "" |
voter | weight | wgt% | rshares | pct | time |
---|---|---|---|---|---|
gadrian | 0 | 49,486,039,528 | 30% | ||
gitplait | 0 | 62,269,642,707 | 100% |
Thanks, appreciate it!
author | gadrian |
---|---|
permlink | re-gitplait-mod1-qc0zsf |
category | hive-139531 |
json_metadata | {"tags":["hive-139531"],"app":"peakd/2020.05.5"} |
created | 2020-06-16 15:45:54 |
last_update | 2020-06-16 15:45:54 |
depth | 2 |
children | 0 |
last_payout | 2020-06-23 15:45:54 |
cashout_time | 1969-12-31 23:59:59 |
total_payout_value | 0.000 HBD |
curator_payout_value | 0.000 HBD |
pending_payout_value | 0.000 HBD |
promoted | 0.000 HBD |
body_length | 22 |
author_reputation | 643,815,277,325,139 |
root_title | "Calculate the APR of your delegations (not dlease)" |
beneficiaries | [] |
max_accepted_payout | 1,000,000.000 HBD |
percent_hbd | 10,000 |
post_id | 98,002,013 |
net_rshares | 0 |
Congratulations @gadrian! You have completed the following achievement on the Hive blockchain and have been rewarded with new badge(s) : <table><tr><td><img src="https://images.hive.blog/60x70/http://hivebuzz.me/@gadrian/replies.png?202006181555"></td><td>You got more than 3250 replies. Your next target is to reach 3500 replies.</td></tr> </table> <sub>_You can view [your badges on your board](https://hivebuzz.me/@gadrian) And compare to others on the [Ranking](https://hivebuzz.me/ranking)_</sub> <sub>_If you no longer want to receive notifications, reply to this comment with the word_ `STOP`</sub> ###### Support the HiveBuzz project. [Vote](https://hivesigner.com/sign/update_proposal_votes?proposal_ids=%5B%22109%22%5D&approve=true) for [our proposal](https://peakd.com/me/proposals/109)!
author | hivebuzz |
---|---|
permlink | hivebuzz-notify-gadrian-20200618t162756000z |
category | hive-139531 |
json_metadata | {"image":["http://hivebuzz.me/notify.t6.png"]} |
created | 2020-06-18 16:27:57 |
last_update | 2020-06-18 16:27:57 |
depth | 1 |
children | 0 |
last_payout | 2020-06-25 16:27:57 |
cashout_time | 1969-12-31 23:59:59 |
total_payout_value | 0.000 HBD |
curator_payout_value | 0.000 HBD |
pending_payout_value | 0.000 HBD |
promoted | 0.000 HBD |
body_length | 803 |
author_reputation | 369,874,061,244,718 |
root_title | "Calculate the APR of your delegations (not dlease)" |
beneficiaries | [] |
max_accepted_payout | 1,000,000.000 HBD |
percent_hbd | 10,000 |
post_id | 98,041,396 |
net_rshares | 0 |