Building and Backtesting your first Stock Trading System

This topic contains 0 replies, has 1 voice, and was last updated by Seer Seer 7 years, 1 month ago.

Viewing 1 post (of 1 total)
  • Author
    Posts
  • #623
    Seer
    Seer
    Keymaster

    In this tutorial, we will step through the basic process of building and backtesting a stock trading system upon the following observation (or illusion?) of market behaviour:

    I have noticed that when a stock rises 3 days in a row it can signal a small bull run in the near term

    We will now build a system to test this theory.

    First we will create a empty trading account:

    Run Seer

     Click on new. This will create a new account called NewAccount
     Click on the NewAccount Test object and change the name to MyAccount. Changing the name of any object is the same process as renaming files, simply click on the text, then click again and you’ll be able to edit the text.

    Uncheck the the three options (short selling, negative balance and pyramiding) if they are set. Click OK to save the changes.

    Click on the plus near MyAccount in the treeview, this will show you all the objects within the account.

    Click on the Systems Folder.

     Click on New. This will create a new system within MyAccount called NewSystem1
     Click on the NewSystem1 system object. Change the name to MySystem. Click OK to save the changes.
     Click on the save icon to save the account. In the window that pops up, type MyAccount.xml. Click save.

    You have just created a trading account (MyAccount) which contains one system (MySystem).

    Click on the plus near MySystem in the treeview, this will show you all the objects with in the system.

    We now need to add some symbols in the portfolio object.
     Click on the Portfolio object. We are going to add Microsoft to the portfolio. Either type in MSFT from the drop down, or click the find icon and select Microsoft. Once you have selected Microsoft, click OK to add it to the portfolio. The system MySystem will now use MSFT during backtesting.

    We now need to replace the empty Money Management Object with one that will place trades for us during backtesting.

    Click on the plus near Library in the treeview.

    Click on the plus near Money Management in the treeview.

    Click on FixedCash
     Click on Copy

    Click on MySystem
     Click on Paste.

    We have now replaced the empty money management with Fixed Cash – a basic money management object that will enter positions with a cash value of 5000.

    We now need to create the trading rules for the system MySystem.
     Click on the Bar Event. The bar event will be run at the end of every bar between the from date and the to date. Cut and paste the following code into the event object.

      #Only test the symbol if we do not hold a position.
      if (not Position) {
        #Has the stock risen today?
        if (Today(Close)>Today(Open)) {
          #Did the stock rise yesterday?
          if (Yesterday(Close)>Yesterday(Open)) {
            #Did the Stock rise two days ago?
            if (Ago(Close,2)>Ago(Open,2)) {
              #The stock has risen 3 days in a row - buy on the open.
              BuyOpen;
            }
          }
        }
      }
      #Exit the position after 10 days.
      SellOpen if BarsSinceEntry>10;

    Change the from date to 1-Jan-1996 and the to date to 01-Jan-2000. Then Click on Backtest.

    The results of the system has a winning percentage rate of 62%, with a profit factor of over 3. If we think a little more about the entry rules, would we be more likely to have a winning trade if MSFT was trending down, with the 3 up days signalling a breakout?

    Replace:

      BuyOpen;

    With:

      if (Today(EMA(Close,9))<Today(EMA(Close,15))) {
        BuyOpen;
      }

    The new version of the trading system has a winning percentage of over 80% as well as a profit factor of over 10. However, as we trade so few times the results of this backtest is not statistically meaningful. We also have to consider that as Microsoft shown so much growth over the test period any trading system buying at random and holding for 10 bars would likely also be profitable.

    To include the full dataset upto the latest bar in the datastore, click on the last date button, and then click backtest. We now have more trades, but the peformance profile has changed, with over 60% of trades begin profitable and a profit factor of over 3.

    What if we traded this system against a portfolio of stocks?

     Click on the Portfolio object. Add IBM and INTC to the portfolio, then click backtest. The results of the system are far from impressive, now with just over 50% of trades being profitable. You’ll notice that Seer gives you a breakdown of the contribution of each symbol to overall trading performance.

    You’ll also notice that we have several warnings. Click on the Output tab, and you’ll see that our trading system was unable to take positions as there was not enough cash in the account to open a new position – this is the effect of true portfolio backtesting.

     Click on the Account object. Change the starting capital to 20000 and click OK to save your changes. Click backtest. Notice that as we’ve increased the amount of cash in the account we’re now able to enter more trades.

    We’re now going to add more symbols to the portfolio.

     

     Click on the Portfolio object. Repeatedly click on the delete button until the portfolio is empty, now click on the search icon. Find and select AA (Alcoa), scroll to the bottom of the list and while holding down shift click on XOM (Exxon). Click on OK. We’ve now added all DOW constituents to the portfolio. Click on backtest, you’ll again notice that we have lots of warnings as we’re unable to enter positions as our account balance does not have enough cash. Increase the starting capital in the account object to 200000, and click backtest.

    Conclusions

    The process of building and backtesting trading systems is an iterative process with the initial aim being to validate the observation/theory.

    We have chosen this example as the end result produces conflicting results – on MSFT alone, the trading system shows some promise, but when backtesting against a portfolio of stocks we find a different result. This leads us to more questions, which will lead to more testing, and perhaps to a better trading system.

    You also need to remember that this example focuses on the entry rules with no attention played to money management/position sizing or the exit rules (including stoploss) – both these elements are crucial to trading system performance.

Viewing 1 post (of 1 total)

You must be logged in to reply to this topic.