Introduction
The indicator/system we will present is an alert system called TN alerts. This system is designed to identify entry and exit opportunities in the market using weighted moving averages and a trailing stop. The goal of this system is to provide clear signals for executing long and short trades while managing risk through the use of a dynamic trailing stop.
The TN alerts system stands out for its ability to adapt to different market conditions, offering a structured way to identify trends and manage trades efficiently. This system includes advanced features such as re-entries and signal visualization directly on the chart, facilitating decision-making for traders.
Indicator Parameters
The input parameters of the system are as follows:
- TS (Trailing Stop): Percentage of trailing stop. This parameter defines the trailing stop adjustment percentage relative to the closing price.
- TSswitch: Enable/disable the display of stop loss. When enabled, the stop loss is shown on the chart.
- BGswitch: Enable/disable the background color on the chart. It allows clear visualization of long and short entry zones.
- REswtich: Enable/disable the display of re-entries. Shows re-entry signals on the chart.
- fastperiod: Fast period for calculating the moving average.
- slowperiod: Slow period for calculating the moving average.
- MAtype: Type of moving average to use (e.g., weighted).
These parameters allow high flexibility and customization of the system, adapting it to the trader’s specific needs and market behavior over different periods.
Moving Averages Calculation
Moving averages are a fundamental tool in technical analysis, and this system uses two:
- wmaSlow: Slow moving average calculated over the period defined by slowperiod.
- wmaFast: Fast moving average calculated over the period defined by fastperiod.
The moving averages are calculated as follows:
1 2 |
wmaSlow = average[slowperiod, MAtype](close) wmaFast = average[fastperiod, MAtype](close) |
The fast moving average (wmaFast) and the slow moving average (wmaSlow) help identify short-term and long-term trends, respectively. These moving averages are essential for determining the entry and exit conditions in the market.
Entry and Re-entry Conditions (Long and Short)
The conditions for long and short entries are defined as follows:
- Long Entry: The slow moving average is greater than its previous value, and the current closing price is above the fast moving average, among other conditions.
- Short Entry: The slow moving average is less than its previous value, and the current closing price is below the fast moving average, among other conditions.
1 2 |
enterlong = wmaSlow > wmaSlow[1] and close > wmaFast and close[1] > wmaFast[1] and close > close[1] entershort = wmaSlow < wmaSlow[1] and close < wmaFast and close[1] < wmaFast[1] and close < close[1] |
Re-entries
The system also considers re-entries in the same direction of the current trend, allowing to capitalize on extended market movements. This is visualized on the chart with specific colored arrows.
1 2 3 4 |
elsif REswtich and enterlong and inlong and inlong[1] then drawarrowup(barindex, low - 0.25 * tr) coloured("blue", 85) elsif REswtich and entershort and inshort and inshort[1] then drawarrowdown(barindex, high + 0.25 * tr) coloured("gray", 85) |
Re-entries are marked with blue arrows for long trades and gray arrows for short trades, indicating moments of reinforcement of the initial position.
Trailing Stop Calculation
The trailing stop is a crucial tool for risk management in trading. This system uses a dynamic trailing stop that adjusts based on the closing price. The trailing stop is calculated as follows:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
tsPercent = TS / 100 once trailTop = close if close > trailTop[1] or close < trailTop[1] * (1 - tsPercent) or entershort then trailTop = close else trailTop = trailTop[1] endif once trailBot = close if close < trailBot[1] or close > trailBot[1] * (1 + tsPercent) or enterlong then trailBot = close else trailBot = trailBot[1] endif |
In this code, tsPercent defines the trailing stop percentage. trailTop and trailBot are used to store the trailing stop values for long and short positions, respectively. The trailing stop adjusts dynamically, allowing to lock in profits while minimizing losses.
Exit Conditions (Long and Short)
The system’s exit conditions are determined by the trailing stop. When the price reaches the trailing stop level, an exit signal is generated. The exit conditions are implemented as follows:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
exitlong = trailTop < trailTop[1] exitsh = trailBot > trailBot[1] if enterlong then inlong = 1 elsif entershort or exitlong then inlong = 0 else inlong = inlong[1] endif if entershort then inshort = 1 elsif enterlong or exitsh then inshort = 0 else inshort = inshort endif |
Here, exitlong and exitsh determine if long or short positions should be closed based on the trailing stop. The variables inlong and inshort are used to track the current position status.
Visualization on the Chart
The system visualizes signals on the chart using arrows and background colors. This facilitates the interpretation of signals and decision-making in trading. The visualization is implemented as follows:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
if inlong and Tsswitch then drawpoint(barindex, trailTop * (1 - tsPercent), 3) coloured("red") elsif inshort and Tsswitch then drawpoint(barindex, trailBot * (1 + tsPercent), 3) coloured("green") endif if inlong and BGswitch then backgroundcolor("green", 15) elsif inshort and BGswitch then backgroundcolor("red", 15) endif if enterlong and not inlong[1] then drawarrowup(barindex, low - 0.25 * tr) coloured("green") elsif entershort and not inshort[1] then drawarrowdown(barindex, high + 0.25 * tr) coloured("red") elsif REswtich and enterlong and inlong and inlong[1] then drawarrowup(barindex, low - 0.25 * tr) coloured("blue", 85) elsif REswtich and entershort and inshort and inshort[1] then drawarrowdown(barindex, high + 0.25 * tr) coloured("gray", 85) elsif exitlong and inlong[1] and not entershort then drawtext("▼", barindex, high + 0.25 * tr) coloured("black") elsif exitsh and inshort[1] and not enterlong then drawtext("▲", barindex, low - 0.25 * tr) coloured("black") endif |
In this code, green and red arrows indicate long and short entries, respectively. Blue and gray arrows indicate re-entries, while black symbols indicate exits.
Complete Code of the Indicator/System
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
//--------------------------------------------------------------------------------// //PRC_TN alerts //version = 0 //11.03.24 //Iván González @ www.prorealcode.com //Sharing ProRealTime knowledge //--------------------------------------------------------------------------------// //-----Inputs---------------------------------------------------------------------// TS = 20 //% Trailing stop TSswitch = 1 // Display Stoploss BGswitch = 1 // Display BG color REswtich = 1 // Display Re-entry fastperiod=4 slowperiod=62 MAtype=2 //--------------------------------------------------------------------------------// //-----Moving average calculation-------------------------------------------------// wmaSlow = average[slowperiod,MAtype](close) wmaFast = average[fastperiod,MAtype](close) //--------------------------------------------------------------------------------// //-----Setup Long&Short-----------------------------------------------------------// enterlong = wmaSlow>wmaSlow[1] and close>wmaFast and close[1]>wmaFast[1] and close>close[1] entershort = wmaSlow<wmaSlow[1] and close<wmaFast and close[1]<wmaFast[1] and close<close[1] //--------------------------------------------------------------------------------// //-----Trailing stop calculation--------------------------------------------------// tsPercent = TS/100 once trailTop = close if close>trailtop[1] or close<trailtop[1]*(1-tspercent) or entershort then trailTop = close else trailTop = trailTop[1] endif once trailBot = close if close<trailBot[1] or close>trailBot[1]*(1+tspercent) or enterlong then trailBot = close else trailBot = trailBot[1] endif //--------------------------------------------------------------------------------// //-----Exit Long&Short------------------------------------------------------------// exitlong = trailTop < trailTop[1] exitsh = trailBot > trailBot[1] if enterlong then inlong = 1 elsif entershort or exitlong then inlong = 0 else inlong = inlong[1] endif if entershort then inshort = 1 elsif enterlong or exitsh then inshort = 0 else inshort = inshort endif //--------------------------------------------------------------------------------// //-----Drawing trails-------------------------------------------------------------// if inlong and Tsswitch then drawpoint(barindex,TrailTop*(1-tspercent),3)coloured("red") elsif inshort and Tsswitch then drawpoint(barindex,TrailBot*(1+tspercent),3)coloured("green") endif //--------------------------------------------------------------------------------// //-----Background colors----------------------------------------------------------// if inlong and BGswitch then backgroundcolor("green",15) elsif inshort and BGswitch then backgroundcolor("red",15) endif //--------------------------------------------------------------------------------// //-----Plot signals---------------------------------------------------------------// if enterlong and not inlong[1] then drawarrowup(barindex,low-0.25*tr)coloured("green") elsif entershort and not inshort[1] then drawarrowdown(barindex,high+0.25*tr)coloured("red") elsif REswtich and enterlong and inlong and inlong[1] then drawarrowup(barindex,low-0.25*tr)coloured("blue",85) elsif REswtich and entershort and inshort and inshort[1] then drawarrowdown(barindex,high+0.25*tr)coloured("gray",85) elsif exitlong and inlong[1] and not entershort then drawtext("▼",barindex,high+0.25*tr)coloured("black") elsif exitsh and inshort[1] and not enterlong then drawtext("▲",barindex,low-0.25*tr)coloured("black") endif //--------------------------------------------------------------------------------// return wmaFast as "WMA Fast"coloured("blue"), wmaSlow as "WMA Slow"coloured("purple") |
Conclusion
This system provides a powerful tool for identifying entry and exit opportunities in the market using moving averages and trailing stops. The customization of parameters allows the system to adapt to different trading styles and markets. It is important to test and adjust the system in various market conditions to maximize its effectiveness.
The TN alerts is a versatile system that can be used by both novice and experienced traders. Its clear visualization and well-defined rules make it a valuable addition to any trading strategy.
Share this
No information on this site is investment advice or a solicitation to buy or sell any financial instrument. Past performance is not indicative of future results. Trading may expose you to risk of loss greater than your deposits and is only suitable for experienced investors who have sufficient financial means to bear such risk.
ProRealTime ITF files and other attachments :PRC is also on YouTube, subscribe to our channel for exclusive content and tutorials
Thank Ivan for this code !
Now could you clarify one detail : when I compare line 58 with line 50, I wonder if there is a typo. Shouldn’t we write inshort = inshort[1]?
Thank for your reply.
Gabriel
Hola Ivan. Thanks for your great job.
I would appreciate if you have look at my request
https://www.prorealcode.com/topic/convert-code-pivot-ribbon/
Thank you for all these useful information!! Very great job Ivan! THank you so much
Merci Ivan, super travail. On peut toujours compter sur toi pour apporter des innovations sur ce site. Est-ce que tu as créé un groupe whatsapp ou telegram ?