markcheno / go-talib

A pure Go port of TA-Lib (http://ta-lib.org)
MIT License
780 stars 216 forks source link

Discuss a possible implementation upgrade to better match tradingview's pinescript way of code #10

Closed saniales closed 6 years ago

saniales commented 6 years ago

This would allow to easily import pinescript strategies into go talib based programs

as far as I've seen, the more problematic feature is the security function by tradingview, which allows to switch context and have new candles and manipulate them

from function signature (https://it.tradingview.com/study-script-reference/#fun_security) in its simplest form:

security(symbol, resolution, expression)

where

an example of call would be

security("BTCUSD", "1d", close) // gets the close values from btcusd 1 day chart
security(heikinashi("BTCUSD"), "30m", hlc3) // gets the heikinashi candles of btcusd 30m chart, then performs HLC3 [(high+low+close) / 3] to the gotten series and returns it

To translate this in go-talib way I have an idea: let's create the following function

func security(inHigh, inOpen, inClose, inLow []float64, expression Manipulator) []float64

the in-arrays represent the symbol + resolution parameters results while the manipulator function will defined as follow:

type Manipulator func(inHigh, inOpen, inClose, inLow []float64) []float64

In this way we would be able to use it like this:

// we do not really care about how candles are extracted, let's assume to use the following function
inHigh, inOpen, inClose, inLow := extractFromSomeCandles(symbol, resolution) // we can extract it from cryptocurrency exchanges as well as from forex brokers
manipulator := talib.Hlc3 // we need to change the signature of the function to follow manipulator signature
// then we use it
result := talib.Security(inHigh, inOpen, inClose, inLow, manipulator)

We can also wrap manipulators one inside another, like the following:

manipulator1 := talib.Hlc3
manipulator2 := func(inHigh, inOpen, inClose, inLow []float64) []float64 {
    manipulatedHigh, manipulatedOpen, manipulatedClose, manipulatedLow := manipulateInSomeWay(inHigh, inOpen, inClose, inLow)
    // pass manipulated array to the second function
    return manipulator1(manipulatedHigh, manipulatedOpen, manipulatedClose, manipulatedLow)
}

A better way should be found though, maybe through the use of a Wrap function in some way

Another idea would be to create a ContextSwitcher function which is like the following

func(inHigh, inOpen, inClose, inLow []float64) ([]float64, []float64, []float64, []float64)

@markcheno what do you think? is it possible in your opinion. In case it's not I can create a separate repo, detaching from talib

saniales commented 6 years ago

Pinescript general reference : https://it.tradingview.com/study-script-reference Of course functions like plot and study (which are inherently used by trading view for graphical purposes) can be avoided to be ported, while input function can be substituted with input from go program (or user)

markcheno commented 6 years ago

Are you proposing to re-create Pinescript in Go? If so, I think a new repository would be best.

saniales commented 6 years ago

What I was thinking was to fuse existing talib-implementation into pinescript way of code, not to directly translate pinescript itself, since pinescript has talib features built into it. I am asking here since it would inherently mean to recreate all talib functions and give them a new signature.

saniales commented 6 years ago

@markcheno let's use an example from the library

inHigh, inOpen, inClose, inLow := extractFromSomeCandles(symbol, resolution) // we can extract it from cryptocurrency exchanges as well as from forex brokers
results := talib.Security(inHigh, inOpen, inClose, inLow, talib.AvgPrice)
markcheno commented 6 years ago

If you want to re-create all the functions with a different signature, I think it would be best to start a new repository.