Backtest strategy with API

Hi all,

I am new to portfolio123 and trying to speed up the learning curve with the API. I currently have a simulated strategy that I am working on. The factor, buy rules, universe, period, # stocks etc,

are all fixed and I am looking to experiment different combination of sell rules with the API. Previously what I have been doing was to manually duplicate a simulated strategy and manually tweak the sell rules (not sure whether that’s the correct way of doing it). So I am attempting to replicate what I previously did with the API.

I am aware that strategy(), strategy_trading_system() are all read, whweras the strategy_rerun() re-execute the strategy (not sure why we want to do that), and the strategy_trading_system_update() allows modification of sell rules (only for live strategy).

I am sure somebody probably came across the same situation as it is rather common in quant backtesting. It would be great if the community can point me how that’s usually done within portfolio123, and the API

1 Like

I don't think this is possible with the API, but have you tried the optimizer?

Go to Research, then Optimizer Studies, then New, then Trading System Study, then choose a simulation to base your study on, then click Save. You can then scroll down to Sell Rules and add some rules. Then click Generate Combinations, then Run Combinations.

1 Like

The steps would be:

  • Get the id from the simulation that you want to use as your starting point.
  • The strategy rerun endpoint lets you modify the sell rules (and other things) in the simulation and rerun it.
  • Use the strategy endpoint to read the strategy details and store them in a file for each sell rule tested.

Hi Gents,

Thanks for helping out. Here’s how I’m currently doing it. Make the template (fixed buy rules, factor rank method etc), then loop across different rules in python and then store the reults. To do so I will need 2 api calls, one to obtain the base line system (0th iteration, 1st iteration…), then try out the new rules.

I am assuming the the api cannot launch new strategy so just using a single id (so the result changes with each iteration)

rules = [sell_rule1, sell_rule2…]

// extract info from baseline
get_baseline_system(id=xxxx)
for rule in rules:
     client.strategy_rerun(STRATEGY_ID, payload)

     // get the results from rule
     fetch_modified(client)