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

(Small improvement) -> STDE + SMA = Bollinger Bands #133

Closed Camille92 closed 7 years ago

Camille92 commented 7 years ago

Hello Carles,

It's just to propose a small improvement that should make STDEV work a bit more like EWMA but being a bit more "intelligent".

The idea is to have the option to use STDEV as it "should be" ie: reporting STDEV not from FV but from the mean we calculate STDEV from.

https://github.com/ctubio/tribeca/blob/master/src/server/quoting-engine.ts#L162

The thing is I thought we could use FV as a proxy for mean of STDEV, and it works well in most of the cases, however it is still not perfect. I hope this could fix that:)

Having to report STDEV from the mean would give more "realistic" targets for STDEV to achieve.

Tell me what you think,

Best,

Camille

PS: I've seen you suppressed the maths, are you now using the library?

ctubio commented 7 years ago

thanks Camille''

FYI: STDEV is calculated at https://github.com/ctubio/tribeca/blob/master/src/lib/stdev.cc#L27, not using any library but C++ that is faster than whatever else out there

Camille92 commented 7 years ago

Ohh ok clever! I get it!

So just need to report https://github.com/ctubio/tribeca/blob/master/src/lib/stdev.cc#L33 and it's done no? :)

ctubio commented 7 years ago

~i reread this a few times and im not understanding from where you wanna calculate the stdev now if is not the fv?~

~i think you wanna keep STDEV onTops/onTop equal, and only modify STDEV onFV, right? can you please explain again with other words what is the modification you would like to be applied to STDEV onFV? or you want to modify also other STDEV on*?~

ah yea the mean you mean at L33 okok xD so you want to have a new STDEV onMEAN? or you want to remove STDEV onFV?

thanks for yor time'¡

Camille92 commented 7 years ago

Haha yes from the mean that is used to calculate STDEV it makes more sense.

I would say more having both if it's doable, actually don't know how STDEV on mean performs against the market.

But here it what is an error in my opinion for STDEV on FV.

So let's say the market is quite stable and you use STDEV on TOPS. Everything goes fine as MEAN is approximately equal to FV. Then the markets go down and STDEV goes up (because of volatility).

You try to BUY now at FV - STDEV that might be very low and SELL at FV + STDEV that might not be relevant (too low as well).

While if you use the mean, your selling point and buying point will be higher.

I mean I don't know if it's very clear but that should better reflect "what's a good price to buy or what's a good price to sell".

Acting a bit like EWMA protection but including volatility as well.

Little exampel of the screenshot: At the little drop in the middle, FV +STDEV will look for even lower to buy in while MEAN + STDEV will find that a good price (coz far enough from MEAN)

capture d ecran 2017-06-13 a 13 55 21

PS: I'm sorry I feel like I'm not clear at all :/

ctubio commented 7 years ago

soo.. the plan is to accumulate ina sequence of periods the value of each calculated MEAN (from how many last periods of FV?), and once we have a sequence of MEANs as well as we have another of FVs, calculate the STDEV of each sequence as usual independently, right? :dancer:

otherwise, i didnt understand nothing xD but i think thats your point? also, the quesiton iss.. how many periods of FV values uses the MEAN formula to calculate the MEAN value of each period in MEANs sequence? (maybe should be as many as in the sequence of FV? [that is the value of periodsˢᵗᵈᶜᵛ quoting parameter?]) thanks'¡

Camille92 commented 7 years ago

Hey Carles,

No, it's way easier than that, the idea is to propose the option to report STDEV from the mean of STDEV period instead of FV.

So let's say periods = 120 and during this period the price moved by $5 from 2500 to 2505, STDEV = 3 with a width of 1.

If we use the current formula then after the 120 seconds when the price is at 2505 you will try to sell at 2508 and buy at 2502.

But if we report that from the mean so the average price of the last period (and let's say this mean is at 2503) then we sell at 2506 and buy at 2500.

The goal is to act like Bollinger bands :)

http://www.investopedia.com/terms/b/bollingerbands.asp

If you want to have Bollinger bands, just put factor = 2 and on TOPS or FV and TADA !

Bid = Mean(periods) - (Stdev (periods) Factor) Ask = Mean(periods) + (stdev(periods) factor)

Camille92 commented 7 years ago

Oh I got was I was not clear when I'm saying mean I'm meaning the SMA (Simple Moving average) of the price during X periods of 1 seconds!

So if X = 200 then SMA = (price(1) + price(2) + price(3) + (...) + price(200) ) / 200

ctubio commented 7 years ago

so.. in one hand we have STDEV onFV * factor, and in the other we have the same amount of periods of FV, but instead of calculate the STDEV, we calculate de SMA, then we simply take this SMA and add it or remove it for Bid/Ask side to the STDEV onFV, right?

sooo.. we end up having STDEV: onFV onTops, onTop, onFV-/+SMA, onTops-/+SMA

im understand this because: Bid = Mean(periods) - (Stdev (periods) Factor) Ask = Mean(periods) + (stdev(periods) factor)

right? xD

ctubio commented 7 years ago

so onFV-/+SMA (and also onTops-/+SMA) will end up generating 2 values each resulting period (to honor John Bollinger)

we all know what is onFV and onTops, and the new thing is just SMA that is the average of FV in same X periods, right? xD

Camille92 commented 7 years ago

Yes exactly (although you need to remove/add STDEV from the SMA because otherwise, you're going to get negative values for the BidPrice ;) )

The good thing is that we already calculate the SMA for the calculation of STDEV here: https://github.com/ctubio/Krypto-trading-bot/blob/master/src/lib/stdev.cc#L33

For the name I think to be more understandable for anyone we could call that:

BollingerBands (or BB in short) on Tops, FV, Top...

The result should look approximately like that for settings of 180 periods and a factor of 1 (but more detailed because we're at the second level, and the image is at 1mn tick).

capture d ecran 2017-06-19 a 14 10 52

The protection makes you buy at the bottom line and sell on the upper line, the middle line is the SMA. Pretty cool no?

Camille92 commented 7 years ago

Lastly, the cool thing about it is to get rid of the "always more" problem of K.

Basically what happen right now is that if the price goes down a bit, instead of considering that to be a good price, K will still try to go width lower.

Using this system it will, hopefully, allow running with lower widths settings but still getting most of the market.

ctubio commented 7 years ago

hope you dont miss anything'¡

Camille92 commented 7 years ago

Yesss ! I think it's perfect, I will try that soon.

Btw concercing https://github.com/ctubio/Krypto-trading-bot/issues/6 , If you use BB with a factor of 0 it is quasi similar to using ewma second. (The only difference is one is sma the other ewma). :)

Camille92 commented 7 years ago

Hey after a bit of testing, BB is working amazingly! So if you want to give it a go, here are the settings I'm using -> OnFV, 180 periods, factor 1.2 :) with width reduced to a minimum :).