I received lot of request to publish better version of Mean Reversion V-F so here you go
In V.1 WE going in Long trade after MA falling # of bars
The # of bars can be adjust from the settings
New calculation for deviations - after the first trade calculation start from close of the bar and so on
The first trade is market trade the rest are limit trades so can get to it in any time if the price
drop to that deviations level
Can be use for crypto and stocks
For stocks just enable Use for Stocks and input # of stocks
The strategy gave my lot better results and less drown down
If any question let my know
Happy trading
![]()
// This Pine Script® code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © fullmax
//@version=6
strategy('Mean Reversion V-F V.1', overlay = true)
//input variables
STK = input.bool(defval = false, title = 'Use for Stocks- how many Stocks in units level')
//// settings
deviation = input.float(title = 'Deviation Increment (%)', defval = 3, minval = 0.01, maxval = 100,step=0.1) / 100
deviation1 = input.float(title = 'Deviation Increment1 (%)', defval = 5, minval = 0.01, maxval = 100,step = 0.1) / 100
deviation2 = input.float(title = 'Deviation Increment2 (%)', defval = 7, minval = 0.01, maxval = 100,step = 0.1) / 100
deviation3 = input.float(title = 'Deviation Increment3 (%)', defval = 9, minval = 0.01, maxval = 100,step = 0.1) / 100
deviation4 = input.float(title = 'Deviation Increment4 (%)', defval = 13, minval = 0.01, maxval = 100,step=0.1) / 100
deviation5 = input.float(title = 'Deviation Increment5 (%)', defval = 17, minval = 0.01, maxval = 100,step=0.1) / 100
deviation6 = input.float(title = 'Deviation Increment6 (%)', defval = 25, minval = 0.01, maxval = 100,step=0.1) / 100
deviation7 = input.float(title = 'Deviation Increment7 (%)', defval = 31, minval = 0.01, maxval = 100,step=0.1) / 100
unitsLevel = input.float(title = 'First trade (units in cash)', defval = 50, maxval = 10000)
unitsLevel1 = input.float(title = 'Level 1 (units in cash)', defval = 100, maxval = 10000)
unitsLevel2 = input.float(title = 'Level 2 (units in cash)', defval = 200, maxval = 10000)
unitsLevel3 = input.float(title = 'Level 3 (units in cash)', defval = 400, maxval = 10000)
unitsLevel4 = input.float(title = 'Level 4 (units in cash)', defval = 600, maxval = 10000)
unitsLevel5 = input.float(title = 'Level 5 (units in cash)', defval = 800, maxval = 10000)
unitsLevel6 = input.float(title = 'Level 6 (units in cash)', defval = 1000, maxval = 10000)
unitsLevel7 = input.float(title = 'Level 7 (units in cash)', defval = 1000, maxval = 10000)
//moving average
string maType = input.string("WMA", "MA type", options = ["WMA", "SMA", "RMA", "EMA" , "HMA"])
int maLength = input.int(20, "MA length", minval = 2)
float ma = switch maType
"EMA" => ta.ema(close, maLength)
"SMA" => ta.sma(close, maLength)
"RMA" => ta.rma(close, maLength)
"WMA" => ta.wma(close, maLength)
"HMA" => ta.hma(close, maLength)
=>
runtime.error("No matching MA type found.")
float(na)
//falling ma for the first trade
fm= input(5,"# of bars MA to fall to fet in the first trade")
/// buy signal
rdma = ta.falling(ma,fm)
/////////// set static levels
float s1 = 0.00
s1 := na(s1[1]) ? na : s1[1]
s2 = 0.00
s2 := na(s2[1]) ? na : s2[1]
s3 = 0.00
s3 := na(s3[1]) ? na : s3[1]
s4 = 0.00
s4 := na(s4[1]) ? na : s4[1]
s5 = 0.00
s5 := na(s5[1]) ? na : s5[1]
s6 = 0.00
s6 := na(s6[1]) ? na : s6[1]
s7 = 0.00
s7 := na(s6[1]) ? na : s7[1]
////
//// take Profit
take_profit1 = input.float(1.67, title = 'Target Take Profit (%)', step = 0.01, minval = 0.0) / 100
take_profit_level1 = strategy.position_avg_price * (1 + take_profit1)
plot(take_profit_level1, style = plot.style_linebr, linewidth = 2, color = color.green)
plot(strategy.position_avg_price, style = plot.style_linebr, linewidth = 2, color = color.black)
/////brake even plot
fee = input.float(0.1,step= 0.1,title = 'exchange fee')/100
long_order_fee = strategy.position_avg_price * (1 + fee)
plot(long_order_fee, style = plot.style_linebr, linewidth = 2, color = strategy.position_size > 0 ? color.rgb(63, 69, 54, 84) : na)
/// trailing take profit % or hull
takeProfitTrailingEnabled = input.bool(defval = false, title = 'Enable Trailing', tooltip = 'Enable or disable the trailing for take profit. WARNING! This feature will repaint. Make sure you use it with "Bar Magnifier" and "Deep Backtesting" for realistic backtest results')
trailingTakeProfitDistancePerc = input.float(defval = 1.0, title = ' Trailing Distance %', minval = 0.01, maxval = 100, step = 0.01, tooltip = 'The distance as a percentage of the take profit price to keep from the high price after the target is reached when trailing.') / 100
// LOGIC
longTrailingTakeProfitStepTicks = take_profit_level1 * trailingTakeProfitDistancePerc / syminfo.mintick
////// # of trades and take profit
nt = str.tostring(strategy.opentrades)
if strategy.position_size > 0
strategy.exit('B', limit = takeProfitTrailingEnabled ? na : take_profit_level1, trail_price = takeProfitTrailingEnabled ? take_profit_level1 : na, trail_offset = takeProfitTrailingEnabled ? longTrailingTakeProfitStepTicks : na)
//mode
if rdma and strategy.opentrades == 0
strategy.entry('B' + nt, strategy.long, STK ? unitsLevel : unitsLevel/close)
s1 := close*(1 - deviation1)
if strategy.opentrades == 1
strategy.entry('B'+ nt, strategy.long, STK ? unitsLevel1 : unitsLevel1/close,limit = s1)
s2 := close*(1 - deviation2)
if strategy.opentrades == 2 and strategy.opentrades > 1
strategy.entry('B'+ nt, strategy.long, STK ? unitsLevel2 : unitsLevel2/close,limit = s2)
s3 := close*(1 - deviation3)
if strategy.opentrades == 3 and strategy.opentrades > 2
strategy.entry('B'+ nt, strategy.long, STK ? unitsLevel3 : unitsLevel3/close,limit = s3 )
s4 := close*(1 - deviation4)
if strategy.opentrades == 4 and strategy.opentrades > 3
strategy.entry('B'+ nt, strategy.long, STK ? unitsLevel4 : unitsLevel4/close,limit = s4)
s5 := close*(1 - deviation5)
if strategy.opentrades == 5 and strategy.opentrades > 4
strategy.entry('B'+ nt, strategy.long, STK ? unitsLevel5 : unitsLevel5/close,limit = s5)
s6 :=close*(1 - deviation6)
if strategy.opentrades == 6 and strategy.opentrades > 5
strategy.entry('B'+ nt, strategy.long, STK ? unitsLevel6 : unitsLevel6/close,limit = s6)
s7 := close*(1 - deviation7)
if strategy.opentrades == 7 and strategy.opentrades > 6
strategy.entry('B'+ nt, strategy.long, STK ? unitsLevel7 : unitsLevel7/close,limit = s7)
//plot
maupdn = ma > ma[1] ? #0aff68 : ma < ma[1] ? #ff0a0a : na
plot(ma, color = maupdn, linewidth = 3)
// plot statics level's
st1 = plot(strategy.opentrades > 0 ? s1 : na, title = 'Long 2 SLayer', style = plot.style_linebr,color = color.gray)
st2 = plot(strategy.opentrades > 1 ? s2 : na, title = 'Long 3 SLayer', style = plot.style_linebr, color = color.yellow)
st3 = plot(strategy.opentrades > 2 ? s3 : na, title = 'Long 4 SLayer', style = plot.style_linebr,color = color.orange)
st4 = plot(strategy.opentrades > 3 ? s4 : na, title = 'Long 5 SLayer', style = plot.style_linebr, color = color.lime)
st5 = plot(strategy.opentrades > 4 ? s5 : na, title = 'Long 6 SLayer', style = plot.style_linebr, color = color.green)
st6 = plot(strategy.opentrades > 5 ? s6 : na, title = 'Long 7 SLayer', style = plot.style_linebr, color = color.blue)
st7 = plot(strategy.opentrades > 6 ? s7 : na, title = 'Long 7 SLayer', style = plot.style_linebr, color = color.red)
//////////