- ago
I am working on a short system.
I have a short position with entry price $12.86, exit price $11.49.
This is a profit of about 11%. But Backtest Results->Positions shows this:


Also note the Exit Signal name. It was produced by this code:
CODE:
// Profit taking: 5% Double profitPercent = (bars.Close[idx] / p.EntryPrice - 1.0) * -100.0; // Profit of short position in percent if (profitPercent > 5.0) { ClosePosition(p, OrderType.MarketClose, 0.0, "Take Profit ProfitPct=" + profitPercent + ", p.ProfitPct=" + p.ProfitPercent); }


Please note: Position.ProfitPercent shows the same WRONG value as the position table.

(In this example the Close price at idx is the same as the close price at idx+1)
0
285
Solved
15 Replies

Reply

Bookmark

Sort
Cone8
 ( 25.05% )
- ago
#1
What were the trade's commissions?

Here's my result for that trade -
0
MIH8
- ago
#2
Just a guess. The last time i had issues with the equity curve (and single trades) it was related to " Collect Dividends on backtest Positions". After a cleanup of the related data everything did work as expected.

Edit: I looked into the subject. The profit calculation was not affected. Sorry for the confusion.
0
- ago
#3
QUOTE:
What were the trade's commissions?

Commissions are zero.

Data Provider is Q-Data.


0
Cone8
 ( 25.05% )
- ago
#4
Since the results for the 2 other trades showing look correct, it must be symbol-related.

Is there any change if you turn off "Futures Mode" in the Backtest Preferences? (But even if it were futures, the trade profit should be positive!)
0
Cone8
 ( 25.05% )
- ago
#5
I was hoping to wake up today and read that you figured out what was going on. Still stumped?
0
MIH8
- ago
#6
@DrKoch

1. Can you reproduce this numbers?
2. Did you ran the strategy for the single symbol?
3. What is the setting for Max Open Short positions? (Multiple position context?!)
4. Could you observe a second error with a different symbol?
5. Were there changes in the environment (configuration files, updates of WL components etc), if so, can you recreate the same setup for the run?

@Cone

1. Can PlaceTrade() and ClosePosition() make a difference in that context? If yes, which one did you use?
0
Cone8
 ( 25.05% )
- ago
#7
1. No. ClosePosition() is a wrapper that uses PlaceTrade().

The trade prices are correct (so it's not a data issue) and there's no way without an astronomical non-zero commission that the normal profit calculations could return that result.

I'd still like to know if there's an entry that matches TXT in Markets & Symbols and if the result is the same with Futures Mode disabled.
0
Cone8
 ( 25.05% )
- ago
#8
As often is the case, we don't get to see all of the Strategy code. Is it possible that you're "splitting" positions by selling a partial (like half) of the original position? That would give us something to go on.

.. but of course if you're actually using ClosePosition(), that's not a possibility.
0
- ago
#9
Steps to reproduce:

1. Create a DataSet with these symbols: ALGN ISRG TTD TXT.
(Just a small example)
It happened with both providers I checked: WealthData and Q-Data

2. Run this short strategy:

CODE:
using WealthLab.Backtest; using System; using WealthLab.Core; using WealthLab.Indicators; namespace WealthScript1 { public class MyStrategy : UserStrategyBase { public override void Initialize(BarHistory bars) {          atr10 = new ATR(bars, 10);          rsi3 = new RSI(bars.Close, 3);          StartIndex = 20; } public override void Execute(BarHistory bars, int idx) {          //=== Exit ===          Position p = LastOpenPosition;          if (p != null)          {             // Stop Loss             ClosePosition(p, OrderType.Stop, p.EntryPrice + 3.0 * atr10[p.EntryBar], "Stop Loss");             if (p.IsOpen)             {                // Profit taking: 4%                if (p.ProfitPercent > 4.0)                {                   ClosePosition(p, OrderType.MarketClose, 0.0, "Take Profit");                }             }          }                             //=== Entry ===          // One position per symbol          if (p != null && p.IsOpen) return;         //--- Setup ---     // RSI above ninety     if(rsi3[idx] <= 90.0) return;     // The last two days the close was higher than the previous day     if(bars.Close[idx] <= bars.Close[idx-1]) return;     if(bars.Close[idx-1] <= bars.Close[idx-2]) return;          // sell short four percent above previous close          Double limit = bars.Close[idx] * 1.04;          Transaction t = PlaceTrade(bars, TransactionType.Short, OrderType.Limit, limit, "Short Entry");       }       // private variables       IndicatorBase avgTurnover, atr10, rsi3;    } }


On my machine Backtest Results->Positions looks like this:



Data Range 1995-2019.
No fancy settings. No commissions, No slippage. No futures mode.

0
- ago
#10
Can you clear the WD and QD data cache for these 4 symbols entirely, restart WL or "clear tracking info", update data and retry?

Looks like bad data or a region settings something somewhere.
0
Glitch8
 ( 8.38% )
- ago
#11
When attempting to capture the profit of an open position in strategy code, the intention is to use ProfitPctAsOf instead of ProfitPercent. Using ProfitPercent causes some unwanted side effects. We'll create an issue to address the side effects because this could surprise users who attempt to use Profit/ProfitPercent in this way.

Change this code:

CODE:
               // Profit taking: 4%                if (p.ProfitPctAsOf(idx) > 4.0)                {                   ClosePosition(p, OrderType.MarketClose, 0.0, "Take Profit");                }
1
Best Answer
MIH8
- ago
#12
Ok, that's a neat approach to get around the problem (if it works).

For my taste, one should still look for the reason. No one knows how often such an error occurs in the backtest and systematically influences the results. As long as the reason cannot be narrowed down, it is a problem for everyone. Or did you mean the problem is definitely connected with the ProfitPercent value of the position?

If that is the case, why did Cone did not get the same problem?
0
Cone8
 ( 25.05% )
- ago
#13
Like I said, I didn't have the code. I created my own code to create the positions.

In a strategy, if you're testing the profit of a live position on a specific bar, you need to use ProfitPctAsOf(). The non "AsOf" counterpart is for post-processing at the end of a backtest, or for Visualizers.

This is by design. The problem is that it's easy to shoot yourself in the foot. I don't know what Glitch has planned, but maybe we can throw an error if one of these functions is found in the Execute() method.
1
- ago
#14
Ok, with ProfitAsOf() the wrongly calculated numbers for the Stop Loss go away,
albeit ProfitPercent() was used in the Take Profit leg...
... very strange ...
0
Glitch8
 ( 8.38% )
- ago
#15
It's actually documented behavior, but I agree we need to eliminate the side effect. We have it as an open issue.

0

Reply

Bookmark

Sort