Backtesting Developer Guide

Developer Guide

1. Enter the Backtesting Interface and Log In

After entering the backtesting interface, follow the instructions to complete the login.

2. View the Default Strategy Code

Click the promotion button on the page to view the default strategy code.

3. Write Strategy Code Around Model Signals

Our strategy code needs to revolve around the signals of our model. The current signals of our model include:

  • prediction: predicted price
  • price: real price
  • volume: volume
  • mean_pnl: mean profit and loss
  • max_pnl: maximum profit and loss
  • transactions: transactions

4. Example Trading Strategy

Now, suppose we need to write a strategy to buy when the model's predicted increase is greater than 15%. The implementation logic can be:

if prediction/price > 0.15:  # If predicted increase is greater than 15%
    if self.pos == 0:  # If there is no current position
        # Buy to open a position
        self.buy(bar.close_price, abs(open_volume))
    if self.pos < 0:  # If there is a short position
        # First, buy to close a position
        self.cover(bar.close_price, abs(self.pos))
        # Then, buy to open a position
        self.buy(bar.close_price, abs(open_volume))
  • prediction means our predicted price
  • price means real price
  • self.pos means current position
    • self.pos == 0 means no position
    • self.pos < 0 means a short position

5. Unchangeable Code

The following two lines of code should not be changed during development:

self.buy(bar.close_price, abs(open_volume))
self.cover(bar.close_price, abs(self.pos))