0% found this document useful (0 votes)
13 views12 pages

ZBSKVFRT

The document contains a trading strategy script named 'Nikitha ORB Strategy v2.2' designed for use on the TradingView platform. It includes various parameters for trade settings, target settings, stop loss settings, and trade modes, allowing users to customize their trading approach. The script also implements logic for entry and exit conditions based on market data, aiming to automate trading decisions within specified timeframes.

Uploaded by

sonwalkaranurag6
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)
13 views12 pages

ZBSKVFRT

The document contains a trading strategy script named 'Nikitha ORB Strategy v2.2' designed for use on the TradingView platform. It includes various parameters for trade settings, target settings, stop loss settings, and trade modes, allowing users to customize their trading approach. The script also implements logic for entry and exit conditions based on market data, aiming to automate trading decisions within specified timeframes.

Uploaded by

sonwalkaranurag6
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
You are on page 1/ 12

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

0 at
https://mozilla.org/MPL/2.0/
// © GrusTech

//@version=5
strategy("Nikitha ORB Strategy v2.2", shorttitle="Nikitha ORB Strategy v2.2",
overlay=true, default_qty_type=strategy.fixed, default_qty_value=1,
initial_capital=300000, currency=currency.NONE, commission_value=0,
commission_type=strategy.commission.percent, process_orders_on_close=false,
calc_on_every_tick=true, calc_on_order_fills=false)

////======================================================
paraRefTF = input.timeframe("5", "Reference Timeframe", group = "Trade Settings")

paraFCHLMode = true //input.bool(true, "First Candle HighLow Mode?", inline = "T1",


group = "Trade Settings")

paraSetupValid = input.string(defval="AllDay", title="Signal Setup Validation",


options=["AllDay", "NextCandleOnly"], inline = "T5", group = "Trade Settings")

paraEntryBuffer = input.float(0, "Entry Buffer (pts)", minval = 0, inline = "R1",


group = "Trade Settings")

paraReEntryValid = input.string(defval="NextCandleOnly", title="Re-Entry


Validation", options=["NextSetup", "NextCandleOnly"], inline = "R3", group = "Trade
Settings")
paraRELossMax = input.int(2, "Max RE-Entry Loss Count", minval = 0, inline = "R3",
group = "Trade Settings")

paraTGTMode = input.string(defval="Pts", title="Target : ", options=["Off", "%",


"Pts"], inline = "TGT", group = "Target Settings")
paraTGT1 = input.float(15, "T1 : ", minval = 0, inline = "TGT", group = "Target
Settings")
paraTGT2 = input.float(25, "T2 : ", minval = 0, inline = "TGT", group = "Target
Settings")
paraTGT3 = input.float(35, "T3 : ", minval = 0, inline = "TGT", group = "Target
Settings")
paraTGT = input.float(60, "T4 : ", minval = 0, inline = "TGT", group = "Target
Settings")
paraTGTTSL = input.bool(true, "Trail SL on TGT", inline = "TGT", group = "Target
Settings")

paraSLMode = input.string(defval="PrevCandle", title="Stoploss : ", options=["Off",


"%", "Pts", "PrevCandle", "EntryCandle"], inline = "SL", group = "Stoploss
Settings")
paraSL = input.float(1, "Value : ", minval = 0.1, inline = "SL", group = "Stoploss
Settings")

paraTSLMode = input.string(defval="Custom", title="Trail SL : ", options=["Off",


"%", "Pts", "Custom"], inline = "TSL", group = "TSL Settings")
paraTSL = input.float(1, "TSL Value : ", minval = 0.1, inline = "TSL", group = "TSL
Settings")
paraCustomTSLMove = input.float(1000, "Custom TSL Move", minval = 0.1, inline =
"CTSL", group = "TSL Settings")
paraCustomTSLTrail = input.float(1000, "Custom TSL Trail", minval = 0.1, inline =
"CTSL", group = "TSL Settings")

paraShowDashboard = input.bool(false, "Show Strategy Dashboard")


////======================================================
////======================================================
paraSystemMode = input.session(defval="Intraday", title="System Mode",
options=["Intraday", "Positional"], group = "System Settings")
s = input.session(title='Intraday Start Session:', defval='0915-1505',
group='System Settings')
e = input.session(title='Intraday End Session:', defval='1510-1515', group='System
Settings')

paraBacktestMode = input.bool(true, "Use Backtesting Mode?", group="Backtest


Settings")
BacktestStart = input.time(timestamp("01 Apr 2023 00:00:00 GMT+530"), title="Start
Date and Time", group="Backtest Settings")
BacktestEnd = input.time(timestamp( "31 Dec 2023 23:59:00 GMT+530"), title="End
Date and Time", group="Backtest Settings")
BacktestCheck = paraBacktestMode ? (time >= BacktestStart and BacktestEnd >=
time) : true

paraTradeMode = input.string(title='Trade Mode', defval='Both', options=['Both',


'LongOnly', 'ShortOnly'], group='AlgoFox Setup:')
paraStag = input.string(title='Strategy Tag', defval='OPT1', group='AlgoFox
Setup:')
paraCode = input.string(title='Code', defval='F1372A7853352U', group='AlgoFox
Setup:')
paraProductType = input.string(title="Product Type",
defval='NRML',options=['MIS','NRML','CNC'], group='AlgoFox Setup:')
paraInstr = input.string(title='Instrument Type',
defval='OPTIDX',options=['EQ','FUTIDX','OPTIDX','FUTSTK','OPTSTK','FUTCOM','FUTCUR'
], group='AlgoFox Setup:')
paraQtyType = input.string(title="Quantity Type",
defval='Fixed',options=['Fixed','Exposure'], group='AlgoFox Setup:') //,'Risk'
paraQty = input.int(title='Quantity ', defval=1, minval=1, group='AlgoFox Setup:',
tooltip='Qty in Lots for Futures')
paraT1Qty = input.float(title='Target-1 Exit Qty (%)', defval=0, minval=0, maxval =
100, group='AlgoFox Setup:', tooltip='Qty in Percentage')
paraT2Qty = input.float(title='Target-2 Exit Qty (%)', defval=0, minval=0, maxval =
100, group='AlgoFox Setup:', tooltip='Qty in Percentage')
paraT3Qty = input.float(title='Target-3 Exit Qty (%)', defval=0, minval=0, maxval =
100, group='AlgoFox Setup:', tooltip='Qty in Percentage')
////======================================================

////======================================================
GetTradeSetup() =>

_FC = session.isfirstbar
_BSFC = ta.barssince(_FC) + 1
_FCH = ta.valuewhen(_FC, high, 0)
_FCL = ta.valuewhen(_FC, low, 0)

[_FC, _BSFC, _FCH, _FCL]

////======================================================

////======================================================
FC = session.isfirstbar
LC = session.islastbar

UpC = close > open


DnC = close < open
[RefFC, RefBSFC, RefFCH, RefFCL] = request.security(syminfo.tickerid, paraRefTF,
GetTradeSetup(), lookahead = barmerge.lookahead_off)

st = paraSystemMode=="Positional" ? 1 : time(timeframe.period, s)
et = paraSystemMode=="Intraday" and time(timeframe.period, e)

RefBarChange = ta.change(RefBSFC)

plotchar(RefBSFC, "RefBSFC", "")

BuyEntryCond = RefBarChange and not FC


ShortEntryCond = RefBarChange and not FC
if (paraSetupValid == "NextCandleOnly")
BuyEntryCond := RefFC
ShortEntryCond := RefFC

plotchar(BuyEntryCond, "BuyEntryCond", "")

eSignal = 0
eBuyPos = 0
eShortPos = 0
eBuy = st and paraTradeMode!="ShortOnly" and BuyEntryCond and barstate.isconfirmed
and BacktestCheck
eShort = st and paraTradeMode!="LongOnly" and ShortEntryCond and
barstate.isconfirmed and BacktestCheck
eSell = et or LC
eCover = et or LC
eSignal := eBuy ? 1 : eShort ? -1 : eSell and eSignal[1] > 0 ? 0 : eCover and
eSignal[1] < 0 ? 0 : eSignal[1]
eBuyPos := eBuy ? 1 : eSell and eSignal[1] > 0 ? 0 : eBuyPos[1]
eShortPos := eShort ? -1 : eCover and eSignal[1] < 0 ? 0 : eShortPos[1]

eBuyHigh = ta.valuewhen(eBuy, RefFCH, 0)


eShortLow = ta.valuewhen(eBuy, RefFCL, 0)
////======================================================

////======================================================
symbol = syminfo.ticker

eBuyPrice = ta.valuewhen(eBuy, eBuyHigh, 0)


eShortPrice = ta.valuewhen(eShort, eShortLow, 0)

LESym = str.tostring(syminfo.ticker)
LXSym = str.tostring(syminfo.ticker)
SESym = str.tostring(syminfo.ticker)
SXSym = str.tostring(syminfo.ticker)

var float BuyTradeQty = na


var float ShortTradeQty = na
var float BuyRisk = na
var float ShortRisk = na

BuyTradeQty := paraQty
ShortTradeQty := paraQty

if (paraQtyType=="Exposure")
BuyTradeQty := paraQty / eBuyPrice
BuyTradeQty := math.round(BuyTradeQty / syminfo.pointvalue)
ShortTradeQty := paraQty / eShortPrice
ShortTradeQty := math.round(ShortTradeQty / syminfo.pointvalue)

if (BuyTradeQty < 0)
BuyTradeQty := 1
if (ShortTradeQty < 0)
ShortTradeQty := 1

buyData = '{"Exchange": "' + str.tostring(syminfo.prefix) + '","Close": ' +


str.tostring(close) + ',"Ticker": "' + LESym + '","OrderType": "BUY","ProductType":
"' + paraProductType + '","InstrumentType": "' + paraInstr + '","Quantity":
'+str.tostring(BuyTradeQty)+', "Strategy": "'+paraStag+'", "Code": "'+paraCode+'"}'
sellData = '{"Exchange": "' + str.tostring(syminfo.prefix) + '","Close": ' +
str.tostring(close) + ',"Ticker": "' + LXSym + '","OrderType":
"SELL","ProductType": "' + paraProductType + '","InstrumentType": "' + paraInstr +
'","Quantity": '+str.tostring(BuyTradeQty)+', "Strategy": "'+paraStag+'", "Code":
"'+paraCode+'"}'
shortData = '{"Exchange": "' + str.tostring(syminfo.prefix) + '","Close": ' +
str.tostring(close) + ',"Ticker": "' + SESym + '","OrderType":
"SHORT","ProductType": "' + paraProductType + '","InstrumentType": "' + paraInstr +
'","Quantity": '+str.tostring(ShortTradeQty)+', "Strategy": "'+paraStag+'", "Code":
"'+paraCode+'"}'
coverData = '{"Exchange": "' + str.tostring(syminfo.prefix) + '","Close": ' +
str.tostring(close) + ',"Ticker": "' + SXSym + '","OrderType":
"COVER","ProductType": "' + paraProductType + '","InstrumentType": "' + paraInstr +
'","Quantity": '+str.tostring(ShortTradeQty)+', "Strategy": "'+paraStag+'", "Code":
"'+paraCode+'"}'
////======================================================

////======================================================
var int BTF = 0
var int STF = 0
var int BREF = 0
var int SREF = 0
var float PosSize = na
var int sellLossCtr = 0
var int coverLossCtr = 0
var bool LongPosChanged = false
var bool ShortPosChanged = false
var bool BuyEntryCandle = false
var bool ShortEntryCandle = false
var float LTFPrevHigh = na
var float LTFPrevLow = na

PosSize := math.abs(strategy.position_size)
LongPosChanged := (strategy.position_size > 0 and strategy.position_size[1] <= 0)
and barstate.isconfirmed
ShortPosChanged := (strategy.position_size < 0 and strategy.position_size[1] >= 0)
and barstate.isconfirmed

plotchar(eBuyHigh, "BuyBreakout", "")


plotchar(eShortLow, "ShortBreakout", "")

if RefBarChange

if (paraReEntryValid == "NextCandleOnly")
if strategy.position_size <= 0
BREF := 0
if strategy.position_size >= 0
SREF := 0

if (paraSetupValid == "NextCandleOnly")
BTF := 0
STF := 0
strategy.cancel("BUY")
strategy.cancel("SHORT")

if (eBuy)
BTF := 1
LTFPrevHigh := high
if strategy.position_size <= 0
BREF := 0
else
BTF := 0

if (eShort)
LTFPrevLow := low
STF := 1
if strategy.position_size >= 0
SREF := 0
else
STF := 0

if ((BTF and ShortPosChanged) or (eBuy)) and st and strategy.position_size < 0 and


barstate.isconfirmed
strategy.entry('BUY', strategy.long, qty=BuyTradeQty, comment="Buy",
stop=math.max(eBuyHigh, LTFPrevHigh), alert_message="["+coverData+","+buyData+"]")
sellLossCtr := 0
else if (eBuy) and st and strategy.position_size >= 0 and barstate.isconfirmed
strategy.entry('BUY', strategy.long, qty=BuyTradeQty, comment="Buy",
stop=math.max(eBuyHigh, LTFPrevHigh), alert_message="["+buyData+"]")
sellLossCtr := 0
else if (eBuyPos <= 0 or et or not st) and barstate.isconfirmed
strategy.cancel("BUY")

if ((STF and LongPosChanged) or (eShort)) and st and strategy.position_size > 0 and


barstate.isconfirmed
strategy.entry('SHORT', strategy.short, qty=ShortTradeQty, comment="Sell",
stop=math.min(eShortLow, LTFPrevLow), alert_message="["+sellData+","+shortData+"]")
coverLossCtr := 0
else if (eShort) and st and strategy.position_size <= 0 and barstate.isconfirmed
strategy.entry('SHORT', strategy.short, qty=ShortTradeQty, comment="Sell",
stop=math.min(eShortLow, LTFPrevLow), alert_message="["+shortData+"]")
coverLossCtr := 0
else if (eShortPos >= 0 or et or not st) and barstate.isconfirmed
strategy.cancel("SHORT")

plotchar(eBuy, "eBuy", "")


plotchar(eShort, "eShort", "")
plotchar(BREF, "BREF", "")
plotchar(SREF, "SREF", "")

if (BREF and not eBuy and not BTF) and PosSize == 0 and eBuyPos > 0 and st and
barstate.isconfirmed
strategy.entry('BUY', strategy.long, qty=BuyTradeQty, comment="Re-Buy",
stop=math.max(eBuyHigh, high[1]), alert_message="["+buyData+"]")
if (SREF and not eShort and not STF) and PosSize == 0 and eShortPos < 0 and st and
barstate.isconfirmed
strategy.entry('SHORT', strategy.short, qty=ShortTradeQty, comment="Re-Sell",
stop=math.min(eShortLow, low[1]), alert_message="["+shortData+"]")

var float BuyPrice = na


var float ShortPrice = na
var float BuyTGT = na
var float ShortTGT = na
var float BuyTGT1 = na
var float ShortTGT1 = na
var float BuyTGT2 = na
var float ShortTGT2 = na
var float BuyTGT3 = na
var float ShortTGT3 = na
var float BuySL = na
var float ShortSL = na
var float BuyTSL = na
var float ShortTSL = na
var float T1ExQty = na
var float T2ExQty = na
var float T3ExQty = na

ut = (paraTGTMode != "Off")
us = (paraSLMode != "Off")

if (strategy.position_size > 0 and strategy.position_size[1] <= 0 and


barstate.isconfirmed)
BuyPrice := strategy.position_avg_price
BuyEntryCandle := false
BTF := 0
BREF := BREF + 1

T1ExQty := math.round(BuyTradeQty*(paraT1Qty/100))
T2ExQty := math.round(BuyTradeQty*(paraT2Qty/100))
T3ExQty := math.round(BuyTradeQty*(paraT3Qty/100))
sellData := '{"Exchange": "' + str.tostring(syminfo.prefix) + '","Close": ' +
str.tostring(close) + ',"Ticker": "' + LXSym + '","OrderType":
"SELL","ProductType": "' + paraProductType + '","InstrumentType": "' + paraInstr +
'","Quantity": '+str.tostring(BuyTradeQty)+', "Strategy": "'+paraStag+'", "Code":
"'+paraCode+'"}'

if (paraSLMode=="%")
BuySL := BuyPrice * (1-(paraSL/100))
else if (paraSLMode=="Pts")
BuySL := BuyPrice - (paraSL)
else if (paraSLMode=="PrevCandle")
BuySL := low[1]
else if (paraSLMode=="EntryCandle")
BuySL := low

BuyRisk := BuyPrice - BuySL

if (paraTGTMode=="%")
BuyTGT1 := BuyPrice * (1+(paraTGT1/100))
BuyTGT2 := BuyPrice * (1+(paraTGT2/100))
BuyTGT3 := BuyPrice * (1+(paraTGT3/100))
BuyTGT := BuyPrice * (1+(paraTGT/100))
else if (paraTGTMode=="Pts")
BuyTGT1 := BuyPrice + (paraTGT1)
BuyTGT2 := BuyPrice + (paraTGT2)
BuyTGT3 := BuyPrice + (paraTGT3)
BuyTGT := BuyPrice + (paraTGT)

if (paraTGT1 == 0)
BuyTGT1 := na
if (paraTGT2 == 0)
BuyTGT2 := na
if (paraTGT3 == 0)
BuyTGT3 := na
else
BuyEntryCandle := false

if (strategy.position_size < 0 and strategy.position_size[1] >= 0 and


barstate.isconfirmed)

ShortPrice := strategy.position_avg_price
ShortEntryCandle := false
STF := 0
SREF := SREF + 1

T1ExQty := math.round(ShortTradeQty*(paraT1Qty/100))
T2ExQty := math.round(ShortTradeQty*(paraT2Qty/100))
T3ExQty := math.round(ShortTradeQty*(paraT3Qty/100))
coverData := '{"Exchange": "' + str.tostring(syminfo.prefix) + '","Close": ' +
str.tostring(close) + ',"Ticker": "' + SXSym + '","OrderType":
"COVER","ProductType": "' + paraProductType + '","InstrumentType": "' + paraInstr +
'","Quantity": '+str.tostring(ShortTradeQty)+', "Strategy": "'+paraStag+'", "Code":
"'+paraCode+'"}'

if (paraSLMode=="%")
ShortSL := ShortPrice * (1+(paraSL/100))
else if (paraSLMode=="Pts")
ShortSL := ShortPrice + (paraSL)
else if (paraSLMode=="PrevCandle")
ShortSL := high[1]
else if (paraSLMode=="EntryCandle")
ShortSL := high

ShortRisk := ShortSL - ShortPrice

if (paraTGTMode=="%")
ShortTGT1 := ShortPrice * (1-(paraTGT1/100))
ShortTGT2 := ShortPrice * (1-(paraTGT2/100))
ShortTGT3 := ShortPrice * (1-(paraTGT3/100))
ShortTGT := ShortPrice * (1-(paraTGT/100))
else if (paraTGTMode=="Pts")
ShortTGT1 := ShortPrice - (paraTGT1)
ShortTGT2 := ShortPrice - (paraTGT2)
ShortTGT3 := ShortPrice - (paraTGT3)
ShortTGT := ShortPrice - (paraTGT)

if (paraTGT1 == 0)
ShortTGT1 := na
if (paraTGT2 == 0)
ShortTGT2 := na
if (paraTGT3 == 0)
ShortTGT3 := na
else
ShortEntryCandle := false
if (strategy.position_size > 0 and strategy.position_size[1] > 0 and
barstate.isconfirmed)

if (paraTSLMode != "Off")

if (paraTSLMode=="%")
BuyTSL := high[1] * (1-(paraTSL/100))
else if (paraTSLMode=="Custom")

tmp = (BuyPrice + paraTSL)


if (high[1] >= tmp and BuySL < BuyPrice)
BuySL := BuyPrice

tmp2 = (BuyPrice + paraCustomTSLMove)


if (high[1] >= tmp2)
BuySL := BuySL + paraCustomTSLTrail

else if (paraTSLMode=="Pts")
BuyTSL := high[1] - paraTSL

if (BuySL < BuyTSL)


BuySL := BuyTSL

if (paraTGTTSL)
if (high[1] >= BuyTGT1 and BuySL < BuyPrice)
BuySL := BuyPrice
if (high[1] >= BuyTGT2 and BuySL < BuyTGT1)
BuySL := BuyTGT1
if (high[1] >= BuyTGT3 and BuySL < BuyTGT2)
BuySL := BuyTGT2

if (strategy.position_size < 0 and strategy.position_size[1] < 0 and


barstate.isconfirmed)

if (paraTSLMode != "Off")

if (paraTSLMode=="%")
ShortTSL := low[1] * (1+(paraTSL/100))
else if (paraTSLMode=="Custom")

tmp = (ShortPrice - paraTSL)


if (low[1] <= tmp and ShortSL > ShortPrice)
ShortSL := ShortPrice

tmp2 = (ShortPrice - paraCustomTSLMove)


if (low[1] <= tmp2)
ShortSL := ShortSL - paraCustomTSLTrail

else if (paraTSLMode=="Pts")
ShortTSL := low[1] + paraTSL

if (ShortSL > ShortTSL)


ShortSL := ShortTSL

if (paraTGTTSL)
if (low[1] <= ShortTGT1 and ShortSL > ShortPrice)
ShortSL := ShortPrice
if (low[1] <= ShortTGT2 and ShortSL > ShortTGT1)
ShortSL := ShortTGT1
if (low[1] <= ShortTGT3 and ShortSL > ShortTGT2)
ShortSL := ShortTGT2

PosSize := math.abs(strategy.position_size)

TP1sellData = '{"Exchange": "' + str.tostring(syminfo.prefix) + '","Close": ' +


str.tostring(close) + ',"Ticker": "' + LXSym + '","OrderType":
"SELL","ProductType": "' + paraProductType + '","InstrumentType": "' + paraInstr +
'","Quantity": '+str.tostring(T1ExQty)+', "Strategy": "'+paraStag+'", "Code":
"'+paraCode+'"}'
TP1coverData = '{"Exchange": "' + str.tostring(syminfo.prefix) + '","Close": ' +
str.tostring(close) + ',"Ticker": "' + SXSym + '","OrderType":
"COVER","ProductType": "' + paraProductType + '","InstrumentType": "' + paraInstr +
'","Quantity": '+str.tostring(T1ExQty)+', "Strategy": "'+paraStag+'", "Code":
"'+paraCode+'"}'
TP2sellData = '{"Exchange": "' + str.tostring(syminfo.prefix) + '","Close": ' +
str.tostring(close) + ',"Ticker": "' + LXSym + '","OrderType":
"SELL","ProductType": "' + paraProductType + '","InstrumentType": "' + paraInstr +
'","Quantity": '+str.tostring(T2ExQty)+', "Strategy": "'+paraStag+'", "Code":
"'+paraCode+'"}'
TP2coverData = '{"Exchange": "' + str.tostring(syminfo.prefix) + '","Close": ' +
str.tostring(close) + ',"Ticker": "' + SXSym + '","OrderType":
"COVER","ProductType": "' + paraProductType + '","InstrumentType": "' + paraInstr +
'","Quantity": '+str.tostring(T2ExQty)+', "Strategy": "'+paraStag+'", "Code":
"'+paraCode+'"}'
TP3sellData = '{"Exchange": "' + str.tostring(syminfo.prefix) + '","Close": ' +
str.tostring(close) + ',"Ticker": "' + LXSym + '","OrderType":
"SELL","ProductType": "' + paraProductType + '","InstrumentType": "' + paraInstr +
'","Quantity": '+str.tostring(T3ExQty)+', "Strategy": "'+paraStag+'", "Code":
"'+paraCode+'"}'
TP3coverData = '{"Exchange": "' + str.tostring(syminfo.prefix) + '","Close": ' +
str.tostring(close) + ',"Ticker": "' + SXSym + '","OrderType":
"COVER","ProductType": "' + paraProductType + '","InstrumentType": "' + paraInstr +
'","Quantity": '+str.tostring(T3ExQty)+', "Strategy": "'+paraStag+'", "Code":
"'+paraCode+'"}'

if ((paraT1Qty > 0 and paraTGT1 > 0) or (paraT2Qty > 0 and paraTGT2 > 0) or
(paraT3Qty > 0 and paraTGT3 > 0))
sellData := '{"Exchange": "' + str.tostring(syminfo.prefix) + '","Close": ' +
str.tostring(close) + ',"Ticker": "' + LXSym + '","OrderType":
"SELL","ProductType": "' + paraProductType + '","InstrumentType": "' + paraInstr +
'","Quantity": '+str.tostring(PosSize)+', "Strategy": "'+paraStag+'", "Code":
"'+paraCode+'"}'
coverData := '{"Exchange": "' + str.tostring(syminfo.prefix) + '","Close": ' +
str.tostring(close) + ',"Ticker": "' + SXSym + '","OrderType":
"COVER","ProductType": "' + paraProductType + '","InstrumentType": "' + paraInstr +
'","Quantity": '+str.tostring(PosSize)+', "Strategy": "'+paraStag+'", "Code":
"'+paraCode+'"}'

if ut == true and us == false and barstate.isconfirmed


if (strategy.position_size > 0)
if (paraT1Qty > 0 and paraTGT1 > 0)
strategy.exit(id="LongT1Exit", from_entry="BUY", qty = T1ExQty,
limit=BuyTGT1, comment="BT1", alert_message="["+TP1sellData+"]", oca_name = "LX1")
if (paraT2Qty > 0 and paraTGT2 > 0)
strategy.exit(id="LongT2Exit", from_entry="BUY", qty = T2ExQty,
limit=BuyTGT2, comment="BT2", alert_message="["+TP2sellData+"]", oca_name = "LX2")
if (paraT3Qty > 0 and paraTGT3 > 0)
strategy.exit(id="LongT3Exit", from_entry="BUY", qty = T3ExQty,
limit=BuyTGT3, comment="BT3", alert_message="["+TP3sellData+"]", oca_name = "LX3")
strategy.exit(id='LongExit', comment="Exit Buy", from_entry='BUY',
limit=BuyTGT, alert_message="["+sellData+"]")
if (strategy.position_size < 0)
if (paraT1Qty > 0 and paraTGT1 > 0)
strategy.exit(id="ShortT1Exit", from_entry="SHORT", qty = T1ExQty,
limit=ShortTGT1, comment="ST1", alert_message="["+TP1coverData+"]", oca_name =
"SX1")
if (paraT2Qty > 0 and paraTGT2 > 0)
strategy.exit(id="ShortT2Exit", from_entry="SHORT", qty = T2ExQty,
limit=ShortTGT2, comment="ST2", alert_message="["+TP2coverData+"]", oca_name =
"SX2")
if (paraT3Qty > 0 and paraTGT3 > 0)
strategy.exit(id="ShortT3Exit", from_entry="SHORT", qty = T3ExQty,
limit=ShortTGT3, comment="ST3", alert_message="["+TP3coverData+"]", oca_name =
"SX3")
strategy.exit(id='ShortExit', comment="Exit Sell", from_entry='SHORT',
limit=ShortTGT, alert_message="["+coverData+"]")
if us == true and ut == false and barstate.isconfirmed
if (strategy.position_size > 0 and not BuyEntryCandle)
strategy.exit(id='LongExit', comment="Exit Buy", from_entry='BUY',
stop=BuySL, alert_message="["+sellData+"]")
if (strategy.position_size < 0 and not ShortEntryCandle)
strategy.exit(id='ShortExit', comment="Exit Sell", from_entry='SHORT',
stop=ShortSL, alert_message="["+coverData+"]")
if ut == true and us == true and barstate.isconfirmed
if (strategy.position_size > 0 and not BuyEntryCandle)
if (paraT1Qty > 0 and paraTGT1 > 0)
strategy.exit(id="LongT1Exit", from_entry="BUY", qty = T1ExQty,
limit=BuyTGT1, stop=BuySL, comment="BT1", alert_message="["+TP1sellData+"]",
oca_name = "LX1")
if (paraT2Qty > 0 and paraTGT2 > 0)
strategy.exit(id="LongT2Exit", from_entry="BUY", qty = T2ExQty,
limit=BuyTGT2, stop=BuySL, comment="BT2", alert_message="["+TP2sellData+"]",
oca_name = "LX2")
if (paraT3Qty > 0 and paraTGT3 > 0)
strategy.exit(id="LongT3Exit", from_entry="BUY", qty = T3ExQty,
limit=BuyTGT3, stop=BuySL, comment="BT3", alert_message="["+TP3sellData+"]",
oca_name = "LX3")
strategy.exit(id='LongExit', comment="Exit Buy", from_entry='BUY',
limit=BuyTGT, stop=BuySL, alert_message="["+sellData+"]")
if (strategy.position_size < 0 and not ShortEntryCandle)
if (paraT1Qty > 0 and paraTGT1 > 0)
strategy.exit(id="ShortT1Exit", from_entry="SHORT", qty = T1ExQty,
limit=ShortTGT1, stop=ShortSL, comment="ST1", alert_message="["+TP1coverData+"]",
oca_name = "SX1")
if (paraT2Qty > 0 and paraTGT2 > 0)
strategy.exit(id="ShortT2Exit", from_entry="SHORT", qty = T2ExQty,
limit=ShortTGT2, stop=ShortSL, comment="ST2", alert_message="["+TP2coverData+"]",
oca_name = "SX2")
if (paraT3Qty > 0 and paraTGT3 > 0)
strategy.exit(id="ShortT3Exit", from_entry="SHORT", qty = T3ExQty,
limit=ShortTGT3, stop=ShortSL, comment="ST3", alert_message="["+TP3coverData+"]",
oca_name = "SX3")
strategy.exit(id='ShortExit', comment="Exit Sell", from_entry='SHORT',
limit=ShortTGT, stop=ShortSL, alert_message="["+coverData+"]")

if ( et ) and strategy.position_size > 0


strategy.cancel('LongExit')
strategy.cancel('LongT1Exit')
strategy.cancel('LongT2Exit')
strategy.cancel('LongT3Exit')
strategy.close(id='BUY', comment="Exit Buy", alert_message="["+sellData+"]")

if ( et ) and strategy.position_size < 0


strategy.cancel('ShortExit')
strategy.cancel('ShortT1Exit')
strategy.cancel('ShortT2Exit')
strategy.cancel('ShortT3Exit')
strategy.close(id='SHORT', comment="Exit Sell",
alert_message="["+coverData+"]")

SellCandle = (strategy.position_size <= 0) and (strategy.position_size[1] > 0) and


barstate.isconfirmed
CoverCandle = (strategy.position_size >= 0) and (strategy.position_size[1] < 0) and
barstate.isconfirmed
lastTradeNo = strategy.closedtrades - 1
lastPNL = strategy.closedtrades.profit(lastTradeNo)
if SellCandle and strategy.closedtrades.entry_comment(lastTradeNo) == "Re-Buy" and
lastPNL < 0
sellLossCtr := sellLossCtr + 1
if CoverCandle and strategy.closedtrades.entry_comment(lastTradeNo) == "Re-Sell"
and lastPNL < 0
coverLossCtr := coverLossCtr + 1

plotchar(sellLossCtr, "sellLossCtr", "")


plotchar(coverLossCtr, "coverLossCtr", "")

if (sellLossCtr >= paraRELossMax)


BREF := 0
if (coverLossCtr >= paraRELossMax)
SREF := 0

if (strategy.position_size <= 0)
strategy.cancel('LongExit')
strategy.cancel('LongT1Exit')
strategy.cancel('LongT2Exit')
strategy.cancel('LongT3Exit')
if (strategy.position_size >= 0)
strategy.cancel('ShortExit')
strategy.cancel('ShortT1Exit')
strategy.cancel('ShortT2Exit')
strategy.cancel('ShortT3Exit')
////======================================================

////======================================================
plotshape(strategy.position_size<=0?eBuy:na, style=shape.diamond ,
location=location.belowbar, color=color.green, size=size.tiny)
plotshape(strategy.position_size>=0?eShort:na, style=shape.diamond,
location=location.abovebar, color=color.red, size=size.tiny)

plot((strategy.position_size > 0)?BuyPrice:na, color=color.fuchsia, linewidth=1,


style=plot.style_linebr)
plot((strategy.position_size > 0)?BuyTGT1:na, color=color.blue, linewidth=1,
style=plot.style_linebr)
plot((strategy.position_size > 0)?BuyTGT2:na, color=color.blue, linewidth=1,
style=plot.style_linebr)
plot((strategy.position_size > 0)?BuyTGT3:na, color=color.blue, linewidth=1,
style=plot.style_linebr)
plot((strategy.position_size > 0)?BuyTGT:na, color=color.blue, linewidth=1,
style=plot.style_linebr)
plot((strategy.position_size > 0)?BuySL:na, color=color.orange, linewidth=1,
style=plot.style_linebr)

plot((strategy.position_size < 0)?ShortPrice:na, color=color.fuchsia, linewidth=1,


style=plot.style_linebr)
plot((strategy.position_size < 0)?ShortTGT1:na, color=color.blue, linewidth=1,
style=plot.style_linebr)
plot((strategy.position_size < 0)?ShortTGT2:na, color=color.blue, linewidth=1,
style=plot.style_linebr)
plot((strategy.position_size < 0)?ShortTGT3:na, color=color.blue, linewidth=1,
style=plot.style_linebr)
plot((strategy.position_size < 0)?ShortTGT:na, color=color.blue, linewidth=1,
style=plot.style_linebr)
plot((strategy.position_size < 0)?ShortSL:na, color=color.orange, linewidth=1,
style=plot.style_linebr)
////======================================================

You might also like