dscotese / kraken-grid

A bot that extends grid trading once you use it to create a grid.
GNU General Public License v3.0
9 stars 3 forks source link

The math is slightly off... #6

Closed dscotese closed 1 year ago

dscotese commented 2 years ago

The bot trades the same amount of a coin at each grid point. It ignores transaction fees and so will I because that is a different issue than the one I'm describing.

If the price goes down, we buy, which means we now have less cash and more bitcoin. We assume the grid trading is to maintain a specific allocation, so another fall of proportional magnitude should be handled with a similar trade, but slightly smaller in terms of dollars, and slightly larger in terms of crypto (not the same amount as the bot currently does). The only basis we have (until the bot asks for off-exchange amounts of assets) is the assets on the exchange. The trade entered by the user is for a specific proportion of the holdings at a specific proportion of the current price. I think the best strategy is to determine the next point in the grid as we are now (by maintaining the geometric proportion between grid points), and to adjust the amount to trade by the proportion of the new holdings to the previous holdings of the asset instead of just using the exact same amount.

Even better is to allow the user to enter an "off-exchange" amount of each asset so that this can be added to the on-exchange amount and thus calculate a more accurate ratio for trade amounts. I think I will solve this with off-exchange amounts instead of basing the proportion to how much is on the exchange.

dscotese commented 2 years ago

Anyway, it's not the only basis. We have H of a coin and we buy m more when the price drops from o to p. A safe assumption is that the money we spent (m*p) should be recovered if the price goes back up, but we should make a little extra. Specifically, if the price drops from o to p, let d = p/o (so o = p/d) and, since o (or p/d) is the sell price, what we make is m(p/d-p) = mp(1/d-1). The question is, how should that profit be divided between cash and the coin? 50/50 is a safe bet until the bot collects more data. How do we find the amount n to sell at p/d? pn/d - mp = mp(1/d-1)/2, so n/d-m=m(1/d-1)/2, n/d = m(1/d-1)/2+m, and n=dm((1/d-1)/2+1) Example: A price fall from 100 to 90 makes d 0.9. The order buys 1 coin, so m = 1. Therefore, n = 0.9*1*(1/0.9 - 1)/2+1) =0.9(19/18) = 19/20 = 0.95. We end up with an extra 0.05 coin and 0.05o in $, which are equal in value, at o.

When the bot allows the user to specify the explicit ratio of coin to cash, the formula can be updated.

dscotese commented 2 years ago

I don't plan to encode the 50/50 assumption as described in the above comment into the bot. Instead, the bot will: A) Create an object persistent which will have keys for names of data (like key and secret), B) Add a key externalAccs to persistent which will hold as many savings objects as the user wants, to represent off-exchange assets, C) Store persistent in the file where it has been storing API keys, and D) Assume the file contains the key and secret separated by a space, and ask whether or not to convert it, if any error is thrown while converting the file back into the persistent object.

Once that works well, the bot will: E) Allow the user to create external accounts to be saved in persistent, F) View and adjust the current allocation according to all the externalAccs, and G) Offer to create trades to balance according to the chosen allocation.

dscotese commented 1 year ago

This is really two issues, with the math being off requiring the first issue (giving the bot a record of external assets) being a precondition for the second. The refactor (#30 ) (now a closed pull request) added the external accounts (portfolio.Savings, an array of Savings objects - see savings.js) so I am closing this issue and making a new one to propose calculating the correct amounts as prices change.