def LastBuyTrailStop; def LastSellTrailStop; def VarDot = (ATR(21, AverageType.SIMPLE)[0] * 0.40); def HH = Highest( Max(open, close), 9 ) ; def ProRange = HH - Lowest( Min(open, close), 9 ) ; def VProR = If(ProRange <> 0, 100 - ( ( HH - close ) / ProRange ) * 100, 0); def VPosition = If (VProR < 30 and VProR[1] > 30 and VPosition[1]>-1, -1, If (VProR > 70 and VProR[1] < 70 and VPosition[1]<1, 1, VPosition[1])); def buySignal = VPosition == 1 and VPosition[1] < 1; def sellSignal = VPosition == -1 and VPosition[1] > -1; def buyTrend = (VPosition == 1 and VPosition[1] == 1) or ((VPosition == -1 and VPosition[1] > -1) and VPosition[1] <> 0); def sellTrend = (VPosition == -1 and VPosition[1] == -1) or ((VPosition == 1 and VPosition[1] < 1) and VPosition[1] <> 0); plot Sell = if sellSignal then high + VarDot else double.NaN; Sell.DefineColor("SellDotColor", Color.RED); Sell.AssignValueColor(Sell.Color("SellDotColor")); Sell.SetPaintingStrategy(PaintingStrategy.POINTS); Sell.SetLineWeight(5); plot Buy = if buySignal then Low- VarDot else double.NaN; Buy.DefineColor("BuyDotColor", Color.BLUE); Buy.AssignValueColor(Buy.Color("BuyDotColor")); Buy.SetPaintingStrategy(PaintingStrategy.POINTS); Buy.SetLineWeight(5); def BuyTrailStart = if (buySignal, Lowest(Low,2), BuyTrailStart[1]); def BuyTrail= if (buySignal, BuyTrailStart, if (buyTrend, LastBuyTrailStop[1],double.NaN)); def BuyDotGap = if (buySignal, (Close - Lowest(Low,4))*1, BuyDotGap[1]); def BuyIncrement = if (buyTrend and High[1] > Highest(High,5)[2],(Close[1] - Highest(High,3)[2]),0); def BuyGap = if (buyTrend and Low - BuyTrail > ATR(10,AverageType.SIMPLE)*2 ,((Low - BuyTrail) - ATR(10, AverageType.SIMPLE)*2), if (buyTrend and (Low - LastBuyTrailStop[1])-BuyDotGap > 0, (Low - LastBuyTrailStop[1])-BuyDotGap, 0)); def SellTrailStart = if (sellSignal, Highest(High,4), SellTrailStart[1]) ; def SellTrail = if (sellSignal, SellTrailStart, if (sellTrend, LastSellTrailStop[1],double.NaN)); def SellDotGap = if (sellSignal,(Close - Highest(High,4))*1.0,SellDotGap[1]); def SellIncrement = if (sellTrend and Low[1] < Lowest(Low,5)[2], (Close[1] - Lowest(Low,3)[2]), 0); def SellGap = if (sellTrend and SellTrail - High > ATR(10,AverageType.SIMPLE)*2, ATR(10, AverageType.SIMPLE)*2 - (SellTrail - High), if (sellTrend and (High - LastSellTrailStop[1])-SellDotGap < 0 , ((High - LastSellTrailStop[1])-SellDotGap), 0)); plot TrailStop = if (buySignal) then BuyTrail else if (buyTrend) then Max(LastBuyTrailStop[1],LastBuyTrailStop[1] + Max(BuyIncrement,BuyGap)) else if (sellSignal) then SellTrail else if (sellTrend) then Min(LastSellTrailStop[1],LastSellTrailStop[1]+ Min(SellIncrement,SellGap)) else double.NaN; TrailStop.DefineColor("BuyTrailColor", Color.GREEN); TrailStop.DefineColor("SellTrailColor", Color.MAGENTA); TrailStop.AssignValueColor(if buySignal or buyTrend then TrailStop.Color("BuyTrailColor") else TrailStop.Color("SellTrailColor")); TrailStop.SetPaintingStrategy(PaintingStrategy.POINTS); TrailStop.SetLineWeight(1); LastBuyTrailStop = if (buySignal, buyTrail, if (buyTrend, TrailStop, double.NaN)); LastSellTrailStop = if (sellSignal,SellTrail, if (sellTrend, TrailStop, double.NaN)); Alert(buySignal, "Buy Alert", Alert.BAR ); Alert(sellSignal, "Sell Alert", Alert.BAR ) ;