ctubio / tribeca

Self-hosted crypto trading bot (automated high frequency market making) in node.js, angular, typescript and c++
https://127.0.0.1:3000
Other
95 stars 26 forks source link

Improvement : Auto managing width with volatility #46

Closed Camille92 closed 7 years ago

Camille92 commented 7 years ago

Hi Carles and everyone,

Couldn't sleep but just got what I believe might be a simple and great idea! This would act like the EWMA safety but in a more clever way imho.

The idea is to use standard deviation of the price as a safety for defining the width. 20 period is the most commonly used value (BB bands etc) so it might be good just to start with that and maybe change it later.

The standard deviation is not very hard to calculate and is a very useful tool to measure how much a price moves from its mean.

We need to calculate the SMA(20) = the average of the price on close on a 20mn period Then substrat this mean to the 20 composing the moving average and square them. Lastly to find the average of this number and find the square root of it.

You can see at the bottom of thus chart what the value would be for the above period. It has the ability to move quickly from a period with big change to very calm time.

telechargement

At the end we get, like for the ewma protection a minimum value that should protect from bad trade even if the width is set to be smaller.

Lastly it can be used for defining the bullets of AK47. For instance with 2 bullets the second one could be at Standard deviation * 2 from fair value!

Camille92 commented 7 years ago

More development on the idea. The goal of a market maker bot is to make money by both selling and buying a product at the same time.

The difficulty is to guess what is maximum price investors are willing to pay me my BTC and what is the minimum price they are willing to sell me those their BTC at any given time.

The simplest strategy is to always be on top of the market at any given point, doing so you're going to make a lot of trades but with a very small share of profit. Also, another bot on the market might compete with you and that will drastically reduce profit for the two bots.

In order to not fall into excesses we have the 'width' parameters that prevent from buying and selling with a too small profit. However the market change quickly and those settings not as they have to be changed manually.

In time of big price movement you can expect more from the market as the price is evolving a lot. When it's calm you might want to take advantage of very small profits rather than nothing.

In the current state of tribeca the ewma security setting can help, as it will protect from selling too low or buying too high during a price swing. However it has a serious drawback: if it's not a price swing but a real movement it will prevent tribeca from getting rid of its coins or to jump on a move.

The proposed solution here is to have an evolutionary width based on what the market is actually willing to spend for that. Or at least trying to guess it. By using the standard deviation of the price, we know how much the price moved from its mean on average during the last 20m.

There is two ways to implement this feature: The first one is to use a modified version of the famous Bollinger bands, by reporting the standard deviation of the price above and under the 20mn simple moving average of this price. This would give us a price that should be the target (if superior to the width settings).

Ie: If the SMA 20 = 1200 and Sdev = 2 You buy at 1198 and sell at 1202.

However this can suffer a bit from the same drawbacks of the ewma security as it is based on a moving average. Also this would not give us an interesting information during quick price move.

If we take the same example, once the price reaches 1203 the security would still have 1202 as a target.

The second and in my opinion best solution is to report the Sdev from the Fair value calculated from tribeca. Using the same example if the price is at 1200 and Sdev = 2 Tribeca buys at 1198 and sell at 1202. However if the price moves to 1203 Tribeca will then try to sell at 1205 and buy at 1201.

ctubio commented 7 years ago

well lets try to first dispay the SMA(20) value in the UI and later we take it in consideration as a safety, hope you agree with the plan'

this could be en/disabled with a simple checkbox like ewma i think

i just need to find out the math behind it xD can you put an example of lets saaay, what is the ¿3? values for SMA(20) and in what times they are recorded if we have a timeline of 1h and every minute the price goes up +1 starting by 1200? many thanks'¡ (is just to make sure that im not messing it)

ctubio commented 7 years ago

what i dont understand is:

The second and in my opinion best solution is to report the Sdiv from the Fair value calculated from tribeca. Using the same example if the price is at 1200 and Sdiv = 2 Tribeca buys at 1198 and sell at 1202. However if the price moves to 1203 Tribeca will then try to sell at 1205 and buy at 1201.

the first two lines suggest to use pDiv as a width? im not seen why pDiv can be a width for quotes based on the fair value, that is the width, no? the pDiv is the width ok but from TBP to stop quotes (not to start them, and not based on fair value), can you elaborate maybe in this last point? (anyway for now i just want to display the SMA(20), not to use it for nothing yet xD but always helps to fully understand from the begining'¡)

Camille92 commented 7 years ago

I'm sorry for the confusion with pdiv, I wanted to write Sdev for standard deviation. I corrected it in the post :)

Camille92 commented 7 years ago

Concerning the first comment, I'll try to explain better the maths behind the idea.

Here we are trying to define how much the price usually diverge from the mean. To understand better let's take the example of flowers.

You have two bouquets of flowers. In the first one: You have 20 roses and they all have different numbers of petals. Let's say you have 1 rose with 1 petal, 1 with 2 petals, 1 with 3, 1 with 4 etc until the last rose has 20 petals.

So you have a total of 210 petals, so an average of 10.5 petals per rose.

Now in the second one, you have still 20 roses but in this case, 10 have 10 petals and 10 have 11 petals. You still have 210 petals total and an average of 10.5 petals per flower, however, your two bouquets are very very different and that can be seen in the standard deviation of you 2 bouquet.

The standard deviation (or Sdev) determines by how many petals on average a random flower diverges from the mean (or your 10.5 petals).

To calculate the standard deviation we need: 1) to calculate the mean (already done) 2) To subtract the mean from each number (flower) and square the results 3) To Calculate the mean of the squared results 4) To square root this last result

In the first bouquet your standard deviation is equal to: Step one: (1 - 10.5)^2 + (2 -10.5)^2 + (3-10.5)^2 + ... + (20 - 10.5)^2 = 90.25 + 72.25 + 56.25 + ... + 90.25 = 665

Step 2: get the mean of the deviation: 665/20 = 33.5 Last step: take the square root: V33.5 = 5.788

Now for the Second bouquet: (10 - 10.5)^2 10 + (11-10.5)^2 10 = 5 Mean = 5/20 = 0.25 Square root = 0.5

Now you know that if you pick a random flower from bouquet 1 it will be on average 5.788 petals away from the mean and if you pick from bouquet 2 will be on average 0.5 petals away from the mean.

Let's go back to our topic now and change the flowers for minutes and the petals with a price and you can see how it is useful for Tribeca.

If you're in a situation of bouquet 1: The prices are quite random and move a lot from one minute to another by calculating the standard deviation you know that you should be always 5.78 away from FV (your width) because it's on average how much the price will diverge each time.

If the price is very calm and don't move a lot like in bouquet number 2, you should only be 0.5 away from fair value because that's how much the market is diverging.

I hope it's more clear for you know what I'm trying to predict with that. The SMA20 is just a way to have an approximation of how much the price is diverging from one minute to another.

Of course, it needs some tweaking and to try it against a market to be most efficient but the logic is there.

Trying to guess how much the price will move from one minute to another so Tribeca can adjust it width to be not too low or too high! 🤓

Camille92 commented 7 years ago

Some readings http://en.tradimo.com/learn/technical-analysis/indicators/standard-deviation/ http://stockcharts.com/school/doku.php?id=chart_school:technical_indicators:standard_deviation_volatility

Camille92 commented 7 years ago

Lastly, I want to add that it helps with the setting of the bot.

If I have a pretty good idea of what's the optimum width for the bitcoin market, I'm quite lost when I have to enter settings for ETH/BTC or LTC/USD!

This system solves the issue as you just need to enter minimums and tribeca will calculate the "optimum" width according to the standard deviation :)

ctubio commented 7 years ago

please review https://github.com/ctubio/tribeca/commit/868f345827e238b29f8cf4009fbc8f752091061f and also https://github.com/ctubio/tribeca/commit/2ea1c2b9152e044906ab9ed58254e35f4fc0898c to see if you miss anything :dancer:

Camille92 commented 7 years ago

Hello and thank you for the work !!!!

I thought you would do that next week! 😃 I'm' going to review that and try that.

I'm also going to try different "ways" of using stdev and come back to you! :DD

ctubio commented 7 years ago

i may be wrong cos i have zero math knowledge, but i can see in my Total values a clearly an amazing positive benefit after resolving this issue (because the app doesnt drains so easilly now, before fast moving markets were eating the funds xD; and if it drains, it does in much less frequency and with higher widths :D), can you please consider to answer if protecting the widh with STDEV is matematically compatible with protecting the widh also with EWMA? or is best to just have enabled only one of them? (from the mathematical point of view, not from the practical xD if you know what i mean.. :P [i see no problem in having both enabled but asking to the experts never hurts'¡]) thanks'¡!¡ (if the answer is yes, then i will leave both enabled by default for everybody)

ctubio commented 7 years ago

:sunflower: @ekienmeier @beegmon check this out! Camille is a genius xDD

beegmon commented 7 years ago

YES!!!! I am pulling this down now to give it a run...but looks very promising!

Awesome work all!

Camille92 commented 7 years ago

Yes! It's very cool!

I'm trying to play with it for a bit with different settings, I'll send you a feedback about it tonight: )