0% found this document useful (0 votes)
84 views2 pages

Pivot SuperTrend EMA Strategy Guide

This document outlines a trading strategy implemented in Pine Script, combining Pivot SuperTrend and EMA indicators with customizable parameters for stop loss and take profit. The strategy allows for long and short trades based on market conditions and includes a date range filter for backtesting. Key features include dynamic stop loss adjustments and risk-reward calculations to optimize trade entries and exits.

Uploaded by

cattongki
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
84 views2 pages

Pivot SuperTrend EMA Strategy Guide

This document outlines a trading strategy implemented in Pine Script, combining Pivot SuperTrend and EMA indicators with customizable parameters for stop loss and take profit. The strategy allows for long and short trades based on market conditions and includes a date range filter for backtesting. Key features include dynamic stop loss adjustments and risk-reward calculations to optimize trade entries and exits.

Uploaded by

cattongki
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

//@version=4

strategy("Pivot SuperTrend + EMA Strategy with SL Multiplier", overlay=true,


default_qty_type=strategy.percent_of_equity, default_qty_value=10)

// Inputs
emaLen = input(50, "EMA Length")
pivotPrd = input(2, "Pivot Point Period", minval=1, maxval=50)
atrFactor = input(3.0, "ATR Factor", step=0.1)
atrPeriod = input(10, "ATR Period", minval=1)
riskReward = input(2.0, "Risk to Reward Ratio", minval=0.1, step=0.1)

stopLossMultiplier = input(1.0, "Stop Loss Multiplier", type=[Link],


minval=0.0, maxval=1.0, step=0.01)

longAllowed = input(true, "Allow Long Trades")


shortAllowed = input(true, "Allow Short Trades")

// Date range limiter inputs


fromYear = input(2022, "From Year")
fromMonth = input(1, "From Month")
fromDay = input(1, "From Day")
toYear = input(2099, "To Year")
toMonth = input(12, "To Month")
toDay = input(31, "To Day")

// Date range function


f_inDateRange(_fromYear, _fromMonth, _fromDay, _toYear, _toMonth, _toDay) =>
_fromDate = timestamp(_fromYear, _fromMonth, _fromDay, 0, 0)
_toDate = timestamp(_toYear, _toMonth, _toDay, 23, 59)
time >= _fromDate and time <= _toDate

inDateRange = f_inDateRange(fromYear, fromMonth, fromDay, toYear, toMonth, toDay)

// EMA Calculation
emaValue = ema(close, emaLen)

// Pivot Point SuperTrend Calculation


float ph = pivothigh(pivotPrd, pivotPrd)
float pl = pivotlow(pivotPrd, pivotPrd)

var float center = na


float lastpp = ph ? ph : pl ? pl : na
if lastpp
if na(center)
center := lastpp
else
center := (center * 2 + lastpp) / 3

Up = center - (atrFactor * atr(atrPeriod))


Dn = center + (atrFactor * atr(atrPeriod))

float TUp = na
float TDown = na
var int Trend = 0

TUp := na(TUp[1]) ? Up : (close[1] > TUp[1] ? max(Up, TUp[1]) : Up)


TDown := na(TDown[1]) ? Dn : (close[1] < TDown[1] ? min(Dn, TDown[1]) : Dn)
Trend := close > TDown[1] ? 1 : close < TUp[1] ? -1 : nz(Trend[1], 1)
Trailingsl = Trend == 1 ? TUp : TDown
bsignal = (Trend == 1 and Trend[1] == -1)
ssignal = (Trend == -1 and Trend[1] == 1)

longEntry = bsignal and close > emaValue and longAllowed and inDateRange
shortEntry = ssignal and close < emaValue and shortAllowed and inDateRange

// Variables to store SL and TP prices fixed at entry


var float entryStopLoss = na
var float entryTakeProfit = na

// Function to calculate adjusted SL between entry and supertrend line


calcAdjustedSL(entryPrice, superTrendLine, multiplier) =>
entryPrice + multiplier * (superTrendLine - entryPrice)

// On new entry bar - place orders with fixed SL and TP


if (longEntry) and (strategy.position_size <= 0)
entryStopLoss := calcAdjustedSL(close, Trailingsl, stopLossMultiplier)
risk = close - entryStopLoss
if risk <= 0
risk := close * 0.01 // minimal risk fallback
entryTakeProfit := close + risk * riskReward
[Link]("Long", [Link])
[Link]("Exit Long", "Long", stop=entryStopLoss, limit=entryTakeProfit)

if (shortEntry) and (strategy.position_size >= 0)


entryStopLoss := calcAdjustedSL(close, Trailingsl, stopLossMultiplier)
risk = entryStopLoss - close
if risk <= 0
risk := close * 0.01
entryTakeProfit := close - risk * riskReward
[Link]("Short", [Link])
[Link]("Exit Short", "Short", stop=entryStopLoss, limit=entryTakeProfit)

// Plot for visibility


plot(emaValue, color=[Link], title="EMA")
plot(Trailingsl, color=Trend == 1 ? [Link] : [Link], linewidth=2,
title="Pivot SuperTrend")

plotshape(longEntry, title="Long Entry", location=[Link],


color=[Link], style=[Link], size=[Link])
plotshape(shortEntry, title="Short Entry", location=[Link],
color=[Link], style=[Link], size=[Link])

plot(entryStopLoss, title="Entry Stop Loss", color=[Link], linewidth=1,


style=plot.style_cross)
plot(entryTakeProfit, title="Entry Take Profit", color=[Link], linewidth=1,
style=plot.style_cross)

Common questions

Powered by AI

The strategy dynamically adjusts stop-loss levels using the ATR, which measures volatility. By incorporating ATR into the calculation of the SuperTrend bands, the strategy effectively adapts the stop loss and take profit limits to compensate for changing market volatility, making trade decisions more robust under fluctuating conditions .

Buy and sell signals are determined by a change in the calculated trend direction: a buy signal occurs when the trend switches from down to up (bsignal), and a sell signal occurs when the trend moves from up to down (ssignal). These signals are based on whether the closing price crosses the SuperTrend trailing levels and confirms the trend direction .

The strategy uses a date range function that converts specified start and end dates into timestamps. It checks the current time against these timestamps to ensure trades only occur within this range by using the boolean flag inDateRange in the entry conditions .

The strategy incorporates the risk-to-reward ratio to ensure that the potential profit of a trade outweighs the risk, thereby helping maximize profitability. This ratio defines the distance between the entry and take-profit lines in comparison to the stop-loss, compelling the trader to only enter trades where the expected reward is a multiple of the potential loss, influencing the overall success and efficiency of the trading model .

The Pivot SuperTrend indicator helps identify the trend direction of the asset's price by calculating the trend using the center line and ATR adjustments. It determines whether the strategy should be in a trend-following position (long or short) based on the closing price relative to the trailing stop level (Trailingsl).

The strategy determines entry into a long position by checking several conditions: it requires a buy signal (bsignal), which occurs when the trend switches from down to up, the closing price is above the EMA value, long trades are allowed, and the current time is within a specified date range .

The 'center' variable in the SuperTrend logic is designed to track the mid-point of the trend. It initializes with the most recent pivot high or low and is updated by averaging with the next price extreme. This iterative update method stabilizes the trend center, providing a dynamic baseline for calculating the upper and lower trend bands, which are critical to determining the positioning of trades and risk levels .

The stop-loss level is adjusted based on the entry price and the SuperTrend line, using a multiplier to calculate the adjusted stop-loss (entryStopLoss). This accounts for the volatility as captured by the ATR. Similarly, the take-profit level (entryTakeProfit) is set based on the risk to reward ratio, which calculates the expected gain relative to the risk taken on the trade, using the distance from entry to adjusted stop-loss as the risk measure .

The EMA length influences the sensitivity of the moving average line to price changes, affecting the identification of entry points. A longer EMA provides a smoother, more stable trend signal, potentially reducing false entries by acting as a strong confirmation for trend reversals or continuations, especially when compared to the closing price for potential long or short entries .

The strategy applies trailing stops based on calculated trend levels (Trailingsl) that adjust to current price movements. This mechanism allows the stop level to move along with favorable price movements, locking in profits while protecting against adverse shifts. The benefit is that it provides dynamic risk management, helping traders minimize losses and optimize gains by ensuring that the exit strategy adapts to ongoing market conditions .

You might also like