Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def _update_account(self, symbol: str, trade_type: TradeType, fill_amount: float, fill_price: float):
self._trades = self._trades.append({
'step': self.current_step,
'symbol': symbol,
'type': trade_type,
'amount': fill_amount,
'price': fill_price
}, ignore_index=True)
if trade_type is TradeType.BUY:
self._balance -= fill_amount * fill_price
self._portfolio[symbol] = self._portfolio.get(
symbol, 0) + fill_amount
elif trade_type is TradeType.SELL:
self._balance += fill_amount * fill_price
self._portfolio[symbol] -= fill_amount
self._performance.append({
'balance': self._balance,
'net_worth': self.net_worth(),
}, ignore_index=True)
def get_trade(self, action: Union[int, tuple], exchange: AssetExchange):
"""Suggest a trade to the trading environment
# Arguments
action: optional number of timesteps used to calculate the trade amount
exchange: the AssetExchange
# Returns
the asset symbol, the type of trade, amount and price
"""
action_type, trade_amount = action
trade_type = TradeType(int(action_type * 3))
current_price = exchange.current_price(
symbol=self.asset_symbol, output_symbol=self.base_symbol)
commission_percent = exchange.commission_percent
base_precision = exchange.base_precision
asset_precision = exchange.asset_precision
price = current_price
amount_to_trade = 0
if trade_type == TradeType.BUY:
balance = exchange.balance(self.base_symbol)
price_adjustment = 1 + (commission_percent / 100)
price = round(current_price * price_adjustment, base_precision)
amount_to_trade = round(balance * trade_amount / price, asset_precision)
def get_trade(self, action: Union[int, tuple], exchange: AssetExchange):
"""Suggest a trade to the trading environment
# Arguments
action: optional number of timesteps used to calculate the trade amount
exchange: the AssetExchange
# Returns
the asset symbol, the type of trade, amount and price
"""
trade_type = TradeType(action % len(TradeType))
trade_amount = float(1 / (action % self.n_bins + 1))
current_price = exchange.current_price(
symbol=self.asset_symbol, output_symbol=self.base_symbol)
commission_percent = exchange.commission_percent
base_precision = exchange.base_precision
amount_to_trade = 0
price = current_price
if trade_type == TradeType.BUY:
balance = exchange.balance(self.base_symbol)
price_adjustment = 1 + (commission_percent / 100)
price = round(current_price * price_adjustment, base_precision)
amount_to_trade = round(balance * trade_amount / price, exchange.asset_precision)
def _update_account(self, symbol: str, trade_type: TradeType, fill_amount: float, fill_price: float):
self._trades = self._trades.append({
'step': self.current_step,
'symbol': symbol,
'type': trade_type,
'amount': fill_amount,
'price': fill_price
}, ignore_index=True)
if trade_type is TradeType.BUY:
self._balance -= fill_amount * fill_price
self._portfolio[symbol] = self._portfolio.get(
symbol, 0) + fill_amount
elif trade_type is TradeType.SELL:
self._balance += fill_amount * fill_price
self._portfolio[symbol] -= fill_amount
self._performance.append({
'balance': self._balance,
'net_worth': self.net_worth(),
}, ignore_index=True)
current_price = exchange.current_price(
symbol=self.asset_symbol, output_symbol=self.base_symbol)
commission_percent = exchange.commission_percent
base_precision = exchange.base_precision
asset_precision = exchange.asset_precision
price = current_price
amount_to_trade = 0
if trade_type == TradeType.BUY:
balance = exchange.balance(self.base_symbol)
price_adjustment = 1 + (commission_percent / 100)
price = round(current_price * price_adjustment, base_precision)
amount_to_trade = round(balance * trade_amount / price, asset_precision)
elif trade_type == TradeType.SELL:
portfolio = exchange.portfolio()
price_adjustment = 1 + (commission_percent / 100)
price = round(current_price * price_adjustment, base_precision)
amount_to_trade = round(portfolio.get(self.asset_symbol, 0)
* trade_amount, asset_precision)
return self.asset_symbol, trade_type, amount_to_trade, price