Skip to content

Commit

Permalink
add the logic of including the handling fee
Browse files Browse the repository at this point in the history
  • Loading branch information
seii-saintway committed Jan 26, 2021
1 parent 4a0233c commit c3c1cbe
Show file tree
Hide file tree
Showing 4 changed files with 624 additions and 135 deletions.
151 changes: 18 additions & 133 deletions graph/main-test.ipynb → graph/main-martingale.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -116,13 +116,13 @@
" return self.total_gained_fiat_money + self.get_gain_fiat_money()\n",
"\n",
" def __str__(self):\n",
" sell_fiat_price_description = f'{self.now_sell_fiat_price}'\n",
" sell_fiat_price_description = f'{self.now_sell_fiat_price:.2f}'\n",
" if self.bought_average_fiat_price is not None:\n",
" diff_fiat_price = self.now_sell_fiat_price - self.bought_average_fiat_price\n",
" sell_fiat_price_description = f'{diff_fiat_price:.2f} = {self.now_sell_fiat_price} - {self.bought_average_fiat_price:.2f}'\n",
" sell_fiat_price_description = f'{diff_fiat_price:.2f} = {self.now_sell_fiat_price:.2f} - {self.bought_average_fiat_price:.2f}'\n",
" return (\n",
" f'GAIN {self.get_total_gain_fiat_money():+.2f} = {self.total_gained_fiat_money:+.2f} {self.get_gain_fiat_money():+.2f} JPY in total by {self.get_robot_title()}\\n'\n",
" f'SELL {self.bought_amount:.4f} ETH for {sell_fiat_price_description} JPY/ETH. BUY for {self.now_buy_fiat_price} JPY/ETH.\\n'\n",
" f'SELL {self.bought_amount:.4f} ETH for {sell_fiat_price_description} JPY/ETH. BUY for {self.now_buy_fiat_price:.2f} JPY/ETH.\\n'\n",
" )\n",
"\n",
" def estimate_gained_fiat_money(self, sell_unit_amount, sell_fiat_price=None):\n",
Expand Down Expand Up @@ -304,8 +304,8 @@
" def get_price(self):\n",
" now_ticker = self.pub.get_ticker('eth_jpy')\n",
" return {\n",
" 'now_sell_fiat_price': int(now_ticker.get('buy', '0')),\n",
" 'now_buy_fiat_price': int(now_ticker.get('sell', '1'))\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",
"\n",
" def buy(self, unit_amount, fiat_price=None):\n",
Expand All @@ -331,7 +331,7 @@
" new_percentage = new_status.get_usage() * 100\n",
"\n",
" self.last_transaction = (\n",
" f'BOUGHT {amount:.4f} = -{old_amount:.4f}({old_percentage:.2f}%) +{new_amount:.4f}({new_percentage:.2f}%) ETH for {fiat_price} JPY/ETH:'\n",
" f'BOUGHT {amount:.4f} = -{old_amount:.4f} ({old_percentage:.2f}%) +{new_amount:.4f} ({new_percentage:.2f}%) ETH for {fiat_price:.2f} JPY/ETH:'\n",
" f' NO.{new_status.trade_count + 1}-{new_status.buy_count} at {datetime.now()}\\n'\n",
" )\n",
"\n",
Expand Down Expand Up @@ -368,8 +368,8 @@
" new_percentage = new_status.get_usage() * 100\n",
"\n",
" self.last_transaction = (\n",
" f'SOLD {amount:.4f} = +{old_amount:.4f}({old_percentage:.2f}%) -{new_amount:.4f}({new_percentage:.2f}%) ETH'\n",
" f' for {diff_price:.2f} = {fiat_price} - {old_avg_price:.2f} JPY/ETH: NO.{new_status.trade_count + 1}-{new_status.sell_count}\\n'\n",
" f'SOLD {amount:.4f} = +{old_amount:.4f} ({old_percentage:.2f}%) -{new_amount:.4f} ({new_percentage:.2f}%) ETH'\n",
" f' for {diff_price:.2f} = {fiat_price:.2f} - {old_avg_price:.2f} JPY/ETH: NO.{new_status.trade_count + 1}-{new_status.sell_count}\\n'\n",
" f'GAINED {gained_fiat_money:+.2f} JPY: GAINED {new_status.total_gained_fiat_money:+.2f} JPY in total at {datetime.now()}\\n'\n",
" )\n",
"\n",
Expand Down Expand Up @@ -453,17 +453,17 @@
" from unittest.mock import Mock\n",
" bookkeeper.prv = Mock()\n",
" while True:\n",
" # bought_unit_amounts = [\n",
" # int(Decimal(asset.get('free_amount', '0')) / Decimal(repr(TRADE_UNIT))) for asset in prv.get_asset().get('assets', []) \\\n",
" # if asset.get('asset', '') == 'eth'\n",
" # ] if not isinstance(prv, Mock) else []\n",
"# bought_unit_amounts = [\n",
"# int(Decimal(asset.get('free_amount', '0')) / Decimal(repr(TRADE_UNIT))) for asset in prv.get_asset().get('assets', []) \\\n",
"# if asset.get('asset', '') == 'eth'\n",
"# ] if not isinstance(prv, Mock) else []\n",
"\n",
" # if len(bought_unit_amounts) > 0:\n",
" # bought_unit_amount_before = status.bought_unit_amount\n",
"# if len(bought_unit_amounts) > 0:\n",
"# bought_unit_amount_before = status.bought_unit_amount\n",
"\n",
" # if bought_unit_amount_before > 0:\n",
" # status.bought_unit_amount = bought_unit_amounts[0]\n",
" # used_jpy *= status.bought_unit_amount / bought_unit_amount_before\n",
"# if bought_unit_amount_before > 0:\n",
"# status.bought_unit_amount = bought_unit_amounts[0]\n",
"# used_jpy *= status.bought_unit_amount / bought_unit_amount_before\n",
"\n",
" status.update(bookkeeper.get_price())\n",
"\n",
Expand Down Expand Up @@ -514,7 +514,7 @@
"\n",
" send_slack(\n",
" f'{bookkeeper.last_transaction}'\n",
" f'BY {status.get_robot_title()}',\n",
" f'BY {new_status.get_robot_title()}',\n",
" 'Power by https://jhub.name/', 'good' if status.total_gained_fiat_money > 0 else 'danger'\n",
" )\n",
"\n",
Expand Down Expand Up @@ -593,121 +593,6 @@
" status.sample_number += 1\n",
" time.sleep(SAMPLE_INTERVAL / TEST_RATIO)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Simulated One"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from unittest.mock import Mock\n",
"prv = Mock()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Actual One"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import os\n",
"prv = python_bitbankcc.private(\n",
" os.environ['BITBANK_API_KEY'],\n",
" os.environ['BITBANK_API_SECRET']\n",
")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Trading Operations"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def sell_all():\n",
" now_ticker = pub.get_ticker('eth_jpy')\n",
" now_sell_jpy = float(now_ticker.get('buy', '0'))\n",
" now_buy_jpy = float(now_ticker.get('sell', '1'))\n",
"\n",
" please_sell_amount = TRADE_UNIT * bought_unit_amount\n",
" prv.order(\n",
" pair='eth_jpy', price=str(now_sell_jpy), amount=str(please_sell_amount), side='sell', order_type='market'\n",
" )\n",
"\n",
" bought_amount = TRADE_UNIT * bought_unit_amount\n",
" total_gain_jpy = total_gained_jpy + now_sell_jpy * bought_amount - used_jpy\n",
" with open('sample-history.txt', 'a', 1) as fsh:\n",
" fsh.write(\n",
" f'BUY ETH for {now_buy_jpy} JPY: SELL ETH for {now_sell_jpy} JPY\\n'\n",
" f'GAIN {total_gain_jpy} JPY in total at {datetime.now()}\\n\\n'\n",
" )\n",
" print(\n",
" f'BUY ETH for {now_buy_jpy} JPY: SELL ETH for {now_sell_jpy} JPY\\n'\n",
" f'GAIN {total_gain_jpy} JPY in total at {datetime.now()}\\n\\n',\n",
" end=''\n",
" )\n",
"\n",
" used_jpy_after = used_jpy * (bought_unit_amount - please_sell_unit_amount) / bought_unit_amount\n",
"\n",
" avg_jpy = used_jpy / bought_amount\n",
" diff_jpy = now_sell_jpy - avg_jpy\n",
" gained_jpy = diff_jpy * please_sell_amount\n",
" with open('transaction-history.txt', 'a', 1) as fth:\n",
" fth.write(\n",
" f'SOLD {please_sell_amount:.4f}=+{bought_amount:.4f}({used_jpy/MAX_TRADED_JPY*100:.2f}%)'\n",
" f'-{bought_amount-please_sell_amount:.4f}(0.00%) ETH'\n",
" f' for {diff_jpy:.2f}={now_sell_jpy}-{avg_jpy:.2f} JPY:BUY ETH for {now_buy_jpy} JPY\\n'\n",
" f'GAINED {gained_jpy} JPY: GAINED {total_gained_jpy + gained_jpy} JPY in total at {datetime.now()}\\n\\n'\n",
" )\n",
" print(\n",
" f'SOLD {please_sell_amount:.4f}=+{bought_amount:.4f}({used_jpy/MAX_TRADED_JPY*100:.2f}%)'\n",
" f'-{bought_amount-please_sell_amount:.4f}(0.00%) ETH'\n",
" f' for {diff_jpy:.2f}={now_sell_jpy}-{avg_jpy:.2f} JPY:BUY ETH for {now_buy_jpy} JPY\\n'\n",
" f'GAINED {gained_jpy} JPY: GAINED {total_gained_jpy + gained_jpy} JPY in total at {datetime.now()}\\n\\n',\n",
" end=''\n",
" )\n",
"\n",
" return {\n",
" 'sample_number': sample_number,\n",
" 'buy_count': buy_count,\n",
" 'sell_count': sell_count,\n",
" 'bought_unit_amount': bought_unit_amount,\n",
" 'used_jpy': used_jpy,\n",
" 'please_buy_unit_amount': please_buy_unit_amount,\n",
" 'please_sell_unit_amount': please_sell_unit_amount,\n",
" 'cooling_time': cooling_time,\n",
" 'total_gained_jpy': total_gained_jpy\n",
" }"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"sell_all()"
]
}
],
"metadata": {
Expand Down
Loading

0 comments on commit c3c1cbe

Please sign in to comment.