Skip to content

order is removed due to no enough cash/margin, however, it's a sell order #328

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
zillionare opened this issue Apr 30, 2021 · 5 comments
Closed
Labels
bug Something isn't working wontfix This will not be worked on

Comments

@zillionare
Copy link

zillionare commented Apr 30, 2021

Currently position is long and there's buy trade in queue:

<Trade size=476 time=7- price=21.0- pl=652>

Now a sell signal is triggerred, and a sell order is placed, however, the order is removed/cancelled in _process_orders due to no cash/margin:

图片

logger is added by me.

Since this is a sell order, why we need cash/margin?

Expected Behavior

if the order is for sell, we need to check if we hold long position; otherwise, check if cash is enough.

Actual Behavior

the sell order is discarded due to no cash/margin. Thus no more buy/sell even buy/sell signals are fired.

Steps to Reproduce

  1. trigger a long trade with size 0.9999
  2. trigger a sell order, see if it's placed

Additional info

  • Backtesting version: 0.3.1
@gaborvecsei
Copy link

(I am not familiar with the codebase, I am a new user)

Since this is a sell order, why we need cash/margin?

Actually I think we need "some" cash as we need to pay a commission.

@kernc
Copy link
Owner

kernc commented May 9, 2021

The commission is already accounted for with spread at trade entry. If we're just selling (buying) some units we already have (owe), we should not be checking available cash.

@kernc kernc added the bug Something isn't working label May 9, 2021
@gaborvecsei
Copy link

gaborvecsei commented May 9, 2021

@kernc, just for the sake of my understanding:

  • When we are closing a position (selling), there is no commissions we need to pay?

I understand that there is a commission when we enter a trade (e.g. buy a stock), but we should also pay a commission when we are selling. At elast for Interactive Brokers, we pay commission when closing a position. (And as I know this is the same for most of the cases).

In theory I could not sell my shares on IB if I would not have enough money to pay the commission at "sell time".

@kernc
Copy link
Owner

kernc commented May 9, 2021

I understand that there is a commission when we enter a trade (e.g. buy a stock), but we should also pay a commission when we are selling. At elast for Interactive Brokers, we pay commission when closing a position. (And as I know this is the same for most of the cases).

Ah, this already came up in #149. The idea then was to just pass Backtest(commission=2*real_commission), which is approximately valid for all but the instruments with very wide price fluctuation.

In theory I could not sell my shares on IB if I would not have enough money to pay the commission at "sell time".

I don't think that's correct. You pay the commission when and upon exchanging the asset for whatever it's priced.

@kernc
Copy link
Owner

kernc commented Dec 13, 2022

  • trigger a long trade with size 0.9999

  • trigger a sell order, see if it's placed

The recommended way of closing existing trades is via Strategy.trades[i].close() or Strategy.position.close().

Alternatively, if after the initial long trade you call self.sell(size=int(long_order_size)) (rather than the equity-relative size), the behavior is as expected.

@kernc kernc closed this as completed Dec 13, 2022
@kernc kernc added the wontfix This will not be worked on label Dec 13, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working wontfix This will not be worked on
Projects
None yet
Development

No branches or pull requests

3 participants