Skip to content

Commit

Permalink
2.1.2
Browse files Browse the repository at this point in the history
  • Loading branch information
DogsTailFarmer committed Feb 17, 2024
1 parent 18dc36c commit cfaf6ec
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 48 deletions.
8 changes: 7 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
## 2.1.2 - 2024-02-17
### Fix
* Create limit order error handler
* Start simulate mode from saved state

## 2.1.1 - 2024-02-16
### Fix
* `check_min_amount()`
* `restore_strategy_state()`
* Fix issue with csv empty lines on windows (#71)

## 2.1.0 - 2024-02-16
### Added for new features
:rocket: Complete cyclic optimization of strategy parameters based on [`optuna` framework](https://optuna.readthedocs.io/en/stable/index.html)
For reference see [Back testing and parameters optimization](https://github.com/DogsTailFarmer/martin-binance/wiki/Back-testing-and-parameters-optimization)
For reference see [Parameters optimization](https://github.com/DogsTailFarmer/martin-binance/wiki/Back-testing-and-parameters-optimization#parameters-optimization)

### Update
* `cancel_order_call`: added timeout handler
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.1"
__version__ = "2.1.2"
__maintainer__ = "Jerry Fedorenko"
__contact__ = "https://github.com/DogsTailFarmer"

Expand Down
51 changes: 12 additions & 39 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.1"
__version__ = "2.1.2"
__maintainer__ = "Jerry Fedorenko"
__contact__ = 'https://github.com/DogsTailFarmer'
##################################################################
Expand Down Expand Up @@ -2801,48 +2801,21 @@ def on_place_order_success(self, place_order_id: int, order: Order) -> None:
else:
self.message_log(f"Did not have waiting order id for {place_order_id}", LogLevel.ERROR)

def on_place_order_error_string(self, place_order_id: int, error: str) -> None:
def on_place_order_error(self, place_order_id: int, error: str) -> None:
# Check all orders on exchange if exists required
self.message_log(f"On place order {place_order_id} error: {error}", LogLevel.ERROR, tlg=True)
open_orders = self.get_buffered_open_orders()
order = None
if self.orders_init.exist(place_order_id):
order = self.orders_init.find_order(open_orders, place_order_id)
_order = self.orders_init.get_by_id(place_order_id)
self.orders_init.remove(place_order_id)
self.orders_hold.orders_list.append(_order)
self.orders_hold.sort(self.cycle_buy)
if self.cancel_grid_hold:
self.message_log('Continue remove grid orders', color=Style.B_WHITE)
self.cancel_grid_hold = False
self.cancel_grid()
elif place_order_id == self.tp_wait_id:
for k, o in enumerate(open_orders):
if (o.buy == self.tp_order[0] and
o.amount == self.tp_order[1] and
o.price == self.tp_order[2]):
order = open_orders[k]
if order:
self.message_log(f"Order {place_order_id} placed", tlg=True)
self.on_place_order_success(place_order_id, order)
else:
self.message_log(f"Trying place order {place_order_id} one more time", tlg=True)
if self.orders_init.exist(place_order_id):
_order = self.orders_init.get_by_id(place_order_id)
self.orders_init.remove(place_order_id)
if self.cancel_grid_hold:
self.message_log('Continue remove grid orders', color=Style.B_WHITE)
self.cancel_grid_hold = False
self.cancel_grid()
if self.check_min_amount(_order['amount'], _order['price'], for_tp=False):
waiting_order_id = self.place_limit_order_check(
_order['buy'],
_order['amount'],
_order['price'],
check=True,
price_limit_rules=True
)
if waiting_order_id:
_order['id'] = waiting_order_id
self.orders_init.orders_list.append(_order)
else:
self.message_log(f"Too small amount for order: {place_order_id}", color=Style.B_WHITE)
self.grid_update(force=True)
elif place_order_id == self.tp_wait_id:
self.tp_wait_id = None
self.tp_error = True
self.tp_wait_id = None
self.tp_error = True

def on_cancel_order_success(self, order_id: int, cancel_all=False) -> None:
if order_id == self.cancel_grid_order_id:
Expand Down
18 changes: 11 additions & 7 deletions martin_binance/margin_wrapper.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.0"
__version__ = "2.1.2"
__maintainer__ = "Jerry Fedorenko"
__contact__ = "https://github.com/DogsTailFarmer"

Expand All @@ -23,6 +23,7 @@
import queue
import pyarrow as pa
import pyarrow.parquet as pq
from shutil import rmtree

from colorama import init as color_init
from decimal import Decimal, ROUND_FLOOR, ROUND_CEILING, ROUND_HALF_EVEN
Expand Down Expand Up @@ -1447,12 +1448,14 @@ async def create_limit_order(_id: int, buy: bool, amount: str, price: str) -> No
if _id in cls.wait_order_id:
# Supress call strategy handler
cls.wait_order_id.remove(_id)

_level = LogLevel.CRITICAL
else:
_fetch_order = True
_level = LogLevel.ERROR
cls.strategy.message_log(f"Exception creating order {_id}: {status_code.name}, {ex.details()}",
log_level=_level)

cls.strategy.on_place_order_error(_id, f"{status_code.name}, {ex.details()}")

except Exception as _ex:
_fetch_order = True
cls.strategy.message_log(f"Exception creating order {_id}: {_ex}")
Expand Down Expand Up @@ -1503,7 +1506,7 @@ async def place_limit_order_timeout(_id):
await asyncio.sleep(ORDER_TIMEOUT)
if _id in cls.wait_order_id:
cls.wait_order_id.remove(_id)
cls.strategy.on_place_order_error_string(_id, 'Place order timeout')
cls.strategy.on_place_order_error(_id, 'Place order timeout')


async def cancel_order_call(_id: int, cancel_all=False, count=0):
Expand Down Expand Up @@ -1750,7 +1753,8 @@ def _back_test_handler_ext(cls):
df_grid_sell.to_pickle(Path(session_path, "sell.pkl"))
df_grid_buy.to_pickle(Path(session_path, "buy.pkl"))
copy(ms.PARAMS, Path(session_path, Path(ms.PARAMS).name))
print(f"Session data saved to: {session_path}")
if ms.LOGGING:
print(f"Session data saved to: {session_path}")


def order_book_prepare(_order_book: {}) -> {}:
Expand Down Expand Up @@ -2057,6 +2061,7 @@ async def main(_symbol):
#
if ms.MODE == 'TC':
BACKTEST_PATH.mkdir(parents=True, exist_ok=True)
rmtree(cls.session_root, ignore_errors=True)
cls.session_root.mkdir(parents=True, exist_ok=True)
# noinspection PyUnboundLocalVariable
raw_path.mkdir(parents=True, exist_ok=True)
Expand Down Expand Up @@ -2153,8 +2158,7 @@ async def main(_symbol):
cls.strategy.cycle_time = datetime.utcnow()
if cls.state_file.exists():
restore_state_before_backtesting(cls)
else:
cls.strategy.start()
cls.strategy.start()
if restored:
loop.create_task(heartbeat(cls.session))
loop.create_task(save_to_csv())
Expand Down

0 comments on commit cfaf6ec

Please sign in to comment.