- ago
Is it possible in WL7 to set position control in the form of "Max Percent Risk", as it was in WL6 - using the function "RiskStopLevel"?
7
1,727
Solved
21 Replies

Closed

Bookmark

Sort
Glitch8
 ( 12.08% )
- ago
#1
No, it's not available yet. We're trying to think of a less confusing way to introduce that kind of position sizing. People would often get confused because it required you call that specific method in your code.
0
- ago
#2
Hello!

In WL6, I test strategies using two methods:

1. Max Percent Risk, hereinafter referred to as MPR.



2. PosSizer: Winning Streaks. MPR method.



In WL8, he also intends to test strategies using MPR.

I open Position Size and I don't find the MPR method.



I also open the Winning / Losing Streaks method and also do not find the MPR method.



Ask:

Add to WL8 in Position Size the MPR test method.

Add to WL8 in Winning/Losing Streaks the MPR test method.
0
- ago
#3
Denis, here's the new home of your question. You'll find it answered (see Glitch's reply in Post #1). I've removed the duplicate topic.
0
- ago
#4
Also you can override AutoStopLossPrice and AutoProfitTargetPrice in your strategy. These are later additions (after Post #1):

https://www.wealth-lab.com/Support/ApiReference/UserStrategyBase
0
Glitch8
 ( 12.08% )
- ago
#5
I think we should open this up as a #FeatureRequest if we don’t already have one.
0
- ago
#6
@Glitch: done.
0
Glitch8
 ( 12.08% )
- ago
#7
Thanks, despite the somewhat clumsy nature of needing to place something in the code to make it work, maybe we can create a block solution as well that would inject that code.
1
- ago
#8
You are talking about calling a specific method, but I never found this method in the documentation. Perhaps I should do it manually, setting the quantity property and AutoStopLossPrice (not sure) based on Stoploss and current Equity? Please specify when exactly will you add a way to control this from the user interface? Maybe there is a similar PosSizer that I can set up to work like an MPR?
0
- ago
#9
Sorry but you cannot make any PosSizer to use Max Percent Risk because it is not supported in WL8 altogether. Topic is tagged #FR so once it raises to the top this feature is considered for development.
0
- ago
#10
I got it, but maybe there are similar strategies in PosSizer? And how to do it through code, can you give an example? Am I right in thinking that I should be doing this manually, setting the quantity property and AutoStopLossPrice (not sure) based on the desired Stoploss level and current Equity?
0
- ago
#11
But have you clicked on "Example Code" near AutoStopLossPrice in the linked article in Post #4?
0
- ago
#12
Yes, as far as I understand, this property allows you to set the StopLoss level, but in order to manage the risk as a percentage of the current Equity, I also need to set the value of the Quantity property for Position. I want to make a fixed StopLoss, for example, 100 pips of the price, and Quantity should be calculated based on StopLoss, maximum risk percentage, 5% for example, and current Equity, am I right? The example just shows how to set StopLoss, as I understand it.
0
Cone8
 ( 28.25% )
- ago
#13
This MaxRiskSizing method should work. Here it is with Sample usage - (untested for Futures Mode, but should work there too)

The strategy checks that it's above the slow moving average, which it uses for the nominal stop loss at entry. Later, the stop is changed to the highest value of that SMA.

CODE:
using WealthLab.Backtest; using System; using WealthLab.Core; using WealthLab.Indicators; using System.Collections.Generic; namespace WealthScript2 { public class MaxRiskExample : UserStrategyBase {       public MaxRiskExample()       {          AddParameter("Max Risk %", ParameterType.Double, 1.0, 0.5, 3, 0.5);       } //create indicators and other objects here, this is executed prior to the main trading loop public override void Initialize(BarHistory bars) {          StartIndex = 50;          _sma1 = SMA.Series(bars.Close, 10);          _sma2 = SMA.Series(bars.Close, 50); // we'll use this as the stop loss for a long trade          PlotIndicator(_sma1, WLColor.Green);          PlotIndicator(_sma2, WLColor.Red);                    _pctRisk = Parameters[0].AsDouble;          PlotStopsAndLimits(3); } //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 (!HasOpenPosition(bars, PositionType.Long))          {             if (bars.Close.CrossesOver(_sma1, idx) && bars.Close[idx] > _sma2[idx])             {                Transaction t = PlaceTrade(bars, TransactionType.Buy, OrderType.Market);                ApplyMaxRiskSizing(bars, t, bars.Close[idx], _sma2[idx], _pctRisk, CurrentEquity);                            } } else {             Position p = LastPosition;             if (idx - p.EntryBar + 1 > 30)                ClosePosition(p, OrderType.Market, 0, "Timeout");             else             {                //stop loss will be the highest value of the slow MA since the entry bar                double stopLoss = Highest.Value(idx, _sma2, idx - p.EntryBar + 1);                ClosePosition(p, OrderType.Stop, stopLoss, "SL");                ClosePosition(p, OrderType.Limit, p.EntryPrice * 1.03, "PTgt");             } } }       //basisPrice should be the Close for a market order or the stop/limit trigger price for entry       void ApplyMaxRiskSizing(BarHistory bars, Transaction t, double basisPrice, double initializeStopLoss, double pctRisk, double currentEquity, int roundLots = 1)       {                   double pointValue = bars.SymbolInfo?.PointValue != null ? bars.SymbolInfo.PointValue : 1;          t.AutoStopLossPrice = initializeStopLoss;          int sign = t.TransactionType == TransactionType.Short ? -1 : 1;          double shareSize = (pctRisk / 100d * currentEquity) / (sign * pointValue * (basisPrice - t.AutoStopLossPrice));                    //Assign the quantity for the round lots specified (or not specified)          t.Quantity = Math.Floor(shareSize / roundLots) * roundLots;                 }       //declare private variables below       SMA _sma1;       SMA _sma2;       double _pctRisk; } }
2
Best Answer
- ago
#14
Thank you. The answer received is exhaustive.
0
Cone8
 ( 28.25% )
- ago
#15
It occurred to me that since we pass the transaction to the sizing method, we can make all the necessary assignments for AutoStopLoss and Quantity there. This encapsulates the method better. I edited the code above.
0
- ago
#16
I too am attempting to include maximum percent risk sizing in my WL7 codes. It is disappointing this feature is not available (yet) in the drop and drag strategy builder.
Question: how do i convert this code to WL7?

Below is a simple code from WL6
Buy rules are :52 week closing high is higher than it was 52 weeks ago
Stock price anywhere between 10 cents and $5000
Sell rule: trailing ATR 3, calculated over 4 weeks


Also please note the 2nd last line of code: RiskStopLevel = Close[bar] - ATR.Value(bar, Bars, 4) * 3;
that is the line I added so the maximum percent risk function will work on WL6

CODE:
using System; using System.Collections.Generic; using System.Text; using System.Drawing; using WealthLab; using WealthLab.Indicators; namespace WealthLab.Strategies {    public class MyStrategy4 : WealthScript    {              protected override void Execute()       {          PlotStops();          for(int bar = GetTradingLoopStartBar(53); bar < Bars.Count; bar++)          {             if (IsLastPositionActive)             {                Position p = LastPosition;                if (p.EntrySignal.Contains("Group1|"))                {                   double high = p.HighestHighAsOfBar(bar);                   double amount = high - ( ATR.Series( Bars, 4 )[bar] * 3 );                   SellAtTrailingStop(bar + 1, p, amount, "Group1");                }             }             else             {                if (Close[bar] > 0.10)                {                   if (Close[bar] < 5000)                   {                      if (Close[bar] > Close[bar - 52])                      {                         RiskStopLevel = Close[bar] - ATR.Value(bar, Bars, 4) * 3;                         BuyAtMarket(bar + 1, "Group1|");                      }                   }                }             }          }       }    } }
0
- ago
#17
This is not much different from the % Volatility position sizing. You could accomplish this with the Percent Volatility position sizer from the PowerPack extension if you configure the Position Size to "Advanced Pos Sizer" as shown below:

https://www.wealth-lab.com/extension/detail/PowerPack



The Building Blocks have a "Sell at ATR limit or stop" - choose "Trailing stop" there:

0
Glitch8
 ( 12.08% )
- ago
#18
I'm activating the item for Build 11 development. I haven't thought of a better way to achieve it than having to assign a value to a new Transaction "RiskStopValue" property.
1
Glitch8
 ( 12.08% )
- ago
#19
I'm thinking one way of improving things is establishing some default for the RiskStop level, so if people forgot to set it, or don't know they need to, some value that makes sense will still be in play. Maybe it can default to a 10-bar low, which can be configured in Trading Preferences.

Another idea, should the Backtester automatically submit a stop loss exit order when this position sizing mode is used?
0
- ago
#20
About it,
QUOTE:
Maybe it can default to a 10-bar low, which can be configured in Trading Preferences.

the 10-bar minimum would be default, but could we use other indicators as well? (for example, a SMA, or an EMA).

About this,
QUOTE:
Backtester automatically submit a stop loss exit order when this position sizing mode is used

Would it be an exit order based on the last value of the indicator above? With the share quantities already determined by the PosSizing method?

If so, it sounds great!
0
Glitch8
 ( 12.08% )
- ago
#21
Yes, these will be the Preferences for Max Risk. I think it's a step forward to provide some reasonable defaults.
2

Closed

Bookmark

Sort