barrybecker4 / bxbot

A simple Bitcoin trading bot written in Java.
MIT License
0 stars 0 forks source link

enhance scalping strategy #2

Closed barrybecker4 closed 3 years ago

barrybecker4 commented 3 years ago

Now that the simple base strategy is functioning, let's try to improve it.

Here is how the algorithm will work:

if lastTransactionPrice is not set  {
    Send buy order at the current ask price. Push that buy order onto a buyOrderStack.
    (It should fill quickly, but might get stuck if price goes up quickly)
    set lastTransactionPrice to the current price.
    set highPrice to current price.
}
else {
   if have any buy orders on buyOrderStack {
      if the top order has been filled {
          pop the top filled buy order from the buyOrderStack.
          set lastTransactionPrice to the price that it was bought at.
          send a sell order for lastTransactionPrice * (1 + percent-change-threshold)
          push that sell order on a sellOrderStack. (it will not fill until market goes up by percent-change-threshold)
      }
   }
   if have any sell orders on sellOrderStack {
      if the top order has been filled {
          pop the top filled sell order from the sellOrderStack.
          set lastTransactionPrice to the price that it was sold at.
      }
   }
   if currentPrice < lastTransactionPrice * (1 - percent-change-threshold) and sellStack.size < max-concurrent-sell-orders {
       then send buy order at the current price. Push that buy order in a buyOrderStack.
       set lastTransactionPrice to the current price.
   }
   if (currentPrice > highPrice * ( 1 + percent-change-threshold) and have enough base currency balance (i.e BTC) {
       Send sell order at the current ask price. Push that sell order onto the sellOrderStack.
       set highPrice to currentPrice
   }
}
barrybecker4 commented 3 years ago

It would be a good idea to add some integration tests that do some level of back-testing for different cases. Here are a few timeseries that could represent BTC prices over time.

Case 1 - monotonically increasing (linear) Case 2 - exponentially increasing (e^x) - with randomnesss Case 3 - volatile, but trending up Case 4 - mountain/inverted V. Case 5 - flat Case 6 - V-shaped - up then down Case 7 - volatile, but trending down Case 8 - exponentially decreasing (e^-x) - with randomness Case 9 - monotonically decreasing (linear)

barrybecker4 commented 3 years ago

switch from Bitstamp to Kraken to have lower fees. Do backtesting.

barrybecker4 commented 3 years ago

I got some results from simulating the scenarios with both strategies. As expected, each strategy does well in some cases and poorly in others. It all depends on what you believe is going to happen. For the set of cases tried, the multi-order strategy preserved value better in the "exponential decreasing" and "linear decreasing" cases, but for most other cases the rebalancing approach did better.

Overall, I do like the rebalancing approach better, but one potential problem is that it can make much bigger large buy/sell orders when there are big swings. Probably it is ok, but I feel a little safer when the size of the orders is limited.

Assuming that I haven't made any errors in the calculations, below are the simulated results. In each case, you start with $500 USD and $500 worth of BTC. At the end of the simulation, you sell all BTC at current price, and the sum is reported. So you can subtract $1000 from each result value below to see the gain you would have to report on your taxes.

// Barry's multi-order strategy
LINEAR_INCREASING, 1246.6687449900676
VOLATILE_INCREASING, 1676.0152523952222
EXPONENTIAL_INCREASING, 3777.0709648135107
FLAT, 1106.8671234190554
RANDOM_WALK, 1782.468174054819
EXPONENTIAL_DECREASING, 706.0117655649542
VOLATILE_DECREASING, 1140.9945772497686
LINEAR_DECREASING, 867.5457903465663
EXPONENTIAL_INCREASE_WITH_CRASH, 607.4068058060816
EXPONENTIAL_INCREASING_WITH_CRASHES, 1608.6149046197206
HISTORICAL_DATA, 1185.3989547175693

// Bill's rebalancing strategy
LINEAR_INCREASING, 1429.0203698769033
VOLATILE_INCREASING, 2021.7335350394198
EXPONENTIAL_INCREASING, 4500.905912653756
FLAT, 1118.444951577584
RANDOM_WALK, 2120.245032765944
EXPONENTIAL_DECREASING, 77.63225380742851  // big loss here
VOLATILE_DECREASING, 1219.1793934840493
LINEAR_DECREASING, 688.8435268038278
EXPONENTIAL_INCREASE_WITH_CRASH, 171.20597914873613
EXPONENTIAL_INCREASING_WITH_CRASHES, 1942.3531632376228
HISTORICAL_DATA, 1844.5837059273517