How can I access the % Drawdown on a continuing bar-by-bar basis to set a timeout after the Drawdown reaches a set level?
Rename
You'd use CurrentEquity, and calculate a drawdown by using a local variable in the Execute to track the highest equity. The current drawdown is CurrentEquity - equity high.
Example:
Example:
CODE:
using WealthLab.Backtest; using System; using WealthLab.Core; using WealthLab.Data; using WealthLab.Indicators; using System.Collections.Generic; namespace WealthScript1 { public class MyStrategy : UserStrategyBase { //create indicators and other objects here, this is executed prior to the main trading loop public override void Initialize(BarHistory bars) { rsi4 = RSI.Series(bars.Close, 4); PlotIndicator(rsi4); dd = new TimeSeries(bars.DateTimes); PlotTimeSeries(dd, "Drawdown", "DD", WLColor.Red, PlotStyle.Mountain); } //execute the strategy rules here, this is executed once for each bar in the backtest history public override void Execute(BarHistory bars, int idx) { if (CurrentEquity > equityHigh) equityHigh = CurrentEquity; double drawdown = CurrentEquity - equityHigh; dd[idx] = drawdown; if (!HasOpenPosition(bars, PositionType.Long)) { //code your buy conditions here if (rsi4[idx] < 40) PlaceTrade(bars, TransactionType.Buy, OrderType.Market, 0); } else { //code your sell conditions here if (rsi4[idx] > 60) PlaceTrade(bars, TransactionType.Sell, OrderType.Market, 0); } } //declare private variables below private RSI rsi4; private TimeSeries dd; private double equityHigh = Double.MinValue; } }
Thanks Glitch! Just what I needed.
One question...is CurrentEquity calculated after each potential trade for each equity or at the end of the bar?
One question...is CurrentEquity calculated after each potential trade for each equity or at the end of the bar?
At the end of each bar.
Your Response
Post
Edit Post
Login is required