You are on page 1of 2

//@version=5

strategy("Moving Average Crossover", overlay=true)

// Calculate moving averages


ma5 = request.security(syminfo.tickerid, "D", ta.sma(close, 5))
ma20 = request.security(syminfo.tickerid, "D", ta.sma(close, 20))
ma50 = request.security(syminfo.tickerid, "D", ta.sma(close, 50))
ma100 = request.security(syminfo.tickerid, "D", ta.sma(close, 100))
ma200 = request.security(syminfo.tickerid, "D", ta.sma(close, 200))

// Input for the percentage threshold


percentageThreshold = input(5, title="Percentage Threshold (%)")
profitTarget = input(6, title="Profit Target (%)")

// Calculate percentage difference


percentDiff5 = (close - ma5) / close * 100
percentDiff20 = (close - ma20) / close * 100
percentDiff50 = (close - ma50) / close * 100
percentDiff100 = (close - ma100) / close * 100
percentDiff200 = (close - ma200) / close * 100

// Check buy condition


buyCondition = (percentDiff5 >= -percentageThreshold and percentDiff5 <=
percentageThreshold) and
(percentDiff20 >= -percentageThreshold and percentDiff20 <=
percentageThreshold) and
(percentDiff50 >= -percentageThreshold and percentDiff50 <=
percentageThreshold) and
(percentDiff100 >= -percentageThreshold and percentDiff100 <=
percentageThreshold) and
(percentDiff200 >= -percentageThreshold and percentDiff200 <=
percentageThreshold)

// Calculate the highest high and its date


highestHighTuple = ta.highest(close, 52*5)
highestHigh = highestHighTuple[0]
highestHighIndex = ta.highestbars(close, 52*7)

// Calculate the lowest low and its date


lowestLowTuple = ta.lowest(close, 52*5)
lowestLow = lowestLowTuple[0]
lowestLowIndex = ta.lowestbars(close, 52*7)

// Condition for date of 52-week high is less than the date of 52-week low
buyDateCondition = highestHighIndex < lowestLowIndex

// Calculate profit target


profitTargetValue = strategy.position_avg_price * (1 + profitTarget / 100)

// Define sell condition


sellCondition = close >= profitTargetValue

// Execute buy and sell orders


strategy.entry("Buy", strategy.long, when=buyCondition and buyDateCondition)
strategy.close("Buy", when=sellCondition)

// Plot moving averages for reference


//plot(ma5, color=color.blue, title="5 DMA")
//plot(ma20, color=color.red, title="20 DMA")
//plot(ma50, color=color.green, title="50 DMA")
//plot(ma100, color=color.orange, title="100 DMA")
//plot(ma200, color=color.purple, title="200 DMA")

// Highlight buy signal


//bgcolor((buyCondition and buyDateCondition) ? color.green : na, transp=70)

// Plot 52-week high and low


//plot(highestHigh, color=color.green, linewidth=2, title="52-Week High")
//plot(lowestLow, color=color.red, linewidth=2, title="52-Week Low")

// Plot markers for 52-week high and low dates


//plotshape(series=highestHighDate, style=shape.triangleup,
location=location.belowbar, color=color.green, size=size.small, title="52-Week High
Date")
//plotshape(series=lowestLowDate, style=shape.triangledown,
location=location.abovebar, color=color.red, size=size.small, title="52-Week Low
Date")

You might also like