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

List of Orders from Margin Trading



  • I am trying to backtest an external set of orders. I know that I need to call

    cerebro.add_order_history(ORDER_HISTORY, notify=False)
    

    I want to run my bot on a margin trading account, but now I am confused. I know my "orders" need to be in this format:

    ORDER_HISTORY = (
    ('2005-02-01', 1, 2984.63),
    ('2005-03-04', -1, 3079.93),
    ...
    ('2006-12-18', 1, 4140.99)
    )
    

    But for the size, how would I represent going short to long or long to short? For example, my strategy will be in a long position, and when it closes the position, it will enter a short at the same time. I thought this might've worked but it definitely didnt.

    ORDER_HISTORY = (
    ('2005-02-01', 1, 2984.63),
    ('2005-03-04', -2, 3079.93),
    ('2006-12-18', 2, 4140.99)
    ...
    )
    

    Thanks for any help


  • administrators

    @doggo said in List of Orders from Margin Trading:

    But for the size, how would I represent going short to long or long to short?

    Orders never represent going long to short or viceversa. Orders have a size. If the final outcome is being in a long or short position that depends on the position you were holding on the asset before the order was issued.

    @doggo said in List of Orders from Margin Trading:

    I thought this might've worked but it definitely didnt.

    What didn't work?

    What's the importance here of "I want to run my bot on a margin trading account" or the relationship to the order history? (Margin is also mentioned in the title, so it seems it is important to you)



  • Thanks for the quick reply. So using 1 then -1 for the next order size, wouldnt I just be opening a long and then closing it, without going into a short?

    Regarding what didn't work, I meant the last bit of code in my original comment. I tried using 1, -2, 2.. alternating for the order size. I thought this would be equivalent to opening a long (1), then going from 1 to -1 by having the second order size as -2 (1 -2 = -1) would be closing long into short. Am I going about this the completely wrong way? I am pretty confused. Thanks for the help.

    edit: I wanted to ask, if I am sending an order_history of length 100 (100 trades), the trade analysis tells me I only had 50 completed trades. This lead me to believe I am doing something wrong, for example if my first order has a size of 1, then backtrader is only registering my longs being closed and not the shorts I enter when my long closes. I hope this make sense and thanks again.



  • @backtrader I will try to explain from scratch what I am trying to do.

    I should pre-face with the fact that I am still pretty noob with python. This has made interpreting the docs very difficult for me. Most of the time I can get things working, but this has stumped me for a few days now, and I really need some guidance.

    Anyways... I have a list of orders (I ran my strategy myself to produce this list of orders, without the use of Backtrader), and each order reverses the position from long-short or short-long. I am trying to feed backtrader my list of orders so that I can do some analysis on the results. My problem is, that I can't figure out how to get Backtrader to recognize my orders as reversals (The "completed trades" stat is half of what it should be, as it seems to be opening and closing longs only).

    After doing some more research, I know that it has to do with sizers. I have found a couple relevant blog entries, but I still can't seem to get it to behave correctly and register my orders as position reversals. I also want to ensure that my entire balance is being used for each trade, so I don't want my order size to be fixed (can't seem to figure this one out either).

    Once I am home in a couple hours, I will post my code so that its easier to help me. Thank you, I really appreciate the help.


  • administrators

    @doggo said in List of Orders from Margin Trading:

    My problem is, that I can't figure out how to get Backtrader to recognize my orders as reversals

    Let me formulate it again: orders are order and are not interpreted as anything. You are interpreting and you expect something which cannot be.

    @doggo said in List of Orders from Margin Trading:

    After doing some more research, I know that it has to do with sizers

    If you supply a history of orders, the size specified for each order will be used no Sizers are involved. What's the role of Sizers here?

    @doggo said in List of Orders from Margin Trading:

    Regarding what didn't work, I meant the last bit of code in my original comment. I tried using 1, -2, 2.. alternating for the order size

    That's clear, but it still doesn't show what didn't work, because no code and/or output log is given.

    @doggo said in List of Orders from Margin Trading:

    I have found a couple relevant blog entries

    Which ones?

    @doggo said in List of Orders from Margin Trading:

    @backtrader I will try to explain from scratch what I am trying to do.

    I should pre-face with the fact that I am still pretty noob with python. This has made interpreting the docs very difficult for me. Most of the time I can get things working, but this has stumped me for a few days now, and I really need some guidance.

    Anyways... I have a list of orders (I ran my strategy myself to produce this list of orders, without the use of Backtrader), and each order reverses the position from long-short or short-long. I am trying to feed backtrader my list of orders so that I can do some analysis on the results. My problem is, that I can't figure out how to get Backtrader to recognize my orders as reversals (The "completed trades" stat is half of what it should be, as it seems to be opening and closing longs only).

    After doing some more research, I know that it has to do with sizers. I have found a couple relevant blog entries, but I still can't seem to get it to behave correctly and register my orders as position reversals. I also want to ensure that my entire balance is being used for each trade, so I don't want my order size to be fixed (can't seem to figure this one out either).

    Once I am home in a couple hours, I will post my code so that its easier to help me. Thank you, I really appreciate the help.

    If you are using a history of orders, how are you planning to use your entire balance? The specified size will be used.

    @doggo said in List of Orders from Margin Trading:

    so I don't want my order size to be fixed (can't seem to figure this one out either).

    If you supply a history of orders, then the size is simply FIXED.

    Bottomline

    • You are telling us that order history is not working
    • But that it has to do with Sizers (which don't apply when working with history)
    • That you want to apply your full balance (but you are supplying sizes in form of history)
    • And that you don't want your orders to have a fixed size (which is the basis of using a history of orders)

    It simply seems you are mixing different and incompatible things.