Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How do you backtest a custom strategy? #204

Open
MrAlvaroPS opened this issue Nov 21, 2022 · 2 comments
Open

How do you backtest a custom strategy? #204

MrAlvaroPS opened this issue Nov 21, 2022 · 2 comments

Comments

@MrAlvaroPS
Copy link

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
Copy link
Owner

cinar commented Nov 21, 2022

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
Copy link
Author

MrAlvaroPS commented Nov 22, 2022

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants