//@version=4 strategy("Hammer Strategy", shorttitle="HS", overlay=true, initial_capital=10000) //Determine up or down trend TEMA(length) => if (length > 0) ema1 = ema(close, length) ema2 = ema(ema1, length) ema3 = ema(ema2, length) (3 * ema1) - (3 * ema2) + ema3 else na temaVal1 = TEMA(200) uptrend = (close > temaVal1) and (temaVal1 > temaVal1[1]) //Patterns bodyheight = abs(close[0] - open[0]) shadowlength = 1.5*bodyheight hammer = not uptrend and bodyheight > 0 and low[0]<(open[0] - shadowlength) and high[0] < (close[0] + bodyheight/2) hanging_man = uptrend and bodyheight > 0 and low[0]<(close[0] - shadowlength) and high[0] < (open[0] + bodyheight/2) SignalUp = hammer SignalDown = hanging_man var longStopPrice=0.0 var shortStopPrice=0.0 //Trailing Stoploss Factor factor = 0.0002 //GOING LONG if(SignalUp and strategy.position_size == 0) atr = atr(40) longStopPrice := close[0] * (1-factor) //max 1% loss per trade //divide by 100 to get 1% max loss and divide again for leverage qty = ((strategy.equity / 100) / 100) / (close - longStopPrice) strategy.entry("long", true, qty=qty) //GOING SHORT if(SignalDown and strategy.position_size == 0) atr = atr(40) shortStopPrice := close[0] * (1+factor) //max 1% loss per trade //divide by 100 to get 1% max loss and divide again for leverage qty = ((strategy.equity / 100) / 100) / (shortStopPrice - close[0]) strategy.entry("short", false, qty=qty) //TRAILING STOP LOSS LONG longStopPrice := if (strategy.position_size > 0) stopValue = close * (1 - factor) max(stopValue, longStopPrice[1]) else 0 shortStopPrice := if (strategy.position_size < 0) stopValue = close * (1 + factor) min(stopValue, shortStopPrice[1]) else 999999 // Plot stop loss values for confirmation plot(series=(strategy.position_size > 0) ? longStopPrice : na, color=color.fuchsia, style=plot.style_cross, linewidth=2, title="Long Trail Stop") plot(series=(strategy.position_size < 0) ? shortStopPrice : na, color=color.red, style=plot.style_cross, linewidth=2, title="Short Trail Stop") if (strategy.position_size > 0) strategy.exit("TP or SP", "long", stop=longStopPrice) if (strategy.position_size < 0) strategy.exit("TP or SP", "short", stop=shortStopPrice)