Navigation

    Backtrader Community

    • Register
    • 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/

    Margin Order - why it's triggered?

    General Code/Help
    1
    1
    837
    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.
    • D
      derek2017 last edited by derek2017

      Hi,

      If multiple orders are placed on the same day and the orders are mix of buy and sell (not short, just sell part of existing stocks), will the sell orders get rejected due to margin ?

      I suppose reducing the position should not trigger any margin requirements, however during backtesting, I have seen my orders got rejected due to margin.

      In below case, Order Ref 22 got rejected due to margin, which is to reduce position from 1860 to 1643.

      2017-02-14, CREATE BUY REBALANCE: CH600028, Close: 5.840000, Old Position: 1316 New Position 1516 Oref: 19
      2017-02-14, CREATE BUY: CH600827, Close: 15.520001, Oref: 20
      2017-02-14, CREATE BUY: CH601628, Close: 26.570008, Oref: 21
      2017-02-14, CREATE SELL REBALANCE: CH000630, Close: 3.560000, Old Position: 1860 New Position 1643 Oref: 22
      2017-02-15, Order Margin, Ref: 19, Name: CH600028
      2017-02-15, Order Margin, Ref: 20, Name: CH600827
      2017-02-15, Order Margin, Ref: 21, Name: CH601628
      2017-02-15, Order Margin, Ref: 22, Name: CH000630
      

      I manually skip the buy orders on 2017-2-14 (everything else remain the same), leaving only rebalance orders, now the sell order got accepted.

      2017-02-14, CREATE BUY REBALANCE: CH600028, Close: 5.840000, Old Position: 1316 New Position 1516 Oref: 19
      2017-02-14, CREATE SELL REBALANCE: CH000630, Close: 3.560000, Old Position: 1860 New Position 1643 Oref: 20
      2017-02-15, Order Margin, Ref: 19, Name: CH600028
      2017-02-15, SELL EXECUTED, Ref: 20, Name: CH000630, Price: 3.49, Cost: 720.44, Size: -217.00, Comm 1.51
      

      It seems the order of Order matters.
      I made changes to the code and issue rebalance orders prior to buy orders. Strangely now the sell order got executed.

      2017-02-14, CREATE BUY REBALANCE: CH600028, Close: 5.840000, Old Position: 1316 New Position 1516 Oref: 19
      2017-02-14, CREATE SELL REBALANCE: CH000630, Close: 3.560000, Old Position: 1860 New Position 1643 Oref: 20
      2017-02-14, CREATE BUY: CH600827, Close: 15.520001, Oref: 21
      2017-02-14, CREATE BUY: CH601628, Close: 26.570008, Oref: 22
      2017-02-15, Order Margin, Ref: 19, Name: CH600028
      2017-02-15, Order Margin, Ref: 21, Name: CH600827
      2017-02-15, Order Margin, Ref: 22, Name: CH601628
      2017-02-15, SELL EXECUTED, Ref: 20, Name: CH000630, Price: 3.49, Cost: 720.44, Size: -217.00, Comm 1.51
      
      

      Could you please advise if this is expected behavior of backtrader? how the margin works internally? Thanks.

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