- ago
I ran the strategy - "Dynamic Breakout System" in the Sample Strategies folder and the profit/loss calculations seem off. I try to factor in the trading commission but the number still not comes out correctly. I am not sure if there any others costs/fees/factors that might affect the profit/loss calculations.

The sample calculations done here are for two trades, the first 2 pictures below are showing entry and exit prices for trade 1 and the last two pics are showing entry and exit prices for trade 2. The profit loss displayed on the chart as seen in the picture not matched with the math.

Strategy settings:
Symbol: SPY
Data Scale: Daily
Data Range: most recent 10 yrs
Position size: $5000


trade 1:
pnl = # of shares * (exit price - entry price)
pnl = 14 * ($407.07 - $353.49)
pnl = $750.12

The pnl on the chart shows a value of +$745.68 instead.

trade 2:
pnl = 12 * ($416.8 - $424.2)
pnl = -$88.8

The pnl on the chart shows a value of -$97.24






#BugReport
0
592
Solved
17 Replies

Reply

Bookmark

Sort
Glitch8
 ( 12.10% )
- ago
#1
Hi Tom,

The short answer: display rounding to 2 decimals

The long answer:
https://www.wealth-lab.com/Discussion/Profit-Calculation-Off-Why-5881
0
- ago
#2
Hi Glitch,

I am not sure if this is due to 2 decimals rounding-off error. On the two trades, the errors are shown below:

error (trade 1) = |$745.68 - $750.12| = $4.44
error (trade 2) = |-$97.24 - (-$88.8)| = $8.44

As you can see, the error magnitudes this large could not be explained with rounding-off errors.
0
- ago
#3
Glitch,

I want to add that the two trades that I selected and showed their calculations on the previous post are picked randomly from the simulation run. I do a quick check on other trades and the error magnitudes are similar to the two shown here.

Could you run a simulation with the strategy and its settings on Post #1 like I did and see whether your results are same/different as/from mine. It is possible that there are settings that I was not aware of.

For the this simulation, I have turned off slippage and the trading commission is set to $4.95/trade by default. (When I set the trading commission to $0, the error magnitudes remain similar to earlier cases). Thanks.
0
- ago
#4
This must be the effect of commissions. It's working for me as expected, returning the expected value on that 1st trade (Wealth-Data, $5000 size, Commission/Slippage/Other ALL disabled)

14 * $407.07 - $353.49 = $750.12
0
Cone8
 ( 6.32% )
- ago
#5
The answer is in the data, so let's start with the Data Provider. Which one is it?
0
- ago
#6
Judging by the prices (not matching Yahoo's) and the symbol description, topic starter must be using Wealth-Data (like I did).
0
Cone8
 ( 6.32% )
- ago
#7
QUOTE:
14 * $407.07 - $353.49 = $750.12
In that case, where did 407.07 and 353.49 come from in this calculation? They should be 407.02 and 353.52.
0
- ago
#8
Just to be on the safe side, I'd recommend topic starter to "Delete local files" and reload cached data.
0
Cone8
 ( 6.32% )
- ago
#9
Please just add this to your strategy, post the debug log and we'll know everything we need to know!

CODE:
public override void BacktestComplete() {          foreach(Position p in Backtester.Positions)          {             WriteToDebugLog(string.Format("{8}\t{9}\t{0:d}\t{1:N4}\t{2:d}\t{3:N4}\t{4:C4}\t{5:N4}\t{6:N4}\t{7}",                p.EntryDate, p.EntryPrice, p.ExitDate, p.ExitPrice, p.Profit, p.EntryCommission, p.ExitCommission, p.FuturesMode,                p.Symbol, p.Quantity));          } }
1
- ago
#10
I am using IQFeed as my data source. The complete strategy code and the debug log output are shown below. Slippage is turned off and $0 commissions is used.

CODE:
--SPY-- SPY   35.5897   8/31/2012   141.2900   10/15/2012   143.2300   $69.0440   0.0000   0.0000   False SPY   34.857699999999994   12/12/2012   144.0000   8/16/2013   166.0600   $768.9609   0.0000   0.0000   False SPY   29.3582   9/17/2013   170.4600   1/27/2014   179.0600   $252.4805   0.0000   0.0000   False SPY   26.839800000000004   3/3/2014   184.6900   8/1/2014   192.5600   $211.2292   0.0000   0.0000   False SPY   25.062599999999996   8/22/2014   199.3400   10/13/2014   190.4600   ($222.5559)   0.0000   0.0000   False SPY   24.612299999999998   11/7/2014   203.1700   12/11/2014   203.8800   $17.4747   0.0000   0.0000   False SPY   23.669800000000002   2/23/2015   210.9400   3/9/2015   207.7400   ($75.7434)   0.0000   0.0000   False SPY   23.6787   4/24/2015   211.6600   6/9/2015   208.4500   ($76.0086)   0.0000   0.0000   False SPY   24.5978   10/19/2015   202.5000   12/14/2015   202.0700   ($10.5771)   0.0000   0.0000   False SPY   25.2385   3/2/2016   197.7400   5/5/2016   205.5600   $197.3651   0.0000   0.0000   False SPY   23.7823   5/31/2016   210.5600   6/27/2016   201.5900   ($213.3272)   0.0000   0.0000   False SPY   23.5128   7/11/2016   213.1900   8/3/2016   215.4800   $53.8443   0.0000   0.0000   False SPY   22.9168   8/8/2016   218.4000   9/12/2016   212.3900   ($137.7300)   0.0000   0.0000   False SPY   22.832   11/18/2016   219.0700   5/18/2017   235.7300   $380.3811   0.0000   0.0000   False SPY   20.681599999999996   5/26/2017   241.5400   6/28/2017   242.5000   $19.8543   0.0000   0.0000   False SPY   20.3616   7/17/2017   245.4700   8/11/2017   244.0200   ($29.5243)   0.0000   0.0000   False SPY   20.0634   9/12/2017   249.6300   6/26/2018   271.6400   $441.5954   0.0000   0.0000   False SPY   17.8833   7/16/2018   279.6400   10/11/2018   277.0800   ($45.7812)   0.0000   0.0000   False SPY   18.686   1/31/2019   267.5100   5/8/2019   287.5300   $374.0937   0.0000   0.0000   False SPY   17.0998   6/19/2019   292.5500   8/2/2019   293.8500   $22.2297   0.0000   0.0000   False SPY   16.5953   9/13/2019   301.7800   10/2/2019   291.5000   ($170.5997)   0.0000   0.0000   False SPY   16.485300000000002   10/29/2019   303.0000   2/26/2020   314.1800   $184.3057   0.0000   0.0000   False SPY   16.949099999999998   5/19/2020   294.3500   9/9/2020   337.5500   $732.2011   0.0000   0.0000   False SPY   14.1872   10/13/2020   352.2800   10/29/2020   326.9100   ($359.9293)   0.0000   0.0000   False SPY   14.1019   11/10/2020   353.4900   5/13/2021   407.0700   $755.5798   0.0000   0.0000   False SPY   11.803299999999998   6/11/2021   424.2000   6/21/2021   416.8000   ($87.3444)   0.0000   0.0000   False SPY   11.7203   6/28/2021   427.1700   9/21/2021   436.5300   $109.7020   0.0000   0.0000   False SPY   10.975700000000002   10/26/2021   457.2000   12/31/9999   0.0000   $110.3058   0.0000   0.0000   False


CODE:
using WealthLab.Backtest; using System; using WealthLab.Core; using WealthLab.Indicators; using System.Drawing; using System.Collections.Generic; namespace WealthScript2 { public class DynamicBreakout : UserStrategyBase {       public override void Initialize(BarHistory bars) {          TimeSeries shiftedMA = (bars.Close >> 1);          TimeSeries Gain = (bars.Close - shiftedMA).Abs();          TimeSeries PctGain = (Gain / shiftedMA );          TimeSeries Short = SMA.Series(PctGain, 14);          TimeSeries Long = SMA.Series(PctGain, 200);          Vol = (( Short / Long) * 30);          PlotTimeSeries(Vol, "Lookback Period", "Vol", Color.Olive, PlotStyles.ThickLine);          HH = new TimeSeries(bars.DateTimes, 0);          LL = new TimeSeries(bars.DateTimes, 0);          PlotTimeSeries(HH, HH.Description, "Price", Color.Red, PlotStyles.Line);          PlotTimeSeries(LL, LL.Description, "Price", Color.Green, PlotStyles.Line);          StartIndex = 200;       } public override void Execute(BarHistory bars, int idx) {          int bar = idx;          int Period = (int)Vol[bar];          HH[bar] = Highest.Value(bar, bars.High, Period);          LL[bar] = Lowest.Value(bar, bars.Low, Period); if (!HasOpenPosition(bars, PositionType.Long)) {             if (bars.Close[bar] > HH[bar - 1])             {                PlaceTrade(bars, TransactionType.Buy, OrderType.Market);             } } else {             if (bars.Close[bar] < LL[bar - 1])             {                PlaceTrade(bars, TransactionType.Sell, OrderType.Market);             } } }       public override void BacktestComplete()       {          foreach(Position p in Backtester.Positions)          {             WriteToDebugLog(string.Format("{8}\t{9}\t{0:d}\t{1:N4}\t{2:d}\t{3:N4}\t{4:C4}\t{5:N4}\t{6:N4}\t{7}",              p.EntryDate, p.EntryPrice, p.ExitDate, p.ExitPrice, p.Profit, p.EntryCommission, p.ExitCommission, p.FuturesMode,              p.Symbol, p.Quantity));          }       }              TimeSeries Vol, HH, LL;    } }


0
- ago
#11
Cone, all the numbers (entry, exit prices, and the number of shares) used in the calculations are being read off from the chart.
0
Glitch8
 ( 12.10% )
- ago
#12
Let's scrutinize just the first logged item.

See, the profit is calculated correctly. Maybe you defined the Market/Symbol to use fractional shares?

0
Best Answer
- ago
#13
Glitch is right to pointed out about the fractional shares.

I think the errors might be due to the rounding-off all decimals in the number of shares that I see on the chart. For the first trade on the log file, the number of shares displayed on the chart is 36 shares, while the actual number of shares used by WL7 to calculate PNL is 35.5897 shares. Is there a switch that I can use to adjust the number of decimal precisions on # of shares and prices on the charts?





0
Glitch8
 ( 12.10% )
- ago
#14
Currently not but since this has been a point of confusion we should address this!
2
Cone8
 ( 6.32% )
- ago
#15
The fractional shares are occurring because you've either defined the symbol's market or the symbol itself with a non-zero "Quantity Decimals" in Tools > Markets & Symbols. If you make that 0, then the shares will be integers - like on our runs.
3
- ago
#16
Thanks.
0
- ago
#17
Offtopic discussion moved to:
https://www.wealth-lab.com/Discussion/Unexpected-Position-Profit-value-when-accessing-it-inside-Execute-7142
0

Reply

Bookmark

Sort