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

// The Intraday Mean Reversion Indicator works well on certain stocks. It should be used for day trading stocks but IMPORTANT! must be applied to the Day to Day timeframe.
// The logic behind the indicator is that stocks that opens substantially lower than yesterdays close, very often bounces back during the day and closes higher than the open price, thus the name Intraday Mean reversdal. The stock so to speak, reverses against the mean.
// The indicator shows 7 levels to choose from: 
// 0.5 * standard deviation
// 0.6 * standard deviation
// 0.7 * standard deviation
// 0.8 * standard deviation 
// 0.9 * standard deviation 
// 1.0 * standard deviation 
// 1.1 * standard deviation 

// The script can easily be modified to test other levels as well.
//
//The info box shows the performance of one of these levels. 
// The Multiplier shows which multiplier is chosen, the sum shows the profit following the strategy if ONE stock is bought on every buy signal. The Ratio shows the ratio between winning and losing trades if we followed the strategy historically.
// We want to find stocks that have a high ratio. That is More Ups than downs. A ration ovcer 0.5 is good, but of course we want a margin of safety so, 0,75 is a better choice.
// If we find a stock that meets our criteria then the strategy will be to buy as early as possible on the open, and sell as close as possible on the close!
// The yellow bars in the graph are days where the indicator gives a buy signal according to the chosen multiplier.
//
// To help study which stocks it performs well against there is also an indicator called Intraday Mean Reversal Money Performance which indicates the performance of the strategy to different levels of open levels.

//@version=5
indicator("Intraday Mean Reversion Main", overlay=false)

multiplier = input.float(title="Multiplier", defval=1.0)

var histcol=color.red
var float summa=0
var float percentage=1.00000


float profit=0

var int plus=0
var int minus=0

var float ratio=0

var table infoTable = table.new(position.top_center, 2, 2, bgcolor=color.white)


out1=0.5*ta.stdev(close,90)
out2=0.6*ta.stdev(close,90)
out3=0.7*ta.stdev(close,90)
out4=0.8*ta.stdev(close,90)
out5=0.9*ta.stdev(close,90)
out6=1.0*ta.stdev(close,90)
out7=1.1*ta.stdev(close,90)




diff=close[1]-open
if (diff>=out1 and bar_index>90)
    profit:=close-open
  
    percentage:=percentage*(1+(profit/open))
    
    if(profit>0)
        plus:=plus+1
    else if(profit<0)
        minus:=minus+1
    ratio:=plus/(plus+minus)
    histcol:=color.yellow
else
    profit:=0
    histcol:=color.red


summa:=summa+profit


plot(request.security(syminfo.tickerid,'D',out1), color=color.rgb(0,0,255), title="Stdev 0.5")
plot(request.security(syminfo.tickerid,'D',out2), color=color.rgb(20,0,235), title="Stdev 0.6")
plot(request.security(syminfo.tickerid,'D',out3), color=color.rgb(40,0,215), title="Stdev 0.7")
plot(request.security(syminfo.tickerid,'D',out4), color=color.rgb(60,0,195), title="Stdev 0.8")
plot(request.security(syminfo.tickerid,'D',out5), color=color.rgb(80,0,175), title="Stdev 0.9")
plot(request.security(syminfo.tickerid,'D',out6), color=color.rgb(100,0,155), title="Stdev 1.0")
plot(request.security(syminfo.tickerid,'D',out7), color=color.rgb(120,0,135), title="Stdev 1.1")




plot(diff,color=histcol,style=plot.style_histogram, linewidth=2, title="Diff")


if barstate.islast
    table.cell(infoTable, 0, 0, "Multiplier: "+str.tostring(multiplier), text_color=color.green)
    table.cell(infoTable, 1, 0, "Sum: "+str.tostring(summa), text_color=color.green)
    table.cell(infoTable,0, 1, "Ratio: "+str.tostring(ratio), text_color=color.green)
    table.cell(infoTable,1,1, "Ups: "+str.tostring(plus)+" Downs: "+str.tostring(minus), text_color=color.green)
