0% found this document useful (0 votes)
24 views16 pages

Daily Bias Indicator for Crude Oil

The document is a source code for a trading indicator called 'BE - Golden Cross Crude Key' designed for use in TradingView. It includes various settings for trade scanning, trend tracking, and trade alerts, allowing users to customize their trading experience. The code also implements features for real-time trade notifications, trade logging, and hedging options for futures trading.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views16 pages

Daily Bias Indicator for Crude Oil

The document is a source code for a trading indicator called 'BE - Golden Cross Crude Key' designed for use in TradingView. It includes various settings for trade scanning, trend tracking, and trade alerts, allowing users to customize their trading experience. The code also implements features for real-time trade notifications, trade logging, and hedging options for futures trading.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

// This source code is subject to the terms of the Mozilla Public License 2.

0 at
[Link]
// © TradeWiseWithEase

//@version=5
indicator("BE - Golden Cross Crude Key",shorttitle = "BE-CRUDE-V0.1.1", overlay =
true)
import TradeWiseWithEase/BE_CustomFx_Library/93 as BEL

var string GroupText1 = "Trade Scan Settings"


var string GroupText2 = "Trend Tracker Settings"
var string GroupText3 = "Trade Alerts Settings"
var string GroupText4 = "Trade Table Settings"
var string RealtimeTip = "If Enabled Trade 'ENTRIES' & 'EXITS' shall
be on the 'REALTIME' upon 'PRICE' hitting the 'ENTRY ZONE' OR 'SL ZONE' even on the
'yet to complete candle' or trade gets 'CLOSED' Upon specific 'PROFIT BOOKING
ZONES' on realtime basis.\n\nSince It doesn't wait for the close of the candle to
happen, there may be possibility of showing the 'ENTRIES' which would have closed
in realtime but it may still be showing as 'RUNNING' upon 'CHART REFRESH'.\n\nIf
set to True, Trades will be notified while the candle is forming / Yet to complete.
Else Trades will be notified upon CLOSE of the candle."
var string HedgeTTIP = "This Option Allows you to place HEDGE Leg
(options) to avail margin benefit for trades placed in FUTURES.\n\nEnsure to Update
Correct Options Symbols along with Expiry Date in the fields provided\n\nDefault 10
refers to 10 OTM CE & PE & the option Legs are brought during the first trade and
the same shall be squared off when the TRADING SESSION is CLOSED"
var string UseA_NameTTP = "Plz use symbol constructor from NLB
webportal\n\n [Link] \n\nIMPORTANT: SPECIFY THE
TRADING ACCOUNT NAME CORRECTLY AND USE ': WITH A SPACE'\n\nUSE 'FORWARDSLASH /' TO
SEPARATE THE SYMBOLS BETWEEN FUTURES AND OPTIONS"
var string SymbolTextA = 'DHANHQ: CRUDEOIL23FEBFUT/CRUDEOIL23FEB' + '\
n' + 'TRADINGVIEW: CRUDEOIL23FEBFUT/CRUDEOIL23FEB'

//#region Trade Scan Settings


var string sess = [Link](
defval = '1800-2330',
title = 'Trade Session',
group = GroupText1,
tooltip = "Default Values refers to -->\n\nTrade Entries Shall be Scanned Post
6 PM and any Open Position Shall be Squared Off Post 11:00 PM or Close of the
Candle Whichever is LATER.")

var string NoFreshEntryTime = "0000-" + [Link](


defval = '2230',
title = 'No Fresh Entry Post',
group = GroupText1)

var bool ShowPlots = [Link](


defval = true,
title = "Show SMA Line",
inline = "SMA",
group = GroupText1)

var color SMAClr = [Link](


defval = [Link],
title = "",
inline = "SMA",
group = GroupText1)
var int SMAWidth = [Link](
defval = 1,
title = "",
inline = "SMA",
group = GroupText1)

var int SMALength = [Link](


defval = 42,
title = "",
inline = "SMA",
minval = 1,
group = GroupText1)

SMAValue = [Link](close, SMALength)


plot(
ShowPlots ? SMAValue : na,
title = "SMA Value",
linewidth = SMAWidth,
color = SMAClr,
editable = false)
//#endregion

//#region Trend Tracker Settings


var int Mode = [Link](
defval = 2,
title = "Tracker Mode",
minval = 1,
group = GroupText2)

var int Sensitivity = [Link](


defval = 1,
title = "Sensitivity [0 - 2]",
group = GroupText2,
minval = 0,
maxval = 2,
tooltip = "Low Value looks for More confirmed pattern, High value predicts for
Possible Confirmation.")

var bool ShowTrendTracker = [Link](


defval = true,
title = "Show Trend Tracker",
group = GroupText2)

var color Tr_LongClr = [Link](


defval = #0cd0deed,
title = "Up",
inline = "TT",
group = GroupText2)

var color Tr_ShortClr = [Link](


defval = #cd13db,
title = "Dn",
inline = "TT",
group = GroupText2)

var int Tr_LineWidth = [Link](


defval = 1,
title = "",
inline = "TT",
group = GroupText2)

//#region Trend Tracker Calculation


var int Trend_Direction = 0
var int NxtTrend_Direction = 0
var float MaxLowPrice = nz(low[1], low)
var float MinHighPrice = nz(high[1], high)
var float TrendLevel = 0.0
var float CapUp = 0.0
var float CapDown = 0.0
float ATR2Level = [Link](100) / 2
float HighPrice = high[[Link]([Link](Mode))]
float LowPrice = low[[Link]([Link](Mode))]
float HighMovAvg = [Link](Sensitivity == 0 ? high :
Sensitivity == 1 ? hlc3 : hl2, Mode)
float LowMovAvg = [Link](Sensitivity == 0 ? low :
Sensitivity == 1 ? hlc3 : hl2, Mode)
float SMAofATR = [Link](ATR2Level[1], 4)
Bars_SMAH_LT_InfoHighestLow = [Link](not (HighMovAvg <
MaxLowPrice))
Bars_SMAL_GT_InfolowestHigh = [Link](not (LowMovAvg >
MinHighPrice))
ConvertHT2Long = (((close - TrendLevel) > SMAofATR * 3.25)
or Bars_SMAL_GT_InfolowestHigh == 3) and NxtTrend_Direction == 1 and close >
TrendLevel and [Link]
ConvertHT2Short = (((TrendLevel - close) > SMAofATR * 3.25)
or Bars_SMAH_LT_InfoHighestLow == 3) and NxtTrend_Direction == -1 and close <
TrendLevel and [Link]

if [Link]
if NxtTrend_Direction == -1
MaxLowPrice := [Link](LowPrice, MaxLowPrice)
if HighMovAvg < MaxLowPrice and close < nz(low[1], low) and (Sensitivity <
1 or (Sensitivity >= 1 and (MaxLowPrice - close) > SMAofATR * 1.75))
Trend_Direction := -1
NxtTrend_Direction := 1
MinHighPrice := HighPrice
else if ConvertHT2Short and Sensitivity >= 1
Trend_Direction := -1
NxtTrend_Direction := 1
MinHighPrice := HighPrice
else
MinHighPrice := [Link](HighPrice, MinHighPrice)
if LowMovAvg > MinHighPrice and close > nz(high[1], high) and (Sensitivity
< 1 or (Sensitivity >= 1 and (close - MinHighPrice) > SMAofATR * 1.75))
Trend_Direction := 1
NxtTrend_Direction := -1
MaxLowPrice := LowPrice
else if ConvertHT2Long and Sensitivity >= 1
Trend_Direction := 1
NxtTrend_Direction := -1
MaxLowPrice := LowPrice

if Trend_Direction == 1
if Trend_Direction[1] == -1 and not na(Trend_Direction[1])
CapUp := na(CapDown[1]) ? CapDown : CapDown[1]
else
CapUp := na(CapUp[1]) ? MaxLowPrice :
[Link](MaxLowPrice, CapUp[1])
else
if Trend_Direction[1] == 1 and not na(Trend_Direction[1])
CapDown := na(CapUp[1]) ? CapUp : CapUp[1]
else
CapDown := na(CapDown[1]) ? MinHighPrice :
[Link](MinHighPrice, CapDown[1])

TrendLevel := Trend_Direction == 1 ? CapUp : CapDown


TrendColor = Trend_Direction == 1 ? Tr_LongClr :
Tr_ShortClr

plot(
ShowTrendTracker ? TrendLevel : na,
title = "Trend Tracker",
linewidth = Tr_LineWidth,
color = TrendColor,
editable = false)
//#endregion

//#endregion

//#region Trade Alerts Settings


var bool IsRealTime = [Link](
defval = true,
title = "Trade On Realtime ?",
group = GroupText3,
tooltip = RealtimeTip)

var string SymbolText = [Link](input.text_area(


defval = SymbolTextA,
title = "Symbol Name. Read Tips -->",
group = GroupText3,
tooltip = UseA_NameTTP))

//#region SymbolExtract Procedure


var ListOfTradingAccount = array.new_string(0)
var ListOfFuturePrefix = array.new_string(0)
var ListOfOptionsPrefix = array.new_string(0)
var SplitOfInputList = [Link](SymbolText,"\n")
var CountOfNLB_AccountsProvided = [Link](SplitOfInputList)

if [Link] and CountOfNLB_AccountsProvided > 0


[Link](ListOfTradingAccount)
for [index, value] in SplitOfInputList
AccountSplit = [Link](value,": ")
if [Link](AccountSplit) > 0
[Link](ListOfTradingAccount, [Link](AccountSplit, 0))
ScriptSplit = [Link]([Link](AccountSplit, 1),"/")
if [Link](ScriptSplit) > 0
for [index_, value_] in ScriptSplit
if [Link](value_,"FUT") or [Link](value_,"F")
[Link](ListOfFuturePrefix, [Link](ScriptSplit,
index_))
else
[Link](ListOfOptionsPrefix, [Link](ScriptSplit,
index_))
else
[Link]("Couldn't Extract the NLB TradingAccount List")
break
//#endregion

var bool AvoidOnWed = [Link](


defval = true,
title = "Avoid Trade On Wednesday",
group = GroupText3)

var bool TakeTrendConfirmation = [Link](


defval = true,
title = "Confirm Trade With Trend Direction",
group = GroupText3)

var int TradeQty = [Link](


defval = 100,
title = "Qty Per Trade",
group = GroupText3,
step = 100,
minval = 1)

var string FutOrOps = [Link](


defval = "FUT",
title = "Trade On Type",
group = GroupText3,
options = ["FUT","OPS"])

var string IAO = [Link](


defval = "ATM",
title = "Option Moneyness. ",
group = GroupText3,
options = ["ITM","OTM","ATM"],
inline = "OD")

var int deepness = [Link](


defval = 1,
title = "",
group = GroupText3,
inline = "OD",
step = 1,
minval = 1)

var bool PlayHedge = [Link](


defval = true,
title = "Hedge Legs ",
group = GroupText3,
inline = "Hedge",
tooltip = HedgeTTIP)

var int HedgeLegDeepness = [Link](


defval = 10,
title = "",
group = GroupText3,
inline = "Hedge",
minval = 1,
maxval = 20)

var string TGTType = [Link](


defval = "Capital",
title = "Target Type ",
group = GroupText3,
inline = "TGT",
options = ["Capital", "Point Based", "Biased"])

var float TGT_Value = [Link](


defval = 12000,
title = "",
group = GroupText3,
inline = "TGT")

var string SLType = [Link](


defval = "Capital DD",
title = "Stop Loss Type ",
group = GroupText3,
inline = "SLT",
options = ["Capital DD", "Point Based", "Biased", "Point Based Trail"])

var float SL_Value = [Link](


defval = 5000,
title = "",
group = GroupText3,
inline = "SLT")

var bool LookForBiasExit = [Link](


defval = false,
title = "Exit based On Bias Method if Possible",
group = GroupText3)

var float TrailPCT = [Link](


defval = 50,
title = "On ",
group = GroupText3,
minval = 0,
maxval = 100,
inline = "TSL") / 100

var float LockSLAt = [Link](


defval = 10,
title = "% of Target Lock SL at %",
group = GroupText3,
minval = -99,
maxval = 99,
inline = "TSL") / 100

var int StrikeDiff = [Link](


defval = 50,
title = "Strike Difference",
group = GroupText3,
minval = 1,
maxval = 5000)
//#endregion

//#region Trade Table Settings


var bool ShowTradeLogTable = [Link](
defval = true,
title = "Show Trade Table",
inline = "TL",
group = GroupText4)

var color TableTextColor = [Link](


defval = [Link],
title = "",
inline = "TL",
group = GroupText4)
//#endregion

//#region Declared Variables


var TradeInfoTable = [Link](position.top_right,2,4,
frame_color = [Link], frame_width = 1, border_width = 1, border_color =
[Link])
if [Link] and ShowTradeLogTable
for xrow = 0 to 3
for ycol = 0 to 1
table.cell_set_text_font_family(TradeInfoTable, ycol, xrow,
font.family_monospace)
table.cell_set_text_size(TradeInfoTable, ycol, xrow, [Link])
table.cell_set_text_color(TradeInfoTable, ycol, xrow, TableTextColor)
table.cell_set_text_halign(TradeInfoTable, ycol, xrow, ycol == 0 ?
text.align_left : text.align_center)
table.cell_set_text(TradeInfoTable, 0, 0, "Total Trades")
table.cell_set_text(TradeInfoTable, 0, 1, "Profitable Trades")
table.cell_set_text(TradeInfoTable, 0, 2, "Profitability %")
table.cell_set_text(TradeInfoTable, 0, 3, "Avg Peak Profitiablity")

varip Days_PNL = array.new_float(0)


varip OverAll_PNL = array.new_float(0)
varip MaxTradeROI_Array = array.new_float(0)
varip LogSummary = array.new_string(0)
varip float Last_TradePNL = 0.0
bool BuyShape = false
bool SellShape = false
bool ExitShape = false
type Setup
float ToBreakValue
int CrossCandle
int MaxToTradeCandle
float SLForTheTrade

var LongSetup = [Link]()


var ShortSetup = [Link]()
varip Log = BEL.Trade_Info.new()
Log.T_Qty := TradeQty
Log.T_QtyString := [Link](TradeQty)
if session.isfirstbar_regular
[Link](Days_PNL)
Log.T_HasExited := 1
Log.T_HasEntered := 0

var string ScriptName = [Link]("Name",


[Link])
var bool StartAllThingsWhen = false
var bool RestrictFreshEntry = false
TimeStringCalculation = [Link] ? timenow :
time_close - 1
bool NotToTradeToday = dayofweek(TimeStringCalculation) == 4 and
AvoidOnWed
DateTimeString = str.format_time(TimeStringCalculation,
"dd-MM-yyyy HH:mm:ss", [Link])
StartAllThingsWhen := BEL.Chk_TradingTime(sess)
RestrictFreshEntry := not
BEL.Chk_TradingTime(NoFreshEntryTime)
ChangeInTradingSession = [Link](StartAllThingsWhen)
HasCrossOverHappened = [Link](close, SMAValue) and
[Link] and not NotToTradeToday
HasCrossUnderHappened = [Link](close, SMAValue) and
[Link] and not NotToTradeToday
//#endregion

//#region EntrySetups
if HasCrossOverHappened
[Link] := high + 5
[Link] := bar_index + 1
[Link] := bar_index + 2
[Link] := Log.T_HasEntered == 1 and
Log.T_Direction == -1 and bar_index > [Link] ? high : na
[Link] := if not na([Link])
bar_index

if HasCrossUnderHappened
[Link] := low - 5
[Link] := bar_index + 1
[Link] := bar_index + 2
[Link] := Log.T_HasEntered == 1 and
Log.T_Direction == 1 and bar_index > [Link] ? low : na
[Link] := if not na([Link])
bar_index

LongTadeEntry = false
ShortTadeEntry = false

if [Link]([Link] ? close : (TakeTrendConfirmation and


Trend_Direction == -1) ? close : high ,"G", [Link]) and
[Link](bar_index,"BE", [Link], [Link])
and StartAllThingsWhen
ExpectedBarstatus = if (TakeTrendConfirmation and Trend_Direction == -1) or not
IsRealTime
[Link]
else
true
LongTadeEntry := if ExpectedBarstatus
true

if [Link]([Link] ? close : (TakeTrendConfirmation and


Trend_Direction == 1) ? close : low ,"L", [Link]) and
[Link](bar_index,"BE", [Link],
[Link]) and StartAllThingsWhen
ExpectedBarstatus = if (TakeTrendConfirmation and Trend_Direction == 1) or not
IsRealTime
[Link]
else
true
ShortTadeEntry := if ExpectedBarstatus
true
//#endregion

//#region Trade Entry


varip HedgeLegOptions = array.new_string(0)
varip MainLegSymbols = array.new_string(0)
if StartAllThingsWhen and bar_index > [Link] and not RestrictFreshEntry
string FinalEntryAlertText = ""
EntryAlertLegsArray = array.new_string(0)
if Log.T_HasEntered == 0
if LongTadeEntry
Log.T_EnPrice := if [Link]
and IsRealTime
close
else
[Link]

if PlayHedge and [Link] and FutOrOps == "FUT"


[Link] := false
[Link](HedgeLegOptions)
for [index, value] in ListOfTradingAccount
Otype = value == "TRADINGVIEW" ?
"" : "M"
[Symbol_C, AlertString_C] =
[Link](true, "OTM", HedgeLegDeepness, StrikeDiff,
true, "C", 0.0, 0.0, "NLB", [Link](ListOfOptionsPrefix,
index), 0, "BUY",
Log.T_QtyString, "", "", value, "MCX", Otype)
[Symbol_P, AlertString_P] =
[Link](true, "OTM", HedgeLegDeepness, StrikeDiff,
false, "C", 0.0, 0.0, "NLB", [Link](ListOfOptionsPrefix,
index), 0, "BUY",
Log.T_QtyString, "", "", value, "MCX", Otype)
[Link](HedgeLegOptions, Symbol_C)
[Link](HedgeLegOptions, Symbol_P)
[Link](EntryAlertLegsArray, AlertString_C)
[Link](EntryAlertLegsArray, AlertString_P)
[Link] := Symbol_C
[Link] := Symbol_P

Log.T_Symbol := ScriptName
[Link](MainLegSymbols)
for [index, value] in ListOfTradingAccount
Otype = value == "TRADINGVIEW" ?
"" : "L"
[Symbol_T, AlertString_T] =
[Link](FutOrOps == "OPS" ? true : false, IAO, deepness,
StrikeDiff,
true, "C", 0.0, 0.75, "NLB", FutOrOps == "OPS" ?
[Link](ListOfOptionsPrefix, index) : [Link](ListOfFuturePrefix, index), 0,
"BUY", Log.T_QtyString, "", "", value, "MCX", Otype, true, "0.1%")
[Link](MainLegSymbols, Symbol_T)
[Link](EntryAlertLegsArray, AlertString_T)
FinalEntryAlertText :=
[Link](EntryAlertLegsArray, "NLB")
if [Link](FinalEntryAlertText) > 3
alert(FinalEntryAlertText, alert.freq_once_per_bar)
Log.T_En_Date_Time := DateTimeString
Log.T_EnCandle := bar_index
Log.T_Name := "Long | Cross Over Trade
on " + Log.T_Symbol
Log.T_HasEntered := 1
Log.T_Direction := 1
Log.T_HasExited := 0
Log.T_ExCandle := 0
Log.T_ExPrice := 0
Log.T_TGTPrice := TGTType == "Point Based"
? math.round_to_mintick(Log.T_EnPrice + TGT_Value) : TGTType == "Capital" ?
math.round_to_mintick(Log.T_EnPrice + (TGT_Value / Log.T_Qty)) : na
Log.T_SLPrice := (SLType == "Point Based"
or SLType == "Point Based Trail") ? math.round_to_mintick(Log.T_EnPrice - SL_Value)
: SLType == "Capital DD" ? math.round_to_mintick(Log.T_EnPrice - (SL_Value /
Log.T_Qty)) : na
[Link] := Log.T_EnCandle
[Link] += 1
[Link] += 1
[Link] += 1
[Link] := na
[Link] := na
[Link] := na
Log.Price2MoveForTrailing := if SLType == "Point
Based Trail" and (TGTType == "Point Based" or TGTType == "Capital")
math.round_to_mintick(Log.T_EnPrice + ([Link](Log.T_TGTPrice -
Log.T_EnPrice) * TrailPCT))
else
na
Log.TrailSL_Val := if not
na(Log.Price2MoveForTrailing)
if LockSLAt == 0
Log.T_EnPrice
else if LockSLAt > 0
math.round_to_mintick(Log.T_EnPrice + ([Link](Log.T_TGTPrice
- Log.T_EnPrice) * LockSLAt))
else
math.round_to_mintick(Log.T_SLPrice + ([Link](Log.T_SLPrice -
Log.T_EnPrice) * [Link](LockSLAt)))
Log.SL_Level := 0
else if ShortTadeEntry
Log.T_EnPrice := if [Link]
and IsRealTime
close
else
[Link]

if PlayHedge and [Link] and FutOrOps == "FUT"


[Link] := false
[Link](HedgeLegOptions)
for [index, value] in ListOfTradingAccount
Otype = value == "TRADINGVIEW" ?
"" : "M"
[Symbol_C, AlertString_C] =
[Link](true, "OTM", HedgeLegDeepness, StrikeDiff,
true, "C", 0.0, 0.0, "NLB", [Link](ListOfOptionsPrefix,
index), 0, "BUY",
Log.T_QtyString, "", "", value, "MCX", Otype)
[Symbol_P, AlertString_P] =
[Link](true, "OTM", HedgeLegDeepness, StrikeDiff,
false, "C", 0.0, 0.0, "NLB", [Link](ListOfOptionsPrefix,
index), 0, "BUY",
Log.T_QtyString, "", "", value, "MCX", Otype)
[Link](HedgeLegOptions, Symbol_C)
[Link](HedgeLegOptions, Symbol_P)
[Link](EntryAlertLegsArray, AlertString_C)
[Link](EntryAlertLegsArray, AlertString_P)
[Link] := Symbol_C
[Link] := Symbol_P

Log.T_Symbol := ScriptName
[Link](MainLegSymbols)
for [index, value] in ListOfTradingAccount
Otype = value == "TRADINGVIEW" ?
"" : "L"
DirectionOfTrade = FutOrOps == "OPS" ? "BUY"
: "SELL"
[Symbol_T, AlertString_T] =
[Link](FutOrOps == "OPS" ? true : false, IAO, deepness,
StrikeDiff,
true, "C", 0.0, 0.75, "NLB", FutOrOps == "OPS" ?
[Link](ListOfOptionsPrefix, index) : [Link](ListOfFuturePrefix, index), 0,
DirectionOfTrade, Log.T_QtyString, "", "", value, "MCX", Otype,
true, "0.1%")
[Link](MainLegSymbols, Symbol_T)
[Link](EntryAlertLegsArray, AlertString_T)
FinalEntryAlertText :=
[Link](EntryAlertLegsArray, "NLB")
if [Link](FinalEntryAlertText) > 3
alert(FinalEntryAlertText, alert.freq_once_per_bar)
Log.T_En_Date_Time := DateTimeString
Log.T_EnCandle := bar_index
Log.T_Name := "Short | Cross Under
Trade on " + Log.T_Symbol
Log.T_HasEntered := 1
Log.T_Direction := -1
Log.T_HasExited := 0
Log.T_ExCandle := 0
Log.T_ExPrice := 0
Log.T_TGTPrice := TGTType == "Point Based"
? math.round_to_mintick(Log.T_EnPrice - TGT_Value) : TGTType == "Capital" ?
math.round_to_mintick(Log.T_EnPrice - (TGT_Value / Log.T_Qty)) : na
Log.T_SLPrice := (SLType == "Point Based"
or SLType == "Point Based Trail") ? math.round_to_mintick(Log.T_EnPrice +
SL_Value) : SLType == "Capital DD" ? math.round_to_mintick(Log.T_EnPrice +
(SL_Value / Log.T_Qty)) : na
[Link] := Log.T_EnCandle
[Link] += 1
[Link] += 1
[Link] += 1
[Link] := na
[Link] := na
[Link] := na
Log.Price2MoveForTrailing := if SLType == "Point
Based Trail" and (TGTType == "Point Based" or TGTType == "Capital")
math.round_to_mintick(Log.T_EnPrice - ([Link](Log.T_TGTPrice -
Log.T_EnPrice) * TrailPCT))
else
na
Log.TrailSL_Val := if not
na(Log.Price2MoveForTrailing)
if LockSLAt == 0
Log.T_EnPrice
else if LockSLAt > 0
math.round_to_mintick(Log.T_EnPrice - ([Link](Log.T_TGTPrice
- Log.T_EnPrice) * LockSLAt))
else
math.round_to_mintick(Log.T_SLPrice - ([Link](Log.T_SLPrice -
Log.T_EnPrice) * [Link](LockSLAt)))
Log.SL_Level := 0
//#endregion

//#region Peforming Regular Exit Check


[Link] := not
[Link] ? Log.T_EnCandle + 1 : Log.T_EnCandle
if Log.T_Direction == 1 and not na(Log.Price2MoveForTrailing) and SLType == "Point
Based Trail" and Log.SL_Level == 0
if [Link](close, "GE", Log.Price2MoveForTrailing)
Log.T_SLPrice := Log.TrailSL_Val
Log.SL_Level := 1
else if Log.T_Direction == -1 and not na(Log.Price2MoveForTrailing) and SLType ==
"Point Based Trail" and Log.SL_Level == 0
if [Link](close, "LE", Log.Price2MoveForTrailing)
Log.T_SLPrice := Log.TrailSL_Val
Log.SL_Level := 1

CloseCrossUnderTrend = [Link](close,
TrendLevel) and [Link]
CloseCrossOverTrend = [Link](close,
TrendLevel) and [Link]

HasHitTGT = if Log.T_Direction == 1
and not na(Log.T_TGTPrice)
[Link]([Link] and IsRealTime ? close :
[Link] and not IsRealTime and [Link] ? close : not
[Link] ? high : close ,"GE", Log.T_TGTPrice)
else if Log.T_Direction == -1 and not na(Log.T_TGTPrice)
[Link]([Link] and IsRealTime ? close :
[Link] and not IsRealTime and [Link] ? close : not
[Link] ? low : close,"LE", Log.T_TGTPrice)
else
false

HasHitSL = if Log.T_Direction == 1
and not na(Log.T_SLPrice)
[Link]([Link] and IsRealTime ? close :
[Link] and not IsRealTime and [Link] ? close : not
[Link] ? low : close ,"L", Log.T_SLPrice)
else if Log.T_Direction == 1 and not na([Link]) and bar_index >
[Link] and (SLType == "Biased" or LookForBiasExit)
[Link]([Link] and IsRealTime ? close :
[Link] and not IsRealTime and [Link] ? close : not
[Link] ? low : close ,"L", [Link] - 1)
else if Log.T_Direction == -1 and not na(Log.T_SLPrice)
[Link]([Link] and IsRealTime ? close :
[Link] and not IsRealTime and [Link] ? close : not
[Link] ? high : close,"G", Log.T_SLPrice)
else if Log.T_Direction == -1 and not na([Link]) and bar_index >
[Link] and (SLType == "Biased" or LookForBiasExit)
[Link]([Link] and IsRealTime ? close :
[Link] and not IsRealTime and [Link] ? close : not
[Link] ? high : close,"G", [Link] + 1)
else
false
HasHitBias = if Log.T_Direction == 1
and CloseCrossUnderTrend and bar_index > [Link] and (SLType ==
"Biased" or LookForBiasExit)
true
else if Log.T_Direction == -1 and CloseCrossOverTrend and bar_index >
[Link] and (SLType == "Biased" or LookForBiasExit)
true

//#endregion

//#region Peforming Regular Exit of Trades


string FinalExitAlertMessage = ""
if StartAllThingsWhen
if (Log.T_HasExited == 0 and Log.T_HasEntered == 1) and bar_index >=
[Link] and (HasHitSL or HasHitTGT or HasHitBias)
ExitAlertLegsArray = array.new_string(0)
if Log.T_Direction == 1
for [index, value] in ListOfTradingAccount
if FutOrOps == "OPS"
[Link](ExitAlertLegsArray, [Link](true, value,
"MCX", [Link](ListOfOptionsPrefix, index)))
else if FutOrOps == "FUT"
[Link](ExitAlertLegsArray, [Link](false, value,
"MCX", [Link](ListOfFuturePrefix, index), "A", "S"))
FinalExitAlertMessage :=
[Link](ExitAlertLegsArray, "NLB")
if [Link](FinalExitAlertMessage) > 3
alert(FinalExitAlertMessage, alert.freq_once_per_bar)
Log.T_Ex_Date_Time := DateTimeString
Log.T_ExCandle := bar_index
Log.T_ExPrice := if [Link]
close
else if not na([Link]) and SLType == "Biased"
[Link]
else if low < Log.T_SLPrice and SLType != "Biased"
Log.T_SLPrice
else if HasHitTGT
Log.T_TGTPrice
else
close
[Link] := Log.T_ExCandle
Log.T_HasExited := 1
Log.T_HasEntered := 0
Log.T_Direction := 0
PNLCalc = (Log.T_ExPrice - Log.T_EnPrice) *
TradeQty
[Link](Days_PNL, PNLCalc)
[Link](OverAll_PNL, PNLCalc)
[Link](LogSummary, Log.T_En_Date_Time + " | " +
[Link](Log.T_EnPrice, [Link]) + " || " + Log.T_Ex_Date_Time + " | " +
[Link](Log.T_ExPrice, [Link]) + " || PNL : " +
[Link](PNLCalc,[Link]))
Last_TradePNL := PNLCalc
if PNLCalc > 0
[Link] += 1
if not na([Link])
[Link] := na
float yHigh = high
for x = 0 to (bar_index - Log.T_EnCandle)
yHigh := [Link](yHigh, high[x])
[Link] := yHigh - Log.T_EnPrice
[Link](MaxTradeROI_Array, math.round_to_mintick([Link]))

else if Log.T_Direction == -1
for [index, value] in ListOfTradingAccount
if FutOrOps == "OPS"
[Link](ExitAlertLegsArray, [Link](true, value,
"MCX", [Link](ListOfOptionsPrefix, index)))
else if FutOrOps == "FUT"
[Link](ExitAlertLegsArray, [Link](false, value,
"MCX", [Link](ListOfFuturePrefix, index), "A", "S"))
FinalExitAlertMessage :=
[Link](ExitAlertLegsArray, "NLB")
if [Link](FinalExitAlertMessage) > 3
alert(FinalExitAlertMessage, alert.freq_once_per_bar)
Log.T_Ex_Date_Time := DateTimeString
Log.T_ExCandle := bar_index
Log.T_ExPrice := if [Link]
close
else if not na([Link]) and SLType == "Biased"
[Link]
else if high > Log.T_SLPrice and SLType != "Biased"
Log.T_SLPrice
else if HasHitTGT
Log.T_TGTPrice
else
close
[Link] := Log.T_ExCandle
Log.T_HasExited := 1
Log.T_HasEntered := 0
Log.T_Direction := 0
PNLCalc = (Log.T_EnPrice - Log.T_ExPrice) *
TradeQty
[Link](Days_PNL, PNLCalc)
[Link](OverAll_PNL, PNLCalc)
[Link](LogSummary, Log.T_En_Date_Time + " | " +
[Link](Log.T_EnPrice, [Link]) + " || " + Log.T_Ex_Date_Time + " | " +
[Link](Log.T_ExPrice, [Link]) + " || PNL : " +
[Link](PNLCalc,[Link]))
Last_TradePNL := PNLCalc
if PNLCalc > 0
[Link] += 1
if not na([Link])
[Link] := na
float yLow = low
for x = 0 to (bar_index - Log.T_EnCandle)
yLow := [Link](yLow, low[x])
[Link] := Log.T_EnPrice - yLow
[Link](MaxTradeROI_Array, math.round_to_mintick([Link]))
else
ExitAlertString = array.new_string(0)
if Log.T_HasExited == 0 and Log.T_HasEntered == 1 and Log.T_Direction != 0
for [index, value] in ListOfTradingAccount
[Link](ExitAlertString, [Link](true, value, "MCX",
[Link](ListOfOptionsPrefix, index)))
[Link](ExitAlertString, [Link](true, value, "MCX",
[Link](ListOfFuturePrefix, index)))
AlertMessage =
[Link](ExitAlertString, "NLB")
alert(AlertMessage, alert.freq_once_per_bar)
Log.T_Ex_Date_Time := DateTimeString
Log.T_ExCandle := bar_index
Log.T_ExPrice := [Link] ? close :
ohlc4
[Link] := bar_index + 100
PNLCalc = if Log.T_Direction == 1
(Log.T_ExPrice - Log.T_EnPrice) * TradeQty
else if Log.T_Direction == -1
(Log.T_EnPrice - Log.T_ExPrice) * TradeQty
float y = Log.T_Direction == 1 ? high : low
for x = 0 to (bar_index - Log.T_EnCandle)
if Log.T_Direction == -1
y := [Link](y, low[x])
else
y := [Link](y, high[x])
[Link] := Log.T_Direction == -1 ?
Log.T_EnPrice - y : y - Log.T_EnPrice
[Link](MaxTradeROI_Array, math.round_to_mintick([Link]))
Log.T_Direction := 0
[Link](Days_PNL, PNLCalc)
[Link](OverAll_PNL, PNLCalc)
[Link](LogSummary, Log.T_En_Date_Time + " | " +
[Link](Log.T_EnPrice, [Link]) + " || " + Log.T_Ex_Date_Time + " | " +
[Link](Log.T_ExPrice, [Link]) + " || PNL : " +
[Link](PNLCalc,[Link]))
Last_TradePNL := PNLCalc
if PNLCalc > 0
[Link] += 1
[Link] := na
[Link] := na
Log.T_HasExited := 1
Log.T_HasEntered := 0
//#endregion

//#region Trade Entry Exit Plot


if [Link](Log.T_HasEntered) and Log.T_Direction == -1 and Log.T_HasEntered == 1
SellShape := true
if [Link](Log.T_HasEntered) and Log.T_Direction == 1 and Log.T_HasEntered == 1
BuyShape := true
if [Link](Log.T_HasExited) and Log.T_Direction == 0 and Log.T_HasExited == 1
ExitShape := true
//#endregion

plotshape(BuyShape, "Long Trade Entry", [Link],


[Link], [Link], 0, size = [Link], display = [Link] -
display.price_scale - display.status_line, text = "LE", textcolor = [Link])
plotshape(SellShape , "Short Trade Entry", [Link],
[Link], [Link], 0, size = [Link], display = [Link] -
display.price_scale - display.status_line, text = "SE", textcolor = [Link])
plotshape(ExitShape , "Trade Exit", [Link],
[Link], [Link], 0, size = [Link], display = [Link] -
display.price_scale - display.status_line, text = "EX", textcolor = [Link])

if [Link] and ShowTradeLogTable


for x = 0 to 3
switch x
0 => table.cell_set_text(TradeInfoTable, 1, x,
[Link]([Link]))
1 =>
table.cell_set_text(TradeInfoTable, 1, x,
[Link]([Link]))
table.cell_set_bgcolor(TradeInfoTable, 1, x, [Link] >
0 ? [Link](25, 56, 26) : [Link](109, 42, 42))
table.cell_set_text_color(TradeInfoTable, 1, x, [Link])
2 =>
table.cell_set_text(TradeInfoTable, 1, x,
[Link]([Link]([Link] / [Link], 2) * 100) + "%")
table.cell_set_bgcolor(TradeInfoTable, 1, x, [Link] >
0 ? [Link](25, 56, 26) : [Link](109, 42, 42))
table.cell_set_text_color(TradeInfoTable, 1, x, [Link])
3 =>
table.cell_set_text(TradeInfoTable, 1, x,
[Link]([Link]([Link](MaxTradeROI_Array),2)))
table.cell_set_text_color(TradeInfoTable, 1, x, [Link])

You might also like