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

PNL calculation broken

  • ![alt text](0_1553812813362_Screen Shot 2019-03-28 at 3.25.52 PM.png image url)

    I'm learning the WriterFile feature. I learned how to backtest a bracket order and now I'm getting weird math.
    I have a buy and sell pair that should report a profit but instead it's reporting pnlminus of -$125.354.

    If helpful here are the settings for my Cerebro:

    Set our desired cash start
    # Add a FixedSize sizer according to the stake
    cerebro.addsizer(bt.sizers.FixedSize, stake=10)
    # Set the commission
    cerebro.addwriter(bt.WriterFile, csv=True, rounding=2, out="/Users/darianhickman/Documents/MorningstarGrep/Results.csv")

    Sheet of analysis:

  • You may want to elaborate why it should be a profit.

    Based on the data shown you open long position at 270 (stop or limit order) on 10/09 and closed at 257.53 on open on 10/12. Loss as reported.

  • administrators

    PNL calculation broken

    Should I state something like: "Your understanding of what's happening is broken." ???

    Because it is, but I would prefer not to start a conversation in such a manner.

    My recommendation:

    • Use the notify_order and notify_trade methods to see your actual execution prices and trades in real time ..

  • @backtrader @ab_trader I dug further and discovered that the buy column value is the value that initiated a buy order. There is no column for the executed buy price. That would be incredibly helpful. I have dug through the source of Writerfile and noticed none of the schema is hardcoded there. I also discovered that I don't have easy access to all the info I need in the notifiy_order function defined in a strategy to just make my own alternative dataframe to write to file. Do you have thoughts on how to create that column that captures the executed price in the results report?

  • @darian-hickman if you want to use bt writer than you can write an observer/analyzer which tracks in and out prices using notify_order or notify_trade. Some time ago I've created extended trades table and used notify_trade and tradehistory to get in and out prices. Check the link: Closed trade list (including MFE/MAE) analyzer

Log in to reply