mdeverdelhan / ta4j-origins

A Java library for technical analysis ***Not maintained anymore, kept for archival purposes, see #192***
https://github.com/mdeverdelhan/ta4j/issues/192
MIT License
375 stars 225 forks source link

Strategy compatible? #44

Closed lolygagv2 closed 9 years ago

lolygagv2 commented 9 years ago

So I'm looking over porting a multi-symbol, multi trade with hedging strategy to see if I can backtest. Until now it's been impossible to backtest due to the fact it hedges on multiple symbols. Do you think I could use this library to record this succesfully? in Both backtesting and Live trading?

This is kind of the list of things I need to find a way for it to do:

  1. Send out multiple buys and sell orders (up to 6, each could be a different trade/rule). Which ever buy or sell side gets triggered, an "exit" trade gets made, and then adjust dynamically to different "take profit" targets as the market/tick series move.
  2. Have a way to cancel/reset the trades and re-send them out. Eg. initial trades are sent at X distance from current market price, these trades cancelled after 30 minutes of inactivity, new trades are sent out at X distance from the "new current market price".
  3. Once a designed level of Loss or profit has been achieved, send out immediate close orders for all trades (all 6 or however many are open).
  4. IF an initial trade reach a desired level of loss, trigger to open a hedge on a different symbol. (I"m guessing this would require to make a new time series for the hedging strategy on the new hedge symbol that either runs simultaneously, or is activated when the "hedging" is triggered through an IF statement. The hedging strategy and time series would need to "sync" up with the original strategy to know when to activate/deactivate. The trades generated would also be recorded with the initial strategy somehow!
  5. The P&L should record successfully if all this works, but my concern would be live trading. Where should I insert the trade recording? Seems like I'd want the broker to send a "trade confirmation" and upon the broker "confirming" the trade was executed, then I would enter the trade into the traderecord and continue with the strategy?
    • Do all these things seem feasible? If so, any examples you have that are closest?

I became interested in this library, because I was looking for some API that had all the indicators I use to determine trading rules, without having to program them all myself :) like 3 different Moving averages, crosses over different time periods etc.

Right now, I was just basically planning to model a FIX engine simulator, have it send all the market data to the strategy, and dummy make a trade confirmation. Then record the trading record and evaluate the results after, but this TA library seems useful for backtesting as well.

(Attaching a screenshot, so u can see what kind of strategy I'm looking at, the main purpose for this GUI was just to monitor, stop/start the strategy with optional "kill" buttons for positions and hedging strategy separately.) strategyscreenie

mdeverdelhan commented 9 years ago

Do you think I could use this library to record this succesfully? in Both backtesting and Live trading?

Not simply. It's always possible but you will have to do some things manually. For backtesting you will have to reimplement the algorithm of the TimeSeries#run() method. Nowadays you can backtest on only one series. For live trading it should be easier: you just have to improve the trading bot example with more time series and if statements.

About your 1st point

You can build one time series per symbol, and one strategy per symbol too. Then all your strategies have to share the same instance of TradingRecord. Since I use ta4j only for one-symbol trading (for now) I didn't try, but it should be possible (restriction: I just think TradingRecord is filled with tick indexes, so attached to a time series, you'll have to make some adjustements).

About your 2nd point

Should be possible. You will have to implements your own trading rules (Rule interface). I will be glad to merge that kind of rules if you would like to make a PR. :)

About your 3rd point

Sure. Have a look to the trading rules ta4j provides. There's already StopLossRule and StopGainRule. You just have to add them to all your strategies (reminder: one per symbol).

About your 4th point

The TradingRecord object could be used to sync your different strategies. But again it relies on tick indexes from a specific series.

About your 5th point

Seems like I'd want the broker to send a "trade confirmation" and upon the broker "confirming" the trade was executed, then I would enter the trade into the traderecord and continue with the strategy?

It has to work like this. I didn't take the responsibility of automatically "record" the trade. So it's up to the bot designer and it won't change.

About your point point

It's feasible (at least for live trading) but it may need some work. Sorry no example, for now I use ta4j only for one-symbol trading.

Thank you for your interest. Don't hesitate to contribute if some indicators are lacking or if you want to improve something. (see the CONTRIBUTING.md file)

If it doesn't fit your needs (for whatever reason) have a look to other TA libs: https://github.com/mdeverdelhan/ta4j/wiki/Alternative%20libraries

lolygagv2 commented 9 years ago

Thanks! Since it's definitely not plug and play, I'll probably try to adopt some of the methods and classes and then do my own implementations to work with my strategy and FIX protocol. If I come up with anything interesting, I'll submit a PR or contribute it!