ysdede / custom_indicators

Technical indicators written in pure Python & Numpy/Numba
16 stars 7 forks source link

Can i have the OTT indicator in javascript? #1

Closed rishikesh2003 closed 2 years ago

rishikesh2003 commented 2 years ago

I am developing a crypto trading bot and i need this indicator for automated trading. It would be great if the ott indicator is implemented in javascript

rishikesh2003 commented 2 years ago

I already developed the trading bot with MACD. And it is not that promising when market is in sideways direction and i found out that this OTT indicator is much more promising than MACD. It would be of great help if you converted this into javascript.

ysdede commented 2 years ago

You are right. Sideways markets are difficult for many reasons. Sorry, I'm not very good at JS. I'm not sure if js has libraries like numpy/numba/pandas, or if it's suitable for data science tasks. I guess you can use a wrapper to call ott.py and get results.

rishikesh2003 commented 2 years ago

Thank you for your reply

rishikesh2003 commented 2 years ago

For example the formula of MACD is MACD=12-Period EMA − 26-Period EMA. Like this OTT have any formulas to calculate? It would be a great help for me. If i know the formula i can easily implement the code in javascript and also it would be great if you explain me the ott indicator

ysdede commented 2 years ago

The hardest part for me was not writing python code but understanding pine code. Pinescript works recursively and looks like magic. Just one line of pinescript code does tons of things internally, that's why my Python code looks like garbage when compared to pine code.

The best way to proceed is to generate the MAvg, difference, longStop, shortStop, MT signals in order and import them into tradingview at each step and compare them with the signal produced by the original code.

Btw, MAvg is optional moving average, you can pick a ready to use one at first like ema, sma, kama. Fark is Difference.

That's the all OTT code, it looks simple.

MAvg=getMA(src, length)
fark=MAvg*percent*0.01
longStop = MAvg - fark
longStopPrev = nz(longStop[1], longStop)
longStop := MAvg > longStopPrev ? max(longStop, longStopPrev) : longStop
shortStop =  MAvg + fark
shortStopPrev = nz(shortStop[1], shortStop)
shortStop := MAvg < shortStopPrev ? min(shortStop, shortStopPrev) : shortStop
dir = 1
dir := nz(dir[1], dir)
dir := dir == -1 and MAvg > shortStopPrev ? 1 : dir == 1 and MAvg < longStopPrev ? -1 : dir
MT = dir==1 ? longStop: shortStop
OTT=MAvg>MT ? MT*(200+percent)/200 : MT*(200-percent)/200 

That's the Python port I created, it is similiar to Js or C code. The only difference is numpy methods like add, subtract. If Js have similiar libraries you can port it easily or rewrite this methods in Js by iterating timeseries. Reading source code of libraries like Tulipy (Written in ANSI C) can help to understand how to create indicators in Js. I use it as a reference.

@numba.njit
def ott_fast(MAvg, percent, length):
    fark = np.multiply(np.multiply(MAvg, percent), 0.01)
    # >>>>>>>
    longStop = np.subtract(MAvg, fark)
    longStopPrev = np.copy(longStop)

    for i in range(length, longStop.size):
        if MAvg[i] > longStop[i - 1]:
            longStop[i] = max(longStop[i], longStop[i - 1])
            longStopPrev[i] = longStop[i - 1]

    for i in range(length, longStop.size):
        if MAvg[i] > longStopPrev[i]:
            longStop[i] = max(longStop[i], longStopPrev[i])
            longStopPrev[i] = longStop[i - 1]
    # <<<<<>>>>>>>
    shortStop = np.add(MAvg, fark)
    shortStopPrev = np.copy(shortStop)
    for i in range(length, shortStop.size):
        if MAvg[i] < shortStop[i - 1]:
            shortStop[i] = min(shortStop[i], shortStop[i - 1])
            shortStopPrev[i] = shortStop[i - 1]

    for i in range(length, shortStop.size):
        if MAvg[i] < shortStopPrev[i]:
            shortStop[i] = min(shortStop[i], shortStopPrev[i])
            shortStopPrev[i] = shortStop[i - 1]
    # <<<<<>>>>>>>
    dir = np.full_like(longStop, 1)
    temp_dir = dir[length - 1]
    for i in range(length, dir.size):
        if temp_dir < 0 and MAvg[i] > shortStopPrev[i]:
            temp_dir = dir[i] = 1
        elif temp_dir < 0 and MAvg[i] < shortStopPrev[i]:
            temp_dir = dir[i] = -1

        elif temp_dir > 0 and MAvg[i] < longStopPrev[i]:
            temp_dir = dir[i] = -1
    # <<<<<>>>>>>>

    MT = np.where(dir > 0, longStop, shortStop)
    OTT = np.where(MAvg > MT, MT * (200 + percent) / 200, MT * (200 - percent) / 200)
    return np.concatenate((np.full(2, 0), OTT[:-2])), longStop, shortStop
rishikesh2003 commented 2 years ago

Thanks, i will try to implement this in js

rishikesh2003 commented 2 years ago

Hi I tried to implement the indicators in JS, but is kinda hard because I am not good at numpy and data structures stuff. So, I used a wrapper called python-bridge to use the indicators in python in javascript Link to the package.

rishikesh2003 commented 2 years ago

And just wanted to tell OTT works way better than MACD!!

ysdede commented 2 years ago

Thanks for informing. Yep I've tried lots of indicator before. OTT is cool.