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

//@version=5

// @description TODO: Creation of Placeholders for Alerts, for using in FNO segment.
library(title = "DerivativeAlertPlaceHolders", overlay = true)

// @BasicPH TODO: Creates Basic PlaceHolders
// @param CustomMessage TODO: Requires Custom Input of Message
// @returns TODO: String with PH
export BasicPH(string CustomMessage) =>
	//TODO : add function body and return value here
	ExchReplacement		= str.replace_all(CustomMessage,	"{{exch}}",			syminfo.prefix)
	TFReplacement		= str.replace_all(ExchReplacement,	"{{timeframe}}", 	timeframe.period)	
	CloseReplacement	= str.replace_all(TFReplacement,	"{{close}}",		str.tostring(close, format.mintick))
	OpenReplacement		= str.replace_all(CloseReplacement,	"{{open}}",			str.tostring(open, format.mintick))
	HighReplacement		= str.replace_all(OpenReplacement,	"{{high}}",			str.tostring(high, format.mintick))
	LowReplacement		= str.replace_all(HighReplacement,	"{{low}}",			str.tostring(low, format.mintick))


// @CustomPlaceHoldersFNO TODO: Creates FNO placeholders, for 1ITM CE strike use place holder { {100R1} }, 
// 100 refers to strike intervals of underlying and R refers to Rounded number of underlying, 1 refers to moneyness, 
// if underlying price is 2508, with the placeholder for 1ITM CE, for the input of { {100R1} } output shall be 2400.
// @param CustomInputMessage TODO: Requires Custom Input of Message
// @returns TODO: Alert String with PH used in major FNO alert Segments
export CustomPlaceHoldersFNO(string CustomInputMessage, float InputPrice = close) =>
	Output							= ""
	Search_String					= array.new_string(0)
	Replace_String					= array.new_string(0)
	BasicConversion					= BasicPH(CustomInputMessage)
	if str.contains(BasicConversion, "{ ")
		CustomInputSplits			= str.split(BasicConversion,"{ ")
		for [index, value] in CustomInputSplits
			if str.startswith(value,"{")
				SplitTextWith2Curly = str.split(value,"} }")
				TextToGetPH			= str.replace_all(SplitTextWith2Curly.first(),"{","")
				SplitsOfRequiredPH	= str.split(TextToGetPH,"R")
				if SplitsOfRequiredPH.size() == 2
					Diviser 		= int(str.tonumber(SplitsOfRequiredPH.first()))
					Moneyness		= int(str.tonumber(SplitsOfRequiredPH.last()))
					Search_String.push("{ {" + TextToGetPH + "} }")
					RoundedNumber	= InputPrice - (InputPrice % Diviser)
					RoundedMoneyNess= (InputPrice % Diviser) > Diviser * 0.5 ? 1 : 0
					Replace_String.push(str.tostring(RoundedNumber + nz((Diviser * (Moneyness + RoundedMoneyNess)),0)))				
		for [index, value] in Search_String
			Output					:= str.replace_all(BasicConversion,value,Replace_String.get(index))
			BasicConversion			:= Output
	else
		Output						:= BasicConversion
	Output
	
if barstate.islast
	label.new(bar_index, high, CustomPlaceHoldersFNO("Place Your TP Alert Message here.\n\n Alert Trigerred when Price was at {{close}}"))


