Python - Backtesting library with variable position sizing

Discussion in 'Automated Trading' started by Zugzwang, Jan 22, 2023.

  1. Zugzwang

    Zugzwang

    Hi, I’m looking for a good backtesting library and backtesting.py appears to be the best. However, part of my backtest should include variable position sizing. For example, if historical vol is 10% I might have a position that’s 50x leveraged vs a 25x leveraged position at 20% vol.

    Reading through backtesting.py’s documentation, it doesn’t appear that variable position sizing is possible. Is there a way to do this or could someone suggest a more appropriate library?

    Thanks in advance!
     
  2. RedDuke

    RedDuke

    Use NinjaTrader. You can do anything there in c#.
     
    Zugzwang likes this.
  3. Zugzwang likes this.
  4. Zugzwang

    Zugzwang

    Thank you both! I will be using Python via VM (probably AWS) to trade FX through OANDA. Essentially, I need a library that can accept position sizing that's based on values in a dataframe.
     
  5. Warren

    Warren

    Well, as for your need, I guess you may try the backtrader library, which has a more comprehensive functionality than backtesting.py.
     
  6. lariati

    lariati

    Personally, I would just look up articles on building your own backtest engine with pandas.
    Pandas has already done most of the work. I have seen a number of articles previously. This is a decent starting point



    https://github.com/gylx/Financial-Machine-Learning-Articles/blob/master/Vectorized Backtesting.ipynb

    Keeping track of historical vol and the position size would be like any other signal. Once you have the statement management done for one signal, it should be rather trivial to add additional signals.