0% found this document useful (0 votes)
537 views

Simple Pull Back Strategy Pinescript Code

This strategy uses a simple moving average crossover technique to enter long positions in an uptrend. It takes moving averages of three different periods as indicators of short, medium, and long-term trends. Positions are entered when the close is above the long-term MA and below the short-term MA in an uptrend, defined as the MAs being above their values from the past 5 periods. Positions are exited on a close above the short-term MA, or a stop loss is triggered if the close falls more than 5% below the buy price. Pretty colors are plotted to visualize the strategy.

Uploaded by

GKMurthy
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
537 views

Simple Pull Back Strategy Pinescript Code

This strategy uses a simple moving average crossover technique to enter long positions in an uptrend. It takes moving averages of three different periods as indicators of short, medium, and long-term trends. Positions are entered when the close is above the long-term MA and below the short-term MA in an uptrend, defined as the MAs being above their values from the past 5 periods. Positions are exited on a close above the short-term MA, or a stop loss is triggered if the close falls more than 5% below the buy price. Pretty colors are plotted to visualize the strategy.

Uploaded by

GKMurthy
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

// @version=5

strategy("Simple Pullback Strategy",


     overlay=true,
     initial_capital=50000,
     default_qty_type=strategy.percent_of_equity,
     default_qty_value=100, // 100% of balance invested on each trade
     pyramiding = 0,
     commission_type=strategy.commission.cash_per_order,
     commission_value=25) // Dhan
  

// Get user input


i_ma1           = input.int(title="MA 1 Length", defval=100, step=10, group="Strategy Parameters", tooltip="Long-
term MA")
i_ma2           = input.int(title="MA 2 Length", defval=50, step=01, group="Strategy Parameters", tooltip="Short-
term MA")
i_ma3           = input.int(title="MA 3 Length", defval=9, step=01, group="Strategy Parameters", tooltip="very Short-
term MA")
i_stopPercent   = input.float(title="Stop Loss Percent", defval=0.05, step=0.01, group="Strategy Parameters",
tooltip="Failsafe Stop Loss Percent Decline")
i_lowerClose    = input.bool(title="Exit On Lower Close", defval= true, group="Strategy Parameters", tooltip="Wait
for a lower-close before exiting above MA2")
i_startTime     = input.time(title="Start Filter", defval=timestamp("01 Jan 1995 13:30 +0000"), group="Time Filter",
tooltip="Start date & time to begin searching for setups")
i_endTime       = input.time(title="End Filter", defval=timestamp("1 Jan 2099 19:30 +0000"), group="Time Filter",
tooltip="End date & time to stop searching for setups")

// Get indicator values


ma1 = ta.sma(close, i_ma1)
ma2 = ta.sma(close, i_ma2)
ma3 = ta.sma(close, i_ma3)

// Check filter(s)
f_dateFilter = time >= i_startTime and time <= i_endTime

//uptrend
int upema1=0
int upema2=0
int upema3=0

for i = 1 to 5
    if ma1 > ma1[i]
        upema1 += 1
    if ma2 > ma2[i]
        upema2 += 1
    if ma3 > ma3[i]
        upema3 += 1
// Check buy/sell conditions
var float buyPrice = 0
buyCondition    =close > ma1 and close < ma2 and upema1>= 5 and ma2 > ma1//and strategy.position_size == 0
and f_dateFilter
sellCondition   = close >ma2 and strategy.position_size > 0 and (not i_lowerClose or  close < low[1])
stopDistance    = strategy.position_size > 0 ? ((buyPrice - close) / close) : na
stopPrice       = strategy.position_size > 0 ? buyPrice - (buyPrice * i_stopPercent) : na
stopCondition   = strategy.position_size > 0 and stopDistance > i_stopPercent

// Enter positions
if buyCondition
    strategy.entry(id="Long", direction=strategy.long)

if buyCondition[1]
    buyPrice := open

// Exit positions
if sellCondition or stopCondition
    strategy.close(id="Long", comment="Exit" + (stopCondition ? "SL=true" : ""))
    buyPrice := na

// Draw pretty colors


plot(buyPrice, title="buyPrice", color=color.lime, style=plot.style_linebr)
plot(stopPrice, title= "stoploss", color=color.red, style=plot.style_linebr, offset=-1)
plot(ma1, title="ma1", color=color.blue)
plot(ma2, title = "ma2", color=color.orange)
plot(ma3, title="ma3", color= color.lime)

You might also like