[ Home ] [ Strategy Overview ] [ Technical Breakdown ] [ How to Use (and the code) ] [ Examples ]
This project contains a simple, yet robust, Pine Script trading strategy designed for use on TradingView. The strategy combines two popular technical indicators—a long-term Exponential Moving Average (EMA) for trend identification and a short-term Relative Strength Index (RSI) for entry signals and to manage buy and sell positions.
This strategy is built on a core idea: trade the trend while exploiting short-term oversold conditions. It uses the following logic:
The code is well-structured and uses several key Pine Script features to manage its state and logic effectively.
var
The most critical technical element is the use of var int positionState = 0. Unlike a regular variable, a var variable retains its value from one bar (or candle) to the next. This is essential for the strategy's logic, as it allows the code to remember whether a position is currently open (positionState = 1) or closed (positionState = 0). This prevents the strategy from continuously buying on every bar that meets the entry conditions.
The strategy is highly customizable via the input.int() function:
maLength: Controls the period of the Exponential Moving Average (default: 200).rsiLength: Adjusts the period of the Relative Strength Index (default: 2).rsiLevel: Sets the oversold level for the RSI entry signal (default: 10).The entry and exit logic are broken down into clear, readable Boolean variables:
buySignal): The strategy only enters a trade if all three conditions are met: the price is above the 200 EMA (isAboveMA), the 2-period RSI is below the set level (isRSIBelowLevel), and there is currently no open position (isInNoPosition).sellSignal): The exit logic is triggered only when there is an open position (isInBuyPosition) and the close price exceeds the previous bar's high (isCloseHigherThanYesterdayHigh). This simple momentum-based exit aims to capture gains during strong upward movements.The core trading actions are performed using built-in Pine Script strategy functions:
strategy.entry("Buy", strategy.long): This command opens a long position with the label "Buy" when the buySignal is true.strategy.close("Buy", comment="Sell"): This command closes the open "Buy" position and adds a comment "Sell" to the trade history.The script also plots the 200-period EMA directly on the chart using the plot() function, allowing for visual confirmation of the trend filter.
View the Pine Script Code Here
Disclaimer: This is a trading strategy for educational and illustrative purposes only. Trading involves risk, and past performance is not indicative of future results. Always backtest and paper trade any strategy before using it with real capital.
73 de YB1SDL
This page was handcrafted in plain HTML. No Java, No Flash.
Last Updated: February 17, 2026