Mean Reversion Source Code (RealTest + Pine Script)
Welcome to this step-by-step guide on how to build a profitable trading strategy! Whether you're new to trading or looking to refine your approach, this video will walk you through the complete process of developing a robust trading strategy from scratch.
Check out my other free lessons!RealTest Source Code
(Pine Script Code Below)
Import: DataSource: Norgate // Trading universe IncludeList: .Russell 1000 Current & Past IncludeList: $RUI Constituency: $RUI // Benchmark IncludeList: $SPX // Dates StartDate: 1/1/2000 EndDate: Latest SaveAs: russell1000_example.rtd Settings: DataFile: russell1000_example.rtd StartDate: 1/1/2000 EndDate: Latest BarSize: Daily UseAvailableBars: False Currency: USD AccountSize: 50000 Parameters: Stretch: 0.5 Positions: 20 Leverage: 200 Data: // Get indicator values ShortTerm_ATR_Value: ATR(5) LongTerm_ATR_Value: ATR(25) ShortTerm_SMA: MA(Close, 20) LongTerm_SMA: MA(Close, 200) Turnover: MA(Volume * Close, 5) ScoreROC: ROC(Close, 5) // Prepare conditions IsUptrend: Close > LongTerm_SMA IsWeak: Close <= Low[1] IsLiquid: Turnover > 1000000 IsVolatile: ShortTerm_ATR_Value > LongTerm_ATR_Value // Check entry conditions LongScore: 100 - ScoreROC LongLimit: Low - (ShortTerm_ATR_Value * Stretch) LongTrigger: IsWeak and IsVolatile LongSetup: InRUI and LongTrigger and IsUptrend and IsLiquid // Check exit conditions StopLossExit: Close < ShortTerm_SMA Benchmark: Benchmark_SP500 Side: Long Allocation: S.Equity EntrySetup: Symbol=$$SPX Strategy: MeanReversion Side: Long Quantity: Leverage / Positions QtyType: Percent MaxPositions: Positions EntryLimit: LongLimit EntrySetup: LongSetup SetupScore: LongScore ExitRule: Close > FillPrice or StopLossExit // Close > High[1] increases avg drawdown significantly, ROR slightly, and max DD slightly ExitTime: NextOpen Commission: Min(Max(0.005 * Shares, 1.00), Shares * FillPrice * 0.01) MaxExposure: Leverage Charts: // On chart MyEntryMA: {#} MA(Close, 200) MyExitMA: {#} MA(Close, 20)
Pine Script Source Code
// This Pine Script™ code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/ // © ZenAndTheArtOfTrading | theartoftrading.com | Note: this version has leverage enabled; set qty_value to 10 and margin_long to 100 to disable leverage. // @version=5 strategy("MR System Example", overlay=true, margin_long=200, default_qty_type=strategy.percent_of_equity, default_qty_value=20, max_lines_count=500) // Get system parameters float Stretch = input.float(0.5, "Stretch") //int Positions = 20 // not applicable to Pine Script, only RealTest // Get indicator values ShortTerm_ATR_Value = ta.atr(5) LongTerm_ATR_Value = ta.atr(25) ShortTerm_SMA = ta.sma(close, 20) LongTerm_SMA = ta.sma(close, 200) Turnover = ta.sma(volume * close, 5) //ScoreROC = not applicable to Pine Script, only RealTest // Prepare conditions IsUptrend = close > LongTerm_SMA IsWeak = close <= low[1] IsLiquid = Turnover > 1000000 IsVolatile = ShortTerm_ATR_Value > LongTerm_ATR_Value // Check entry conditions //LongScore = not applicable to Pine Script, only RealTest LongLimit = low - (ShortTerm_ATR_Value * Stretch) LongTrigger = IsWeak and IsVolatile LongSetup = LongTrigger and IsUptrend and IsLiquid // Check exit conditions ExitReason = close < ShortTerm_SMA or close > strategy.position_avg_price // Enter trades if (LongSetup) strategy.order("Buy", strategy.long, limit=LongLimit) line.new(bar_index, LongLimit, bar_index + 1, LongLimit, extend=extend.none, color=color.lime) else strategy.cancel_all() // Exit trades if (strategy.position_size != 0 and ExitReason) strategy.close_all() // Draw data plot(ShortTerm_SMA, color=color.blue) plot(LongTerm_SMA, color=color.red) plotshape(LongSetup, style=shape.triangleup, location=location.belowbar, color=color.green)