Skip to content

Commit

Permalink
price may raise out of control
Browse files Browse the repository at this point in the history
  • Loading branch information
seii-saintway committed Feb 16, 2021
1 parent cc58d05 commit 01956cd
Showing 1 changed file with 63 additions and 13 deletions.
76 changes: 63 additions & 13 deletions graph/main-martingale.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand All @@ -601,31 +642,40 @@
" 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",
" )[0] / now_buy_fiat_price / trade_unit\n",
" )\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",
" status.max_sold_average_fiat_price = status.bought_average_fiat_price * (1 + GAINABLE_UNIT_CC_SOLD_RATIO)\n",
" 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)"
]
},
{
Expand Down Expand Up @@ -815,7 +865,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.7"
"version": "3.9.1"
}
},
"nbformat": 4,
Expand Down

0 comments on commit 01956cd

Please sign in to comment.