ta4j icon indicating copy to clipboard operation
ta4j copied to clipboard

How to compare strategies

Open MarcDahlem opened this issue 4 years ago • 7 comments

I have checked existing issues and wiki

If possible provide a minimal working example!

Is there a reason, why the BarSeriesManager.run takes an amount of pieces to buy instead of a (normalized) amount of counter currency to use?

https://github.com/ta4j/ta4j/blob/master/ta4j-core/src/main/java/org/ta4j/core/BarSeriesManager.java#L172

I mean, If I run n strategies on m different series and want to find the best strategy for all those series, I cannot compare the results by the overall profit loss.

For example:

  • Series Apple = 100$/piece, Series Google =1000$/piece
  • Strategy1(Apple) = 1x100+10% = 110$, Strategy1(Google)=1x1000+10% =1100$
  • Strategy2(Apple) = 1x100 + 50% = 150$, Strategy2(Google)=1x1000 + 4% = 1040$

Strategy1 overall gain == 110$ Strategy 2 overall gain == 90$

But Strategy 2 is still better than Strategy 1 as it has a (normalized) profit of ~ 54%/2 = 27% instead of the first which just has a profit of 10%

If the prices were normalized and describe how many "counter currency" to use for the trade, the results were completely comparable...

MarcDahlem avatar May 18 '21 21:05 MarcDahlem

If the prices were normalized and describe how many "counter currency" to use for the trade

  1. What do you mean exactly by "normalized"? The resulting TradingRecords (Apple, Google) can be compared by ProfitLossPercentageCriterion and normally you choose the better one regarding to ProfitLossPercentageCriterion#betterThan.

Is there a reason, why the BarSeriesManager.run takes an amount of pieces to buy instead of a (normalized) amount of counter currency to use?

  1. I also think that we can omit the parameter amount (I always set it to 1).

nimo23 avatar May 18 '21 21:05 nimo23

Yes I can compare them with the ProfitLossPercentageCriterion, thanks.

But as I have multiple trading records that belong to the same strategy (just run on different series), I would like to combine them. Is there also a way to combine different TradingRecords? (for profitLoss i can just add the results. For percentage, I cannot add, because the different series have different amount of bars)

tradingRecord1 = managerApple.run(strategy1)
tradingRecord2 = managerApple.run(strategy2)
tradingRecord3 = managerGoogle.run(strategy1)
tradingRecord4 = managerGoogle.run(strategy2)

// TODO combine(tr1, tr3), combine (tr2, tr4).  ProfitLossPercentageCriterion#betterThan(combination1, combination2)
isStrategy1Or2better= ...

What I mean with normalize is, that for a position entry, not the given amount (e.g. 10 pieces, Apple = 10x100$ = 1000$, Google 10x1000$ = 10000$ ) is used. But amount/price is used as pieces to buy (Google 10$/1000$ = 0.01 pieces, Apple 10$/100$ = 0.1 pieces)

          if (strategy.shouldOperate(i, tradingRecord)) {
           
            // normalize amount
            Num closePrice = barSeries.getBar(i).getClosePrice();
            Num normalizedAmount = null;
            if (tradingRecord.getCurrentPosition().isNew())
                normalizedAmount = amount.dividedBy(closePrice);
            else if (tradingRecord.getCurrentPosition().isOpened()) {
                normalizedAmount = tradingRecord.getCurrentPosition().getEntry().getAmount();

            }

            tradingRecord.operate(i, closePrice, normalizedAmount);
        }

MarcDahlem avatar May 18 '21 21:05 MarcDahlem

What I mean with normalize is, that for a position entry, not the given amount

When using an amount of 1, you don't need to normalize it. However, when using an amount > 1, then it may be better to use amount/price..

nimo23 avatar May 18 '21 22:05 nimo23

not really. Also with a price of 1, apple would spent 100$ and Google would spent 1000$ when buying. If you just just want to spent 1$ you can get 0.01 Apple pieces or 0.001 Google pieces. And the overall profit loss would be comparable for both trades ;-)

MarcDahlem avatar May 18 '21 22:05 MarcDahlem

@MarcDahlem Please provide a PR to fix this issue by normalizing the amount with amount/price. And please provide a test case (two trading records with same strategy and counter currency) which shows the correctness of this normalization.

nimo23 avatar May 19 '21 07:05 nimo23

I posted an issue here in order to discuss, if such an optimization is wanted for the project. @team172011 what is your opinion about it? Should I open a PR which changes the backtesting from "amount of pieces to buy" to "amount of counter currency to use"? This would allow better comparison for backtesting of (n x m) (strategies x series) as the price would be normalized.

MarcDahlem avatar May 21 '21 18:05 MarcDahlem

I'd recommend using Expectancy or SQN rather than straight PNL when comparing strategies.

TheCookieLab avatar Jan 07 '23 15:01 TheCookieLab