Position Sizing

Zachc

Zachc

Member
VIP
Warehouse
Has anyone tried position sizing as a variable in their backtesting? I am messing around with this code just simply using a static 5% of the portfolio per trade.
The problem I am running into is I am unable to trade the growth or decline of the P/L in the backtest so say for example a stock like AMZN the trades execute with a position size up until the stock price is above $500 since it is static. Any ideas around this how this could be more dynamic?


Code:
input PortfolioSize = 10000;
input TradeSizePct = .05;

#Position Sizing

def pSize = portfolioSize;
def sizeP = portfolioSize * TradeSizePct;
def tSize = sizep/open[-1];

AddOrder(condition = buySignal, type = OrderType.BUY_TO_OPEN, price = open[-1],tSize, name = "Buy");
 
markos

markos

Well-known member
VIP
Warehouse
@Zachc Excellent question. I hope someone can come up with the answer.. :)
 
Last edited:
Zachc

Zachc

Member
VIP
Warehouse
I thought about it overnight and realized I just need a loop which will add or subtract my profit from the "Portfolio" it turns out thinkScript utilizes a function called Fold which does exactly this. Therefore I just need to create a function that will iterate every time a trade is made...I will be playing around with this idea for the next few days. Just applying some simple static 5% sizing to some of my older backtests have greatly improved returns the goal is to write it as a code snippet so it can be injected into any strategy on the platform.

@markos I have hung out in the thinkscript lounge a bit not sure I am ready to pull out the big guns asking Mobius. I would rather fail a few times first.
 
markos

markos

Well-known member
VIP
Warehouse
@Zachc probably a good idea. Good luck with the fold!
 
Zachc

Zachc

Member
VIP
Warehouse
I am starting to see why no one has already written this script... thinkScript was just not designed to do what I am trying to do.
 
BenTen

BenTen

Administrative
Staff
VIP
Warehouse
@Zachc I don’t think its possible :(
 

Top