Main strategy function, written by the user. It is automatically started after clicking on the [Train], [Test], and [Trade] buttons. After
the first run, it runs again at the end of every bar. This is continued until the end of the
simulation or until [Stop] is hit. The run function initializes sliders and sets up all needed indicators.
- If StartDate is set to NOW,
an extra run is executed in live trading mode immediately after the lookback period, regardless whether the current bar has ended.
If LookBack is 0, this extra run is the
first run. After that extra run, the run function is furtherhin executed at the end of every bar.
- For executing the run function several times within a candle, make the BarPeriod shorter and set up the candle width with TimeFrame so that several bars lie within a candle.
Alternatively, use a tick function.
- At the first call of the run function, is(INITRUN) and is(FIRSTINITRUN) are true.
Since price and bar data are not yet set up at that initial run before the first asset call, all price functions and indicators return 0.
- If the script has both a main and a run function, the main function is executed before the first run, and can be used for initializing variables.
- Local variables 'forget' their values after every call of the run function, but static and global variables keep their values. !! When AutoCompile is set, static and global variables really keep their values until the script is compiled again, which happens only when it was modified or when the [Edit] button was clicked. Therefore make sure to initialize them at every cycle start (is(INITRUN)) to their initial values when this is required.
- For debugging the run function, use the STEPWISE flag and watch commands. For starting debugging after the lookback period, use if(!is(LOOKBACK)) set(STEPWISE);.
- For immediate script reaction on an incoming price quote, use the tick function. For repeated actions independent on bars or price quotes, use the tock function.
vars SmoothPrice = series(LowPass(series(price()),100));
Workshop 4, Bars and Candles, Bar, BarPeriod, tick, tock, user supplied functions