Built my own backtester as a learning project - Open-sourced for feedback

Discussion in 'Strategy Building' started by trenki2, Aug 13, 2025 at 12:42 AM.

  1. trenki2

    trenki2

    I decided to build my own backtesting framework in C# (NovaTrader) primarily to learn how these systems work under the hood. Ended up implementing some features I couldn't find in existing tools. It is designed for portfolio based EOD strategies using market orders.

    Features:
    • Multi asset portfolios - use multiple assets at once
    • Multi strategy portfolios - combine multiple strategies
    • Hierarchical strategy nesting - Strategies within strategies, infinitely deep
    • Online Portfolio Algorithms - FixedShare and ExponentiatedGradient for adaptive allocation without manual weight tuning.
    Simple example:

    Code:
    public class RsiLowHigh(SecurityId sid, int period, double low, double high) : Strategy
    {
        public int Lookback { get; init; } = 42;
    
        protected override void OnInitialize()
        {
             Benchmark = AddSecurity(sid);
        }
    
        protected override void OnUpdate()
        {
            var close = Benchmark!.GetHistory().Close.TakeLast(Lookback).ToArray();
            var rsi = TA.Rsi(close, period)[^1];
            OrderTargetPercent(Benchmark, rsi < low ? 1 : 0);
        }
    }
    
    //////////
    
    // Adaptive example
    var trend = new BuyAndHold("QQQ");
    var meanRev = new RsiLowHigh("QQQ", 2, 25, 75);
    var adaptive = new FixedShare(trend, meanRev) { Eta = 50, Alpha = 1e-4 };
    AddSubStrategy(new ApplyPortfolio(fixedShare) { MinOrderValuePercent = 0.02m });


    Built for EOD portfolio strategies using market orders only. C# framework, small and fast.

    GitHub: https://github.com/trenki2/novatrader-core

    Sharing this in case it's useful for anyone else. The hierarchical nesting feature seems unique - haven't seen it implemented elsewhere in a similar and easy way.

    Looking for feedback from traders who might find these features helpful, or just curious if this fills any gaps in existing tools.

    Thanks for taking a look.
     
    Drawdown Addict, Sekiyo and MarkBrown like this.
  2. MarkBrown

    MarkBrown

    nice project more though and detail than i have seen in a long time.

    i was discussing this the other day with someone

    • switching models based on profit factor deterioration
    • dynamic allocation to highest performing pf model
    • ideally switching off non performing models
    • problem is idle model has to be live to track it
    • goal is to minimize total portfolio drawdown
     
  3. Sekiyo

    Sekiyo

  4. trenki2

    trenki2

    Yes, exactly. By “nesting,” I mean structuring strategies using the Decorator and Composite patterns. This allows a strategy to contain one or more sub-strategies, effectively forming a hierarchical tree of strategies. Each strategy can take the portfolio generated by its sub-strategies, apply additional rules or transformations, and produce a new portfolio.

    This approach has several advantages:

    1. Reusability: Individual strategies can be developed independently and reused in different combinations.

    2. Flexibility: You can dynamically compose complex strategies by combining simpler ones without changing their internal logic.

    3. Scalability: As your system grows, you can add new strategies without modifying existing ones, just by plugging them into the hierarchy.

    4. Separation of Concerns: Each strategy focuses on a specific aspect of portfolio management, making the overall system easier to understand and maintain.
    In practice, you might have a top-level strategy that applies overall portfolio constraints, while its sub-strategies handle individual sectors, risk adjustments, or optimization techniques. The final portfolio is the result of processing all these layers together.
     
  5. Just a note on the requirements section on GitHub, you don't need to be on Windows or any specific IDE.
    Since you are not using any GUI library and everything is console-based you can use it with any other environment. I am on Linux with Rider and can run it.

    I can see that the Strategies project uses a UI, but that is just your choice to run it.
     
  6. nz_melon

    nz_melon

    Ask @fan27 how that worked out for him.