kazuna8
 ( 30.35% )
- ago
On 2003/04/01, the strategy had $112732.72 equity and the closing price was 25.45 so the quantity for the buy order should be 4429 (112732.72 / 25.45) but the quantity is reduced to 4386.

Why does the position sizing reduce the position size despite the available equity and the position sizing setting?

[Backtest Data]
Single Symbol: QQQ
Data Scale: Daily
Data Range: All Data

[Position Sizing]
Starting Capital: 100000
Position Size: Percent of Equity
Percent: 100
Basis Price: Market Close this Bar
Margin Factor: 3.00

CODE:
---Sequential Debug Log--- 2003/03/04 : Close = 24.5 2003/04/01 : Close = 25.45


CODE:
---Backtest Results / Positions--- Position   Symbol   Quantity   "Entry Date"   "Entry Price"   "Exit Date"   "Exit Price"   Profit   "Profit %"   "Bars Held"   "Entry Signal"   "Exit Signal"   "MFEPct"   "MAEPct"    Long   QQQ   12243   2003/03/05   24.4   2003/04/01   25.44   12732.720000000034   4.262295081967224   19         13.7704918032787   -3.5245901639344237    Long   QQQ   4386   2003/04/02   26.06   Open   0   2067034.0799999998   1808.4420567920183   5352         1810.571757482732   -3.8756715272448123   


CODE:
      int BarDate(BarHistory bars, int idx)       {          return bars.DateTimes[idx].Year * 10000 + bars.DateTimes[idx].Month * 100 + bars.DateTimes[idx].Day;       }       public override void Execute(BarHistory bars, int idx) {          if (BarDate(bars, idx) == 20030304)          {             WriteToDebugLog(bars.DateString(idx) + " : Close = " + bars.Close[idx].ToString(), false);             Transaction t = PlaceTrade(bars, TransactionType.Buy, OrderType.Market);             t.Quantity = t.Quantity * 3.0;          }          if (BarDate(bars, idx) == 20030331)          {             PlaceTrade(bars, TransactionType.Sell, OrderType.Market);          }          if (BarDate(bars, idx) == 20030401)          {             WriteToDebugLog(bars.DateString(idx) + " : Close = " + bars.Close[idx].ToString(), false);             Transaction t = PlaceTrade(bars, TransactionType.Buy, OrderType.Market);          }       }
0
688
Solved
14 Replies

Reply

Bookmark

Sort
Cone8
 ( 5.88% )
- ago
#1
What's the output if you change use this in place of the debug statements?
Keep in mind that in the first trade, you're multiplying the sized quantity by 3.

CODE:
            double size = Math.Truncate(CurrentEquity / bars.Close[idx]);             WriteToDebugLog(bars.DateString(idx) + " : Close = " + bars.Close[idx].ToString() + $"\t{CurrentEquity}\tQty = {size}", false);
0
kazuna8
 ( 30.35% )
- ago
#2
Here is the debug output.
CODE:
---Sequential Debug Log--- 2003/03/04 : Close = 24.5   100000   Qty = 4081 2003/04/01 : Close = 25.45   111625.80849398638   Qty = 4386

QUOTE:
Keep in mind that in the first trade, you're multiplying the sized quantity by 3.

Yes, that's right.
0
Cone8
 ( 5.88% )
- ago
#4
Re: Here is the debug output.
CODE:
---Sequential Debug Log--- 2003/03/04 : Close = 24.5   100000   Qty = 4081 2003/04/01 : Close = 25.45   111625.80849398638   Qty = 4386
These results are correct and match WealthLab's results in Post #1.
The equity used in your calculation was not accurate. How did you come up with 112732.72?
0
kazuna8
 ( 30.35% )
- ago
#5
Could you try my code and trade setting to duplicate the same problem?

I guess it's the Quantity my code overwrites that is unveiling an issue in the backtest logic.
0
Glitch8
 ( 9.28% )
- ago
#6
I deleted my post since Cone ran your code and apparently identified an issue where you were mistaken with the equity amount. See above.
0
kazuna8
 ( 30.35% )
- ago
#7
Starting Capital: 100000 + Profit from sale on 2003/04/01: 12732.72 = 112732.72
0
kazuna8
 ( 30.35% )
- ago
#8
How does WealthLab get the equity of 111625.80 after selling 12243 (4081 x 3) SPYs from $24.4 to $25.44?

I cannot come up with an idea of how the gain of 11625.80 is calculated.
0
Glitch8
 ( 9.28% )
- ago
#9
I see it too I think, I’ll mark this for investigation!
0
Cone8
 ( 5.88% )
- ago
#10
Dividends comes to mind.

Edit:
The first dividend wasn't paid until Dec 2004, so that leaves the next possibility...
Do you have "Use UST Yield" checked or a non-zero number for Cash Interest in the Backtest Preferences?



0
kazuna8
 ( 30.35% )
- ago
#11
QUOTE:
Do you have "Use UST Yield" checked or a non-zero number for Cash Interest in the Backtest Preferences?
I have neither "Use UST Yield" nor "Interest on Cash" enabled in the Backtest Preferences.
In fact, I changed nothing in the Backtest Preferences, and I see them all disabled by default.
0
kazuna8
 ( 30.35% )
- ago
#12
QUOTE:
I see it too I think, I’ll mark this for investigation!
Glad you can reproduce the problem.
For the time being, I guess I need to control the position sizes by overwriting the Transaction.Quantity for each trade like I used to use SetShareSize on WL6.
0
Glitch8
 ( 9.28% )
- ago
#13
OK I completed the analysis and everything looks OK as far as I can see. Did you consider the margin loan considerations? This probably accounts for the drop in equity you are observing, reducing the size of the position from what you'd expect.

When I set margin loan rate to zero, set cash interest to zero, and turn off dividends, so we get a clean result, this is what I see.

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) { }       int BarDate(BarHistory bars, int idx)       {          return bars.DateTimes[idx].Year * 10000 + bars.DateTimes[idx].Month * 100 + bars.DateTimes[idx].Day;       }       public override void Execute(BarHistory bars, int idx)       {          if (BarDate(bars, idx) == 20030304)          {             WriteToDebugLog(bars.DateString(idx) + " : Close = " + bars.Close[idx].ToString(), false);             Transaction t = PlaceTrade(bars, TransactionType.Buy, OrderType.Market);             t.Quantity = t.Quantity * 3.0;          }          if (BarDate(bars, idx) == 20030331)          {             PlaceTrade(bars, TransactionType.Sell, OrderType.Market);          }          if (BarDate(bars, idx) == 20030401)          {             WriteToDebugLog("Current Equity=" + CurrentEquity, false);             WriteToDebugLog(bars.DateString(idx) + " : Close = " + bars.Close[idx].ToString(), false);             WriteToDebugLog("Qty should be " + CurrentEquity / bars.Close[idx], false);             Transaction t = PlaceTrade(bars, TransactionType.Buy, OrderType.Market);             WriteToDebugLog("Qty " + t.Quantity, false);          }       } //declare private variables below } }


Debug Log:

CODE:
---Sequential Debug Log--- 3/4/2003 : Close = 24.3987 Current Equity=112732.8958 4/1/2003 : Close = 25.3448 Qty should be 4447.969437517755 Qty 4447


On 4/1/2003 the equity is 112,732 which is the 100,000 plus the profit of 12,732 for the first trade. The close of 4/1/2003 is 25.3448 so the quantity of the trade should be 4447, which it is.

0
Best Answer
kazuna8
 ( 30.35% )
- ago
#14
That was it!

QUOTE:
Did you consider the margin loan considerations?
No, I didn't. I didn't change the settings at all and left them all to default.

Apparently, the default value of the Margin Loan Rate is set to 8 which was causing the mysterious reduction.

After setting the Margin Loan Rate to 0, the reduction is gone.

Thank you for your help.
1

Reply

Bookmark

Sort