femtotrader / TALib.jl

A Julia wrapper for TA-Lib
Other
52 stars 11 forks source link

roadmap for a Julia TA-Lib wrapper #1

Open femtotrader opened 8 years ago

femtotrader commented 8 years ago
femtotrader commented 8 years ago

Some help to define a (better) roadmap will be nice

Pinging @milktrader @mrjbq7

Feel free to ping some other people who might be interested

mrjbq7 commented 8 years ago

There are a lot of functions to define, I ended getting one to work manually then made a code generator that used that as a template to create the code for all of the functions:

https://github.com/mrjbq7/ta-lib/blob/master/tools/generate.py

femtotrader commented 8 years ago

Ideally I think we should have a kind of tabular or JSON format to define functions and group them according similar behavior (return one array (like SMA) or 3 like BBANDS ...) Such intermediate format will help to make wrappers in several languages. Your opinion @mrjbq7 ?

femtotrader commented 8 years ago

This idea is (partially) implemented here: https://github.com/femtotrader/TALib.jl/tree/master/scripts (see parse_headers_manually.py

femtotrader commented 8 years ago

An other interesting approach could be to use a Python C parser library to create intermediate file from parsed headers. pyclibrary http://pyclibrary.readthedocs.io/ seems interesting. You can find in same directory parse_headers_pyclibrary.py and JSON / YAML files.

Clang might also be considered https://github.com/ihnorton/Clang.jl (an example https://github.com/mmacedoeu/amqp.jl/blob/master/gen.jl )

femtotrader commented 8 years ago

MA and BBANDS seems to be working fine (see README).

mrjbq7 commented 8 years ago

Nice!

milktrader commented 8 years ago
femtotrader commented 8 years ago

That's add to roadmap. There is 3 level of functions

But ideally I would like to avoid to add DataFrames.jl (or TimeArray.jl) as required dependencies (just optional dependencies)

I did code for level 0.

see generate_ta_func_raw in scripts/ta_func_api.jl but these function display a string which is output to STDOUT. I generate code using

julia scripts/ta_func_api.jl > out.jl

but I think I can do something more elegant with macros.

Any help on this will be great.

In the meantime, I will try to implement this weekend level 1 functions.

femtotrader commented 8 years ago

@milktrader maybe you can provide some examples of TimeArray usage for MA and BBANDS like I did in TALib.jl for DataFrames. It will be easier for me to implement code generation if I know what code I should generate.

see

function MA(dfOHLCV::DataFrames.DataFrame; timeperiod::Integer=30, matype::TA_MAType=TA_MAType_SMA, price=PRICE)

and

function BBANDS(dfOHLCV::DataFrames.DataFrame; timeperiod::Integer=30, nbdevup::AbstractFloat=2.0, nbdevdn::AbstractFloat=2.0, matype::TA_MAType=TA_MAType_SMA, price=PRICE)
femtotrader commented 8 years ago

level 2 support with DataFrames as inputs / outputs is implemented.

We can do now

using TALib
using DataFrames
using PyPlot

filename = "test/ford_2012.csv"
df = readtable(filename)
df[:Date] = Date(df[:Date])

indic = MA(df)
plot(df[:Date], df[:Close], indic[:Date], indic[:Real])

indic = MA(df, price=:Open)
plot(df[:Date], df[:Close], indic[:Date], indic[:Real])

indic = BBANDS(df)
plot(df[:Date], df[:Close],
    indic[:Date], indic[:UpperBand],
    indic[:Date], indic[:MiddleBand],
    indic[:Date], indic[:LowerBand]
)

Next step will be to have level 2 - TimeArray functions

Code is quite ugly but everyone is free to help.

femtotrader commented 8 years ago

level 2 support with TimeArrays as inputs / outputs is also implemented.

We can do now

julia> using TALib
julia> using TimeSeries
julia> using PyPlot

julia> filename = "test/ford_2012.csv"
julia> ohlcv = readtimearray(filename)
250x5 TimeSeries.TimeArray{Float64,2,Date,Array{Float64,2}} 2012-01-03 to 2012-12-31

             Open     High     Low      Close    Volume
2012-01-03 | 11.0     11.25    10.99    11.13    45709900
2012-01-04 | 11.15    11.53    11.07    11.3     79725200
2012-01-05 | 11.33    11.63    11.24    11.59    67877500
2012-01-06 | 11.74    11.8     11.52    11.71    59840700
⋮
2012-12-26 | 12.31    12.79    12.31    12.79    140331900
2012-12-27 | 12.79    12.81    12.36    12.76    108315100
2012-12-28 | 12.55    12.88    12.52    12.87    95668600
2012-12-31 | 12.88    13.08    12.76    12.95    106908900

julia> indic = MA(ohlcv)
250x1 TimeSeries.TimeArray{Float64,1,Date,Array{Float64,1}} 2012-01-03 to 2012-12-31

             Real
2012-01-03 | NaN
2012-01-04 | NaN
2012-01-05 | NaN
2012-01-06 | NaN
⋮
2012-12-26 | 11.3347
2012-12-27 | 11.3933
2012-12-28 | 11.4667
2012-12-31 | 11.546

julia> plot(ohlcv.timestamp, ohlcv["Close"].values, indic.timestamp, indic["Real"].values)

julia> indic = BBANDS(ohlcv)
250x3 TimeSeries.TimeArray{Float64,2,Date,Array{Float64,2}} 2012-01-03 to 2012-12-31

             UpperBand  MiddleBand  LowerBand
2012-01-03 | NaN        NaN         NaN
2012-01-04 | NaN        NaN         NaN
2012-01-05 | NaN        NaN         NaN
2012-01-06 | NaN        NaN         NaN
⋮
2012-12-26 | 12.9438    12.11       11.2762
2012-12-27 | 13.1808    12.316      11.4512
2012-12-28 | 13.2853    12.536      11.7867
2012-12-31 | 13.1319    12.754      12.3761

julia> plot(ohlcv.timestamp, ohlcv["Close"].values,
    indic.timestamp, indic["UpperBand"].values,
    indic.timestamp, indic["MiddleBand"].values,
    indic.timestamp, indic["LowerBand"].values
)

...