Setting Up a Trading Strategy to Backtest

Adrian Reid walks through preparing a trading strategy for backtesting: pick a timeframe, write rules in pseudocode, turn subjective decisions into objective indicators, define every edge case from entry to position sizing, then code it and verify each trade executes on the right bar and price before optimizing.
Chapters
Welcome back to the trading series on backtesting your trading strategies. My name is Adrian Reed. This is enlightened stock trading. And in this video, what I want to do is share with you about how to set up your trading strategy to backtest. But before we go any further, make sure you hit the subscribe button and give this video a thumbs up so that you get notified of all of my future videos. Because I've got new trading content coming out all the time and I don't want you to miss a thing. So there's a couple of steps you need to go through. The first is, and the most important perhaps is to choose your time frame and document your trading rules. The rules that you want to backtest. Now, time frame could be daily, weekly, monthly, or it could be intraday. It doesn't really matter. The techniques for backtesting is pretty much the same. Documenting your trading rules is a critical step to do before you start the coding process. A lot of people just crack open the trading software and start coding madly. That's a mistake. Before, before you do that, first document your trading rules in terms of pseudocode. If this and this and this happens, buy. If this or this happens, sell. That's what pseudocode is. It's logic statements written in English or whatever language you speak so that you know exactly what will happen under what conditions. Once you've documented your trading strategy rules, the next step is to ensure Those rules are 100% objective. Big, big problem that new systematic traders face when they're trying to codify their strategy is trying to eliminate the subjectivity from their trading rules. A lot of technical analysis traders use things like checklists. Now, checklists are notoriously subjective. You've got to say, is the stock trending up tick? Is it having a volatility compression tick, and so on. The trouble is, is it trending up? Well, that's subjective. How exactly do you define trending up? Is it the volatility compressing? How exactly do you find volatility and how exactly do you define compressing? We need to eliminate that sort of subjectivity from our trading rules so that we can codify it properly. So if you can't point to an indicator on a chart and say, this is exactly the conditions that tell me that rule is true, then you have subjectivity in your trading rules and you've got to eliminate it. Sometimes you just need to put a stake in the ground and say, okay, a stock trending up is when the stock is above the 200 day moving average or low volatility is when the average true range is less than 5% of the closing price or something like that. You just got to put a stake in the ground. If you've got subjective rules, make them objective so that you can then back test them or code them and then backtest them correctly. So once your rules are back tested, you need to make sure that your strategy is fully defined. Now what do I mean by that? Well, sure, you've got your buys and your sells, right? Yes. But you need to make sure your trading strategy.
Documents exactly what you would do under all conditions. So there can't be any edge cases or situations where you have to intervene and manually make a decision, subjectively make a decision. And the reason is if you have that, you can't backtest it because you can't backtest subjectivity. You can only back test objective trading rules. So think through every single circumstance and make sure that the rules cover it.
Once you get a buy signal, how do you prioritize all the different buy signals? How many buy signals will you generate or will you take in any one day? How many open positions will you hold at any one time? Where's the stop loss? Is it in market? Is it next bar on open? What? Where's the trailing stop? Is there a trailing stop? Is there a profit target? Is that in market? Is it next bar an open? What other conditions? Under which will I exit? Is there any panic exit? If the index starts collapsing? Is there an index exit? And so on and so on and so on. Make sure every aspect of the decisions you've got to make for that trading strategy are fully documented. Position sizing, position scoring are also really important here. So work through, just in your mind or on paper, all the different scenarios and make sure your rules cover all of them. Once you've fully defined your strategy, it's time to code it. So you've got fully defined rules written in pseudocode. Then you want to go into the trading software and using the programming language.
Convert those rules to code. Now, if you're a member of the Trader Success System, you get a code library. It's the copy and paste job in the into a preformed trading system template. It's dead easy for members of the Trader Success System to codify their own strategies and backtest them. If you're not a member of the Trader Success System, click the link to join or you're going to have to learn how to code and master that yourself. So convert your pseudocode to code one rule at a time. Then make sure you rigorously error Check that code. Most trading software like amibroker has code testing, syntax testing functionality. So you can press the AFL syntax verifier and it'll tell you if you've got any syntax errors. But a syntax error is not the only error you've got to look out for. You've also got to look out for logic errors and make sure that the code is executing correctly. So how do you make sure the code is executing correctly? Once you've fully codified your strategy and run your first backtest, you then want to look trade by trade in the backtest and look at every single style of trade. An entry that was stopped out, an entry that hit the profit target, an entry that hit the trailing stop.
An exit entry that hit the time stop if you've got one, an indicator exit, all the different permutations and combinations of that trading strategy. You want to check that the signal to buy and sell was generated on the correct bar and then it was executed on the correct bar at the correct price. So you got to make sure here that if you're generating your signal after the close of the day, you're entering at the open the next day. So you want to look into the backtest and make sure, yes, that bar that the signal was generated on was indeed, did indeed tick all of the boxes for my buy rules. And, and then the backtest did indeed enter the next bar at the opening price. So you want to check laboriously trade by trade, not every single trade in the database, but every type of trade, every combination of different trade outcome. You want to make sure they will execute correctly. Once you've done that, you are now ready to back test your trading strategy and really start to investigate performance. But you've got to do all of this work first and really make sure it's coded correctly. There's no syntax errors and you verify that it's executing correctly first before you start investigating performance. If you start investigating trading system performance before you've done all of these things, you run the risk of having errors creep through. So spend the time upfront to really verify that the code is correct and it's executing correctly. Then you can go on to optimization performance improvement, trying a whole bunch of different ideas. So that's how you set up your trading strategy. To backtest. First, choose your time frame and document your trading rules using pseudocode. Secondly, ensure those rules are 100% objective. Third, fully define your trading strategy. Fourth, code and error check your trading strategy and then ensure your backtest executes correctly is the final step you've done all that, it's time to move on and backtest and optimize your trading strategy to try and improve the performance so that you can make the most profit possible out of your trading. So, again, my name is Adrian Reed. This is Enlightened Stock Trading. We've been talking about how to set up your trading strategy to backtest in the next video. I'm going to cover some of the most common backtesting mistakes. So scroll down on the blog post to watch that video or watch the next video in the series and I'll see you there. Bye for now.
Sam.











