- ago
Is there a Building Block Partial Exit one can use in a strategy? Say I want to exit 50% of my current position if crosses above 20 SMA and the rest at Crosses above 50 SMA? Or any indicator of my choice. Thank you.
6
496
9 Replies

Reply

Bookmark

Sort
Glitch8
 ( 10.94% )
- ago
#1
Currently no but it’s conceivable we could design a way to do this as a future enhancement. It’s doable in a code based strategy right now.
0
- ago
#2
Thanks, It would be nice. In the meantime Do you have a code sample or snippet I can use as template and applied to my strategy code?
0
- ago
#3
One way to achieve it in Blocks is to have 2 separate entries and 2 separate exits. One exits one and the other exit the second entry?
0
Glitch8
 ( 10.94% )
- ago
#4
Yes, here's an example of that using RSI:

0
Glitch8
 ( 10.94% )
- ago
#5
And here's an example that splits and sells half of the position if only the SMA20 or SMA50 is crossed. If both SMAs are crossed it sells the full position as normal.

CODE:
using WealthLab.Backtest; using System; using WealthLab.Core; 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) {          sma20 = SMA.Series(bars.Close, 20);          sma50 = SMA.Series(bars.Close, 50);          PlotIndicator(sma20, WLColor.Blue);          PlotIndicator(sma50, WLColor.Red); } //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)) {             //code your buy conditions here             if (bars.Close[idx] < sma20[idx] && bars.Close[idx] < sma50[idx] && bars.Close.TurnsUp(idx))             {                PlaceTrade(bars, TransactionType.Buy, OrderType.Market);                sold20 = false;                sold50 = false;             } } else { //code your sell conditions here        if (bars.Close[idx] > sma20[idx] && bars.Close[idx] > sma50[idx])                PlaceTrade(bars, TransactionType.Sell, OrderType.Market);     else if (bars.Close[idx] > sma20[idx] && !sold20)             {                Transaction t = PlaceTrade(bars, TransactionType.Sell, OrderType.Market);                sold20 = true;                if (!sold50)                   t.Quantity /= 2;             }     else if (bars.Close[idx] > sma50[idx] && !sold50)             {                Transaction t = PlaceTrade(bars, TransactionType.Sell, OrderType.Market);                sold50 = true;                if (!sold20)                   t.Quantity /= 2;             } } }       //declare private variables below       private SMA sma20;       private SMA sma50;       private bool sold20 = false;       private bool sold50 = false; } }


0
- ago
#6
Thank you I'll give it a try.
0
Glitch8
 ( 10.94% )
- ago
#7
Since we have a couple of ways of already accomplishing this let's not make the Building Blocks more complex for such an isolated case.
0
- ago
#8
Was this feature implemented in Blocks recently?
0
Glitch8
 ( 10.94% )
- ago
#9
No I decided to decline this one since it’s possible to accomplish with the indicated methods.

I’m concerned about overloading the Blocks with too many options.
2

Reply

Bookmark

Sort