From 01956cd8bb08a3666c08808606a6ee21a2790334 Mon Sep 17 00:00:00 2001 From: andrew saintway Date: Wed, 17 Feb 2021 02:22:55 +0900 Subject: [PATCH] price may raise out of control --- graph/main-martingale.ipynb | 76 ++++++++++++++++++++++++++++++------- 1 file changed, 63 insertions(+), 13 deletions(-) diff --git a/graph/main-martingale.ipynb b/graph/main-martingale.ipynb index 1e1489c..29bfff5 100644 --- a/graph/main-martingale.ipynb +++ b/graph/main-martingale.ipynb @@ -342,11 +342,13 @@ " def __enter__(self):\n", " self.fsh = open('sample-history.txt', 'a', 1)\n", " self.fth = open('transaction-history.txt', 'a', 1)\n", + "# self.fth_origin = open('transaction-history-origin.txt', 'a', 1)\n", " return self\n", "\n", " def __exit__(self):\n", " self.fsh.close()\n", " self.fth.close()\n", + "# self.fth_origin.close()\n", "\n", " def get_price(self):\n", " now_ticker = self.pub.get_ticker('eth_jpy')\n", @@ -461,6 +463,22 @@ " pair='eth_jpy', price=str(fiat_price), amount=str(amount), side='buy', order_type='market'\n", " )\n", "\n", + "# old_status = self.status\n", + "# new_status = copy.deepcopy(old_status)\n", + "\n", + "# new_status.buy_count += 1\n", + "# new_status.update_bought_status(fiat_price=fiat_price, unit_amount=unit_amount)\n", + "\n", + "# old_amount = old_status.bought_amount\n", + "# old_percentage = old_status.get_usage() * 100\n", + "# new_amount = new_status.bought_amount\n", + "# new_percentage = new_status.get_usage() * 100\n", + "\n", + "# self.fth_origin.write(\n", + "# f'BOUGHT {amount:.4f} = -{old_amount:.4f} ({old_percentage:.2f}%) +{new_amount:.4f} ({new_percentage:.2f}%) LTC for {fiat_price:.2f} JPY/LTC:'\n", + "# f' NO.{new_status.trade_count + 1}-{new_status.buy_count} at {datetime.now()}\\n\\n'\n", + "# )\n", + "\n", " def buy(self, unit_amount, fiat_price=None):\n", " if unit_amount <= 0:\n", " return\n", @@ -510,6 +528,29 @@ " pair='eth_jpy', price=str(fiat_price), amount=str(amount), side='sell', order_type='market'\n", " )\n", "\n", + "# old_status = self.status\n", + "# new_status = copy.deepcopy(old_status)\n", + "\n", + "# new_status.sell_count += 1\n", + "# new_status.update_bought_status(fiat_price=new_status.bought_average_fiat_price, unit_amount=-unit_amount)\n", + "# new_status.update_sold_status(fiat_price=fiat_price, unit_amount=unit_amount)\n", + "\n", + "# old_avg_price = old_status.bought_average_fiat_price\n", + "# diff_price = fiat_price - old_avg_price\n", + "# gained_fiat_money = old_status.estimate_gained_fiat_money(unit_amount, fiat_price)\n", + "# new_status.total_gained_fiat_money += gained_fiat_money\n", + "\n", + "# old_amount = old_status.bought_amount\n", + "# old_percentage = old_status.get_usage() * 100\n", + "# new_amount = new_status.bought_amount\n", + "# new_percentage = new_status.get_usage() * 100\n", + "\n", + "# self.fth_origin.write(\n", + "# f'GAINED {gained_fiat_money:+.2f} JPY: GAINED {new_status.total_gained_fiat_money:+.2f} JPY in total at {datetime.now()}\\n'\n", + "# f'SOLD {amount:.4f} = +{old_amount:.4f} ({old_percentage:.2f}%) -{new_amount:.4f} ({new_percentage:.2f}%) LTC'\n", + "# f' for {diff_price:.2f} = {fiat_price:.2f} - {old_avg_price:.2f} JPY/LTC: NO.{new_status.trade_count + 1}-{new_status.sell_count}\\n\\n'\n", + "# )\n", + "\n", " def sell(self, unit_amount, fiat_price=None):\n", " if unit_amount <= 0:\n", " return\n", @@ -591,7 +632,7 @@ " lossable_unit_cc_bought_jpy = (status.bought_average_fiat_price - MIN_UNIT_CC_TRADE_JPY) * LOSSABLE_UNIT_CC_BOUGHT_RATIO\n", " min_unit_cc_diff_jpy = float(lossable_unit_cc_bought_jpy) / scimath.log(float(status.get_max_used_fiat_money() / status.used_fiat_money))\n", " status.min_bought_average_fiat_price = status.bought_average_fiat_price - lossable_unit_cc_bought_jpy\n", - " status.max_next_buy_fiat_price = status.bought_average_fiat_price - Decimal(repr(min_unit_cc_diff_jpy))\n", + " status.max_next_buy_fiat_price = status.bought_average_fiat_price - Decimal(repr(min_unit_cc_diff_jpy)) * status.now_buy_fiat_price / status.bought_average_fiat_price\n", "\n", " bought_average_fiat_price = float(status.bought_average_fiat_price)\n", " now_buy_fiat_price = float(status.now_buy_fiat_price)\n", @@ -601,7 +642,7 @@ " used_fiat_money * opt.fsolve(\n", " lambda c: [\n", " (\n", - " unit_cc_diff_jpy - min_unit_cc_diff_jpy * scimath.log(1 + c[0]) / (bought_average_fiat_price * c[0]) * (bought_average_fiat_price * c[0] + now_buy_fiat_price)\n", + " unit_cc_diff_jpy - min_unit_cc_diff_jpy * scimath.log(1 + c[0]) * (bought_average_fiat_price * c[0] + now_buy_fiat_price) / (bought_average_fiat_price * c[0])\n", " ).real\n", " ],\n", " [-0.5]\n", @@ -609,6 +650,9 @@ " )\n", "\n", "def get_sell_unit_amount(status):\n", + " if status.bought_average_fiat_price is None:\n", + " return 0\n", + "\n", " sold_unit_amount = float(status.sold_unit_amount)\n", " unit_cc_diff_jpy = float(abs(status.now_sell_fiat_price - status.sold_average_fiat_price))\n", "\n", @@ -616,16 +660,22 @@ " min_unit_cc_diff_jpy = float(status.max_sold_average_fiat_price - status.sold_average_fiat_price) / scimath.log(float(status.get_max_sold_unit_amount() / status.sold_unit_amount))\n", " status.min_next_sell_fiat_price = status.sold_average_fiat_price + Decimal(repr(min_unit_cc_diff_jpy))\n", "\n", - " return math.ceil(\n", - " sold_unit_amount * opt.fsolve(\n", - " lambda c: [\n", - " (\n", - " unit_cc_diff_jpy - min_unit_cc_diff_jpy * scimath.log(1 + c[0]) / c[0] * (c[0] + 1)\n", - " ).real\n", - " ],\n", - " [-0.5]\n", - " )[0]\n", - " )" + " c = opt.fsolve(\n", + " lambda c: [\n", + " (\n", + " unit_cc_diff_jpy - min_unit_cc_diff_jpy * scimath.log(1 + c[0]) * (c[0] + 1) / c[0]\n", + " ).real\n", + " ],\n", + " [-0.5]\n", + " )[0]\n", + "\n", + " if c < -1:\n", + " return status.bought_unit_amount\n", + "\n", + " if c < 0:\n", + " return 0\n", + "\n", + " return math.ceil(sold_unit_amount * c)" ] }, { @@ -815,7 +865,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.6.7" + "version": "3.9.1" } }, "nbformat": 4,