Skip to content

Instantly share code, notes, and snippets.

@yuwash
Created January 30, 2020 21:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yuwash/5cf389086a087b7718d3586731069a85 to your computer and use it in GitHub Desktop.
Save yuwash/5cf389086a087b7718d3586731069a85 to your computer and use it in GitHub Desktop.
Calculate optimal standing order for prepaid account
#!/usr/bin/env python
import datetime
current_balance = 450
desired_buffer_days = 30
planned_charge_period_days = 365.25/6
past_charges = [
(datetime.date(2019, 6, 30), 525),
(datetime.date(2019, 7, 22), 1250),
(datetime.date(2019, 12, 1), 500),
]
boundary_days = [d for d, __ in past_charges[:2] + past_charges[-2:]]
earliest_period_days = (boundary_days[1] - boundary_days[0]).days
latest_period_days = (boundary_days[-1] - boundary_days[-2]).days
total_days = (boundary_days[-1] - boundary_days[0]).days + 0.5*(
earliest_period_days + latest_period_days)
daily_cost = sum(x for __, x in past_charges)/total_days
planned_charge = planned_charge_period_days*daily_cost
min_balance = desired_buffer_days*daily_cost
days_until_first_charge = max(
0, int((current_balance - min_balance)/daily_cost))
first_charge_date = datetime.date.today() + datetime.timedelta(
days=days_until_first_charge)
print(
'charge the amount {:d}, start on {}\n'
'daily cost: {:g}, min balance: {:d}'.format(
int(round(planned_charge)), first_charge_date, daily_cost,
int(round(min_balance))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment