|
|
@ -1,18 +1,17 @@
|
|
|
|
# Twisted imports
|
|
|
|
# Twisted imports
|
|
|
|
|
|
|
|
import asyncio
|
|
|
|
import logging
|
|
|
|
import logging
|
|
|
|
from datetime import datetime
|
|
|
|
from datetime import datetime
|
|
|
|
import asyncio
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import urllib3
|
|
|
|
import urllib3
|
|
|
|
|
|
|
|
|
|
|
|
# Other library imports
|
|
|
|
|
|
|
|
from core.models import Aggregator, Platform
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
from aiocoingecko import AsyncCoinGeckoAPISession
|
|
|
|
from aiocoingecko import AsyncCoinGeckoAPISession
|
|
|
|
from django.conf import settings
|
|
|
|
from django.conf import settings
|
|
|
|
from elasticsearch import AsyncElasticsearch
|
|
|
|
from elasticsearch import AsyncElasticsearch
|
|
|
|
from forex_python.converter import CurrencyRates
|
|
|
|
from forex_python.converter import CurrencyRates
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Other library imports
|
|
|
|
|
|
|
|
from core.models import Aggregator, Platform
|
|
|
|
|
|
|
|
|
|
|
|
# TODO: secure ES traffic properly
|
|
|
|
# TODO: secure ES traffic properly
|
|
|
|
urllib3.disable_warnings()
|
|
|
|
urllib3.disable_warnings()
|
|
|
|
|
|
|
|
|
|
|
@ -47,19 +46,17 @@ class Money(object):
|
|
|
|
"""
|
|
|
|
"""
|
|
|
|
if not all([user, nordigen, agora]):
|
|
|
|
if not all([user, nordigen, agora]):
|
|
|
|
raise Exception
|
|
|
|
raise Exception
|
|
|
|
|
|
|
|
|
|
|
|
# I hate circular dependencies
|
|
|
|
# I hate circular dependencies
|
|
|
|
self.nordigen = nordigen
|
|
|
|
self.nordigen = nordigen
|
|
|
|
self.agora = agora
|
|
|
|
self.agora = agora
|
|
|
|
|
|
|
|
|
|
|
|
aggregators = Aggregator.objects.filter(user=user, enabled=True)
|
|
|
|
aggregators = Aggregator.objects.filter(user=user, enabled=True)
|
|
|
|
platforms = Platform.objects.filter(user=user, enabled=True)
|
|
|
|
platforms = Platform.objects.filter(user=user, enabled=True)
|
|
|
|
|
|
|
|
|
|
|
|
total = await self.get_total(aggregators, platforms, trades=True)
|
|
|
|
total = await self.get_total(aggregators, platforms, trades=True)
|
|
|
|
|
|
|
|
|
|
|
|
return total
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return total
|
|
|
|
|
|
|
|
|
|
|
|
# def setup_loops(self):
|
|
|
|
# def setup_loops(self):
|
|
|
|
# """
|
|
|
|
# """
|
|
|
@ -113,26 +110,27 @@ class Money(object):
|
|
|
|
rates = self.cr.get_rates("USD")
|
|
|
|
rates = self.cr.get_rates("USD")
|
|
|
|
return rates
|
|
|
|
return rates
|
|
|
|
|
|
|
|
|
|
|
|
async def get_acceptable_margins(self, platform, currency, amount):
|
|
|
|
# TODO: pass platform
|
|
|
|
"""
|
|
|
|
# async def get_acceptable_margins(self, platform, currency, amount):
|
|
|
|
Get the minimum and maximum amounts we would accept a trade for.
|
|
|
|
# """
|
|
|
|
:param currency: currency code
|
|
|
|
# Get the minimum and maximum amounts we would accept a trade for.
|
|
|
|
:param amount: amount
|
|
|
|
# :param currency: currency code
|
|
|
|
:return: (min, max)
|
|
|
|
# :param amount: amount
|
|
|
|
:rtype: tuple
|
|
|
|
# :return: (min, max)
|
|
|
|
"""
|
|
|
|
# :rtype: tuple
|
|
|
|
sets = util.get_settings(platform)
|
|
|
|
# """
|
|
|
|
rates = await self.get_rates_all()
|
|
|
|
# sets = util.get_settings(platform)
|
|
|
|
if currency == "USD":
|
|
|
|
# rates = await self.get_rates_all()
|
|
|
|
min_amount = amount - float(sets.AcceptableUSDMargin)
|
|
|
|
# if currency == "USD":
|
|
|
|
max_amount = amount + float(sets.AcceptableUSDMargin)
|
|
|
|
# min_amount = amount - float(sets.AcceptableUSDMargin)
|
|
|
|
return (min_amount, max_amount)
|
|
|
|
# max_amount = amount + float(sets.AcceptableUSDMargin)
|
|
|
|
amount_usd = amount / rates[currency]
|
|
|
|
# return (min_amount, max_amount)
|
|
|
|
min_usd = amount_usd - float(sets.AcceptableUSDMargin)
|
|
|
|
# amount_usd = amount / rates[currency]
|
|
|
|
max_usd = amount_usd + float(sets.AcceptableUSDMargin)
|
|
|
|
# min_usd = amount_usd - float(sets.AcceptableUSDMargin)
|
|
|
|
min_local = min_usd * rates[currency]
|
|
|
|
# max_usd = amount_usd + float(sets.AcceptableUSDMargin)
|
|
|
|
max_local = max_usd * rates[currency]
|
|
|
|
# min_local = min_usd * rates[currency]
|
|
|
|
return (min_local, max_local)
|
|
|
|
# max_local = max_usd * rates[currency]
|
|
|
|
|
|
|
|
# return (min_local, max_local)
|
|
|
|
|
|
|
|
|
|
|
|
async def get_minmax(self, min_usd, max_usd, asset, currency):
|
|
|
|
async def get_minmax(self, min_usd, max_usd, asset, currency):
|
|
|
|
rates = await self.get_rates_all()
|
|
|
|
rates = await self.get_rates_all()
|
|
|
@ -246,7 +244,7 @@ class Money(object):
|
|
|
|
total = sum(total)
|
|
|
|
total = sum(total)
|
|
|
|
|
|
|
|
|
|
|
|
return total
|
|
|
|
return total
|
|
|
|
|
|
|
|
|
|
|
|
async def gather_wallet_balance_xmr(self, platforms):
|
|
|
|
async def gather_wallet_balance_xmr(self, platforms):
|
|
|
|
"""
|
|
|
|
"""
|
|
|
|
Gather the total XMR of the specified platforms.
|
|
|
|
Gather the total XMR of the specified platforms.
|
|
|
@ -274,19 +272,18 @@ class Money(object):
|
|
|
|
btc = [float(x["response"]["data"]["total"]["balance"]) for x in btc_pre]
|
|
|
|
btc = [float(x["response"]["data"]["total"]["balance"]) for x in btc_pre]
|
|
|
|
btc = sum(btc)
|
|
|
|
btc = sum(btc)
|
|
|
|
return btc
|
|
|
|
return btc
|
|
|
|
|
|
|
|
|
|
|
|
def gather_base_usd(self, platforms):
|
|
|
|
def gather_base_usd(self, platforms):
|
|
|
|
total = 0
|
|
|
|
total = 0
|
|
|
|
for platform in platforms:
|
|
|
|
for platform in platforms:
|
|
|
|
total += platform.base_usd
|
|
|
|
total += platform.base_usd
|
|
|
|
return total
|
|
|
|
return total
|
|
|
|
|
|
|
|
|
|
|
|
def gather_withdrawal_limit(self, platforms):
|
|
|
|
def gather_withdrawal_limit(self, platforms):
|
|
|
|
total = 0
|
|
|
|
total = 0
|
|
|
|
for platform in platforms:
|
|
|
|
for platform in platforms:
|
|
|
|
total += platform.withdrawal_trigger
|
|
|
|
total += platform.withdrawal_trigger
|
|
|
|
return total
|
|
|
|
return total
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# TODO: possibly refactor this into smaller functions which don't return as much
|
|
|
|
# TODO: possibly refactor this into smaller functions which don't return as much
|
|
|
|
# check if this is all really needed in the corresponding withdraw function
|
|
|
|
# check if this is all really needed in the corresponding withdraw function
|
|
|
@ -334,7 +331,6 @@ class Money(object):
|
|
|
|
|
|
|
|
|
|
|
|
profit = total_usd - total_base_usd
|
|
|
|
profit = total_usd - total_base_usd
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Convert the total USD price to GBP and SEK
|
|
|
|
# Convert the total USD price to GBP and SEK
|
|
|
|
price_sek = rates["SEK"] * total_usd
|
|
|
|
price_sek = rates["SEK"] * total_usd
|
|
|
|
price_usd = total_usd
|
|
|
|
price_usd = total_usd
|
|
|
@ -353,8 +349,6 @@ class Money(object):
|
|
|
|
|
|
|
|
|
|
|
|
total_profit = total_with_trades - total_base_usd
|
|
|
|
total_profit = total_with_trades - total_base_usd
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# cast = (
|
|
|
|
# cast = (
|
|
|
|
# (
|
|
|
|
# (
|
|
|
|
# price_sek,
|
|
|
|
# price_sek,
|
|
|
|