Last active
February 13, 2019 01:31
-
-
Save joeykrug/e9b23731bff59b456d348335da2cfc79 to your computer and use it in GitHub Desktop.
Profit and loss calcs for augur
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Whenever user does a trade of trade(outcome, price, amount) (regardless if due to getting filled or them doing a taker order): | |
# amount owned is display amount, raw is raw amount | |
# prior means prior to a trade | |
Goto (goto considered harmful but screw it this is pseudocode) outcome in UI: | |
if(amount > 0): | |
if(amount_owned >= 0): | |
amount_of_shares_closed = 0 | |
prior_shares_remaining = amount_owned | |
sum_of_shares = prior_shares_remaining + amount | |
amount_owned = amount_owned + amount | |
unrealized_pnl = if(ui_price) (last_price - ui_price) * amount_owned else 0 | |
realized_pnl = realized_pnl | |
ui_price = prior_shares_remaining * ui_price / sum_of_shares + price * (abs(amount) - amount_of_shares_closed) / sum_of_shares | |
# positive means it'll cost you, negative means you get paid for this one | |
capital_change_if_closed = if(amount_owned < 0 and only_short_this_outcome) -(max_value-last_price) * amount_owned, else-if(amount_owned >= 0) -last_price * amount_owned, else-if(amount_owned < 0 and short multiple) last_price * amount_owned | |
profits = sum(realized_pnl for all outcomes) + gains from market creator fees + reporting fees gains | |
available_funds = users current eth | |
frozen_profit = if(users_available_funds_before_trade - available_funds < 0 and realized_pnl_from_this_trade): realized_pnl_from_this_trade | |
frozen_funds += users_available_funds_before_trade - available_funds + frozen_profit | |
total_funds = frozen_funds + available_funds | |
assert(total_funds - frozen_funds == available_funds) | |
elif(amount_owned < 0): | |
amount_of_shares_closed = min(amount, abs(amount_owned)) | |
prior_shares_remaining = abs(amount_owned) - amount_of_shares_closed | |
sum_of_shares = prior_shares_remaining + amount | |
amount_owned = amount_owned + amount | |
unrealized pnl = if(ui_price) (last_price - ui_price)*amount_owned if amount_owned > 0, else (ui_price - last_price)*abs(amount_owned) else 0 | |
realized pnl = realized_pnl + (ui_price - price) * amount_of_shares_closed | |
ui_price = prior_shares_remaining * ui_price / sum_of_shares + price * (abs(amount) - amount_of_shares_closed) / sum_of_shares | |
capital_change_if_closed = if(amount_owned < 0 and only_short_this_outcome) -(max_value-last_price) * amount_owned, else-if(amount_owned >= 0) -last_price * amount_owned, else-if(amount_owned < 0 and short multiple) last_price * amount_owned | |
profits = sum(realized_pnl for all outcomes) + gains from market creator fees + reporting fees gains | |
available_funds = users current eth | |
frozen_profit = if(users_available_funds_before_trade - available_funds < 0 and realized_pnl_from_this_trade): realized_pnl_from_this_trade | |
frozen_funds += users_available_funds_before_trade - available_funds + frozen_profit | |
total_funds = frozen_funds + available_funds | |
assert(total_funds - frozen_funds == available_funds) | |
elif(amount < 0): | |
if(amount_owned >= 0): | |
amount_of_shares_closed = min(abs(amount), amount_owned) | |
prior_shares_remaining = amount_owned - amount_of_shares_closed | |
sum_of_shares = prior_shares_remaining + abs(amount) | |
amount_owned = amount_owned + amount | |
unrealized pnl = if(ui_price) (last_price - ui_price)*amount_owned if amount_owned > 0, else (ui_price - last_price)*abs(amount_owned) else 0 | |
realized pnl = realized_pnl + (price - ui_price) * amount_of_shares_closed | |
ui_price = prior_shares_remaining * ui_price / sum_of_shares + price * (abs(amount) - amount_of_shares_closed) / sum_of_shares | |
capital_change_if_closed = if(amount_owned < 0 and only_short_this_outcome) -(max_value-last_price) * amount_owned, else-if(amount_owned >= 0) -last_price * amount_owned, else-if(amount_owned < 0 and short multiple) last_price * amount_owned | |
frozen_funds = frozen_funds + -price * min(prior_raw_amount_owned, abs(amount)) + (max_value-price) * (abs(amount) - min(prior_raw_amount_owned, abs(amount))) | |
available_funds = users current eth | |
profits = sum(realized_pnl for all outcomes) + gains from market creator fees + reporting fees gains | |
available_funds = users current eth | |
frozen_profit = if(users_available_funds_before_trade - available_funds < 0 and realized_pnl_from_this_trade): realized_pnl_from_this_trade | |
frozen_funds += users_available_funds_before_trade - available_funds + frozen_profit | |
total_funds = frozen_funds + available_funds | |
assert(total_funds - frozen_funds == available_funds) | |
elif(amount_owned < 0): | |
amount_of_shares_closed = 0 | |
prior_shares_remaining = abs(amount_owned) | |
sum_of_shares = prior_shares_remaining + abs(amount) | |
amount_owned = amount_owned + amount | |
unrealized pnl = if(ui_price) (ui_price - last_price) * abs(amount_owned) else 0 | |
realized pnl = existing realized pnl | |
ui_price = prior_shares_remaining * ui_price / sum_of_shares + price * (abs(amount) - amount_of_shares_closed) / sum_of_shares | |
capital_change_if_closed = if(amount_owned < 0 and only_short_this_outcome) -(max_value-last_price) * amount_owned, else-if(amount_owned >= 0) -last_price * amount_owned, else-if(amount_owned < 0 and short multiple) last_price * amount_owned | |
frozen_funds = frozen_funds + -price * min(prior_raw_amount_owned, abs(amount)) + (max_value-price) * (abs(amount) - min(prior_raw_amount_owned, abs(amount))) | |
available_funds = users current eth | |
profits = sum(realized_pnl for all outcomes) + gains from market creator fees + reporting fees gains | |
available_funds = users current eth | |
frozen_profit = if(users_available_funds_before_trade - available_funds < 0 and realized_pnl_from_this_trade): realized_pnl_from_this_trade | |
frozen_funds += users_available_funds_before_trade - available_funds + frozen_profit | |
total_funds = frozen_funds + available_funds | |
assert(total_funds - frozen_funds == available_funds) | |
When market resolves and user claims shares the realized pnl has (final_price - ui_price)*amount_owned if amount_owned > 0, else (ui_price)*abs(amount_owned) added to it and unrealized pnl goes to 0 | |
Prior means prior to the trade executing |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment