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

Problem with position size #1134

Unanswered
raphaela13 asked this question in Q&A
Discussion options

Hi all,
I really struggle with the position sizing. From what I read, a number between 0 and 1 is suppose to be the fraction of current available liquidity. The data is only one loss to see the risk taken per trade, I have 100k cash and size = 0.01 which is suppose to risk 1000 per trade.
After the loss, the equity has only be reduced by 14$ so I really don't get it, I tried many different things but I don't understand.

The trade is a sell, it says to put a negative order size for short orders but I get an error "size must be a positive fraction of equity, or a positive whole number of units"

It could only be opening 0.01 lot but then how can we get a portion of the balance?

I attached the code and the stat display.
Thanks a lot for your help!

from backtesting import Backtest, Strategy
class AH_grab (Strategy):
 prct_trade = 0.01 # 1% risk per trade
 
 def init(self):
 super().init()
 self.signal1 = self.I(SIGNAL)
 
 def next(self):
 super().next()
 
 if self.signal1 == 1:
 sl1 = self.data.sl[-1]
 tp1 = self.data.tp[-1]
 self.sell(sl=sl1, tp=tp1, size= self.prct_trade )
 
 if self.signal1 == 2:
 sl1 = self.data.sl[-1]
 tp1 = self.data.tp[-1]
 self.buy(sl=sl1, tp=tp1, size= self.prct_trade )
 
bt = Backtest(df, AH_grab, cash = 100000.0, margin = 1/100, commission= .00)
stat = bt.run()
stat

Start 0.0
End 238.0
Duration 238.0
Exposure Time [%] 0.41841
Equity Final [$] 99986.16685
Equity Peak [$] 100000.0
Return [%] -0.013833
Buy & Hold Return [%] -0.261797
Return (Ann.) [%] 0.0
Volatility (Ann.) [%] NaN
Sharpe Ratio NaN
Sortino Ratio NaN
Calmar Ratio 0.0
Max. Drawdown [%] -0.013833
Avg. Drawdown [%] -0.013833
Max. Drawdown Duration 75.0
Avg. Drawdown Duration 75.0
Trades 1.0
Win Rate [%] 0.0
Best Trade [%] -0.013833
Worst Trade [%] -0.013833
Avg. Trade [%] -0.013833
Max. Trade Duration 0.0
Avg. Trade Duration 0.0
Profit Factor 0.0
Expectancy [%] -0.013833
SQN NaN
_strategy AH_grab
_equity_curve Equit...
_trades Size EntryB...
dtype: object

You must be logged in to vote

Replies: 1 comment

Comment options

I have 100k cash and size = 0.01 which is suppose to risk 1000 per trade.

No, with these settings it will open position for 1K and you'll have 99K cash if long or 101K cash if short.

You manage risk using stop-losses and whatever you put in Strategy.next function

You may find this repo - https://github.com/s-kust/python-backtesting-template - helpful. It's an advanced template.

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet

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