FLX8
- ago
If I was in a position where that position has gained/lossed X% of its value, is there a way to start cutting that position?

For instance:
bought $100 worth of Random Stock at $10/Share. That position is now worth 50%, so $50, I want to sell half the shares I hold.

Is this possible?
0
739
Solved
5 Replies

Reply

Bookmark

Sort
- ago
#1
It is possible to accomplish in C# code using Transaction.Quantity:
https://www.wealth-lab.com/Discussion/WealthScript-SplitPosition-5952
0
FLX8
- ago
#2
Alright, this makes sense. But how would you go about making the script aware of its position starting value and its current value?
0
- ago
#3
Sure, the Position object has such properties as ProfitPctOfAs, ValueAsOf etc. You can learn more in the QuickRef.
0
Cone8
 ( 4.98% )
- ago
#4
Maybe this is the example that you were looking for. You can define the symbol, shares, price, and date you entered a position and then trade the position with a script. Here, we enter 100 shares of "U" at 100.20 using OrderType.FixedPrice and sell half the shares after a 50% loss, on a closing basis.

CODE:
using WealthLab.Backtest; using System; using WealthLab.Core; using WealthLab.Indicators; using System.Collections.Generic; namespace WealthScript123 { public class LoadAndSellPositionDemo : UserStrategyBase { //create indicators and other objects here, this is executed prior to the main trading loop public override void Initialize(BarHistory bars) {          StartIndex = 1;          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 (bars.Symbol != _symbol)             return;           if (!HasOpenPosition(bars, PositionType.Long)) {             // enter the trade hypothetically             if (bars.DateTimes[idx].Date == _dte)             {                Transaction tn = PlaceTrade(bars, TransactionType.Buy, OrderType.FixedPrice, _price);                tn.Quantity = _shares;                _stopped = false;             }             } else {             // sell half on a 50% stop             Position p = LastPosition;             if (!_stopped && p.ProfitPctAsOf(idx) <= -50)             {                Transaction tn = PlaceTrade(bars, TransactionType.Sell, OrderType.Market, 0, "Stop 50%");                tn.Quantity = p.Quantity / 2;                _stopped = true;             }     else if (p.ProfitPctAsOf(idx) > 50)             {                ClosePosition(p, OrderType.Market, 0, "Profit Tgt");             }    } }       // example trade data       string _symbol = "U";       DateTime _dte = new DateTime(2022, 4, 5);       double _price = 105.20;       int _shares = 100;       bool _stopped = false; } }


Also see Trade History Strategies that let you analyze a trade history by reason a set of trades from a file.
1
Best Answer
FLX8
- ago
#5
thanks Cone, your code is definitely giving me a framework to better understand and navigate WL.
0

Reply

Bookmark

Sort