OPEN-SOURCE SCRIPT
מעודכן Probabilities Module - The Quant Science

This module can be integrate in your code strategy or indicator and will help you to calculate the percentage probability on specific event inside your strategy. The main goal is improve and simplify the workflow if you are trying to build a quantitative strategy or indicator based on statistics or reinforcement model.
Logic
The script made a simulation inside your code based on a single event. For single event mean a trading logic composed by three different objects: entry, take profit, stop loss.
The script scrape in the past through a look back function and return the positive percentage probability about the positive event inside the data sample. In this way you are able to understand and calculate how many time (in percentage term) the conditions inside the single event are positive, helping to create your statistical edge.
You can adjust the look back period in you user interface.
How can set up the module for your use case
At the top of the script you can find:
1. entry_condition: replace the default condition with your specific entry condition.
2. TPcondition_exit: replace the default condition with your specific take profit condition.
3. SLcondition_exit: replace the default condition with your specific stop loss condition.
Logic
The script made a simulation inside your code based on a single event. For single event mean a trading logic composed by three different objects: entry, take profit, stop loss.
The script scrape in the past through a look back function and return the positive percentage probability about the positive event inside the data sample. In this way you are able to understand and calculate how many time (in percentage term) the conditions inside the single event are positive, helping to create your statistical edge.
You can adjust the look back period in you user interface.
How can set up the module for your use case
At the top of the script you can find:
1. entry_condition: replace the default condition with your specific entry condition.
2. TPcondition_exit: replace the default condition with your specific take profit condition.
3. SLcondition_exit: replace the default condition with your specific stop loss condition.
הערות שחרור
- Pine Script Version 6 Update
- Script Description Update
Description Update | 2026
This script demonstrates how Pine Script can serve as a powerful diagnostic and advanced filtering tool for any trading system by leveraging quantitative methods and mathematical modeling.
Introduction
Calculates and tracks the real time based Win Rate % on a simple trading strategy:
Entry: enter the market at the close of a red candle (close < open), betting on a continuation or an immediate rebound.
Take Profit: trade closes successfully (outcome 1) if the next candle is green (close > open).
Stop Loss: trade fails (result 0) if the closing price falls below the close of the previous candle (close < close[1]).
How does it work?
Calculates the success rate based only on the last N trades defined by the "look" parameter, for example, last 30. This allows you to see whether the strategy is performing well right now or going through a period of decline, effectively implementing an adaptive performance metric.
🧠 Could duplicate the logic blocks within the same script to test different exit strategies applied to the exact same input signal:
- Array 1: record the win rate if you exit with a fixed 2% take profit.
- Array 2: records the win rate when you exit using a trailing stop.
This way, with the same entry point, you can see in real time which exit strategy is performing better in the current market conditions.
🧠 Could easily build a strategy backtesting only enter the market if this subsystem's recent win rate is above 55%. If it drops, stay flat because strategy isn't performing as well right now.
By incorporating this array logic (rolling win rate) into any Pine Script strategy script, you can transform a rigid trading system into an adaptive and intelligent algorithm.
🧬 Automatic Kill Switch (Drawdown Protection)
You can use the recent win rate as a safety switch.
How it works: if the win rate of the last N trades falls below a critical threshold (e.g., 40%), it means the strategy is entering an unfavorable market condition (e.g., the trending market has stalled and is now ranging).
What it does: strategy temporarily stops opening positions until conditions improve or until the moving win rate starts to rise again, preventing you from incurring a string of losses.
🧬 Dynamic Position Sizing
Instead of risking the same percentage of capital, you can tie the lot size to the strategy’s recent performance.
How it works: if the moving win rate is high (e.g., > 65%), the strategy is “on a roll.”
What it does: slightly increases exposure or trade size. If the win rate drops, it minimizes the capital invested on each individual signal until the strategy regains stability.
🧬 Meta-Trend Filter on Signals
An indicator often generates false signals when the macroeconomic context is not ideal.
How it works: track the moving win rate of a sub-signal or the strategy itself.
What it does: authorizes an entry (strategy.entry) only if the win rate of the most recent trades is above average. If it’s in a losing streak, it ignores entry signals even if the main indicator (e.g., a moving average crossover) is strongly signaling to buy or sell.
🧬 Real-Time Output Exits
Duplicate the array logic to test two different exit strategies in parallel (e.g., fixed Take Profit vs. Trailing Stop) and adapt the strategy accordingly.
How it works: the basic idea is to split the calculation engine into two independent “clones” that run in parallel within the same script.Imagine you have two invisible virtual assistants that use the exact same input signal but apply two different output rules (Exit A vs. Exit B), storing their respective results in two separate arrays.
What it does: dynamically chooses which exit strategy to apply to the current trade based on which of the two has performed better in recent trades on the current market.
הערות שחרור
- Array Function Update
סקריפט קוד פתוח
ברוח האמיתית של TradingView, יוצר הסקריפט הזה הפך אותו לקוד פתוח, כך שסוחרים יוכלו לעיין בו ולאמת את פעולתו. כל הכבוד למחבר! אמנם ניתן להשתמש בו בחינם, אך זכור כי פרסום חוזר של הקוד כפוף ל־כללי הבית שלנו.
🔥 Unlock the power of Symbiosis, the first Python XGBoost model natively built for Pine Script. Grab the last available spots on Patreon: patreon.com/c/xgboost_symbiosis
כתב ויתור
המידע והפרסומים אינם מיועדים להיות, ואינם מהווים, ייעוץ או המלצה פיננסית, השקעתית, מסחרית או מכל סוג אחר המסופקת או מאושרת על ידי TradingView. קרא עוד ב־תנאי השימוש.
סקריפט קוד פתוח
ברוח האמיתית של TradingView, יוצר הסקריפט הזה הפך אותו לקוד פתוח, כך שסוחרים יוכלו לעיין בו ולאמת את פעולתו. כל הכבוד למחבר! אמנם ניתן להשתמש בו בחינם, אך זכור כי פרסום חוזר של הקוד כפוף ל־כללי הבית שלנו.
🔥 Unlock the power of Symbiosis, the first Python XGBoost model natively built for Pine Script. Grab the last available spots on Patreon: patreon.com/c/xgboost_symbiosis
כתב ויתור
המידע והפרסומים אינם מיועדים להיות, ואינם מהווים, ייעוץ או המלצה פיננסית, השקעתית, מסחרית או מכל סוג אחר המסופקת או מאושרת על ידי TradingView. קרא עוד ב־תנאי השימוש.