- 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.
8
721
12 Replies

Reply

Bookmark

Sort
Glitch8
 ( 10.97% )
- 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.97% )
- ago
#4
Yes, here's an example of that using RSI:

1
Glitch8
 ( 10.97% )
- 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.97% )
- 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.97% )
- 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
- ago
#10
Hi.

I tested a simple strategy to verify how it would work in blocks, but I have a doubt.




I did as mentioned above:

An entry when the close is above the ATRtrail and also crosses the SMA200 with exit either 3x ATR20 or stop with close below the ATRtrail.

I repeated the entry blocks and only kept the exit at the stop below the ATRtrail.

In my mind, this would result in two entries, one with a stop only and another that had the same stop but could also exit at a target - in other words, partial exit.



Apparently it worked for the first few entries.

But then...



The parameters screen below:



Does anyone understand what I'm doing wrong or what I can change to make it work properly?
0
- ago
#11
I've just understood why.

It's distributing the entries over multiple assets, because I'm testing on a dataset. If it's on a single asset, I don't have this problem.

Does anyone know how I could link the two entries in each asset so that they always come in together?
0
Cone8
 ( 5.68% )
- ago
#12
There's no guarantee of that since even with only one symbol buying power can be insufficient for both trades. But if you assign the same, deterministic Transaction Weight (there's a block for that), then you'll probably come close to what you want to see.
1

Reply

Bookmark

Sort