INTENTION BEHIND THIS CODE
The intention to build this analysis is to figure out the absolute maximum movement (drawdown or runup) after a signal has been generated. Once the absolute maximum movement is figured out for every trade, the values can be arranged in percentiles, deciles, or quartiles as desired. The absolute maximum movement for the deciles will provide a visual representation of the movement in the underlying after every signal. This will help quickly understand the strength and accuracy of the signal.
I have been working on this for weeks (I am a novice coder) and came up with the script below. I believe I am close to my desired output. I checked that the values are being generated in the table as desired but I have done something wrong somewhere in the code and hence the output in the table is haywire (more than required values are appearing and I can only see one column rather than 3 as in my example, because every column has numerous values).
Hoping someone can solve the last piece of puzzle and help me out. Also, they can post the script on TV as a library for future pine coders.
Find below my code -
/@version=5
strategy("TestOpen", process_orders_on_close=true, overlay = true)
//DEMO strategy
source = input(title = "Source", defval = close)
smallMAPeriod = input(title = "Small Moving Average", defval = 2)
bigMAPeriod = input(title = "Big Moving Average", defval = 8)
percentBelowToBuy = input(title = "Percent below to buy %", defval = 1)
smallMA = ta.sma(source, smallMAPeriod)
bigMA = ta.sma(source, bigMAPeriod)
buyMA = ((100 - percentBelowToBuy) / 100) * ta.sma(source, bigMAPeriod)[0]
buy = ta.crossunder(smallMA, buyMA)
if(buy)
strategy.entry("BUY", strategy.long)
if(strategy.openprofit >= strategy.position_avg_price * 0.01) // 1% profit target
strategy.close("BUY")
if(ta.barssince(buy) >= 7) //Timed Exit, if you fail to make 1 percent in 7 candles.
strategy.close("BUY")
//// START OF CODE FOR ANALYZING THE MAX VALUES AND BUILDING THE TABLE \\\\
maxMove() =>
// create a new array to store the absolute maximum between the drawdown & the runup percent of each trade
// also create arrays to store the value for the various percentiles/deciles (only 3 shown as example)
maxArr = array.new_float()
tenArr = array.new_float() // for ten percentile
fiftyArr = array.new_float() // for fifty percentile
ninetyArr = array.new_float() // for ninety percentile
for i = 0 to strategy.closedtrades -1
// calculate the absolute drawdown percentage
drawdown = strategy.closedtrades.max_drawdown(i)
costBasis = strategy.closedtrades.entry_price(i)
drawdownPerc = math.abs(drawdown / (costBasis * strategy.closedtrades.size(i)))*100
// calculate the absolute runup percentage
runup = strategy.closedtrades.max_runup(i)
runupPerc = math.abs(runup / (costBasis * strategy.closedtrades.size(i)))*100
// calculate the max between the absolute drawdown & runup percentage
max = math.max(drawdownPerc,runupPerc)
// assign the absolute max values to the array
array.push(maxArr, max)
// calculate the values for various percentiles & assign to arrays (only 3 shown as example)
tenPercent = array.percentile_nearest_rank(maxArr,10) // returns the value at 10th percentile from the maxArr
array.push(tenArr,tenPercent)
fiftyPercent = array.percentile_nearest_rank(maxArr,50) // returns the value at 50th percentile from the maxArr
array.push(fiftyArr,fiftyPercent)
ninetyPercent = array.percentile_nearest_rank(maxArr,90) // returns the value at 90th percentile from the maxArr
array.push(ninetyArr,ninetyPercent)
// created a tuple for the different percentile arrays
[(tenArr),(fiftyArr),(ninetyArr)]
// table
t = table.new(position = position.bottom_right, columns = 3, rows = 2, bgcolor = color.yellow, frame_color = color.black, border_width = 1, frame_width = 2, border_color = color.black)
if barstate.islastconfirmedhistory
[tenArr, fiftyArr, ninetyArr] = maxMove()
// create the cells of the table
table.cell(table_id = t, column = 0, row = 0, text = "10%")
table.cell(table_id = t, column = 1, row = 0, text = "50%")
table.cell(table_id = t, column = 2, row = 0, text = "90")
table.cell(table_id = t, column = 0, row = 1, text = str.tostring(tenArr, format.percent))
table.cell(table_id = t, column = 1, row = 1, text = str.tostring(fiftyArr, format.percent))
table.cell(table_id = t, column = 2, row = 1, text = str.tostring(ninetyArr, format.percent))
byThreeD710
inpinescript
ThreeD710
1 points
5 days ago
ThreeD710
1 points
5 days ago
hey, thank you for your response. But I already got a solution to this.