- ago
My use case:
I trade one strategy on minute bars throughout a day. My dataset varies, it usually holds between 40 and 100 stocks.

I set single position to 14 percent, so I do hold 7 positions max without leverage.

Stop loss for a single stock ist set to 3%. Target rules are present.

My question:
Is it possible to implement a combined stop loss for the strategy?
I would like to: exit all trades at market, and terminate the strategy for this day, if overall combined loss is 2 percent.

I assume, that I can't do that with building blocks.

Can it be done (even better: is it documented anywhere)?

I have the (weak) confidence to program this myself, but I am not very experienced with WL7 and C#, so every hint to point me in the right direction is welcome.
0
629
Solved
4 Replies

Reply

Bookmark

Sort
- ago
#1
QUOTE:
I would like to: exit all trades at market, and terminate the strategy for this day, if overall combined loss is 2 percent.

In live trading you can accomplish this (though on account basis) with Trading Preferences > Trading Thresholds by setting "Account BP/Cash below..." a Dollar value and adjust it every day (week etc.)

@Glitch, does "Overall loss < N%" sound like a potential criteria for inclusion in Trading Preferences > Trading Thresholds?

QUOTE:
I assume, that I can't do that with building blocks.

Hmm I don't see how what's known as "crash stop loss" or "portfolio stop loss" can be implemented with Blocks.
0
- ago
#2
Hi Eugene,

thank you for your reply.

Your suggestion could act as a workaround, but I hold other positions (mid-term and long-term) in my account, and they should not interfere with the calculations for the crash stop loss.

And when trading multiple strategies at the same time, I do not want to cancel all strategies.

Are there perhaps some more tips on which approaches or classes could ideally serve as a starting point? Is there access to the equity curve within a strategy or should I build that myself with custom variables?

To my understanding, the Class UserStrategyBase is the place to look at. I found the method AverageEntryPrice(), should I start here (with my own calculations to sum above all entries)?

Can the property CurrentEquity be used in live mode? The documentation says: The property 'Returns the current equity level in the simulation.'

Are there other classes that could help?
0
- ago
#3
Hi,
QUOTE:
Can the property CurrentEquity be used in live mode?

We do not have any sample code for a portfolio-based crash stop loss in live mode. Sounds like a possible candidate for a future Wealth-Lab blog post.

You can get its value on the complete bar (i.e. idx = bars.Count-1) which lets you backtest the idea. Here's some sample code to help you in your own coding:

https://www.wealth-lab.com/Discussion/7208
https://www.wealth-lab.com/Discussion/7217
https://www.wealth-lab.com/Discussion/6268

CODE:
using WealthLab.Backtest; using System; using WealthLab.Core; using WealthLab.Indicators; using System.Drawing; using System.Collections.Generic; namespace WealthScript1 { public class MyStrategy : UserStrategyBase {       TimeSeries equity;       TimeSeries equitySMA;       //Configure your usual position size as % of equity (say 5% per position):       double percentEquity = 5;       public override void Initialize(BarHistory bars)       {          equity = new TimeSeries(bars.DateTimes);          equitySMA = new TimeSeries(bars.DateTimes);          PlotTimeSeries(equity, "Equity", "Equity", Color.Green, PlotStyles.ThickHistogram);          PlotTimeSeries(equitySMA, "SMA(Equity)", "Equity", Color.Black, PlotStyles.Line);       }       public override void Execute(BarHistory bars, int idx)       {          equity[idx] = CurrentEquity;          equitySMA[idx] = SMA.Value(idx, equity, 10);          //Calculate the usual size in shares based on the close price          double c = bars.Close[idx];          var usualSize = (int)(CurrentEquity / c) / 100 * percentEquity;          //Trade twice the size if equity is above its moving average          var pctEquity = (equity[idx] > equitySMA[idx]) ? usualSize * 2 : usualSize;          if (!HasOpenPosition(bars, PositionType.Long))          {             var stop = Highest.Series(bars.High, 20)[idx];             PlaceTrade(bars, TransactionType.Buy, OrderType.Stop, stop).Quantity = pctEquity;             //As alternative...             //var t = PlaceTrade(bars, TransactionType.Buy, OrderType.Stop, stop);             //t.Quantity = pctEquity;          }          else          {             var stop = Lowest.Series(bars.Low, 20)[idx];             PlaceTrade(bars, TransactionType.Sell, OrderType.Stop, stop);          }       } //declare private variables below } }
1
Best Answer
- ago
#4
Hi,
thank you very much for the precise answer.
Looks like I can use it as a base for my usecase very well.
1

Reply

Bookmark

Sort