The code in this post contains a set of default strategy inputs I use in new projects / backtests in Tradingview. As you develop more scripts, I am sure you will notice that there are features/functions you add time and time again. I am no different in that regard. There are certain things I will always want in my strategy, either for exploration (such as testing the effect of various stops) or to limit the strategy in some way (such as setting a date range).
Default Strategy Inputs
The complete code is below. You can also find a copy of on Backtest-Rookies’ Tradingview profile here:
https://www.tradingview.com/script/lkHZqpCT-Default-Strategy-Inputs-Forex-Crypto/
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
//@version=3 strategy("Default Strategy Inputs", overlay=false) /////////////////////////////////////////////////////////////////////////////// // Date:2017.11.13 // Author: Backtest-Rookies.com // Version: 1.0 // Description: A Series of default strategy inputs /////////////////////////////////////////////////////////////////////////////// // Notes: // - This template is better suited to instruments that trade 24/5 or 24/7 (crypto) // as trading sessions can be selected. ////////////////////////////////////////////////////////////////////////////// // Create General Strategy Inputs dir_inp = input(defval="Both", title='Trade Direction', options=["Long Only","Short Only","Both"]) st_yr_inp = input(defval=2017, title='Backtest Start Year', type=integer) st_mn_inp = input(defval=01, title='Backtest Start Month', type=integer) st_dy_inp = input(defval=01, title='Backtest Start Day', type=integer) en_yr_inp = input(defval=2025, title='Backtest End Year', type=integer) en_mn_inp = input(defval=01, title='Backtest End Month', type=integer) en_dy_inp = input(defval=01, title='Backtest End Day', type=integer) // Default Stop Types fstp = input(defval=true, title="Fixed Perc stop") fper = input(defval=0.1, title='Percentage for fixed stop', type=float) atsp = input(defval=false, title="ATR Based stop") atrl = input(defval=7, title='ATR Length for stop') atrm = input(defval=1, title='ATR Multiplier for stop') // Sessions asa_inp = input(defval=true, title="Trade the Asian Session") eur_inp = input(defval=true, title="Trade the European Session") usa_inp = input(defval=true, title="Trade the US session") ses_cls = input(defval=true, title="End of Session Close Out?") // Session Start / End times (In exchange TZ = UTC-5) asa_ses = "1700-0300" eur_ses = "0200-1200" usa_ses = "0800-1700" in_asa = time(period, asa_ses) in_eur = time(period, eur_ses) in_usa = time(period, usa_ses) // Long / Short Logic dir = dir_inp == "Both" ? strategy.direction.all : dir_inp == "Short Only" ? strategy.direction.short : strategy.direction.long strategy.risk.allow_entry_in(dir) // Set start and end dates for backtest start = timestamp(st_yr_inp, st_mn_inp, st_dy_inp,00,00) end = timestamp(en_yr_inp, en_mn_inp, en_dy_inp,00,00) // Check if we are in a sessions we want to trade can_trade = asa_inp and not na(in_asa) ? true : eur_inp and not na(in_eur) ? true : usa_inp and not na(in_usa) ? true : false // atr calc for stop atr = atr(atrl) atr_stp_dst = atr * atrm // --------------------------------------------------------- // // // // INSERT STRATEGY CODE // // // // --------------------------------------------------------- // // // Notes: // 1 - To use the backtest start and end time you must include // time >= start and time <= end in your entry checks // // 2 - To use the session input functions you must include the // can_trade check in your entry conditions // // 3 - End of session supported but requires that that close_all // call below is not removed from the strategy. // // --------------------------------------------------------- // long_condition = false // can_trade, time >= start etc... short_condition = false if (long_condition) strategy.entry("Long Entry", strategy.long) if strategy.position_size <= 0 // Less than as in both direction strat - Could be long before switching if atsp atr_stop = open - atr_stp_dst strategy.exit('ATR Fixed Short Stop', "Long Entry", stop=atr_stop) if fstp stop = open - (open * fper) strategy.exit('Perc Fixed Long Stop Exit', "Long Entry", stop=stop) if (short_condition) strategy.entry("Short Entry",strategy.short) if strategy.position_size >= 0 // Greater than as in both direction strat - Could be long before switching if atsp atr_stop = open + atr_stp_dst strategy.exit('ATR Fixed Short Stop Exit', "Short Entry", stop=atr_stop) if fstp stop = open + (open * fper) strategy.exit('Perc Fixed Short Stop Exit', "Short Entry", stop=stop) strategy.close_all(when=not can_trade and ses_cls) // Testing & Debug Plots //plot(can_trade ? 1 : 0, color=purple, linewidth=2) //plot(na(in_asa) ? 0 : 1, color=red) //plot(na(in_eur) ? 0 : 1, color=blue) //plot(na(in_usa) ? 0 : 1, color=green) |
Code Commentary
Once you load the script and add it to the chart, you will see that there are inputs for:
- Trade Direction: So that you can limit the strategy for long only, short only or trade in both directions. It is important to note that when you select “Long Only”, you will still see Short signals on the chart. However, they are only used to close a position rather than reverse it. This is the default behaviour for strategies. The same applies to “Short Only”.
- Date Ranges: So that you can isolate backtesting to specific periods of interest such as bull or bear markets.
- Sessions: So you can easily get an idea of the expected results during your own session. You may also notice that performance of the strategy varies depending on which session it is deployed in.
- Some example stop losses: It is not an exhaustive list but it should be enough to provide some inspiration for different types of stops that you can experiment with.
When adding your strategy code, you will need to editlong_condition
andshort_condition
to meet your criterea. In addition to this, you also need to include checks for time and session using the can_trade
, start
andend
variables.
Screenshot
If you copy and paste the code, then place it on your charts, you will be able to see the inputs as follows.
I hope this can help you save some time on the mundane stuff when testing a new strategy. If you find it useful, and expand on it, please share your results with the community!
hi, is there any way to set these options side by side instead of having one long scroll? i.e. in 2 columns or 3 columns?