diff --git a/graph/main-martingale.ipynb b/graph/main-martingale.ipynb index 87a80b7..f640048 100644 --- a/graph/main-martingale.ipynb +++ b/graph/main-martingale.ipynb @@ -31,20 +31,31 @@ "BACKUP_RATE = 1\n", "\n", "TRADE_UNIT = 0.0001\n", - "INIT_BUY_JPY = 5000\n", - "INIT_SELL_JPY = 5000\n", - "MAX_TRADED_JPY = 350000\n", + "MAX_BOUGHT_JPY = 1350000\n", "\n", - "TRADABLE_UNIT_CC_GAIN_JPY = 25000 # TODO: self-adaptive by trend analysis\n", - "TRADABLE_UNIT_CC_LOSS_JPY = 125000\n", + "GAINABLE_UNIT_CC_SOLD_JPY = 25000 # TODO: self-adaptive by trend analysis\n", + "LOSSABLE_UNIT_CC_BOUGHT_JPY = 125000\n", "\n", - "MIN_UNIT_CC_INIT_TRADE_JPY = 10000\n", - "MAX_UNIT_CC_INIT_TRADE_JPY = MIN_UNIT_CC_INIT_TRADE_JPY << 10\n", - "MAX_UNIT_CC_LOSS_JPY = MAX_UNIT_CC_INIT_TRADE_JPY\n", + "MIN_UNIT_CC_TRADE_JPY = 5000\n", + "MAX_UNIT_CC_TRADE_JPY = MIN_UNIT_CC_TRADE_JPY << 10\n", + "LOSSABLE_UNIT_CC_SELL_JPY = MAX_UNIT_CC_TRADE_JPY\n", "\n", - "MAX_GAIN_JPY = MAX_TRADED_JPY << 10 # useless\n", - "MAX_LOSS_JPY = MAX_TRADED_JPY << 10 # useless\n", - "MAX_TOTAL_LOST_JPY = MAX_TRADED_JPY << 10" + "MAX_GAIN_JPY = MAX_BOUGHT_JPY << 10 # useless\n", + "MAX_LOSS_JPY = MAX_BOUGHT_JPY << 10 # useless\n", + "MAX_LOST_JPY = MAX_BOUGHT_JPY << 10" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "import math\n", + "MIN_UNIT_CC_BOUGHT_JPY = 15000\n", + "INIT_SELL_JPY = 50\n", + "INIT_BUY_JPY = MAX_BOUGHT_JPY / math.exp(LOSSABLE_UNIT_CC_BOUGHT_JPY / (MIN_UNIT_CC_BOUGHT_JPY - MIN_UNIT_CC_TRADE_JPY))\n", + "INIT_BUY_JPY" ] }, { @@ -101,7 +112,7 @@ " self.total_gained_fiat_money = 0\n", "\n", " def get_usage(self):\n", - " return self.used_fiat_money / MAX_TRADED_JPY\n", + " return self.used_fiat_money / MAX_BOUGHT_JPY\n", "\n", " def get_robot_title(self):\n", " robot_status_description = f'{self.get_usage() * 100:.2f}%'\n", @@ -153,13 +164,13 @@ " self.bought_unit_amount = 0\n", " self.used_fiat_money = 0\n", " self.bought_amount = 0\n", - " self.bought_average_fiat_price = None # MAX_UNIT_CC_INIT_TRADE_JPY\n", + " self.bought_average_fiat_price = None # MAX_UNIT_CC_TRADE_JPY\n", "\n", " def clear_sold_status(self):\n", " self.sold_unit_amount = 0\n", " self.got_fiat_money = 0\n", " self.sold_amount = 0\n", - " self.sold_average_fiat_price = None # MAX_UNIT_CC_INIT_TRADE_JPY\n", + " self.sold_average_fiat_price = None # MAX_UNIT_CC_TRADE_JPY\n", "\n", " def trim_bought_status(self):\n", " if self.bought_unit_amount > 0:\n", @@ -307,23 +318,28 @@ " self.fsh = open('sample-history.txt', 'a', 1)\n", " self.fth = open('transaction-history.txt', 'a', 1)\n", " return self\n", - " \n", + "\n", " def __exit__(self):\n", " self.fsh.close()\n", " self.fth.close()\n", "\n", " def get_price(self):\n", - " import requests\n", - " while True:\n", - " try:\n", - " now_ticker = self.pub.get_ticker('eth_jpy')\n", - " return {\n", - " 'now_sell_fiat_price': int(now_ticker.get('buy', '0')) * (1 - 0.0012),\n", - " 'now_buy_fiat_price': int(now_ticker.get('sell', '1')) * (1 + 0.0012)\n", - " }\n", - " except requests.exceptions.ConnectionError:\n", - " import time\n", - " time.sleep(SAMPLE_INTERVAL / TEST_RATIO)\n", + " now_ticker = self.pub.get_ticker('eth_jpy')\n", + " return {\n", + " 'now_sell_fiat_price': int(now_ticker.get('buy', '0')) * (1 - 0.0012),\n", + " 'now_buy_fiat_price': int(now_ticker.get('sell', '1')) * (1 + 0.0012)\n", + " }\n", + "# import requests\n", + "# while True:\n", + "# try:\n", + "# now_ticker = self.pub.get_ticker('eth_jpy')\n", + "# return {\n", + "# 'now_sell_fiat_price': int(now_ticker.get('buy', '0')) * (1 - 0.0012),\n", + "# 'now_buy_fiat_price': int(now_ticker.get('sell', '1')) * (1 + 0.0012)\n", + "# }\n", + "# except requests.exceptions.ConnectionError:\n", + "# import time\n", + "# time.sleep(SAMPLE_INTERVAL / TEST_RATIO)\n", "\n", " def get_portfolio(self):\n", " return Portfolio([\n", @@ -532,12 +548,12 @@ "from scipy import optimize as opt\n", "from numpy.lib import scimath\n", "\n", - "def get_trade_unit_amount(now_trade_jpy, traded_unit_amount, traded_jpy, tradable_unit_cc_diff_jpy, init_jpy):\n", + "def get_trade_unit_amount(now_trade_jpy, traded_unit_amount, traded_jpy, mutable_unit_cc_traded_jpy, max_traded_jpy, init_jpy):\n", " if traded_unit_amount == 0:\n", " return 1\n", " avg_jpy = traded_jpy / (TRADE_UNIT * traded_unit_amount)\n", " diff_jpy = abs(now_trade_jpy - avg_jpy)\n", - " min_diff_jpy = tradable_unit_cc_diff_jpy / scimath.log(MAX_TRADED_JPY / init_jpy)\n", + " min_diff_jpy = mutable_unit_cc_traded_jpy / scimath.log(max_traded_jpy / init_jpy)\n", " return math.ceil(\n", " traded_jpy * opt.fsolve(\n", " lambda c: [\n", @@ -592,7 +608,7 @@ " )\n", "\n", " send_slack(\n", - " f'{new_status} => Support level is {MIN_UNIT_CC_INIT_TRADE_JPY} JPY.',\n", + " f'{new_status} => Support level is {MIN_UNIT_CC_TRADE_JPY} JPY.',\n", " 'Power by https://jhub.name/', 'good' if new_status.get_total_gain_fiat_money() > 0 else 'danger'\n", " )\n", "\n", @@ -603,7 +619,7 @@ " f'{new_status.robot_name} is turned to Mock', 'Power by https://jhub.name/', 'good'\n", " )\n", "\n", - " if new_status.total_gained_fiat_money < -MAX_TOTAL_LOST_JPY:\n", + " if new_status.total_gained_fiat_money < -MAX_LOST_JPY:\n", " break\n", "\n", " new_status.sample_number += 1\n", @@ -627,7 +643,7 @@ "\n", " if status.sold_unit_amount > 0 and status.now_sell_fiat_price > status.sold_average_fiat_price:\n", " trade_unit_amount = min(\n", - " get_trade_unit_amount(status.now_sell_fiat_price, status.sold_unit_amount, status.got_fiat_money, TRADABLE_UNIT_CC_GAIN_JPY, INIT_SELL_JPY),\n", + " get_trade_unit_amount(status.now_sell_fiat_price, status.sold_unit_amount, status.got_fiat_money, GAINABLE_UNIT_CC_SOLD_JPY, status.used_fiat_money + status.got_fiat_money, INIT_SELL_JPY),\n", " status.bought_unit_amount\n", " )\n", " if trade_unit_amount > 0:\n", @@ -646,12 +662,12 @@ " status.please_sell_unit_amount = status.bought_unit_amount\n", " status.cooling_time = SAD_COOLING_TIME\n", "\n", - " if status.bought_average_fiat_price is not None and status.now_sell_fiat_price < status.bought_average_fiat_price - MAX_UNIT_CC_LOSS_JPY and status.get_usage() > 90/100:\n", + " if status.bought_average_fiat_price is not None and status.now_sell_fiat_price < status.bought_average_fiat_price - LOSSABLE_UNIT_CC_SELL_JPY and status.get_usage() > 90/100:\n", " status.sample_number = 0\n", " status.please_sell_unit_amount = status.bought_unit_amount\n", " status.cooling_time = SAD_COOLING_TIME\n", "\n", - " if status.bought_unit_amount > 0 and status.now_sell_fiat_price < MIN_UNIT_CC_INIT_TRADE_JPY:\n", + " if status.bought_unit_amount > 0 and status.now_sell_fiat_price < MIN_UNIT_CC_TRADE_JPY:\n", " status.please_sell_unit_amount = status.bought_unit_amount\n", " status.cooling_time = SAD_COOLING_TIME\n", "\n", @@ -660,14 +676,14 @@ " bookkeeper.fsh.write(f'{status}\\n')\n", " continue\n", "\n", - " if status.used_fiat_money < INIT_BUY_JPY and INIT_BUY_JPY - status.used_fiat_money <= (MAX_TRADED_JPY - status.used_fiat_money) * 0.75 \\\n", - " and status.now_buy_fiat_price > MIN_UNIT_CC_INIT_TRADE_JPY and status.now_buy_fiat_price < MAX_UNIT_CC_INIT_TRADE_JPY:\n", + " if status.used_fiat_money < INIT_BUY_JPY and INIT_BUY_JPY - status.used_fiat_money <= (MAX_BOUGHT_JPY - status.used_fiat_money) * 0.75 \\\n", + " and status.now_buy_fiat_price > MIN_UNIT_CC_TRADE_JPY and status.now_buy_fiat_price < MAX_UNIT_CC_TRADE_JPY:\n", " status.sample_number = 0\n", " status.please_buy_unit_amount = math.ceil((INIT_BUY_JPY - status.used_fiat_money) / status.now_buy_fiat_price / status.trade_unit)\n", "\n", " if status.used_fiat_money >= INIT_BUY_JPY and status.bought_average_fiat_price is not None and status.now_buy_fiat_price < status.bought_average_fiat_price:\n", - " trade_unit_amount = get_trade_unit_amount(status.now_buy_fiat_price, status.bought_unit_amount, status.used_fiat_money, TRADABLE_UNIT_CC_LOSS_JPY, INIT_BUY_JPY)\n", - " while trade_unit_amount > 0 and status.now_buy_fiat_price * status.trade_unit * trade_unit_amount > (MAX_TRADED_JPY - status.used_fiat_money) * 0.75:\n", + " trade_unit_amount = get_trade_unit_amount(status.now_buy_fiat_price, status.bought_unit_amount, status.used_fiat_money, LOSSABLE_UNIT_CC_BOUGHT_JPY, MAX_BOUGHT_JPY, INIT_BUY_JPY)\n", + " while trade_unit_amount > 0 and status.now_buy_fiat_price * status.trade_unit * trade_unit_amount > (MAX_BOUGHT_JPY - status.used_fiat_money) * 0.75:\n", " trade_unit_amount >>= 1\n", " if trade_unit_amount > 0:\n", " status.sample_number = 0\n", @@ -688,7 +704,7 @@ "\n", " if status.sample_number % NOTIFY_RATE == 0:\n", " send_slack(\n", - " f'{status} => Support level is {MIN_UNIT_CC_INIT_TRADE_JPY} JPY.',\n", + " f'{status} => Support level is {MIN_UNIT_CC_TRADE_JPY} JPY.',\n", " 'Power by https://jhub.name/', 'good' if status.get_total_gain_fiat_money() > 0 else 'danger'\n", " )\n", "\n",