panifie / PingPong.jl

Cryptocurrency trading bot, and backtesting framework in julia
https://panifie.github.io/PingPong.jl/
GNU General Public License v3.0
36 stars 9 forks source link

Convert some Trality examples #25

Closed c0indev3l closed 8 months ago

c0indev3l commented 1 year ago

Hello,

Trality, a Python crypto trading bot will stop. See https://www.trality.com/blog/trality-winding-down-product I'm considering moving to Julia and an open source trading environment. Converting some Trality examples to PingPong.jl could help to attract new users. Here are some examples https://gist.github.com/c0indev3l/855381f07865dfca079e8252e736ae35 and documentation https://docs.trality.com/

Kind regards

c0indev3l commented 1 year ago

An important concept in Trality bots is the concept of "handler" https://docs.trality.com/trality-code-editor/core-concepts/overview See for example https://gist.github.com/c0indev3l/855381f07865dfca079e8252e736ae35#file-two_intervals-py Functions are schedule to be run periodically (every hour for one and every 15 minutes for an other one). My guess is that their bot is composed of several parts. When live trading, one part is probably simply receiving price data and storing them into a database (maybe a TSDB). An other part is responsible of scheduling what function will have to be executed in the future. A third one is responsible of executing scheduled function at expected time. What is your opinion about that ?

panbonker commented 1 year ago

The bot doesn't manage data for you, see here https://github.com/panifie/PingPong.jl/issues/16

To access ohlcv data from a strategy you can do so from an AssetInstance variable: ai.data is a dict where keys are timeframes and values are dataframes of ohlcv data. The first pair should always be the smallest timeframe of the strategy, which is the one you monitor for updates (during paper and live mode). The rest of the dataframes in the dict (if any) should be resampled (downsampled) from the smaller one. For convenience the "main" timeframe of the asset instance (and strategy) can be accessed with the ohlcv property (ai.ohlcv).

During backtest you need to pre populate the dataframes your self. There are utilities in the bot to download data in different ways. see here a function to load data from the Scrapers module https://github.com/panifie/PingPong.jl/blob/master/PingPong/test/env.jl and here a function to load a strategy, and fill its assets with stub data https://github.com/panifie/PingPong.jl/blob/master/PingPong/test/env_strat.jl

During paper/live you create a Watcher from the Watchers module and use the watcher view which is a dict of dataframes (ohlcv) and use those dataframes in your strategy universe, see here where we create the watcher and fill the strategy universe with the data monitored by the watcher https://github.com/panifie/PingPong.jl/blob/2ef17a8b1b4915c74389082e798cced395631edd/user/strategies/Example.jl#L26-L28

If you want to execute something at specified interval the simplest approach is to just track timestamps:

isstale15m(s, ts) = ts - attr(s, :job_15m, DateTime(0)) > tf"15m"
job15m(s, ts, ats) = (println("Running job every 15m"); setattr!(s, :job_15m, ts))

function ping!(s, ts::DateTime, args...)
    ats = available(ts)
    isstale15m(s, ts) && job15m(s, ts, ats)
end

A fancier approach would involve using either Rocket.jl or DiscreteEvents.jl but I don't think that's necessary, If I implement it it would be more like an optional feature where you can call a pong! function and get a virtual clock to which you can subscribe functions that run at a particular time, and that clock would be replaced with a realtime clock in paper/live mode.