Skip to content

Commit

Permalink
2.1.4
Browse files Browse the repository at this point in the history
  • Loading branch information
DogsTailFarmer committed Feb 22, 2024
1 parent b79293e commit a55d3f3
Show file tree
Hide file tree
Showing 7 changed files with 128 additions and 117 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
## 2.1.4 - 2024-02-22
### Fix
* `on_new_funds()`

### Update
* Refine initialization backtesting
* Up requirements for exchanges-wrapper==1.4.15

## 2.1.3 - 2024-02-20
### Fix
* `calc_profit_order()`: rounding for `target_amount_first`
Expand Down
2 changes: 1 addition & 1 deletion martin_binance/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
__author__ = "Jerry Fedorenko"
__copyright__ = "Copyright © 2021 Jerry Fedorenko aka VM"
__license__ = "MIT"
__version__ = "2.1.3"
__version__ = "2.1.4"
__maintainer__ = "Jerry Fedorenko"
__contact__ = "https://github.com/DogsTailFarmer"

Expand Down
80 changes: 37 additions & 43 deletions martin_binance/backtest/exchange_simulator.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
__author__ = "Jerry Fedorenko"
__copyright__ = "Copyright © 2021 Jerry Fedorenko aka VM"
__license__ = "MIT"
__version__ = "2.1.0"
__version__ = "2.1.4"
__maintainer__ = "Jerry Fedorenko"
__contact__ = "https://github.com/DogsTailFarmer"

Expand All @@ -27,44 +27,35 @@ def __init__(self):
self.quote = {}

def get_funds(self):
return [self.base, self.quote]
base = self.base.copy()
base |= {'free': str(base['free']), 'locked': str(base['locked'])}
quote = self.quote.copy()
quote |= {'free': str(quote['free']), 'locked': str(quote['locked'])}
return [base, quote]

def on_order_created(self, buy: bool, amount: str, price: str):
def on_order_created(self, buy: bool, amount: Decimal, price: Decimal):
if buy:
self.quote['free'] = str(Decimal(self.quote.get('free', 0)) - Decimal(amount) * Decimal(price))
self.quote['locked'] = str(Decimal(self.quote.get('locked', 0)) + Decimal(amount) * Decimal(price))
self.quote['free'] -= amount * price
self.quote['locked'] += amount * price
else:
self.base['free'] = str(Decimal(self.base.get('free', 0)) - Decimal(amount))
self.base['locked'] = str(Decimal(self.base.get('locked', 0)) + Decimal(amount))
self.base['free'] -= amount
self.base['locked'] += amount

def on_order_canceled(self, side: str, amount: str, price: str):
def on_order_canceled(self, side: str, amount: Decimal, price: Decimal):
if side == 'BUY':
self.quote['free'] = str(Decimal(self.quote.get('free', 0)) + Decimal(amount) * Decimal(price))
self.quote['locked'] = str(Decimal(self.quote.get('locked', 0)) - Decimal(amount) * Decimal(price))
self.quote['free'] += amount * price
self.quote['locked'] -= amount * price
else:
self.base['free'] = str(Decimal(self.base.get('free', 0)) + Decimal(amount))
self.base['locked'] = str(Decimal(self.base.get('locked', 0)) - Decimal(amount))

def on_order_filled(self, side: str, amount: str, price: str, fee: Decimal):
_amount = Decimal(amount)
_price = Decimal(price)

base_free = Decimal(self.base.get('free', 0))
base_locked = Decimal(self.base.get('locked', 0))

quote_free = Decimal(self.quote.get('free', 0))
quote_locked = Decimal(self.quote.get('locked', 0))
self.base['free'] += amount
self.base['locked'] -= amount

def on_order_filled(self, side: str, amount: Decimal, price: Decimal, fee: Decimal):
if side == 'BUY':
quote_locked -= _amount * _price
base_free += _amount - fee * _amount / 100
self.quote['locked'] = str(quote_locked)
self.base['free'] = str(base_free)
self.base['free'] += amount - fee * amount / 100
self.quote['locked'] -= amount * price
else:
base_locked -= _amount
quote_free += _amount * _price - fee * (_amount * _price) / 100
self.base['locked'] = str(base_locked)
self.quote['free'] = str(quote_free)
self.base['locked'] -= amount
self.quote['free'] += amount * price - fee * (amount * price) / 100


class Order:
Expand Down Expand Up @@ -183,7 +174,7 @@ def create_order(
if self.save_ds:
self.grid_sell[lt] = self.orders_sell
#
self.funds.on_order_created(buy=buy, amount=amount, price=price)
self.funds.on_order_created(buy=buy, amount=Decimal(amount), price=Decimal(price))
self.orders[order_id] = order

if self.ticker_last and ((buy and Decimal(price) >= self.ticker_last) or
Expand Down Expand Up @@ -226,7 +217,7 @@ def cancel_order(self, order_id: int, ts: int):
raise UserWarning(f"Order {order_id} not active: {ex}") from ex
else:
self.orders[order_id] = order
self.funds.on_order_canceled(order.side, order.orig_qty, order.price)
self.funds.on_order_canceled(order.side, Decimal(order.orig_qty), Decimal(order.price))
return {'symbol': order.symbol,
'origClientOrderId': order.client_order_id,
'orderId': order.order_id,
Expand Down Expand Up @@ -322,11 +313,23 @@ def on_ticker_update(self, ticker: {}, ts: int) -> [dict]:
'quote_order_quantity': order.quote_order_quantity}
#
orders_filled.append(res)
self.funds.on_order_filled(order.side, order.orig_qty, order.last_executed_price, self.fee_maker)
self.funds.on_order_filled(
order.side,
Decimal(order.orig_qty),
Decimal(order.last_executed_price),
self.fee_maker
)
#
return orders_filled

def restore_state(self, symbol: str, lt: int, orders: [], tp=()):
def restore_state(self, symbol: str, lt: int, orders: [], sum_amount: ()):
if sum_amount[0]:
self.funds.base['free'] += sum_amount[1]
self.funds.quote['free'] -= sum_amount[2]
else:
self.funds.base['free'] -= sum_amount[1]
self.funds.quote['free'] += sum_amount[2]

for order in orders:
self.create_order(
symbol=symbol,
Expand All @@ -337,12 +340,3 @@ def restore_state(self, symbol: str, lt: int, orders: [], tp=()):
lt=lt,
order_id=order['id']
)

if tp:
funds = self.funds
if order['buy']:
funds.base['free'] = str(Decimal(funds.base.get('free', 0)) - tp[0])
funds.quote['free'] = str(Decimal(funds.quote.get('free', 0)) + tp[1])
else:
funds.base['free'] = str(Decimal(funds.base.get('free', 0)) + tp[0])
funds.quote['free'] = str(Decimal(funds.quote.get('free', 0)) - tp[1])
11 changes: 5 additions & 6 deletions martin_binance/executor.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
__author__ = "Jerry Fedorenko"
__copyright__ = "Copyright © 2021 Jerry Fedorenko aka VM"
__license__ = "MIT"
__version__ = "2.1.3"
__version__ = "2.1.4"
__maintainer__ = "Jerry Fedorenko"
__contact__ = 'https://github.com/DogsTailFarmer'
##################################################################
Expand Down Expand Up @@ -2565,16 +2565,15 @@ def on_balance_update(self, balance: Dict) -> None:
self.cancel_grid(cancel_all=True)

def on_new_funds(self, funds: Dict[str, FundsEntry]) -> None:
# print(f"on_new_funds.funds: {funds}")
ff = funds.get(self.f_currency, O_DEC)
fs = funds.get(self.s_currency, O_DEC)
if self.wait_refunding_for_start:
ff = ff.total_for_currency if ff else O_DEC
fs = fs.total_for_currency if fs else O_DEC
if self.cycle_buy:
go_trade = fs >= (self.initial_reverse_second if self.reverse else self.initial_second)
tf = fs.total_for_currency if fs else O_DEC
go_trade = tf >= (self.initial_reverse_second if self.reverse else self.initial_second)
else:
go_trade = ff >= (self.initial_reverse_first if self.reverse else self.initial_first)
tf = ff.total_for_currency if ff else O_DEC
go_trade = tf >= (self.initial_reverse_first if self.reverse else self.initial_first)
if go_trade:
self.message_log("Started after receipt of funds")
self.start()
Expand Down
Loading

0 comments on commit a55d3f3

Please sign in to comment.