Skip to content

Instantly share code, notes, and snippets.

@TechNickAI
Last active May 11, 2020 20:25
Show Gist options
  • Save TechNickAI/e549ec4966ea860f2fdb6462b12b8380 to your computer and use it in GitHub Desktop.
Save TechNickAI/e549ec4966ea860f2fdb6462b12b8380 to your computer and use it in GitHub Desktop.
Pulling earnings data from bitfinex
def pull_earnings_data(self):
""" Fetch and store interest performance data (returns) """
c = self.api_client
since = timestamp_from_date(datetime.datetime.utcnow() - datetime.timedelta(days=30))
# Don't supply the wallet' param or you will only get 1 days worth of data. See
# https://docs.bitfinex.com/reference#rest-auth-balance-history
# TODO: Upgrade to v2 API
history = c.brequest(1, "history", authenticate=True, method="POST",
data={"limit": 5000, "since": since, "currency": self.lba.currency.abbr})
created = 0
history.reverse() # Reverse the order so that we get to fill the start/end balance in correctly
for ledger in history:
if ledger["currency"] != self.lba.currency.abbr:
continue
if ledger["description"].lower() != "margin funding payment on wallet deposit": # This is sometimes a different case
continue
timestamp = date_from_timestamp(int(float(ledger["timestamp"])))
# Note the timestamp supplied is for ~2am the day *after* the interest is for. Reset for reporting clarity
day = timestamp - datetime.timedelta(days=1)
if not DailyEarning.objects.filter(lend_bot_account=self.lba, day = day).exists():
created += 1
DailyEarning.objects.create(
lend_bot_account=self.lba,
day = day,
earning = D(ledger["amount"]),
end_balance = D(ledger["balance"])
)
return created
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment