added ledger and API for profit/loss retrieval

This commit is contained in:
Giulio De Pasquale 2020-12-24 13:47:33 +00:00
parent f9e3ad500c
commit 54878ef323

View File

@ -1,4 +1,5 @@
import asyncio import asyncio
import time
from typing import Dict, List, Optional, Tuple from typing import Dict, List, Optional, Tuple
from bfxapi import Order from bfxapi import Order
@ -23,6 +24,8 @@ class BfxBot:
self.__ticker: Ticker = Ticker(tick_duration) self.__ticker: Ticker = Ticker(tick_duration)
self.__status: Dict[TradingPair, SymbolStatus] = {} self.__status: Dict[TradingPair, SymbolStatus] = {}
self.__quote: Symbol = quote self.__quote: Symbol = quote
self.__account_info = None
self.__ledger = None
if isinstance(symbols, TradingPair): if isinstance(symbols, TradingPair):
symbols = [symbols] symbols = [symbols]
@ -112,6 +115,9 @@ class BfxBot:
async def get_balances(self): async def get_balances(self):
return await self.__bfx.get_current_balances(self.__quote) return await self.__bfx.get_current_balances(self.__quote)
async def get_profit_loss(self, start: int, end: int):
return await self.__bfx.profit_loss(start, end, self.__ledger, self.__quote)
def set_strategy(self, symbol, strategy: Strategy): def set_strategy(self, symbol, strategy: Strategy):
if symbol in self.__status: if symbol in self.__status:
self.__status[symbol].strategy = strategy self.__status[symbol].strategy = strategy
@ -119,6 +125,9 @@ class BfxBot:
self.__status[symbol] = SymbolStatus(symbol, strategy) self.__status[symbol] = SymbolStatus(symbol, strategy)
async def start(self): async def start(self):
self.__account_info = self.__bfx.get_account_information()
self.__ledger = await self.__bfx.ledger_history(0, time.time() * 1000)
await self.__update_status__() await self.__update_status__()
def symbol_event_handler(self, symbol) -> Optional[EventHandler]: def symbol_event_handler(self, symbol) -> Optional[EventHandler]:
@ -137,3 +146,6 @@ class BfxBot:
await asyncio.sleep(self.__ticker.seconds) await asyncio.sleep(self.__ticker.seconds)
self.__ticker.inc() self.__ticker.inc()
await self.__update_status__() await self.__update_status__()
async def __update_ledger(self):
self.__ledger = await self.__bfx.ledger_history(0, time.time() * 1000)