Movatterモバイル変換


[0]ホーム

URL:


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

Clarification about number of trades and available liquidity#1193

Unanswered
sotos82 asked this question inQ&A
Discussion options

Hello,
I am new to Backtesting.py and I have the following question:

Within my strategy class I use the following to place an order based on some condition.
self.buy(sl = stopLoss, tp = takeProfit, size= .99999)

Before the run I also set:

bt = backtesting.Backtest(df_Test, MyStrategy, trade_on_close = True, exclusive_orders = False, cash = initialCash)

Here is part of the trading plot
1

Can someone explain to me why Backtesting starts another trade (see arrow) before the active trade becomes inactive since there is not enough available liquidity because size= .99999 (So if I understand the whole initial capital is committed to the previous trade)??

thanks

You must be logged in to vote

Replies: 2 comments 2 replies

Comment options

You can add condition in front of the self.buy:

for example, from:

if (1 == 1):    self.buy()

to:

if (1 == 1 and not self.position.is_long):    self.buy()
You must be logged in to vote
2 replies
@sotos82
Comment options

Hello, thank you for your answer. I understand about conditions.

My question however was why it initiates another trade if the whole capital is committed to the 1st trade (size = 0.9999) still active and there is no money left.

@iimka
Comment options

Maybe yourinitial cash is too large.

After you buy 99.999% your cash in stocks, your remaining 0.001% cash may still be able to buy a small amount of stock. You can take a look at the Profit/Loss graph to see how much size you bought in the second action.

You can delete the code 'size= .99999' or add the 'not self.position.is_long' to avoid the problem.

Comment options

May be your present position is generating some profit which is used for taking the trade. As per documentation If size is a value between 0 and 1, it is interpreted as a fraction of current available liquidity (cash plus [Position.pl] minus used margin).

You must be logged in to vote
0 replies
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Category
Q&A
Labels
None yet
3 participants
@sotos82@rvk80@iimka

[8]ページ先頭

©2009-2025 Movatter.jp