I have a small feature request. I would like to be able to click on the quantity buttons even if I don't have enough money to buy. I want this because:
-Sometimes I want to sell using those buttons.
-Sometimes I want to buy more BTC after the price goes up.
Needing to refresh the page or enter the number manually is rather annoying (when those buttons are already there but just grayed out).
I'm using the calculations posted by zhoutong, so I think they're correct
I found your problem.
This:
// Long
if (amount > 0)
min_price = Math.round((ask - ((net_value - maintenance)/(amount + btc))) * 100) / 100;
// Short
else
min_price = Math.round((bid - ((net_value - maintenance)/(amount - btc))) * 100) / 100;
Should be this:
// Long
if (amount > 0)
min_price = Math.round((bid - ((net_value - maintenance)/(amount + btc))) * 100) / 100;
// Short
else
min_price = Math.round((ask - ((net_value - maintenance)/(amount - btc))) * 100) / 100;
(Notice the change to "bid" and "ask").
It seems to fix it.
To test it to see if it was accurate, I changed it like this:
// Long
if (amount > 0)
min_price = (bid - ((net_value - maintenance)/(amount + btc));
// Short
else
min_price = (ask - ((net_value - maintenance)/(amount - btc));
(Notice the removal of "Math.round").
This allowed me to have it display all of the digits it uses.
I tested it for a 10:1 account:
-Manually doing the calculation resulted in 6.665561463414634146341463414635
-Using the test script resulted in 6.665561463414635
So it's accurate to 15 digits. (By accurate, I mean it yields the same result as doing it manually).
Now that it works correctly, I wanted it to show 4 digits instead of 2, so I changed it like this.
// Long
if (amount > 0)
min_price = Math.round((bid - ((net_value - maintenance)/(amount + btc))) * 10000) / 10000;
// Short
else
min_price = Math.round((ask - ((net_value - maintenance)/(amount - btc))) * 10000) / 10000;
(Notice the change from "100" to "10000").
With this change, it will now display the correct minimum price to 4 digits.
PS: Thank you for making this script.