- ago
Ok, so in WL6 if the take profit % is within the open and bar high, it will fill as it should in real trading. In WL7, the same bars will not fill, but only fill on the next bar at the earliest. Is this a defect, or a setting issue. If a setting issue, what setting needs to change?
Ex. Buy at open. Sell at 1% profit target. Stock opens at 100. Hits 105 at the high. Should fill the 1% profit target. But it doesn't.
Anyone know how to make WL7 take profits on the same bar as the entry?

0
1,201
Solved
14 Replies

Reply

Bookmark

Sort
- ago
#1
What does your strategy code look like?
0
Cone8
 ( 5.57% )
- ago
#2
See: AutoProfitTargetPrice, AutoStopLossPrice

And the preferred way to use them is with this override method:
UserStrategyBase > AssignAutoStopTargetPrices

https://www.wealth-lab.com/Support/ApiReference/UserStrategyBase#:~:text=Strategy%20Execution-,AssignAutoStopTargetPrices,-public%20virtual%20void
0
- ago
#3
Here's an example of a simple one from 2 blocks and a weekly timeframe:
1 Buy at market (open)
2 Sell at profit target .5%
it should happen within most bars, but never does. Always only fills on next bar at the earliest. Thanks for your help.

CODE:
using WealthLab.Backtest; using System; using WealthLab.Core; using WealthLab.Indicators; using System.Drawing; using System.Collections.Generic; namespace WealthScript4 { public class MyStrategy : UserStrategyBase {     public MyStrategy() : base() { } public override void Initialize(BarHistory bars) {          StartIndex = 0; } public override void Execute(BarHistory bars, int idx) {          int index = idx;          bool condition0;             condition0 = false;             {                condition0 = true;             }             if (condition0)             {                _transaction = PlaceTrade(bars, TransactionType.Buy, OrderType.Market, 0, 0, "Buy At Market (1)");             }             condition0 = false;             {                condition0 = true;             }             if (condition0)             {             foreach(Position foundPosition0 in OpenPositions)             {                if (foundPosition0.PositionTag == 0)                {                   value = (0.5 / 100.0) + 1.0;                   ClosePosition(foundPosition0, OrderType.Limit, foundPosition0.EntryPrice * value, "Sell at 0.50% profit target");                }             }             } }       private double value;       private Transaction _transaction; } }
0
Glitch8
 ( 10.10% )
- ago
#4
Here is what you need to add to your strategy to enable selling at a profit target on the same bar of entry:

CODE:
public override void AssignAutoStopTargetPrices(Transaction t, double basisPrice, double executionPrice) {          double profitTarget = executionPrice * 1.005;          t.AutoProfitTargetPrice = profitTarget; }
0
Best Answer
- ago
#5
It works - thanks! Where is the best place to insert it?

Also, Is there a way to make
executionPrice * 1.005;
follow whatever number is entered in the parameter?

And last, if I always want to have same bar fills as a possibility, is there a way to save it as a block or something so I can always plunk it in?
0
- ago
#6
QUOTE:
Where is the best place to insert it?

It doesn't matter as long as code would correctly compile.

QUOTE:
follow whatever number is entered in the parameter?

Of course. If you defined a parameter labeled "Multiple" then it could work like this, for example:
CODE:
double profitTarget = executionPrice * Parameters.FindByOptParamName("Multiple", false).AsDouble;
0
Glitch8
 ( 10.10% )
- ago
#7
Currently "same-bar" exits aren't available via Building Blocks. But it wouldn't be too hard to add a check-box parameter for Profit Target and Stop Loss. If you want to submit a new topic and flag it with #FeatureRequest this can be in our wish list.
1
- ago
#8
Can someone just show me how to utilize same bar exits in plain english? That would be very helpful for starters.

GlitchA ( 39.92% )
Jan 15, 2022, 06:22 PM - 1 year ago
#7
Currently "same-bar" exits aren't available via Building Blocks. But it wouldn't be too hard to add a check-box parameter for Profit Target and Stop Loss. If you want to submit a new topic and flag it with #FeatureRequest this can be in our wish list.
1
Glitch8
 ( 10.10% )
- ago
#9
Sure, same bar exits are available in code only, they haven’t been implemented in building blocks.

They work by assigning values to properties of the Transaction that is the result of the PlaceTrade call. The properties are AuotStopLossPrice and AutoProfitTargetPrice.
0
- ago
#10
Hi Glitch,
I've been trying to get this code to reflect "same bar exits" for SL and TP with no luck. Could you adjust this basic code example so I have at least 1 working example of how to do it? Hopefully I can take it from there. It would help to see it to learn from it.
Thank you!

CODE:
using WealthLab.Backtest; using System; using WealthLab.Core; using WealthLab.Data; using WealthLab.Indicators; using System.Collections.Generic; namespace WealthScript6 { public class MyStrategy : UserStrategyBase {     public MyStrategy() : base() {          AddParameter("Profit Target", ParameterType.Double, 5, 1, 10, 1);          AddParameter("Stop Loss", ParameterType.Double, 6, 1, 30, 1); StartIndex = 0; } public override void Initialize(BarHistory bars) {          PlotStopsAndLimits(3);          trailing = false;          PlotStopsAndLimits(3); } public override void Execute(BarHistory bars, int idx) {          int index = idx;          bool condition0;             condition0 = false;             {                condition0 = true;             }             if (condition0)             {                _transaction = PlaceTrade(bars, TransactionType.Short, OrderType.Market, 0, 0, "Short At Market (1)");             }             condition0 = false;             {                condition0 = true;             }             if (condition0)             {             foreach(Position foundPosition0 in OpenPositions)             {                if (foundPosition0.PositionTag == 0)                {                   Backtester.CancelationCode = 253;                   ClosePosition(foundPosition0, OrderType.Limit, foundPosition0.EntryPrice * (1.0 - Parameters[0].AsDouble / 100.0), "Cover at 5% profit target");                }             }             }             condition0 = false;             {                condition0 = true;             }             if (condition0)             {             foreach(Position foundPosition0 in OpenPositions)             {                if (foundPosition0.PositionTag == 0)                {                   Backtester.CancelationCode = 253;                   value = 1.0 + (Parameters[1].AsDouble / 100.0);                   ClosePosition(foundPosition0, OrderType.Stop, foundPosition0.EntryPrice * value, "Cover at 6% stop loss");                }             }             } } public override void NewWFOInterval(BarHistory bars) { }       private double value;       private bool trailing;       private TimeSeries stops;       private Transaction _transaction; } }
0
Glitch8
 ( 10.10% )
- ago
#11
Here's some stripped down sample code that illustrates how to use same-bar exits. If you want to base the same-bar prices on the execution price you need to override this other method as shown below.

CODE:
using WealthLab.Backtest; using System; using WealthLab.Core; using WealthLab.Data; using WealthLab.Indicators; using System.Collections.Generic; namespace WealthScript1 {    public class MyStrategy : UserStrategyBase    {       public MyStrategy() : base()       {          AddParameter("Profit Target", ParameterType.Double, 5, 1, 10, 1);          AddParameter("Stop Loss", ParameterType.Double, 6, 1, 30, 1);          StartIndex = 0;       }       public override void Initialize(BarHistory bars)       {          PlotStopsAndLimits(3);       }       public override void Execute(BarHistory bars, int idx)       {          //do we have a position?          if (LastOpenPosition == null)          {             //no, entry rules             PlaceTrade(bars, TransactionType.Short, OrderType.Market, 0, 0, "Short At Market (1)");          }          else          {             //yes, exit rules             double stopValue = Parameters[1].AsDouble;             double stopPrice = (stopValue / 100.0 + 1.0) * LastOpenPosition.EntryPrice;             PlaceTrade(bars, TransactionType.Cover, OrderType.Stop, stopPrice);             double targetValue = Parameters[0].AsDouble;             double targetPrice = (1.0 - (targetValue / 100.0)) * LastOpenPosition.EntryPrice;             PlaceTrade(bars, TransactionType.Cover, OrderType.Limit, targetPrice);          }       } //this will assign same-bar stop and profit target exit prices based on the execution price public override void AssignAutoStopTargetPrices(Transaction t, double basisPrice, double executionPrice) {          double targetValue = Parameters[0].AsDouble;          t.AutoProfitTargetPrice = (1.0 - (targetValue / 100.0)) * executionPrice;          double stopValue = Parameters[1].AsDouble;          t.AutoStopLossPrice = (stopValue / 100.0 + 1.0) * executionPrice; }    } }
0
- ago
#12
Thank you - that would be awesome! I love Wealth-Lab but have no clue about C# so I generally stick to building blocks. I appreciate the help with same bar exits.
0
Glitch8
 ( 10.10% )
- ago
#13
We found out that the problem I mentioned was actually a script error on my part, see the post above for the example code!
1
Cone8
 ( 5.57% )
- ago
#14
I'd recommend coding AssignAutoStopTargetPrices() for both long and short trades so that it switches automatically for the TransactionType.

CODE:
      //this will assign same-bar stop and profit target exit prices based on the execution price       public override void AssignAutoStopTargetPrices(Transaction t, double basisPrice, double executionPrice)       {          int sign = t.TransactionType == TransactionType.Buy ? -1 : 1;                   double targetValue = Parameters[0].AsDouble;          t.AutoProfitTargetPrice = (1.0 - (sign * targetValue / 100.0)) * executionPrice;          double stopValue = Parameters[1].AsDouble;          t.AutoStopLossPrice = (sign * stopValue / 100.0 + 1.0) * executionPrice;       }


And don't forget to cast your vote on the starter post here to make this feature available in the Building Blocks -
https://www.wealth-lab.com/Discussion/Same-Bar-Profit-Stops-for-Building-Blocks-9326
0

Reply

Bookmark

Sort