- ago
Is there a WL8 C# class available where I can read out the max. exposure % per bar?
0
409
Solved
7 Replies

Reply

Bookmark

Sort
- ago
#1
No, there isn't. Do you need this metric as the resulting value or on a bar by bar basis?
0
- ago
#2
QUOTE:
Do you need this metric as the resulting value or on a bar by bar basis?


Bar by bar, as shown in the bottom left of the equity curve.


I could of course calculate it by myself since I need equity - cash, but I don't know how to get the cash value via code?
0
Cone8
 ( 24.56% )
- ago
#3
See: QuickRef > Class: Backtester > CurrentCash

Very quick example -
CODE:
public override void Execute(BarHistory bars, int idx) {    if (!HasOpenPosition(bars, PositionType.Long)) { if (Backtester.CurrentCash > 5000)                PlaceTrade(bars, TransactionType.Buy, OrderType.Market); } else { //code your sell conditions here } }
2
- ago
#4
It is not possible/meaningfull to access equity/cash in a WL Strategies code because the sequence of events is different from what you think.

A single symbol is executed completely before the code is run for the next symbol. So you dont get correct equity/cash for all but the last symbol.

Whatever you want to do you should do it in a PositionSizer.
Probably send some info from the strategy to the PosSizer in the position tag.
0
Cone8
 ( 24.56% )
- ago
#5
QUOTE:
It is not possible/meaningfull to access equity/cash in a WL Strategies code because the sequence of events is different from what you think.
With respect to WealthLab versions prior to Version 7, that's correct.
Beginning with Version 7 that statement is incorrect.

Strategies have access to the portfolio's end-of-bar Cash and Equity for the current bar/index. You can access these values from the Backtester properties CurrentCash and CurrentEquity.
3
- ago
#6
Thank you for your assistance. In this case, I'll code the max. exposure by myself.
0
- ago
#7
In return for the quick help, a sample system with the Max Exposure as indicator.

CODE:
using WealthLab.Backtest; using System; using WealthLab.Core; using WealthLab.Indicators; using System.Collections.Generic; namespace WealthScript22 { public class MyStrategy : UserStrategyBase {       private IndicatorBase consDown;       private IndicatorBase consUp;       private TimeSeries stops;       private Transaction _transaction;       private TimeSeries cashCurve;       private TimeSeries equityCurve;       private TimeSeries exposureCurve;              public MyStrategy() : base() {          AddParameter("Entry Consecutive Down", ParameterType.Double, 9, 1, 15, 1);          AddParameter("Exit Consecutive Up", ParameterType.Double, 3, 1, 15, 1);          AddParameter("Exit After N Days", ParameterType.Int32, 17, 1, 20, 1);          AddParameter("% Stop Loss", ParameterType.Int32, 10, 1, 20, 1); // 10 9 7       } public override void Initialize(BarHistory bars) {          consDown = new ConsecDown(bars.Close,4);          consUp = new ConsecUp(bars.Close, 1);          PlotIndicator(consDown,new WLColor(0,0,0));          PlotIndicator(consUp,new WLColor(0,0,255));          cashCurve = new TimeSeries(bars.DateTimes);          equityCurve = new TimeSeries(bars.DateTimes);          exposureCurve = new TimeSeries(bars.DateTimes);          PlotTimeSeries(cashCurve, "Equity-Cash-Curve USD", "Equity-Cash", WLColor.Red, PlotStyle.Line);          PlotTimeSeries(equityCurve, "Equity-Cash-Curve USD", "Equity-Cash", WLColor.DarkGray, PlotStyle.Histogram);          PlotTimeSeries(exposureCurve, "Max Exposure %", "Exposure", WLColor.CornflowerBlue, PlotStyle.Line);          PlotStopsAndLimits(3);          StartIndex = 20; } public override void Execute(BarHistory bars, int idx) {          cashCurve[idx] = Backtester.CurrentCash;          equityCurve[idx] = Backtester.CurrentEquity;          exposureCurve[idx] = (((equityCurve[idx] - cashCurve[idx]) / equityCurve[idx]) * 100);                    int index = idx;          Position posLong = FindOpenPosition(0);          if (posLong == null)          {             if (consDown[index] >= Parameters[0].AsDouble)                _transaction = PlaceTrade(bars, TransactionType.Buy, OrderType.Market, 0, 0, "Enter Long");          }          else          {             if (consUp[index] >= Parameters[1].AsDouble)                if (index - 1 >= 0 && bars.Open[index] > bars.Open[index - 1])                   ClosePosition(posLong, OrderType.Market, 0, "Sell when condition is true");             if (idx - posLong.EntryBar + 1 >= Parameters[2].AsInt)                ClosePosition(posLong, OrderType.Market, 0, "Sell after 10 bars");             ClosePosition(posLong, OrderType.Stop, posLong.EntryPrice * (1.0 - (Parameters[3].AsInt / 100.0)), "Sell at stop loss");          } }        } }


1
Best Answer

Reply

Bookmark

Sort