cinar / indicatorts

IndicatorTS - Stock technical indicators and strategies in TypeScript for browser and server programs.
MIT License
293 stars 53 forks source link

How do you backtest a custom strategy? #204

Open MrAlvaroPS opened 1 year ago

MrAlvaroPS commented 1 year ago

Hi!

First of all, awesome work!!

I'm struggling with backtesting a custom strategy. I think I don't really understand how your backtesting works. I mean, how do you set your TP or SL for a strategy?

Let's say I want to test an RSI strategy, selling with the RSI over 70 and buying with de RSI under 30, and I want to try with a 0.5% profit point under (or over) the entry (order) point.

Is there a way to try this?

Thanks!

cinar commented 1 year ago

Hi!

I'm glad to hear that you found the project useful! I tried to produce a quick example based on your description on repl.it:

https://replit.com/@onurcinar/indicatorts-rsi-strategy-backtest#index.ts

The current backtest doesn't support the profit points, but certainly would be nice to add that. I just need to understand how that works definitely.

The default RSI strategy rsi2Strategy currently sells over 90, and buys under 10, but a new one can be defined by the user easily:{

const myRsiStrategy: StrategyFunction = (asset: Asset): Action[] => {
  const indicator = customRsi(2, asset.closings);

  const actions = new Array<Action>(indicator.length);
  for (let i = 0; i < actions.length; i++) {
    if (indicator[i] < 30) {
      actions[i] = Action.BUY;
    } else if (indicator[i] > 70) {
      actions[i] = Action.SELL;
    } else {
      actions[i] = Action.HOLD;
    }
  }

  return actions;
};

Once you have that, you can easily use it with the backtest function:

const myRsiStrategyInfo: StrategyInfo = {
  name: "My RSI 70-30 Strategy",
  strategy: myRsiStrategy,
};

const results: StrategyResult[] = backtest(sp500, [myRsiStrategyInfo]);
console.table(results);

I have a working example at the repl.it link. Please take a look. If you can help me with how the profit margin will work there, I will be happy to add that here.

Best regards,

-onur

MrAlvaroPS commented 1 year ago

Hi! Thanks for the answer!!

Yep, I imagined the current approach do not consider the Take Profit points. It's currently not that hard, but I need to fully understand your code first.

I think the correct approach to Take Profit points should be something like, if the percentage you put as Take Profit it's inside the next 2 candles, then it'a a success, always keeping in mind that the TP point should be implemented with a SL point too.

For example, something like this: SL: 5% (configurable and optional) TP: 1% (configurable and optional)

If the next 1- 2 candles contains the TP value and it doesnt contains the SL value, then is a success by the percentage value. If it contains the SL, then it's a loss equivalent to the SL percentage value.

image

This way you can actually backtest a complete strategy, giving more or less space to SL and TP points to reach the sweet spot.

Edit: If I get some spare time today, i'll give it a code-try

Edit2: How do you set the gain right now? Just by watching the closing value?