0

I'm working on backtesting a strategy. I need to simulate stop-limit order, stop-loss, and take-profit. Here's my understanding of these concepts:

  1. Stop-limit order: You ask the broker to buy the security if its price is between the stop and limit price you specified.

  2. Stop-loss: You ask the broker to sell the security if its price falls below the stop-loss price you defined.

  3. Take-profit: You ask the broker to sell the security if its price goes above the take-profit price you defined.

Here are my assumptions for implementing these in a backtest:

  1. On a specific date, if the High price is above the stop price and the Low price is below the limit price, buy the security with an 80% chance (I assume 20% for the cases where the price is in the desired range but for any reason, the order is not filled on that date by the broker).

  2. On the same date that the stock is purchased or later if the High price is above the take-profit price, sell the security at the price of the take-profit price.

  3. On the same date that the stock is purchased or later if the Low price is below the stop-loss price, sell the security at the price of the stop-loss price.

Since the order of executing (2) or (3) is important (i.e., checking for a profit on that date or loss on that date), 50% of the times I execute (2) first, and for the rest I execute (3) first.

I'm not sure how realistic are these assumptions for the purpose of the backtest. Note that small changes in this implementation could make a huge difference in the overall return of the strategy. Given this implementation, I find that it is more profitable if you give 10% of the price for stop-loss and 5% of the price for take-profit (which is against my expectation that take-profit should be 2X higher than stop-loss).

2 Answers 2

2

No, your assumptions about the stop-limit order are not correct.

A "Stop Limit" order is basically a stop-loss order with a safety limit. It means: after reaching the stop price, you place a limit order instead of a market order. This is not the same as buying within in a window.

The common use case is to prevent to buy/sell at any price, which is what could happen with a stop-loss order: if you e.g. place a stop-loss order to sell at $100, it places a market order at the moment the price reaches $100. But a market order executes at any price, so is possible that, if the market is in panic, illiquid (and/or outside of trading hours) or your order size is large, you may only be able to sell at $90. If you place a stop-limit order, you can set the limit at e.g. $95. This means, after reaching $100, you place a limit order to sell only above $95. But this also means it may not be (fully) executed.

So for your implementation, I would suggest to ignore stop-limit and treat it like a stop-loss order, as you don't have the data to know if you get the price or not (and you want it to be executed). You could maybe want to execute a stop-limit order 0.1% below the stop price to make up for it.

What you are probably looking for to open a position is a standard buy limit order, e.g. you buy at or below the limit price.

Also, some nit-picking in your definition: a stop-loss order can be used to both buy and sell (e.g. to close a short position).

Now, some thoughts regarding your implementation:

Unless you have intraday data, I doubt you can simulate same-day trades based on high and low alone.

E.g. assume your algorithm sets a limit order for $100, take profit for $105 and stop loss at $95. The values for a specific day are: high $110, low $99.

Given your algorithm, you buy according to (1) (with a limit order, see above). You sell according to (2) (On the same date that the stock is purchased or later if the High price is above the take-profit price). (3) doesn't happen. Nice $5 profit.

Now I tell you how the price actually went that day: it opened $105 and fell to $99. You bought at $100. It is worth $99 now. A $1 loss (for now).

I also doubt that a 50% chance on (2) or (3) is a reasonable assumption to get valid results. It will give you, for small enough windows, a trivial win if the distance from buy price to take-profit is larger than the distance from buy price to stop-loss.

E.g. assume you buy at $100 and set take profit at $100.10 and stop loss at $99.95. It is reasobable to assume that high and low will cover this window on every day. So, given your rules, you would execute (2) with 50% probability (for $0.10 profit) and execute (3) with 50% probability (for $0.05 loss). So on average, you make $0.05 every 2 days by that 50% implementation assumption. In reality though, a drop by $0.05 happens more likely than (and before) an increase by $0.10.

If your windows are larger (e.g. the 10% and 5% you mentioned), this will probably not happen within a day (or at least you could probably filter those events out). You should make sure though that enough occurrences happened to give you good statistics (as a drop by 10% may take some time), so your bad events may by random chance have not happened often enough to eat up the wins from the good events - but could catch up. E.g. if your commodity is the housing market, your result may vary if you include a subprime crisis or not.

Also note that the time frame might be relevant: if you buy a stock that increases by 5% in 2 years, you could instead put the money into a money account and maybe get more than 2.5% interest per year.

Also, generally, I would be careful if small changes in the implementation make a huge difference in the overall return of the strategy. It could indicate overfitting or general problems with your model (e.g. if you make a profit with 4.92% take-profit distance, but not with 4.90% or 4.94%, the result is probably just random).

2
  • Thanks for the clarification. For implementation, I changed it to 100% of times check for a loss (3) and if there is no loss, check for a profit (2). Also, instead of a buy limit order, I changed it to a market order, assuming that you will get the stock within 1% of the Open price of that date and then place a stop-loss and take-profit order (2% profit, 0.01% loss). Assuming the selling price will be 0.1% less than the stop price, the annual return over 62 years is 10% with a success rate of 7.8%. Is it reasonable?
    – A User
    Commented Oct 10, 2023 at 5:49
  • I cannot judge your algo based on your info, I just pointed at some suspicious things. But honestly: 10% is the average S&P 500 market return. Many professionals don't reach that, so I wouldn't expect you to get that. O.t.o.h.: success rate depends on what you define as success. If you get 10%+ in 7.8% of cases, that could be right, but worthless. A simple algo that buys one tech stock at ipo may get an average 10% in 8% of cases (amazon, google, bitcoin, many dot.com crashers if you sell early enough). Does this mean it's a good algo? Or could it just measure how many tech stocks exploded?
    – Solarflare
    Commented Oct 11, 2023 at 8:05
0

Bonus points for including the warning about over fitting; that's a classic mistake and is essentially the modelling equivalent of forgetting that you can't just project the curve.

Normally the solution is to make sure you run your model against a stochastic simulation of the market, repeatedly, to see how it would have performed if random historical events had gone another way. ("Monte-Carlo" simulation.) Building a good stochastic model is not easy either, admittedly; at best it's a statistical match for the market and that requires having enough statistics to validate it before assuming it can validate a strategy.

Beware GIGO. The market is an extremely noisy signal at best, and any strategy needs to be able to tolerate that noise over the expected time horizon(s) before you'll need the money.

2
  • Thanks! Just wondering what you mean by "random historical events had gone another way"? Do you mean considering the situations that could be against the strategy?
    – A User
    Commented Oct 10, 2023 at 5:52
  • You can't just test it against the market's history. You have to test it against market behavior generally. The dot-com surge and dot-bomb correction might have happened very differently. Ditto the mortgage crisis. Ditto the COVID-19 epidemic. Ditto a huge number of other things, large and small. Your strategy needs to work across many, many possible market patterns, not just the one we happen to have seen. Which means you want a good model with the right randomness and statistics, including exceptional events, and you want to test many times against different runs of the model.
    – keshlam
    Commented Oct 10, 2023 at 6:21

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .