- ago
For example, is it possible to have an open long position and have sell half of the position if price crosses the SMA and the closes out entirely on another indicator? Is it possible to close portions of an original long (or inverse) in percentages?
0
253
Solved
4 Replies

Reply

Bookmark

Sort
Glitch8
 ( 13.87% )
- ago
#1
Yes it’s possible. You can adjust the Quantity of the Transaction object that you get when you call PlaceTrade, divide it by 2 for example to sell half the position.
1
Best Answer
- ago
#2
Struggling with setting the first ClosePosition to include a 50% sale and the second one to close the position entirely. Can you assist?

CODE:
            }             if (condition0)             {             foreach(Position foundPosition0 in OpenPositions)             {                if (foundPosition0.PositionTag == 0)                {                   Backtester.CancelationCode = 518;                   ClosePosition(foundPosition0, OrderType.Market, 0, "Sell At Market (1)");                }             }             }             condition0 = false;             {                if(bars.DateTimes[index].GetTime() >= 1555)                {                   condition0 = true;                }             }             if (condition0)             {             foreach(Position foundPosition0 in OpenPositions)             {                if (foundPosition0.PositionTag == 0)                {                   Backtester.CancelationCode = 518;                   ClosePosition(foundPosition0, OrderType.Market, 0, "Sell At Market (1)");                }             }             } }
0
Cone8
 ( 23.86% )
- ago
#3
You can't use ClosePosition() for this purpose because it doesn't return a Transaction object. Generally, it would be like this, where foundPosition0 is Position object you're operating with -

CODE:
               Transaction t = PlaceTrade(bars, TransactionType.Sell, OrderType.Market);                t.Quantity = foundPosition0.Quantity / 2;

But you'll need to add a little more logic to shift the logic to the other exit. Otherwise, you may keep hitting this rule, resulting in selling progressively half positions... 10, 5, 2.5, 1.25... shares before arriving at 1555.
0
Glitch8
 ( 13.87% )
- ago
#4
Here is a minimal example that buys when RSI14 goes below 35, then sells half the position when it goes above 60, and the remainder when it goes above 70 (which could be on the same bar)

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) {          rsi14 = RSI.Series(bars.Close, 14);          PlotIndicator(rsi14); } //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 (rsi14[idx] < 35)             {                PlaceTrade(bars, TransactionType.Buy, OrderType.Market);                soldHalf = false;             } } else {             //code your sell conditions here             if (!soldHalf)             {                //sell first half when RSI14 goes above 60                if (rsi14[idx] > 60)                {                   soldHalf = true;                   Transaction t = PlaceTrade(bars, TransactionType.Sell, OrderType.Market);                   t.Quantity /= 2;                }             }             if (soldHalf)             {                //sell remaining half when RSI14 goes above 70                if (rsi14[idx] > 70)                   PlaceTrade(bars, TransactionType.Sell, OrderType.Market);             } } }       //declare private variables below       private RSI rsi14;       private bool soldHalf; } }
0

Reply

Bookmark

Sort