For code/output blocks: Use ``` (aka backtick or grave accent) in a single line before and after the block. See: http://commonmark.org/help/

Incorrect values in quickstart sample: shows original purchase value instead of sales value



  • I'm seeing incorrect order execution values in the quickstart guide example, quickstart07.py located in backtrader/docs2/quickstart of the backtrader source tree. Thisexample sets up a test strategy that buys every time a price goes down twice in a row and sells after 5 bars. When I execute quickstart07.py, it outputs

    Starting Portfolio Value: 100000.00
    2000-01-03, Close, 29.53
    2000-01-04, Close, 26.92
    2000-01-05, Close, 25.50
    2000-01-05, BUY CREATE, 25.50
    2000-01-06, BUY EXECUTED, Price: 25.04, Cost: 25.04, Comm 0.03
    2000-01-06, Close, 24.00
    2000-01-07, Close, 25.84
    2000-01-10, Close, 28.94
    2000-01-11, Close, 28.09
    2000-01-12, Close, 26.41
    2000-01-13, Close, 26.27
    2000-01-13, SELL CREATE, 26.27
    2000-01-14, SELL EXECUTED, Price: 27.25, Cost: 25.04, Comm 0.03
    2000-01-14, OPERATION PROFIT, GROSS 2.21, NET 2.16
    ...
    

    Firstly, the close prices are different from what the quickstart gives, which is weird but OK, I guess.

    More importantly, the cost of the sales is a copy of the cost of the corresponding purchase. This holds for all of the remaining buy/sell pairs output from this example (as far as I've eyeballed).

    Because this code is buying and selling 1 contract at a time, common sense tells me -- and the quickstart (https://www.backtrader.com/docu/quickstart/quickstart.html#the-broker-says-show-me-the-money) (*1) confirms -- that this cost should instead match the price of the sales like this:

    Starting Portfolio Value: 100000.00
    2000-01-03, Close, 29.53
    2000-01-04, Close, 26.92
    2000-01-05, Close, 25.50
    2000-01-05, BUY CREATE, 25.50
    2000-01-06, BUY EXECUTED, Price: 25.04, Cost: 25.04, Comm 0.03
    2000-01-06, Close, 24.00
    2000-01-07, Close, 25.84
    2000-01-10, Close, 28.94
    2000-01-11, Close, 28.09
    2000-01-12, Close, 26.41
    2000-01-13, Close, 26.27
    2000-01-13, SELL CREATE, 26.27
    2000-01-14, SELL EXECUTED, Price: 27.25, Cost: 27.25, Comm 0.03  ## Note the change in Cost here!
    2000-01-14, OPERATION PROFIT, GROSS 2.21, NET 2.16
    

    What am I missing here?

    (*1) Technically, the code in that linked page is slightly different from quickstart07.py ; I couldn't locate an exact copy. But I'm pretty sure the differences are immaterial here.

    PS. This is probably the same issue as the following post, though due to the poster's vague language the point didn't seem to come across back then:
    Re: Sell executed 'notify_trade' showing orignal purchase value



  • Just to clarify, what are you expecting the Cost value to be when the sell is executed?

    Referring to the example here:
    https://github.com/mementum/backtrader/blob/master/docs2/quickstart/quickstart07.py

    order.executed.price (listed as "Price:" in the print statement) will give you the price at which the sell was executed
    order.executed.value (listed as "Cost:" in the print statement) will (as I recall) give you the value of the original order

    I'm not clear from your question exactly what data you're looking for though, or are you just curious why they're different?



  • @alpha
    I also wonder about the discrepancy. The closing price is 26.27. The sell creation order is 26.27. But then price and cost of that order is 27.25. There could be something like slippage, but in the tutorial no slippage is being set. The value 27.25 is not in the CSV either so it's generated somehow.

    2000-01-13, Close, 26.27
    2000-01-13, SELL CREATE, 26.27
    2000-01-14, SELL EXECUTED, Price: 27.25, Cost: 27.25, Comm 0.03  ## Note the change in Cost here!
    

Log in to reply
 

Looks like your connection to Backtrader Community was lost, please wait while we try to reconnect.