OHLC Backtesting Accuracy: Determining the Critical Stop Distance

OHLC Backtesting Accuracy: Determining the Critical Stop Distance
Download ALL MT5 scripts (408)
YouTube Video Thumbnail



Similar MetaTrader Tools

OHLC Backtesting Accuracy: Determining the Critical Stop Distance

Info

The OHLC Backtesting Accuracy: Determining the Critical Stop Distance is a Script for MetaTrader 5 that every backtest using 1-minute ohlc data operates under a fundamental assumption: that price movement within a bar follows a specific pattern (open-low-high-close for bullish, open-high-low-close for bearish). This assumption is necessary because OHLC data does not record the intraday price path, unlike real tick data.

Usage

This tool is typically used for enhancing chart analysis and decision making.

Platform

This Script works exclusively on MetaTrader 5 (both build 600+ and newer versions).

Setup

Place the downloaded file in MQL5/Scripts folder via File ? Open Data Folder in MetaTrader 5.


How to Install and Use OHLC Backtesting Accuracy: Determining the Critical Stop Distance

1. Installation: Move your script file into the MQL/Scripts directory and restart the platform.

2. Execution: Drag the script onto a chart; it will perform a one-time action, such as closing all open orders or clearing chart objects.

3. Editing: Use MetaEditor (F4) to modify code, click "Compile," and verify no errors appear in the terminal before running.

4. Removing: Scripts stop automatically, but you can remove them manually by right-clicking the chart and choosing "Remove Script."

Frequently Asked Questions

Q: How are scripts different from EAs? A: Scripts execute a single action and then stop; EAs monitor the market and trade continuously.

Q: Can I assign a hotkey to a script? A: Yes, right-click the script in the Navigator, select "Set Hotkey," and define your preferred keyboard shortcut.

Q: Why did my script stop? A: Scripts are designed to stop immediately after finishing their programmed command; this is normal behavior.

What this tool does

Every backtest using 1-minute OHLC data operates under a fundamental assumption: that price movement within a bar follows a specific pattern (Open-Low-High-Close for bullish, Open-High-Low-Close for bearish).

Typical Use Case

This Script excels in automated trading and technical analysis on MetaTrader 5.

Compatible Platform & Setup

This Script works on MetaTrader 5. Place the file in the MQL5/Scripts folder and restart the terminal.

Description & Settings

Related: Close Positions by Symbol - another powerful script for MetaTrader 5 traders.

Every backtest using 1-minute OHLC data operates under a fundamental assumption: that price movement within a bar follows a specific pattern (Open-Low-High-Close for bullish, Open-High-Low-Close for bearish). This assumption is necessary because OHLC data does not record the intraday price path, unlike real tick data.

Also recommended: strategy report output into a chart - similar script with strong performance on MetaTrader 5.


This assumption becomes significant only when the bar that concludes a trade intersects with both the stop-loss and take-profit levels. In such scenarios, the order execution determines whether the trade is ultimately a win or a loss, and the backtester makes an inference. This script quantifies the frequency of such erroneous inferences.

Methodology:
For each M1 bar, a virtual bracketed trade is initiated. This involves placing two orders: one above the bar's open and one below, both equidistant. The script tracks the price movement until one of these levels is reached. If only one level is hit by the closing bar, the trade outcome is clear. However, if both levels are touched, the script compares the backtester's assumed order execution with the actual tick data for that minute.

The script analyzes various bracket widths (stop distances) to illustrate how the error rate is influenced by this parameter. The results are presented as a curve.

Example Analysis on XAUUSD (30 days, 27,844 virtual trades):

Stop Distance: 20 points
- Contested Trades: 62.6%
- Trades Resolved Incorrectly: 23.84%

Stop Distance: 50 points
- Contested Trades: 30.1%
- Trades Resolved Incorrectly: 8.25%

Stop Distance: 100 points
- Contested Trades: 8.9%
- Trades Resolved Incorrectly: 1.66%

Stop Distance: 200 points
- Contested Trades: 1.5%
- Trades Resolved Incorrectly: 0.18%

Stop Distance: 500 points
- Contested Trades: 0.1%
- Trades Resolved Incorrectly: 0.00%

Stop Distance: 1000 points
- Contested Trades: 0.0%
- Trades Resolved Incorrectly: 0.00%

A "contested" trade occurs when the closing bar touches both the stop-loss and take-profit levels, necessitating an order decision. "Incorrectly resolved" refers to the proportion of all resolved trades where the backtester's assumed order execution was contrary to the actual tick behavior, thus misclassifying a win as a loss, or vice-versa.

The key takeaway is not that OHLC data is universally unreliable or consistently accurate. Instead, the inaccuracy has a quantifiable threshold that is symbol-dependent. This script provides a method to measure this threshold efficiently.

Inputs:
InpSymbol: Specifies the trading instrument (defaults to the chart's symbol if empty).
InpDays: The number of past trading days to analyze.
InpBrackets: A list of stop distances (in points) to evaluate.
InpHorizonBars: The maximum duration (in bars) a trade can remain open before being considered unresolved.
InpStepBars: Determines the interval (in bars) for opening new virtual trades, adjustable for performance on slower systems.
InpWriteCSV: If enabled, logs every mis-resolved trade, including entry time, closing bar details, the model's assumed outcome, and the ticks' actual outcome, allowing for individual case review.

This script analyzes historical data exclusively. It does not execute trades or modify open positions.

Note: This script requires access to real tick history for the selected symbol. Please download this data via the Symbols window before running the script to ensure accurate resolution of contested trades.

You may also like: Separators - excellent alternative for script users on MetaTrader 5.

Source Code

#property copyright "RobotFX"
#property link "https://robotfx.org"
#property version   "3.10"
#property script_show_inputs
#property description "This code is educational. Feel free to use, modify and upgrade as you wish. Visit ROBOTFX.ORG for professional trading tools"
input string InpSymbol      = "";
input int    InpDays        = 30;
input string InpBrackets    = "20,50,100,200,500,1000";
input int    InpHorizonBars = 240;
input int    InpStepBars    = 1;

.......

Leave your opinion, ask a question, share some knowledge

⚠ Limitations & Risk Warning

  • This tool is provided for educational and testing purposes only.
  • Past performance does not guarantee future results.
  • Trading involves substantial risk of loss. Use on a demo account first.
  • Results may vary depending on market conditions, broker, and settings.
  • We recommend thorough backtesting and forward testing before using with real funds.
© ROBOTFX Free educational tools by RobotFX. Use entirely at your own risk; we are not liable for any financial losses incurred.