Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Position sizing for backtesting #1197

Answered by iimka
ek-ex asked this question in Q&A
Discussion options

When setting a value like cash=10000, what is the position size used per trade? Is it always fixed to 10,000,ドル or its using the max account value at the time of the trade?

Backtest(GOOG, SmaCross,
 cash=10000, commission=.002,
 exclusive_orders=True)

I think this will have impact on how the ratios like the Sharpe are being calculated.

TIA and cheers,

You must be logged in to vote

Setting cash=10000 simply initializes the account with 10,000ドル at the start. As the account balance changes, the amount of capital/size used for each trade is controlled by self.buy(size=...).

If you want to trade with 10,000ドル each time, I haven't tried that (since using a percentage of the account balance for trading seems more reasonable than a fixed amount as the account balance increases).

However, I guess we can use the closing price of the previous bar to calculate the number of shares that can be bought with 10,000, and then put that into self.buy(size=...). Although there may be some discrepancies, it should be close to trading with 10,000.

Day 0: Cash = 10000
Day 1: self.buy() Ca...

Replies: 1 comment 5 replies

Comment options

The position size depends on whether the size function is used in self.buy() or self.sell(). For example, if you use self.buy(size=0.63), it means you are trading 0.63 times the available cash in your account.

You must be logged in to vote
5 replies
Comment options

Thanks, but this does not answer what is the behavior when setting cash=10000. Does that mean that it will always allocate 10,000ドル for each trade regardless the account size? Or does it start the account at 10,000ドル and always allocate the total account size per trade.

Comment options

Setting cash=10000 simply initializes the account with 10,000ドル at the start. As the account balance changes, the amount of capital/size used for each trade is controlled by self.buy(size=...).

If you want to trade with 10,000ドル each time, I haven't tried that (since using a percentage of the account balance for trading seems more reasonable than a fixed amount as the account balance increases).

However, I guess we can use the closing price of the previous bar to calculate the number of shares that can be bought with 10,000, and then put that into self.buy(size=...). Although there may be some discrepancies, it should be close to trading with 10,000.

Day 0: Cash = 10000
Day 1: self.buy() Cash = 0 StockPrice = 1 Shares = 10000
Day 2: Cash = 0 StockPrice = 1.2 Stock = 10000
Day 3: self.close() Cash = 12000 StockPrice = 1.2 Stock = 0

Day 4: self.buy() Cash = 0 StockPrice = 2 Shares = 6000
or
Day 4 self.buy(size = 10000/2) Cash = 2000 StockPrice = 2 Shares = 5000

Answer selected by ek-ex
Comment options

Follow up question: is the Buy&Hold calculation using the same size as the strategy? Or does the buy&hold utilizes the whole account size?

Comment options

The calculation for Buy & Hold disregards the account size and instead focuses on the stock price. Even if your initial capital is zero and the stock price is high, the Buy & Hold calculation will still yield a number.

Comment options

You can refer to the following code.

class SmaCross(Strategy):
 n1 = 10
 n2 = 20
 def init(self):
 close = self.data.Close
 self.sma1 = self.I(SMA, close, self.n1)
 self.sma2 = self.I(SMA, close, self.n2)
 def next(self):
 if crossover(self.sma1, self.sma2
and not self.position.is_long #<--------- Add this code to all opening conditions.
and not self.position.is_short #<--------- Add this code to all opening conditions.
):
 self.buy()
 elif crossover(self.sma2, self.sma1
and not self.position.is_long #<--------- Add this code to all opening conditions.
and not self.position.is_short #<--------- Add this code to all opening conditions.
): 
 self.sell() #<--------- If you use 'self.close()', you need to delete the entire closing code, or add a line of code with False to the closing conditions.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants

AltStyle によって変換されたページ (->オリジナル) /