-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
-
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,
Beta Was this translation helpful? Give feedback.
All reactions
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
-
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.
Beta Was this translation helpful? Give feedback.
All reactions
-
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.
Beta Was this translation helpful? Give feedback.
All reactions
-
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
Beta Was this translation helpful? Give feedback.
All reactions
-
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?
Beta Was this translation helpful? Give feedback.
All reactions
-
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.
Beta Was this translation helpful? Give feedback.
All reactions
-
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.
Beta Was this translation helpful? Give feedback.