Backtrader Community

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    For code/output blocks: Use ``` (aka backtick or grave accent) in a single line before and after the block. See: http://commonmark.org/help/

    sell order completed even though not enough cash to cover value

    General Code/Help
    1
    1
    46
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • ines broto clemente
      ines broto clemente last edited by

      Hello! I am new to using backtrader and I do not have a strong technical analysis background, so I am having some difficulty understanding what is happening behind the code. I am currently attempting to implement a simple Moving Average Crossover strategy with some additional conditions.

      As far as I am aware, the signals are appearing when I want them to and they are triggering bracket orders correctly (as can be seen in the attached screenshot). However, I am struggling to understand why the second trade, of the ones defined below, is being triggered.

      In this case, the first signal attempts to open a short position of size 139 at a price of 175.67 (139175.67 =24418.13). Since the available cash is lower than the trade value (22780.51), the position is not opened. On the next step, we attempt to open a position of size 135 at 175.48 (135175.48 =23689.8). This number is still greater than the available cash, yet the order is still being accepted and executed.

      2022-12-19 -> order margin
      current cash: 22780.51
      openPositions : 360
      value: 86021.714
      close price: 175.67
      stopPrice: 181.13
      target: 2.8
      targetPrice: 160.3833454
      size = 139

      2022-12-20 -> order completed
      current cash: 22780.51
      openPositions: 360
      value: 85953.31461826575
      close price: 175.48
      stopPrice: 181.13
      target: 2.8
      targetPrice: 159.6613454
      size = 135

      As far as I am aware, the leverage is set to 1 (default value) and no specific margin has been set. I am currently using a simple commission scheme, which is defined as follows:

      cerebro.broker.setcommission(commission=0.002)

      Additionally, I have also set cerebro.broker.set_coc(True).

      In general, I am curious about the default logic that determines whether a Short trade is accepted or rejected based on the available cash.

      I would appreciate it if you could help me understand what is happening. Thank you very much in advance!

      Screenshot from 2023-04-19 17-13-47.png

      1 Reply Last reply Reply Quote 0
      • 1 / 1
      • First post
        Last post
      Copyright © 2016, 2017, 2018, 2019, 2020, 2021 NodeBB Forums | Contributors