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



  • 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?


  • administrators

    @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.



  • 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.



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



  • 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?



  • 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.



  • @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)?



  • @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.


  • administrators

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