Intraday Mean Reversion Money Performance  indicator
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © RandomWalker

// The diagram shows Money Performance when buying stocks for 10.000 at every nuy signal
// The rules is: Buy on Open price if the Mean reversion indicator gives a buy signal. Sell on the daily close price.

// According to my knowledge it is not possible to create a strategy based on these rules. Therefore this indicator can be used to analyze Money performance of this strategy

// The lines show the performance of the intraday mean reversion strategy, based on the different levels in the strategy (from 0.5 Standard deviation to 1.1 standard deviation)

//@version=5
indicator("Intraday Mean Reversal Money Performance", overlay=false, precision=2)

showProfit = input.bool(title="Show Profit", defval=true)
showPercentage = input.bool(title="Show Percentage", defval=true)


var int nrOfBars = 0

var float[] multArray = array.from(0.5, 0.6, 0.7, 0.8, 0.9, 1.0, 1.1)

var int[] antalArray = array.from(0,0,0,0,0,0,0)    // Nr of trades
var int[] antalPlusArray = array.from(0,0,0,0,0,0,0)
var int[] antalMinusArray = array.from(0,0,0,0,0,0,0)


var float[] totalMoneyArray =array.from(0.0,0.0,0.0,0.0,0.0,0.0,0.0)

var table infoTable = table.new(position.bottom_left, 5, 8, bgcolor=color.rgb(255,0,255,70))
var table tradesTable = table.new(position.bottom_center, 2,3, bgcolor=color.rgb(255,255,0,70))

var chartStartTime = time

// Make one label to show the chart's date range
var rangeLabel = label.new(x=na, y=na, style=label.style_label_left, color=color.black, textcolor=color.white, size=size.normal)

var float mp1=0.0
var float mp2=0.0
var float mp3=0.0
var float mp4=0.0
var float mp5=0.0
var float mp6=0.0
var float mp7=0.0


nrOfBars:=nrOfBars+1


float std=ta.stdev(close,90) 

for i=0 to array.size(multArray)-1
    out=array.get(multArray,i)*std 
    
    diff=close[1]-open
    if (diff>=out and bar_index>90)
        cProfit=close-open
        if(cProfit>0)
            cAP=array.get(antalPlusArray,i)
            cAP:=cAP+1
            array.set(antalPlusArray,i,cAP)
        else
            cAM=array.get(antalMinusArray,i)
            cAM:=cAM+1
            array.set(antalMinusArray,i,cAM)

        cA=array.get(antalArray,i)
        cA:=cA+1
        array.set(antalArray,i,cA)
        
        moneyProfit=10000*cProfit/open
        cMP=array.get(totalMoneyArray,i)
        array.set(totalMoneyArray,i,cMP+moneyProfit)



mp1:=array.get(totalMoneyArray,0)
mp2:=array.get(totalMoneyArray,1)
mp3:=array.get(totalMoneyArray,2)
mp4:=array.get(totalMoneyArray,3)
mp5:=array.get(totalMoneyArray,4)
mp6:=array.get(totalMoneyArray,5)
mp7:=array.get(totalMoneyArray,6)
plot(mp1,color=color.rgb(255,0,255), title="Money Profit 0.5", style=plot.style_stepline_diamond)
plot(mp2,color=color.rgb(215,0,185), title="Money Profit 0.6", style=plot.style_stepline_diamond)
plot(mp3,color=color.rgb(185,0,150), title="Money Profit 0.7", style=plot.style_stepline_diamond)
plot(mp4,color=color.rgb(155,0,115), title="Money Profit 0.8", style=plot.style_stepline_diamond)
plot(mp5,color=color.rgb(125,0,80), title="Money Profit 0.9", style=plot.style_stepline_diamond)
plot(mp6,color=color.rgb(95,0,45), title="Money Profit 1.0", style=plot.style_stepline_diamond)
plot(mp7,color=color.rgb(95,0,45), title="Money Profit 1.1", style=plot.style_stepline_diamond)



if barstate.islast
    // We only populate the table on the last bar.
    table.cell(infoTable, 0, 0, "Type", text_color=color.white)
    table.cell(infoTable, 0, 1, "0.5: ", text_color=color.green)
    table.cell(infoTable, 0, 2, "0.6: ", text_color=color.green)
    table.cell(infoTable, 0, 3, "0.7: ", text_color=color.green) 
    table.cell(infoTable, 0, 4, "0.8: ", text_color=color.green)
    table.cell(infoTable, 0, 5, "0.9: ", text_color=color.green)
    table.cell(infoTable, 0, 6, "1.0: ", text_color=color.green)
    table.cell(infoTable, 0, 7, "1.1: ", text_color=color.green)
    
    table.cell(infoTable, 1, 0, "Plus", text_color=color.white)
    table.cell(infoTable, 1, 1, str.tostring(array.get(antalPlusArray,0)), text_color=color.green)
    table.cell(infoTable, 1, 2, str.tostring(array.get(antalPlusArray,1)), text_color=color.green)
    table.cell(infoTable, 1, 3, str.tostring(array.get(antalPlusArray,2)), text_color=color.green) 
    table.cell(infoTable, 1, 4, str.tostring(array.get(antalPlusArray,3)), text_color=color.green)
    table.cell(infoTable, 1, 5, str.tostring(array.get(antalPlusArray,4)), text_color=color.green)
    table.cell(infoTable, 1, 6, str.tostring(array.get(antalPlusArray,5)), text_color=color.green)
    table.cell(infoTable, 1, 7, str.tostring(array.get(antalPlusArray,6)), text_color=color.green)
    
    table.cell(infoTable, 2, 0, "Minus", text_color=color.white)
    table.cell(infoTable, 2, 1, str.tostring(array.get(antalMinusArray,0)), text_color=color.green)
    table.cell(infoTable, 2, 2, str.tostring(array.get(antalMinusArray,1)), text_color=color.green)
    table.cell(infoTable, 2, 3, str.tostring(array.get(antalMinusArray,2)), text_color=color.green) 
    table.cell(infoTable, 2, 4, str.tostring(array.get(antalMinusArray,3)), text_color=color.green)
    table.cell(infoTable, 2, 5, str.tostring(array.get(antalMinusArray,4)), text_color=color.green)
    table.cell(infoTable, 2, 6, str.tostring(array.get(antalMinusArray,5)), text_color=color.green)
    table.cell(infoTable, 2, 7, str.tostring(array.get(antalMinusArray,6)), text_color=color.green)
    
    table.cell(infoTable, 3, 0, "Total", text_color=color.white)
    table.cell(infoTable, 3, 1, str.tostring(array.get(antalArray,0)), text_color=color.green)
    table.cell(infoTable, 3, 2, str.tostring(array.get(antalArray,1)), text_color=color.green)
    table.cell(infoTable, 3, 3, str.tostring(array.get(antalArray,2)), text_color=color.green) 
    table.cell(infoTable, 3, 4, str.tostring(array.get(antalArray,3)), text_color=color.green)
    table.cell(infoTable, 3, 5, str.tostring(array.get(antalArray,4)), text_color=color.green)
    table.cell(infoTable, 3, 6, str.tostring(array.get(antalArray,5)), text_color=color.green)
    table.cell(infoTable, 3, 7, str.tostring(array.get(antalArray,6)), text_color=color.green)
  
    table.cell(infoTable, 4, 0, "Ratio", text_color=color.white)
    table.cell(infoTable, 4, 1, str.tostring(array.get(antalPlusArray,0)/array.get(antalArray,0),'#.##'), text_color=color.green)
    table.cell(infoTable, 4, 2, str.tostring(array.get(antalPlusArray,1)/array.get(antalArray,1),'#.##'), text_color=color.green)
    table.cell(infoTable, 4, 3, str.tostring(array.get(antalPlusArray,2)/array.get(antalArray,2),'#.##'), text_color=color.green) 
    table.cell(infoTable, 4, 4, str.tostring(array.get(antalPlusArray,3)/array.get(antalArray,3),'#.##'), text_color=color.green)
    table.cell(infoTable, 4, 5, str.tostring(array.get(antalPlusArray,4)/array.get(antalArray,4),'#.##'), text_color=color.green)
    table.cell(infoTable, 4, 6, str.tostring(array.get(antalPlusArray,5)/array.get(antalArray,5),'#.##'), text_color=color.green)
    table.cell(infoTable, 4, 7, str.tostring(array.get(antalPlusArray,6)/array.get(antalArray,6),'#.##'), text_color=color.green)
    

    
    labelText = "Ticker:  " + syminfo.ticker +"\n" +
         "Start:  "+str.format("{0,date,YYYY-MM-dd}", chartStartTime)+"\n" + 
         "End:  " + str.format("{0,date,YYYY-MM-dd}", time) + "\n" +
         "Nr of bars:  " + str.tostring(nrOfBars) 
    
    label.set_textalign(rangeLabel, text.align_left)
    
    label.set_xy(id=rangeLabel, x=bar_index + 1, y=array.max(totalMoneyArray))
    label.set_text(id=rangeLabel, text=labelText)
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
