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/

    How to code and track stop loss orders

    General Code/Help
    3
    9
    1611
    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.
    • C
      chaT123 last edited by

      Can I have an example of when I should code in stop loss order?
      The best practice is I submit a stop loss order at the same time I submit the order to open a position?
      Also how can I track what are the stop loss order outstanding (size, and limit price) every day in the WriterFile csv file?

      B 1 Reply Last reply Reply Quote 0
      • B
        backtrader administrators @chaT123 last edited by

        @chat123 said in How to code and track stop loss orders:

        Can I have an example of when I should code in stop loss order?

        There is no definitive answer as to when you should code in a stop-loss order. Some people want them, some people don't and some people want them issued at different times.

        @chat123 said in How to code and track stop loss orders:

        The best practice is I submit a stop loss order at the same time I submit the order to open a position?

        It is your choice. Best practice is what helps you make money and not what some other people believe to be better or worse.

        @chat123 said in How to code and track stop loss orders:

        Also how can I track what are the stop loss order outstanding (size, and limit price) every day in the WriterFile csv file?

        Orders are not written out to the CSV file. You use the notify_order method to receive notifications of orders.

        1 Reply Last reply Reply Quote 0
        • C
          chaT123 last edited by

          Thank you! So there is no way to record my order submit and execution history (e.g notify_order method) in a csv file? Also in WriterFile csv file, I want to specify which closing trade is based on stop loss order and which closing trade is based on other trading rules. e.g I want to add a column in the WriterFile csv file showing "S" if the closing trade is from stop loss order and "O" if closing trade is from other trading rules.

          1 Reply Last reply Reply Quote 0
          • C
            chaT123 last edited by

            also how can I output the order notification to a csv file instead of command line output?

            1 Reply Last reply Reply Quote 0
            • C
              chaT123 last edited by

              I'm thinking maybe I create a dataframe X with date index equal to the security price input. And in the log function, I input the stop loss order information into columns of X[0]. Then at the end, I output the dataframe as a csv file. Is this supported?

              1 Reply Last reply Reply Quote 0
              • A
                ab_trader last edited by

                If you want to have your data saved in the .csv file written by bt standard writer, then this data may come from special observer. This observer will track all data you need and will be added to writer's csv output.

                • If my answer helped, hit reputation up arrow at lower right corner of the post.
                • Python Debugging With Pdb
                • New to python and bt - check this out
                C 1 Reply Last reply Reply Quote 0
                • C
                  chaT123 @ab_trader last edited by

                  @ab_trader Thank you! Can I have an example code of an observer that output whether each trade is closed by stop loss order or market order (based on other rules)?

                  A 1 Reply Last reply Reply Quote 0
                  • A
                    ab_trader @chaT123 last edited by

                    @chat123 such observer depends on your implementation of the strategy. Without the code of you strategy, it is impossible to do. For me at least.

                    • If my answer helped, hit reputation up arrow at lower right corner of the post.
                    • Python Debugging With Pdb
                    • New to python and bt - check this out
                    1 Reply Last reply Reply Quote 1
                    • B
                      backtrader administrators last edited by

                      Or open a file with standard Python code and write out to it.

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