Pin Bar Context Dependency Study
A pin bar is usually taught as a standalone signal, but most traders who actually use them insist context matters - the same shape at a random midpoint of a range is treated very differently from one that forms at a swing level in the direction of the trend. This script checks that claim directly instead of leaving it as chart-reading folklore. Every bar in the loaded history is tested against an explicit shape filter: a long wick on one side (at least InpNoseRatio of the bar's range), a short wick on the other (at most InpOppWickRatio), and a small body (at most InpBodyRatio). Bars satisfying both directions at once are skipped as ambiguous rather than guessed at. Each qualifying pin bar is then scored on two independent conditions - trend alignment (an SMA slope check) and proximity to a prior swing extreme - and sorted into one of four buckets. Trend and level are kept separate rather than folded into a single confluence flag, because a pin bar with only one of the two present is a genuinely different setup from one with both, or neither; collapsing that distinction would hide exactly the comparison this study exists to make. Outcome for each bucket is a simple target-versus-stop race over a fixed lookahead, with stop checked before target when both could have been touched within the same bar, since intrabar order is not recoverable from OHLC data alone. Notes and limitations - This is a descriptive study, not a trading signal. A win rate difference between buckets on a small sample is not proof of an edge; check the reach (sample count) column before drawing conclusions. - The level check compares the pin's own high or low against the single most extreme prior swing point in the lookback window, not every minor swing in between - a market with several nearby levels only registers the outermost one. - Stop is checked before target within the same bar when both could plausibly have been hit, which understates win rate slightly rather than overstates it - the conservative direction for a study meant to be trusted. - Outcomes that hit neither target nor stop within InpLookaheadBars are reported separately as "no hit" and excluded from the win-rate percentage, which is computed only over decided outcomes. - Says nothing about spread or slippage cost - purely a study of where price went relative to the signal bar's close.
Script · MT4 · v1.0 · Free
About Pin Bar Context Dependency Study
Pin Bar Context Dependency Study is a script for MetaTrader 4 based on Moving Average. It can alert you with a pop-up, sound, email or push message and marks candlestick patterns. It has 17 settings, such as Symbol, Timeframe and Max bars. MQL source code included.
Files in the download: PinBarContextStudy.mq4, PinBarContextStudy.txt.
Settings
| Setting | Default | What it does |
|---|---|---|
| Symbol | Symbol to study, blank = this chart's symbol | |
| Timeframe | PERIOD_H1 | Timeframe to study |
| Max bars | 3000 | Most recent bars to include |
| Nose ratio | 0.66 | Long wick must be at least this fraction of the bar range |
| Opp wick ratio | 0.25 | Opposite wick at most this fraction of the bar range |
| Body ratio | 0.35 | Body at most this fraction of the bar range |
| Min range pips | 5.0 | Ignore bars with a range smaller than this, in pips |
| Trend ma period | 50 | SMA period used to judge the prevailing trend |
| Trend slope bars | 10 | Bars back the SMA slope is measured over |
| Trend min slope pips | 2.0 | Minimum SMA move over that span to call a trend, in pips |
| Level lookback bars | 100 | Bars searched for a prior swing high/low |
| Level tolerance pips | 8.0 | Pin extreme within this many pips of that swing counts as "near a level" |
| Lookahead bars | 10 | Bars forward checked for target/stop |
| Target pips | 20.0 | Favourable move counted as a win |
| Stop pips | 10.0 | Adverse move counted as a loss |
| Save csv | true | Write the bucket table to a CSV file |
| Delimiter | DelimiterComma | CSV column separator |
How to install
- Download the ZIP from your PolyMaria library and unzip it.
- In MetaTrader 4 open File → Open Data Folder, then go to MQL4 → Scripts.
- Copy the .mq4 file(s) there. Put any .mqh files in MQL4 → Include.
- Open the .mq4 file in MetaEditor and press Compile (F7) if MetaTrader does not show it.
- Drag the script onto a chart to run it once.
Tags: Candlestick Patterns · Buy/Sell Arrows · Trend · Alerts · Moving Average · MT4 Script · MQL4 Source Code