MIH8
- ago
Today I had some time to make my first steps in the WL Framework and look around a bit. Normally I don't code in C#, but that's no problem in principle. The sense of the strategy can be simply ignored.

I have a question that I'm sure can be answered quickly, but is not obvious to me at this point.
What are the significant differences between the three variants shown (PlaceTrade, ExitPosition, Conditional Exit).
Are the variants semantically the same?

Thank you very much for a short feedback.

CODE:
namespace WealthScript1 { public class MyStrategy : UserStrategyBase {       private EMA _ema4, _ema8,_ema16;       private IndicatorBase indicator4,indicator8,indicator16;        public override void Initialize(BarHistory bars) {          indicator4 = _ema4 = new EMA(bars.Close, 4);          indicator8 = _ema8 = new EMA(bars.Close, 8);          indicator16 = _ema16 = new EMA(bars.Close, 16);                    PlotIndicator(indicator4, new WLColor(128, 0,128));          PlotIndicator(indicator8, new WLColor(128, 128,0));          PlotIndicator(indicator16, new WLColor(0, 128,128)); } public override void Execute(BarHistory bars, int idx) { if (!HasOpenPosition(bars, PositionType.Long)) {             if(bars.Close[idx] > _ema4[idx]              && _ema4[idx] > _ema8[idx]        && _ema8[idx] > _ema16[idx]) {                PlaceTrade(bars, TransactionType.Buy, OrderType.Market);             } } else {             const double TP = 1.01;             const double SL = 0.99;                          Position lp = LastPosition;             double tp = TP * lp.EntryPrice;             double sl = SL * lp.EntryPrice;             PlaceTrade(bars, TransactionType.Sell, OrderType.Limit, tp,"Sell at profit target");             PlaceTrade(bars, TransactionType.Sell, OrderType.Stop, sl, "Sell at stop loss");                 //ClosePosition(lp, OrderType.Limit, tp, "Sell at profit target");             //ClosePosition(lp, OrderType.Stop, sl,"Sell at stop loss");                          // Edited after posting. Alternatively using ClosePosition here too.             //if (tp >= bars.Close[idx]) { ?! idx-1             //   PlaceTrade(bars, TransactionType.Sell, OrderType.Market, 0, "Sell at profit target");             //}             //else if (sl <= bars.Close[idx]) { ?! idx-1             //   PlaceTrade(bars, TransactionType.Sell, OrderType.Market, 0,"Sell at stop loss");             //} } } } }
0
313
Solved
3 Replies

Reply

Bookmark

Sort
Cone8
 ( 24.57% )
- ago
#1
Internally, ClosePosition() uses PlaceTrade() so the differences are subtle. If your code needs to work with the Transaction object, then you need to use PlaceTrade(). ClosePosition is a bit of a shortcut because you don't need to specify the TransactionType - Wealth-Lab will apply the appropriate one for the Position object.

Re: Conditional Exit
Any exit must use ClosePosition() or PlaceTrade(), whether or not you add conditional logic to execute one of those statements.
0
Best Answer
MIH8
- ago
#2
Thanks.

I have edited the conditional code at the moment because my real question would have been whether combining the condition with a market order should give the same result as a limit order where I specify a price. Sorry was too slow and inaccurate.
0
Glitch8
 ( 10.41% )
- ago
#3
ClosePosition becomes important for strategies that can manage multiple open positions for the same symbol, because you can control which of the open positions to close.
1

Reply

Bookmark

Sort