Hosoda N Wave and TPs {fmz}
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// Thi script allows to identificate target points (E,N,V,NT) following  Hosoda theory for N wave
// © Francesco_Marzolo

//@version=5
indicator("Hosoda N Wave and TPs {fmz}",shorttitle="Hosoda N-wave TP", overlay=true)
HorizLinesOnTarget=input.bool(true,"Draw horizontal lines on target price")
HorizLinesOnTargetTrim=input.bool(false,"Trim horizontal lines on target price at last price")
VertLinesOnTarget=input.bool(true,"Draw vertical lines on target time")
ShowBoxAroundTarget=input.bool(true,"Show a box around time & price targets")
ShowTargetPriceLabel=input.bool(true,"Show Label on target price")
AllColor=input.color(color.blue, "Labels, N wave, lines and TPs color")
priceA = input.price(100, inline="PointA", confirm=true)
timeA = input.time(timestamp("2022-02-20"), inline="PointA", confirm=true)

//lblText1 = str.format("Price: {0, number}\nTime: {1, date} {1, time}", myPrice1, myTime1)
//var l1 = label.new(myTime1, myPrice1, lblText1, xloc=xloc.bar_time)

priceB = input.price(100, inline="PointB", confirm=true)
timeB = input.time(timestamp("2022-02-20"), inline="PointB", confirm=true)


priceC = input.price(100, inline="PointC", confirm=true)
timeC = input.time(timestamp("2022-02-20"), inline="PointC", confirm=true)

longShort= (priceB> priceA)? 1:-1

ABdiff=timeB-timeA
BCdiff=timeC-timeB
ACdiff=timeC-timeA

//calculate high of target in price using ATR (half for side)
AAtr=ta.atr(14)/2

if barstate.islast
    // bar distance in seconds between two bars
    secondsPerBar=time[0]-time[1]    

    //draw ABC lines and lables
    line.new(timeA,priceA,timeB,priceB, xloc = xloc.bar_time,color=AllColor)
    line.new(timeB,priceB,timeC,priceC, xloc = xloc.bar_time,color=AllColor)
    label.new(timeA, priceA, "A", xloc=xloc.bar_time,yloc=(longShort>0)?yloc.belowbar:yloc.abovebar, style=label.style_none,color=AllColor)
    label.new(timeB, priceB, "B", xloc=xloc.bar_time,yloc=(longShort<0)?yloc.belowbar:yloc.abovebar, style=label.style_none,color=AllColor)
    label.new(timeC, priceC, "C", xloc=xloc.bar_time,yloc=(longShort>0)?yloc.belowbar:yloc.abovebar, style=label.style_none,color=AllColor)

    HosodaTpEprice=priceB + (longShort * math.abs(priceB-priceA))
    HosodaTpEtime=timeB+ABdiff
    HosodaTpNprice=priceC + (longShort * math.abs(priceB-priceA))
    HosodaTpNtime=timeC+ABdiff
    HosodaTpVprice=priceB + (longShort * math.abs(priceB-priceC))
    HosodaTpVtime=timeC+BCdiff
    HosodaTpNTprice=priceC + (longShort * math.abs(priceC-priceA))
    HosodaTpNTtime=timeC+ACdiff
    extendHorLines=extend.right
    StartxForHorLines=math.min(timeC,HosodaTpEtime,HosodaTpNtime,HosodaTpVtime,HosodaTpNTtime)
    EndxForHorLines=timeC+1
    if HorizLinesOnTargetTrim
        EndxForHorLines:=math.max(HosodaTpEtime,HosodaTpNtime,HosodaTpVtime,HosodaTpNTtime)
        extendHorLines:=extend.none

    //E:
    if ShowBoxAroundTarget
        box.new(HosodaTpEtime-secondsPerBar*2,HosodaTpEprice+AAtr,HosodaTpEtime+secondsPerBar*2,HosodaTpEprice-AAtr,AllColor,1,line.style_solid,extend.none,xloc=xloc.bar_time,bgcolor=na)
    if VertLinesOnTarget
        line.new(HosodaTpEtime,priceC,HosodaTpEtime,HosodaTpEprice, xloc = xloc.bar_time,color=AllColor)    
    if ShowTargetPriceLabel
        label.new(HosodaTpEtime, HosodaTpEprice+AAtr, "E=" +str.tostring(HosodaTpEprice) , xloc=xloc.bar_time,yloc=yloc.price, style=label.style_none,color=AllColor)
    if HorizLinesOnTarget
        line.new(StartxForHorLines,HosodaTpEprice,EndxForHorLines,HosodaTpEprice, xloc = xloc.bar_time,extend=extendHorLines,color=AllColor)

    //N:
    if ShowBoxAroundTarget
        box.new(HosodaTpNtime-secondsPerBar*2,HosodaTpNprice+AAtr,HosodaTpNtime+secondsPerBar*2,HosodaTpNprice-AAtr,AllColor,1,line.style_solid,extend.none,xloc=xloc.bar_time,bgcolor=na)
    if ShowTargetPriceLabel
        label.new(HosodaTpNtime, HosodaTpNprice+AAtr, "N=" +str.tostring(HosodaTpNprice) , xloc=xloc.bar_time,yloc=yloc.price, style=label.style_none,color=AllColor)
    if VertLinesOnTarget
        line.new(HosodaTpNtime,priceC,HosodaTpNtime,HosodaTpEprice, xloc = xloc.bar_time,color=AllColor)
    if HorizLinesOnTarget
        line.new(StartxForHorLines,HosodaTpNprice,EndxForHorLines,HosodaTpNprice, xloc = xloc.bar_time,extend=extendHorLines,color=AllColor)

    //V:
    if ShowBoxAroundTarget
        box.new(HosodaTpVtime-secondsPerBar*2,HosodaTpVprice+AAtr,HosodaTpVtime+secondsPerBar*2,HosodaTpVprice-AAtr,AllColor,1,line.style_solid,extend.none,xloc=xloc.bar_time,bgcolor=na)
    if ShowTargetPriceLabel
        label.new(HosodaTpVtime, HosodaTpVprice+AAtr, "V=" +str.tostring(HosodaTpVprice) , xloc=xloc.bar_time,yloc=yloc.price, style=label.style_none,color=AllColor)
    if VertLinesOnTarget
        line.new(HosodaTpVtime,priceC,HosodaTpVtime,HosodaTpEprice, xloc = xloc.bar_time,color=AllColor)
    if HorizLinesOnTarget
        line.new(StartxForHorLines,HosodaTpVprice,EndxForHorLines,HosodaTpVprice, xloc = xloc.bar_time,extend=extendHorLines,color=AllColor)


    //NT:
    if ShowBoxAroundTarget
        box.new(HosodaTpNTtime-secondsPerBar*2,HosodaTpNTprice+AAtr,HosodaTpNTtime+secondsPerBar*2,HosodaTpNTprice-AAtr,AllColor,1,line.style_solid,extend.none,xloc=xloc.bar_time,bgcolor=na)
    if ShowTargetPriceLabel
        label.new(HosodaTpNTtime, HosodaTpNTprice+AAtr, "NT=" +str.tostring(HosodaTpNTprice) , xloc=xloc.bar_time,yloc=yloc.price, style=label.style_none,color=AllColor)
    if VertLinesOnTarget
        line.new(HosodaTpNTtime,priceC,HosodaTpNTtime,HosodaTpEprice, xloc = xloc.bar_time,color=AllColor)
    if HorizLinesOnTarget
        line.new(StartxForHorLines,HosodaTpNTprice,EndxForHorLines,HosodaTpNTprice, xloc = xloc.bar_time,extend=extendHorLines,color=AllColor)

