```
input BuyorSell = {default Buy, Sell};
input BuyEntry = 3;
input SellEntry = 3;
input BuyExit = 20;
input SellExit = 20;
input ATRLength = 50;
input TargetATRMult = 1;
input DisplayLines = yes;
#input PriceDigits=NumberFormat.TWO_DECIMAL_PLACES;
input PriceDigit=2;
def H = high;
def L = low;
def h1 = Highest(H, SellExit);
def ih2=if H != h1 then H else 0.0;
#def h2 = Highest(ih2, SellExit);
def h2=fold i =1 to SellExit with ip=0.0 do if GetValue(H,i)==h1 or GetValue(H,i)<ip then ip else GetValue(H,i);
#def h3 = Highest(if (H == h1 or H == h2) then 0 else H, SellExit);
def h3=fold i1 =1 to SellExit with ip1=0.0 do if GetValue(H,i1)==h1 or GetValue(H,i1)==h2 or GetValue(H,i1)<ip1 then ip1 else GetValue(H,i1);
def HH = (h2 + h3) / 2.0;
#plot pHH=HH;
def l1 = Lowest(L, BuyExit);
#def l2 = Lowest(if L == l1 then 1000000 else L, BuyExit);
def l2=fold i2 =1 to BuyExit with ip2=10000000.0 do if GetValue(L,i2)==l1 or GetValue(L,i2)>ip2 then ip2 else GetValue(L,i2);
def l3 = Lowest(if L == l1 or L == l2 then 1000000 else L, BuyExit);
def LL = (l2 + l2) / 2.0;
def QB = Highest(H, BuyEntry);
def QS = Lowest(L, SellEntry);
def ATRVal = ATR(length = ATRLength,averageType= AverageType.SIMPLE);
def mATR = Highest(ATRVal, ATRLength);
def dl = DisplayLines == yes;
def trade;
plot entry;
switch (BuyorSell){
case Buy:
trade = 1;
#entry.SetDefaultColor(color.GREEN);
case Sell:
trade = -1;
}
#AddLabel(1,trade);
#def c1 = (BuyorSell == "Buy") and barnumber() > Max(SellExit, BuyExit);
#def c2 = (BuyorSell == "Sell") and Barnumber() > Max(SellExit, BuyExit);
entry.AssignValueColor(if trade == 1 then Color.GREEN else if trade == -1 then Color.RED else Color.GREEN);
#entry.setPaintingStrategy(paintingStrategy.LINE);
plot exit;
exit.SetDefaultColor(Color.CYAN);
def EntryPr;
def co = BarNumber() > Max(SellExit, BuyExit);
def pos;
switch (BuyorSell){
case Buy:
entry = QB[1];
exit = LL[1];
pos = if co and high > QB[1] then 1 else if low < LL[1] then 0 else pos[1];
EntryPr = if high > QB[1] and pos == 1 and pos[1] < 1 then QB[1] else if pos == 0 then Double.NaN else EntryPr[1];
case Sell:
entry = QS[1];
exit = HH[1];
pos = if co and low < QS[1] then -1 else if high[1] > HH[2] then 0 else pos[1];
EntryPr = if low < QS[1] and pos == -1 and pos[1] > -1 then QS[1] else if pos == 0 then Double.NaN else EntryPr[1];
}
def BTarget;
def BTarget2;
def EntryLine;
def TradeRisk;
switch (BuyorSell){
case Buy:
BTarget = if pos == 1 and pos[1] < 1 then (EntryPr + (TargetATRMult * 2 * mATR)) else if pos == 1 then BTarget[1] else Double.NaN;
BTarget2 = if pos == 1 and pos[1] < 1 then (EntryPr + 2 * (TargetATRMult * 2 * mATR)) else if pos == 1 then BTarget2[1] else Double.NaN;
EntryLine = if LL < EntryPr then EntryPr else Double.NaN;
TradeRisk = (EntryPr - LL) / mATR;
case Sell:
BTarget = if pos == -1 and pos[1] > -1 then (EntryPr - (TargetATRMult * 2 * mATR)) else if pos == -1 then BTarget[1] else Double.NaN;
BTarget2 = if pos == -1 and pos[1] > -1 then (EntryPr - 2 * (TargetATRMult * 2 * mATR)) else if pos == -1 then BTarget2[1] else Double.NaN;
EntryLine = if HH > EntryPr then EntryPr else Double.NaN;
TradeRisk = (HH - EntryPr ) / mATR;
}
plot pBTarget = if dl and co then BTarget else Double.NaN;
pBTarget.SetDefaultColor(Color.YELLOW);
plot pBTarget2 = if dl and co then BTarget2 else Double.NaN;
pBTarget2.SetDefaultColor(Color.MAGENTA);
plot pEntryLine = if dl and co then EntryLine else Double.NaN;
pEntryLine.SetDefaultColor(Color.WHITE);
#plot pbt=if dl and co and (pos == 1 or pos == -1) and pos[1] == 0 then BTarget else double.NaN;
#pbt.setPaintingStrategy(paintingStrategy.VALUES_BELOW);
def valco=dl and co and (pos == 1 or pos == -1) and pos[1] == 0;
def rBTarget=round(BTarget,PriceDigit);
def rBTarget2=round(BTarget2,PriceDigit);
def rEntryPr=round(EntryPr,PriceDigit);
AddChartBubble(valco, BTarget,rBTarget, Color.YELLOW);
AddChartBubble(valco, BTarget2, rBTarget2 ,Color.MAGENTA);
AddChartBubble(valco, EntryPr,rEntryPr, Color.WHITE);
def exv=if trade == 1 then LL else HH;
def rexv = round(exv,PriceDigit);
def rTradeRisk = round(TradeRisk,PriceDigit);
AddChartBubble(valco,exv,rexv + "(" +rTradeRisk + "ATR)", Color.CYAN);
#AddChartB=ubble(1,H,pos);
#END
```