LM:AllIn
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © lokenomics

//@version=5
indicator(title="LM:AllIn", shorttitle="LM:AllIn", overlay=true)


// ##################################################################################################
// ############################################### VWAP #############################################
// ##################################################################################################


//##### VOLUME ##### 
// Get Volumes; For NIFTY/ BN /CNXFINANCE get futures volumes; Else get stock volume
symForVolume = (syminfo.tickerid == "NSE:BANKNIFTY" or syminfo.tickerid == "NSE:NIFTY") ? syminfo.tickerid+"1!" : ( syminfo.tickerid == "NSE:CNXFINANCE" ? "NSE:NIFTY1!" : syminfo.tickerid)
[appropriateVolume]=request.security(symForVolume,timeframe.period,[volume])





var cumVol = 0.
cumVol += nz(appropriateVolume)
if barstate.islast and cumVol == 0
    runtime.error("No volume is provided by the data vendor.")
    
computeVWAP(src, isNewPeriod, stDevMultiplier) =>
	var float sumSrcVol = na
	var float sumVol = na
    var float sumSrcSrcVol = na

	sumSrcVol := isNewPeriod ? src * appropriateVolume : src * appropriateVolume + sumSrcVol[1]
	sumVol := isNewPeriod ? appropriateVolume : appropriateVolume + sumVol[1]
	// sumSrcSrcVol calculates the dividend of the equation that is later used to calculate the standard deviation
	sumSrcSrcVol := isNewPeriod ? appropriateVolume * math.pow(src, 2) : appropriateVolume * math.pow(src, 2) + sumSrcSrcVol[1]

	_vwap = sumSrcVol / sumVol
	variance = sumSrcSrcVol / sumVol - math.pow(_vwap, 2)
	variance := variance < 0 ? 0 : variance
	stDev = math.sqrt(variance)

	lowerBand = _vwap - stDev * stDevMultiplier
	upperBand = _vwap + stDev * stDevMultiplier

	[_vwap, lowerBand, upperBand]

hideonDWM = input(false, title="Hide VWAP on 1D or Above", group="VWAP Settings")
var anchor = input.string(defval = "Session", title="Anchor Period",
 options=["Session", "Week", "Month", "Quarter", "Year", "Decade", "Century", "Earnings", "Dividends", "Splits"], group="VWAP Settings")
src = input(title = "Source", defval = hlc3, group="VWAP Settings")
offset = input(0, title="Offset", group="VWAP Settings")

showBands = input(true, title="Calculate Bands", group="Standard Deviation Bands Settings")
stdevMult = input(1.0, title="Bands Multiplier", group="Standard Deviation Bands Settings")

timeChange(period) =>
	ta.change(time(period))

new_earnings = request.earnings(syminfo.tickerid, earnings.actual, barmerge.gaps_on, barmerge.lookahead_on, ignore_invalid_symbol=true)
new_dividends = request.dividends(syminfo.tickerid, dividends.gross, barmerge.gaps_on, barmerge.lookahead_on, ignore_invalid_symbol=true)
new_split = request.splits(syminfo.tickerid, splits.denominator, barmerge.gaps_on, barmerge.lookahead_on, ignore_invalid_symbol=true)

isNewPeriod = switch anchor
	"Earnings" => not na(new_earnings)
	"Dividends" => not na(new_dividends)
	"Splits" => not na(new_split)
	"Session" => timeChange("D")
	"Week" => timeChange("W")
	"Month" => timeChange("M")
	"Quarter" => timeChange("3M")
	"Year" => timeChange("12M")
	"Decade" => timeChange("12M") and year % 10 == 0
	"Century" => timeChange("12M") and year % 100 == 0
	=> false

isEsdAnchor = anchor == "Earnings" or anchor == "Dividends" or anchor == "Splits"
if na(src[1]) and not isEsdAnchor
	isNewPeriod := true

float vwapValue = na
float std = na
float upperBandValue = na
float lowerBandValue = na

if not (hideonDWM and timeframe.isdwm)
    [_vwap, bottom, top] = computeVWAP(src, isNewPeriod, stdevMult)
    vwapValue := _vwap
    upperBandValue := showBands ? top : na
    lowerBandValue := showBands ? bottom : na

plot(vwapValue, title="VWAP", color=#2962FF, linewidth = 2, offset=offset) //vwapValue

upperBand = plot(upperBandValue, title="Upper Band", color=color.new(color.green, 75), offset=offset)
lowerBand = plot(lowerBandValue, title="Lower Band", color=color.new(color.green, 75), offset=offset)

fill(upperBand, lowerBand, title="Bands Fill", color= showBands ? color.new(color.green, 95) : na)
// ##################################################################################################
// ############################################### VWAP #############################################
// ##################################################################################################














// ##################################################################################################
// ############################################### CPR ##############################################
// ##################################################################################################
// © GuruprasadMeduri
//***************GUIDE***********************************
//CPR - Applicable only for daily pivots
//CPR - All 3 lines display enabled by default
//CPR - Central Pivot line display cannot changed
//CPR - Central Pivot is a blue line by default and can be changed from settings
//CPR - Top Range & Bottom Ranage display can be changed from settings
//CPR - Top Range & Bottom Ranage are  Yellow lines by default and can be chaned from settings
//Daily pivots - Pivot line and CPR Central line are same
//Daily pivots - level 1 & 2 (S1, R1, S2  R2) display enabled by default and can be changed from settings
//Daily pivots - level 3 (S3 & R3) is availale and can be seleted from settings
//Daily pivots - Resistance(R) lines are Red lines and can be changed from settings
//Daily pivots - Support(S) lines are Green lines and can be changed from settings
//Weekly pivots - Pivot is a blue line by default and can be changed from settings
//Weekly pivots - 3 levels (S1, R1, S2, R2, S3 & R3) availale and can be seleted from settings
//Weekly pivots - Resistance(R) lines are crossed (+) Red lines and can be changed from settings
//Weekly pivots - Support(S) lines are crossed (+) Green lines and can be changed from settings
//Monthly pivots - Pivot is a blue line by default and can be changed from settings
//Monthly pivots - 3 levels (S1, R1, S2, R2, S3 & R3) availale and can be seleted from settings
//Monthly pivots - Resistance(R) lines are circled (o) Red lines and can be changed from settings
//Monthly pivots - Support(S) lines are circled (o) Green lines and can be changed from settings

//Loki settings
//var cprOffset=timeframe.period


//cenral pivot range
pivot = (high + low + close) /3 //Central Povit
BC = (high + low) / 2 //Below Central povit
TC = (pivot - BC) + pivot //Top Central povot

//3  support levels
S1 = (pivot * 2) - high
S2 = pivot - (high - low)
S3 = low - 2 * (high - pivot)

//3  resistance levels
R1 = (pivot * 2) - low
R2 = pivot + (high - low)
R3 = high + 2 * (pivot-low)

//Checkbox inputs
CPRPlot = input.bool(title = "Plot CPR?", defval=true)
DayS1R1 = input.bool(title = "Plot Daiy S1/R1?", defval=true)
DayS2R2 = input.bool(title = "Plot Daiy S2/R2?", defval=false)
DayS3R3 = input.bool(title = "Plot Daiy S3/R3?", defval=false)
WeeklyPivotInclude = input.bool(title = "Plot Weekly Pivot?", defval=false)
WeeklyS1R1 = input.bool(title = "Plot weekly S1/R1?", defval=false)
WeeklyS2R2 = input.bool(title = "Plot weekly S2/R2?", defval=false)
WeeklyS3R3 = input.bool(title = "Plot weekly S3/R3?", defval=false)
MonthlyPivotInclude = input.bool(title = "Plot Montly Pivot?", defval=false)
MonthlyS1R1 = input.bool(title = "Plot Monthly S1/R1?", defval=false)
MonthlyS2R2 = input.bool(title = "Plot Monthly S2/R2?", defval=false)
MonthlyS3R3 = input.bool(title = "Plot Montly S3/R3?", defval=false)


//OFFSET FIXING
var offsetMultiplier=75
if timeframe.period=='1'
    offsetMultiplier:=375
else if timeframe.period=='3'
    offsetMultiplier:=125
else if timeframe.period=='5'
    offsetMultiplier:=75
else if timeframe.period=='15'
    offsetMultiplier:=25
else if timeframe.period=='30'
    offsetMultiplier:=13
else if timeframe.period=='60'
    offsetMultiplier:=6
else if timeframe.period=='D'
    offsetMultiplier:=1
    
    



//******************DAYWISE CPR & PIVOTS**************************
// Getting daywise CPR
[DayPivot, DayBC, DayTC] = request.security(syminfo.tickerid, "D", [pivot[0], BC[0], TC[0]], barmerge.gaps_off, barmerge.lookahead_on)
//DayBC = request.security(syminfo.tickerid, "D", BC[0], barmerge.gaps_off, barmerge.lookahead_on)
//DayTC = request.security(syminfo.tickerid, "D", TC[0], barmerge.gaps_off, barmerge.lookahead_on)

//Adding linebreaks daywse for CPR
CPColour = DayPivot != DayPivot[0] ? na : color.blue
BCColour = DayBC != DayBC[0] ? na : color.blue
TCColour = DayTC != DayTC[0] ? na : color.blue

//Plotting daywise CPR
plot(DayPivot, title = "CP", offset=offsetMultiplier, color = CPColour, style = plot.style_linebr, linewidth =2)
plot(CPRPlot ? DayBC : na , offset=offsetMultiplier, title = "BC" , color = BCColour, style = plot.style_line, linewidth =1)
plot(CPRPlot ? DayTC : na , offset=offsetMultiplier, title = "TC" , color = TCColour, style = plot.style_line, linewidth =1)

// Getting daywise Support levels
[DayS1,  DayS2, DayS3] = request.security(syminfo.tickerid, "D", [S1[0], S2[0], S3[0]], barmerge.gaps_off, barmerge.lookahead_on)
//DayS2 = request.security(syminfo.tickerid, "D", S2[0], barmerge.gaps_off, barmerge.lookahead_on)
//DayS3 = request.security(syminfo.tickerid, "D", S3[0], barmerge.gaps_off, barmerge.lookahead_on)

//Adding linebreaks for daywise Support levels 
DayS1Color =DayS1 != DayS1[0] ? na : color.green
DayS2Color =DayS2 != DayS2[0] ? na : color.green
DayS3Color =DayS3 != DayS3[0] ? na : color.green

//Plotting daywise Support levels
plot(DayS1R1 ? DayS1 : na, title = "D-S1" , offset=offsetMultiplier, color = DayS1Color, style = plot.style_line, linewidth =1)
plot(DayS2R2 ? DayS2 : na, title = "D-S2" , offset=offsetMultiplier, color = DayS2Color, style = plot.style_line, linewidth =1)
plot(DayS3R3 ? DayS3 : na, title = "D-S3" , offset=offsetMultiplier, color = DayS3Color, style = plot.style_line, linewidth =1)

// Getting daywise Resistance levels
[DayR1, DayR2, DayR3] = request.security(syminfo.tickerid, "D", [R1[0], R2[0], R3[0]], barmerge.gaps_off, barmerge.lookahead_on)
//DayR2 = request.security(syminfo.tickerid, "D", R2[0], barmerge.gaps_off, barmerge.lookahead_on)
//DayR3 = request.security(syminfo.tickerid, "D", R3[0], barmerge.gaps_off, barmerge.lookahead_on)

//Adding linebreaks for daywise Support levels
DayR1Color =DayR1 != DayR1[0] ? na : color.red
DayR2Color =DayR2 != DayR2[0] ? na : color.red
DayR3Color =DayR3 != DayR3[0] ? na : color.red

//Plotting daywise Resistance levels
plot(DayS1R1 ? DayR1 : na, title = "D-R1" , offset=offsetMultiplier, color = DayR1Color, style = plot.style_line, linewidth =1)
plot(DayS2R2 ? DayR2 : na, title = "D-R2" , offset=offsetMultiplier, color = DayR2Color, style = plot.style_line, linewidth =1)
plot(DayS3R3 ? DayR3 : na, title = "D-R3" , offset=offsetMultiplier, color = DayR3Color, style = plot.style_line, linewidth =1)

//******************WEEKLY PIVOTS**************************

// Getting Weely Pivot
WPivot = request.security(syminfo.tickerid, "W", pivot[0], barmerge.gaps_off, barmerge.lookahead_on)

//Adding linebreaks for Weely Pivot 
WeeklyPivotColor =WPivot != WPivot[0] ? na : color.blue

//Plotting Weely Pivot
plot(WeeklyPivotInclude ? WPivot:na, title = "W-P" , color = WeeklyPivotColor, style = plot.style_circles, linewidth =1)


// Getting Weely Support levels
[WS1, WS2, WS3] = request.security(syminfo.tickerid, "W", [S1[0], S2[0], S3[0]],barmerge.gaps_off, barmerge.lookahead_on)
//WS2 = request.security(syminfo.tickerid, "W", S2[0],barmerge.gaps_off, barmerge.lookahead_on)
//WS3 = request.security(syminfo.tickerid, "W", S3[0],barmerge.gaps_off, barmerge.lookahead_on)

//Adding linebreaks for weekly Support levels
WS1Color =WS1 != WS1[0] ? na : color.green
WS2Color =WS2 != WS2[0] ? na : color.green
WS3Color =WS3 != WS3[0] ? na : color.green

//Plotting Weely Support levels
plot(WeeklyS1R1 ? WS1 : na, title = "W-S1" , offset=offsetMultiplier, color = WS1Color, style = plot.style_cross, linewidth =1)
plot(WeeklyS2R2 ? WS2 : na, title = "W-S2" , offset=offsetMultiplier, color = WS2Color, style = plot.style_cross, linewidth =1)
plot(WeeklyS3R3 ? WS3 : na, title = "W-S3" , offset=offsetMultiplier, color = WS3Color, style = plot.style_cross, linewidth =1)

// Getting Weely Resistance levels
[WR1, WR2, WR3] = request.security(syminfo.tickerid, "W", [R1[0], R2[0], R3[0]], barmerge.gaps_off, barmerge.lookahead_on)
//WR2 = request.security(syminfo.tickerid, "W", R2[0], barmerge.gaps_off, barmerge.lookahead_on)
//WR3 = request.security(syminfo.tickerid, "W", R3[0], barmerge.gaps_off, barmerge.lookahead_on)

//Adding linebreaks for weekly Resistance levels
WR1Color = WR1 != WR1[0] ? na : color.red
WR2Color = WR2 != WR2[0] ? na : color.red
WR3Color = WR3 != WR3[0] ? na : color.red

//Plotting Weely Resistance levels
plot(WeeklyS1R1 ? WR1 : na , title = "W-R1" , offset=offsetMultiplier, color = WR1Color, style = plot.style_cross, linewidth =1)
plot(WeeklyS2R2 ? WR2 : na , title = "W-R2" , offset=offsetMultiplier, color = WR2Color, style = plot.style_cross, linewidth =1)
plot(WeeklyS3R3 ? WR3 : na , title = "W-R3" , offset=offsetMultiplier, color = WR3Color, style = plot.style_cross, linewidth =1)

//******************MONTHLY PIVOTS**************************

// Getting Monhly Pivot
MPivot = request.security(syminfo.tickerid, "M", pivot[0],barmerge.gaps_off, barmerge.lookahead_on)

//Adding linebreaks for Monthly Support levels 
MonthlyPivotColor =MPivot != MPivot[0] ? na : color.blue

//Plotting  Monhly Pivot
plot(MonthlyPivotInclude? MPivot:na, title = " M-P" , color = MonthlyPivotColor, style = plot.style_line, linewidth =1)

// Getting  Monhly Support levels
[MS1, MS2, MS3] = request.security(syminfo.tickerid, "M", [S1[0], S2[0], S3[0]],barmerge.gaps_off, barmerge.lookahead_on)
//MS2 = request.security(syminfo.tickerid, "M", S2[0],barmerge.gaps_off, barmerge.lookahead_on)
//MS3 = request.security(syminfo.tickerid, "M", S3[0],barmerge.gaps_off, barmerge.lookahead_on)

//Adding linebreaks for Montly Support levels
MS1Color =MS1 != MS1[0] ? na : color.green
MS2Color =MS2 != MS2[0] ? na : color.green
MS3Color =MS3 != MS3[0] ? na : color.green

//Plotting  Monhly Support levels
plot(MonthlyS1R1 ? MS1 : na, title = "M-S1" , offset=offsetMultiplier, color = MS1Color, style = plot.style_circles, linewidth =1)
plot(MonthlyS2R2 ? MS2 : na, title = "M-S2" , offset=offsetMultiplier, color = MS2Color, style = plot.style_circles, linewidth =1)
plot(MonthlyS3R3 ? MS3 : na, title = "M-S3" , offset=offsetMultiplier, color = MS3Color, style = plot.style_circles, linewidth =1)

// Getting  Monhly Resistance levels
[MR1, MR2, MR3] = request.security(syminfo.tickerid, "M", [R1[0], R2[0], R3[0]],barmerge.gaps_off, barmerge.lookahead_on)
//MR2 = request.security(syminfo.tickerid, "M", R2[0],barmerge.gaps_off, barmerge.lookahead_on)
//MR3 = request.security(syminfo.tickerid, "M", R3[0],barmerge.gaps_off, barmerge.lookahead_on)

MR1Color =MR1 != MR1[0] ? na : color.red
MR2Color =MR2 != MR2[0] ? na : color.red
MR3Color =MR3 != MR3[0] ? na : color.red

//Plotting  Monhly Resistance levels
plot(MonthlyS1R1 ? MR1 : na , title = "M-R1", offset=offsetMultiplier, color = MR1Color, style = plot.style_circles , linewidth =1)
plot(MonthlyS2R2 ? MR2 : na , title = "M-R2" , offset=offsetMultiplier, color = MR2Color, style = plot.style_circles, linewidth =1)
plot(MonthlyS3R3 ? MR3 : na, title = "M-R3" , offset=offsetMultiplier, color = MR3Color, style = plot.style_circles, linewidth =1)

//*****************************INDICATORs**************************




// ##################################################################################################
// ############################################### CPR ##############################################
// ##################################################################################################







// ##################################################################################################
// ############################################# FUTURES  ###########################################
// ##################################################################################################
symFutures = (syminfo.tickerid == "NSE:CNXFINANCE") ? "NSE:NIFTY1!" : syminfo.tickerid+"1!"
[futuresOpen, futuresHigh, futuresLow, futuresClose, futuresVolume]=request.security(symForVolume,timeframe.period,[open, high, low, close, volume])
plot(futuresClose, title='Nifty Futures', color=color.new(color.purple, 0), linewidth=2, style=plot.style_line)
// else if syminfo.ticker=="NSE:BANKNIFTY"
//     [futuresOpen,futuresHigh, futuresLow,futuresClose, volumeFutures]=request.security("NSE:BANKNIFTY1!",timeframe.period,[open, high, low, close, volume])
// else if syminfo.ticker=="NSE:CNXFINANCE"
//     [futuresOpen,futuresHigh, futuresLow,futuresClose, volumeFutures]=request.security("NSE:BANKNIFTY1!",timeframe.period,[open, high, low, close, volume])
// else 
//     [futuresOpen,futuresHigh, futuresLow,futuresClose, volumeFutures]=request.security(syminfo.ticker,timeframe.period,[open, high, low, close, volume])

// ##################################################################################################
// ############################################# FUTURES  ###########################################
// ##################################################################################################








// ##################################################################################################
// ############################################# US FUTURES  ###########################################
// ##################################################################################################
[USFutOpen, USFutOpen1, USFutClose, USFutClose1]=request.security("CURRENCYCOM:US30",'D',[open, open[1], close,close[1]])
//USOpeningRatio=
//[futuresOpen, futuresHigh, futuresLow, futuresClose, futuresVolume]=request.security(symForVolume,timeframe.period,[open, high, low, close, volume])
//plot(futuresClose, title='Nifty Futures', color=color.new(color.purple, 0), linewidth=2, style=plot.style_line)

// tUS = input.symbol("CURRENCYCOM:US30","TickerUS")
// [tsUS,tsUSC1] = request.security(tUS,'D',[close,close[1]])
// tsUSTF1 = request.security(tUS,TF1,close[1]) 
// tsUSTF2 = request.security(tUS,TF2,close[1]) 
// //tsUSTF3 = request.security(tUS,TF3,close[1]) 
// tsUSChng = (tsUS-tsUSC1)
// tsUSp = (tsUS-tsUSC1)*100/tsUSC1
// tsUS1 = (tsUS-tsUSTF1)*100/tsUSTF1
// tsUS2 = (tsUS-tsUSTF2)*100/tsUSTF2
//tsUS3 = (tsUS-tsUSC1)*100/tsUSTF3
// ##################################################################################################
// ############################################# US FUTURES  ###########################################
// ##################################################################################################









// ##################################################################################################
// ######################################## MOVING AVERAGES #########################################
// ##################################################################################################


//timeframe.period
smaExp25   = ta.sma(close, 25)
smaExp50   = ta.sma(close, 50)
smaExp100  = ta.sma(close, 100)
smaExp200  = ta.sma(close, 200)

[sma25, sma50, sma100, sma200]= request.security(syminfo.tickerid, timeframe.period, [smaExp25, smaExp50, smaExp100, smaExp200])

plot(sma25, color=color.new(color.orange, 0), linewidth = 1, title="SMA25", style = plot.style_circles)
plot(sma50, color=color.new(color.orange, 0), linewidth = 1, title="SMA50", style = plot.style_line)
//plot(sma100, color=#00d8ff, linewidth = 1, title="SMA100")
plot(sma200, color=color.new(color.orange, 0), linewidth = 2, title="SMA200", style = plot.style_line)

//Days
[sma50d, sma100d, sma200d]= request.security(syminfo.tickerid, "D", [smaExp50, smaExp100, smaExp200])

plot(sma50d, color=color.new(color.orange, 0), linewidth = 3, title="SMA50D", style = plot.style_line)
plot(sma100d, color=color.new(color.orange, 0), linewidth = 4, title="SMA100D", style = plot.style_line)
plot(sma200d, color=color.new(color.orange, 0), linewidth = 5, title="SMA200D", style = plot.style_line)



// ##################################################################################################
// ######################################## MOVING AVERAGES #########################################
// ##################################################################################################




// ##################################################################################################
// ######################################## SIMPLE TRENDLINE ########################################
// ##################################################################################################
// shortPeriod = input(30, title="shortPeriod")
// longPeriod = input(100, title="longPeriod")

// if barstate.islast
//     float lowest_y2 = 60000
//     float lowest_x2 = 0
    
//     float highest_y2 = 0
//     float highest_x2 = 0
    
    
//     for i = 1 to shortPeriod
//         if low[i] < lowest_y2
//             lowest_y2 := low[i]
//             lowest_x2 := i
//         if high[i] > highest_y2
//             highest_y2 := high[i]
//             highest_x2 := i
            
//     float lowest_y1 = 60000
//     float lowest_x1 = 0
    
//     float highest_y1 = 0
//     float highest_x1 = 0
    
//     for j = shortPeriod + 1 to longPeriod
//         if low[j] < lowest_y1
//             lowest_y1 := low[j]
//             lowest_x1 := j
//         if high[j] > highest_y1
//             highest_y1 := high[j]
//             highest_x1 := j
            
//     sup = line.new(x1=bar_index[lowest_x1], y1=lowest_y1, x2=bar_index[lowest_x2], y2=lowest_y2, extend=extend.right, width=2, color=color.green)
//     res = line.new(x1=bar_index[highest_x1], y1=highest_y1, x2=bar_index[highest_x2], y2=highest_y2, extend=extend.right, width=2, color=color.red)
//     line.delete(sup[1])
//     line.delete(res[1])
//     if ta.crossunder(close, line.get_price(sup, bar_index[0]))
//         alert("break support", freq=alert.freq_once_per_bar_close)

//     if ta.crossover(close, line.get_price(res, bar_index[0]))
//         alert("break resistance", freq=alert.freq_once_per_bar_close)
// ##################################################################################################
// ######################################## SIMPLE TRENDLINE ########################################
// ##################################################################################################










