Search code examples
ruby-on-railsrubyquantitative-financeback-testing

How can I architecture my application to backtest data in Rails?


I want to backtest some data, I would like the output of my backtest and analysis to be an input into the decisions of my application.

I thought about duplicating a model/table and for the purposes of performing my backtest and analysis, but then I would be doubling my workload and isn't particularly scalable.

Is it possible to dynamically toggle the rails environment for particular a particular class? I.e. create a BacktestingService class, any database read/writes are done to a 'test' database whilst the rest of the app continues to use development/production environment?

Can you suggest any other solutions for my use case?


Solution

  • Using connected_to I can switch to a different database (in this case :test), perform my backtest and analysis within a connected_to block:

     ActiveRecord::Base.connected_to(database: :test) do
        backtest stuff here
     end
    
    

    Outside of the block I can use my default database.

    I found this useful: https://prathamesh.tech/2019/08/13/rails-6-multi-database-part-two/