import ccxt_wrapper as ccxt
import time
import psycopg2
+import redis
from store import *
from cachetools.func import ttl_cache
from datetime import datetime
self.balances = BalanceStore(self)
self.processor = Processor(self)
- for key in ["user_id", "market_id", "pg_config"]:
+ for key in ["user_id", "market_id", "pg_config", "redis_config"]:
setattr(self, key, kwargs.get(key, None))
self.report.log_market(self.args)
self.store_file_report(date)
if self.pg_config is not None and self.args.report_db:
self.store_database_report(date)
+ if self.redis_config is not None and self.args.report_redis:
+ self.store_redis_report(date)
def store_file_report(self, date):
try:
except Exception as e:
print("impossible to store report to database: {}; {}".format(e.__class__.__name__, e))
+ def store_redis_report(self, date):
+ try:
+ conn = redis.Redis(**self.redis_config)
+ for type_, log in self.report.to_json_redis():
+ key = "/cryptoportfolio/{}/{}/{}".format(self.market_id, date.isoformat(), type_)
+ conn.set(key, log, ex=31*24*60*60)
+ key = "/cryptoportfolio/{}/latest/{}".format(self.market_id, type_)
+ conn.set(key, log)
+ key = "/cryptoportfolio/{}/latest/date".format(self.market_id)
+ conn.set(key, date.isoformat())
+ except Exception as e:
+ print("impossible to store report to redis: {}; {}".format(e.__class__.__name__, e))
+
def process(self, actions, before=False, after=False):
try:
for action in actions:
self.report.log_stage("follow_orders_end")
def prepare_trades(self, base_currency="BTC", liquidity="medium",
- compute_value="average", repartition=None, only=None):
+ compute_value="average", repartition=None, only=None,
+ available_balance_only=False):
self.report.log_stage("prepare_trades",
base_currency=base_currency, liquidity=liquidity,
compute_value=compute_value, only=only,
- repartition=repartition)
+ repartition=repartition, available_balance_only=available_balance_only)
values_in_base = self.balances.in_currency(base_currency,
compute_value=compute_value)
- total_base_value = sum(values_in_base.values())
+ if available_balance_only:
+ balance = self.balances.all.get(base_currency)
+ if balance is None:
+ total_base_value = portfolio.Amount(base_currency, 0)
+ else:
+ total_base_value = balance.exchange_free + balance.margin_available
+ else:
+ total_base_value = sum(values_in_base.values())
new_repartition = self.balances.dispatch_assets(total_base_value,
liquidity=liquidity, repartition=repartition)
+ if available_balance_only:
+ for currency, amount in values_in_base.items():
+ if currency != base_currency:
+ new_repartition.setdefault(currency, portfolio.Amount(base_currency, 0))
+ new_repartition[currency] += amount
+
self.trades.compute_trades(values_in_base, new_repartition, only=only)
def print_tickers(self, base_currency="BTC"):
"before": False,
"after": True,
"fetch_balances": ["begin", "end"],
- "prepare_trades": { "only": "acquire" },
+ "prepare_trades": { "only": "acquire", "available_balance_only": True },
"prepare_orders": { "only": "acquire", "compute_value": "average" },
"move_balances": {},
"run_orders": {},
"before": False,
"after": True,
"fetch_balances": ["begin", "end"],
- "prepare_trades": {},
+ "prepare_trades": { "available_balance_only": True },
"prepare_orders": { "compute_value": "average" },
"move_balances": {},
"run_orders": {},