Run_algorithm from other script



how can I run a strategy from another python script?

What I want to achieve is to run a strategy inside another script and iterate over different parameters, e.g. RSI thresholds, which are injected into ‘handle_data’.



I’m not sure I understood you correctly, but why don’t you just import the script into the algorithm script, and just call it when needed in handle_data?


it’s more the other way round. I need to call run_algorithm from another script a multiple times with different parameters. Its working so far with import my_strategy and then run_algorithm(). But it takes too much resources to initialize at each iteration.

Is it possible to run_algorithm() a multiple times with different parameters and to initialize only one time at the beginning of the script? I.e. to run the step below only once:

[2018-03-05 22:07:46.174977] WARNING: run_algo: Catalyst is currently in ALPHA. It is going through rapid development and it is subject to errors. Please use carefully. We encourage you to report any issue on GitHub:
[2018-03-05 22:07:49.179248] INFO: run_algo: running algo in backtest mode
[2018-03-05 22:07:49.758811] INFO: exchange_algorithm: initialized trading algorithm in backtest mode
[2018-03-05 22:07:49.797186] INFO: my_strategy: initializing algo



what exactly do you want to run that doesn’t need initialization? can you give an example for the process you are trying to run?


I try to optimize some parameters using a genetic algorithm. For that I’m calling run_algorithm as the fitness function which returns the trading performance (e.g. portfolio_value or sharpe_ratio). Since I need to calculate the fitness (return from the trading period) so many times, I try to find a way to optimize computation time. It seems that starting up catalyst until initialize() is the most demanding task (5/6 of comp. time for a simple strategy). My mockup code:

from catalyst import my_strategy 

def fitness(individual):
    my_strategy.genome = individual
                initialize=my_strategy .initialize,
                handle_data=my_strategy .handle_data,
                start=env.pd.to_datetime('2017-08-01', utc=True),
                end=env.pd.to_datetime('2017-09-01', utc=True),
    return my_strategy.portfolio_value

def evolution:
    individual=np.random.randint(0, 1, 8000) 
    for i in Range(0,5000):


after a bit of research and if I understood correctly I’m afraid it’s not possible to run such a task on catalyst… there is no way to run initialize independently.


Hi Wafram,

I’m doing the same. Here’s an example of how I achieved it.

External script

algorithm = internal.alg()     # create an instance of the alg class
algorithm.example_parameter = 4     # Example of changing a parameter used inside the class

i = 0
while i < 2:
    algorithm.default_run()         # run the algorithm

    print(algorithm.score)          # look at the score
    i += 1


  class alg:

    # auto called on class creation
    def __init__(self):
    # self. defines here

    def initialize(self, context):
    # initialise here

    def handle_data(self, context, data):
    # trading code here

    def default_run(self, base=1000, freq='minute', exchng='bitfinex',   currency='usd'):
            initialize=self.initialize,      # the def to use to initialise
            handle_data=self.handle_data,    # the def to use to run the alg
            analyze=self.analyze,            # the def to use to analyse the data
            start=pd.to_datetime('2017-9-22', utc=True),
            end=pd.to_datetime('2017-9-23', utc=True),


note: what i did above takes ~30 seconds per iteration. Catalyst seems to take a long ass time to do almost nothing. If anyone has hints on speeding it up.

To achieve my goal I want to run it at least 100 times per minute.


hi yubyub, thanks for sharing. I can speed up things a little bit with using ‘class’.

Another thing I found in utils -> -> line:154 -> sleep(3)
It stops initializing for 3 seconds. I think it might be for reading the message “Catalyst is currently in ALPHA…”.
You can save 3 secs per iteration…Not much, but a start