- ago
AssignAutoStopTargetPrices will not exit a position based on the Tag value if there is more than one open position on the bar:

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 { //create indicators and other objects here, this is executed prior to the main trading loop public override void Initialize(BarHistory bars) { } //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 (idx == bars.Count - 8)             PlaceTrade(bars, TransactionType.Buy, OrderType.Market).Tag = "1";          if (idx == bars.Count - 9)             PlaceTrade(bars, TransactionType.Buy, OrderType.Market).Tag = "1";          if (idx == bars.Count - 10)             PlaceTrade(bars, TransactionType.Buy, OrderType.Market).Tag = "1"; }       public override void AssignAutoStopTargetPrices(Transaction t, double basisPrice, double executionPrice)       {          if (t.Tag.ToString().Contains("Stop10"))          {             t.AutoStopLossPrice = t.ExecutionPrice * 1.10;             WriteToDebugLog("Stop10");          }          else          {             if (t.Tag != "NoStop")             {                t.AutoStopLossPrice = executionPrice * 1.15;                WriteToDebugLog("Stop15");             }          }       } //declare private variables below } }

In the example above, WL8 triggers a stop of 10% on a position without ""Stop10" Tag.
Wrong behavior?
0
118
Solved
11 Replies

Reply

Bookmark

Sort
Glitch8
 ( 6.11% )
- ago
#1
Sorry, I'm unable to reproduce this. With the complete example, the AutoStop gets hit with Stop15, not Stop10.

And sorry, I accidentally edited the code in your original post.

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 { //create indicators and other objects here, this is executed prior to the main trading loop public override void Initialize(BarHistory bars) { } //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 (idx == bars.Count - 8)             PlaceTrade(bars, TransactionType.Buy, OrderType.Market).Tag = "1";          if (idx == bars.Count - 9)             PlaceTrade(bars, TransactionType.Buy, OrderType.Market).Tag = "1";          if (idx == bars.Count - 10)             PlaceTrade(bars, TransactionType.Buy, OrderType.Market).Tag = "1"; }       public override void AssignAutoStopTargetPrices(Transaction t, double basisPrice, double executionPrice)       {          if (t.Tag.ToString().Contains("Stop10"))          {             t.AutoStopLossPrice = t.ExecutionPrice * 1.10;             WriteToDebugLog("Stop10");          }          else          {             if (t.Tag != "NoStop")             {                t.AutoStopLossPrice = executionPrice * 1.15;                WriteToDebugLog("Stop15");             }          }       } //declare private variables below } }
0
- ago
#2
The code below shows the bug:

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    {       //create indicators and other objects here, this is executed prior to the main trading loop       public override void Initialize(BarHistory bars)       {       }       //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 (idx == bars.Count - 7)                   {             Transaction T_1 = PlaceTrade(bars, TransactionType.Short, OrderType.Limit, bars.Close[idx] * 0.99, "T_1");             T_1.Quantity = 100;             T_1.Tag = "Stop1";             Transaction T_2 = PlaceTrade(bars, TransactionType.Short, OrderType.Limit, bars.Close[idx] * 0.99, "T_2");             T_2.Quantity = 200;             T_2.Tag = "Stop2";                      }                }       public override void AssignAutoStopTargetPrices(Transaction t, double basisPrice, double executionPrice)       {          if (t.Tag.ToString().Contains("Stop1"))          {             t.AutoStopLossPrice = t.ExecutionPrice * 1.01;             WriteToDebugLog("Stop1");          }          else          {             if (t.Tag != "NoStop")             {                t.AutoStopLossPrice = executionPrice * 1.02;                WriteToDebugLog("Stop2");             }          }       }       //declare private variables below    } }


You can run it on a CHKP as an example.
T_1 should exit with Stop 1% and T_2 should exit with Stop of 2%, but WL8 does it the other way around.
0
Cone8
 ( 2.67% )
- ago
#3
QUOTE:
AssignAutoStopTargetPrices will not exit a position based on the Tag value if there is more than one open position on the bar:
Probably the confusion is that t.AutoStopLossPrice is only for a same-bar exit. To exit Positions on subsequent bars, you need some exit logic that applies your stop and/or limit targets.
0
- ago
#4
I am referring to the same bar exit only (AssignAutoStopTargetPrices). It has nothing to do with other exits on other days. The bug is in the AssignAutoStopTargetPrices.
0
Cone8
 ( 2.67% )
- ago
#5
Okay, that last script has trades on the same bar, but here's my result.
What are you seeing?

0
- ago
#6
You need to go to the trades tab and show the Trades themselves with shares and Entry Signals.
Can you post a picture of your trades here?
0
Cone8
 ( 2.67% )
- ago
#7
I see. The shares for T_1 should have exited at 65.953 and for T_2 at 66.606.

0
- ago
#8
Do you know when these bugs will be fixed including the bug with the "profit target not working correctly on the same bar if the opening Transaction order was a limit"?

I can not move to WL8 before these bugs are fixed.
0
Glitch8
 ( 6.11% )
- ago
#9
The other issue is already fixed for next build.
0
Glitch8
 ( 6.11% )
- ago
#10
AssignAutoStops works by issuing a PlaceTrade behind the scenes. In order for the WL backtester to know which trade to associate with which signal you need to use a PositionTag, not a string Tag property. If you assign a different PositionTag to each entry then it will work as expected.

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    {       //create indicators and other objects here, this is executed prior to the main trading loop       public override void Initialize(BarHistory bars)       {       }       //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 (idx == bars.Count - 7)          {             Transaction T_1 = PlaceTrade(bars, TransactionType.Short, OrderType.Limit, bars.Close[idx] * 0.99, "T_1");             T_1.Quantity = 100;             T_1.Tag = "Stop1";             T_1.PositionTag = 1;             Transaction T_2 = PlaceTrade(bars, TransactionType.Short, OrderType.Limit, bars.Close[idx] * 0.99, "T_2");             T_2.Quantity = 200;             T_2.Tag = "Stop2";             T_2.PositionTag = 2;          }       }       public override void AssignAutoStopTargetPrices(Transaction t, double basisPrice, double executionPrice)       {          if (t.Tag.ToString().Contains("Stop1"))          {             t.AutoStopLossPrice = t.ExecutionPrice * 1.01;             WriteToDebugLog("Stop1");          }          else          {             if (t.Tag != "NoStop")             {                t.AutoStopLossPrice = executionPrice * 1.02;                WriteToDebugLog("Stop2");             }          }       }       //declare private variables below    } }
0
Best Answer
- ago
#11
.PositionTag makes it work as expected.
Thank you!
2

Reply

Bookmark

Sort