Hi! I need some help, I'm trying to write my own possizer so that one of the optimization parameters is the number of shares in the portfolio. Of course, I can indicate the minimum % of capital for each, but then it is very difficult to compare the results obtained in optimization.
CODE:
public override double SizePosition(Transaction t, BarHistory bars, int idx, double basisPrice, double equity, double cash) { double size = 0; int curPosNumber = Candidates.Count; if (Candidates.Count == 0) { } else { if (equity == cash) { size = Math.Truncate(equity / basisPrice / Candidates.Count); } else { size = Math.Truncate(((equity - cash) * 0.99) / basisPrice / Candidates.Count); } } return size; }
Rename
How to make sure that all capital is used? I can increase the margin Factor, but it is important for me in optimization that all parameters can be compared with each other...
It might be because you are trying to take more trades than available simulated capital. In this case the trades can change every time you run the strategy because WL8 will randomly select them from the a available candidates.
Glitch, Thx for your answer!
Maybe you have some idea on how to make sure that all capital is used?
Maybe you have some idea on how to make sure that all capital is used?
How is equity calculated? In my opinion, it should be like this: this is the cache + the amount of open positions (how much the position is worth as of the current date.). It seems to me that equity is calculated only after the position is closed. In the case of daily testing, it seems to me that this is incorrect..
The equity is calculated and updated at the end of each bar, not when the position is closed.
As far as the zero Candidates count, when the Count is zero the backtester is making an initial call to the Position Sizer to set some internal information. You'll get another call at a later point with the Candidates populated. That call is the one that will actually set the quantity of the transaction. Sorry for the confusion about that!
As far as the zero Candidates count, when the Count is zero the backtester is making an initial call to the Position Sizer to set some internal information. You'll get another call at a later point with the Candidates populated. That call is the one that will actually set the quantity of the transaction. Sorry for the confusion about that!
We have a Position Sizer in the Power Pack called Spread Equity that does this, I'll open up the source for that as a learning example.
CODE:
using System; using WealthLab.Backtest; using WealthLab.Core; using WealthLab.Indicators; namespace WealthLab.PowerPack { public class EqualExposure : PositionSizerBase { //Name public override string Name => "Spread Equity Equally"; //description public override string Description => "Equalizes the exposure of a system that generates a variable number of alerts per bar to spread equity equally, taking as many positions as possible."; //employs the basis price logic (ie, if it uses the standard position sizing call). This determines whether the basis price combo box is visible when you select that PositionSizer. public override bool UsesBasisPrice => true; //initialize public override void Initialize() { MinPosSizePct = Parameters[0].AsDouble; MaxPosSizePct = Parameters[1].AsDouble; if (MaxPosSizePct > 100) MaxPosSizePct = 100; if (MinPosSizePct < 0) MinPosSizePct = Math.Abs(this.MinPosSizePct); } //size the position public override double SizePosition(Transaction t, BarHistory bars, int idx, double basisPrice, double equity, double cash) { double size = 0; double cap = equity * (Math.Abs(MaxPosSizePct) / 100d); double min = equity * (Math.Abs(MinPosSizePct) / 100d); if (Candidates.Count > 0) { size = equity / Candidates.Count; size = Math.Max((double)size, min); size = Math.Min((double)size, cap); size /= basisPrice; } return size; } //private members private double MinPosSizePct = 1d; private double MaxPosSizePct = 20d; //generate our parameters public override void GenerateParameters() { Parameters.Add(new Parameter("Min % Equity size", ParameterType.Double, 1.0, 0.1, 100.0)); Parameters.Add(new Parameter("Max % Equity size", ParameterType.Double, 20.0, 1.0, 100.0)); } } }
Your Response
Post
Edit Post
Login is required