areed1192 / interactive-broker-python-api

A python packaged used to interact with the Interactive Brokers REST API.
MIT License
374 stars 122 forks source link

More Order Samples! #24

Closed windowshopr closed 3 years ago

windowshopr commented 3 years ago

Hey!

I just discovered your repo and YouTube video series. Excellent work! I'm looking to use your application to build my strategy using Interactive Brokers.

What I am a bit confused about, is how to create "custom" types of orders. The IB API docs are confusing and don't really highlight what I'm after. (I typed out a request form on their website and hit "Send" where I was then greeted with a "Something went wrong, please try again later" error message eye roll). I'm thinking this might be better served here anyway as I'm hoping we might discover some new order types to add to your examples/samples to help others!

To keep things simple, I'll pseudocode what the order I'm looking to create should look like:

**Buy Stop-Limit Order for 100 shares of AAPL WITH Trailing Stop Loss.**
Stop price (to trigger the buy Limit order) - $25.00
Limit price (once Buy Stop price is triggered) - $30.00
Trailing Stop Loss (once Buy Stop price is triggered) - $20.00
Trailing Stop Amount (to increment every X) - $1.00

I assume one could make some kind of bracket order, just leaving the Take Profit stuff blank as it's not needed in this case? Some potential issues I see with this above order might be:

1) Is it even possible to trigger a Buy Limit order with Trailing SL order like this? 2) If it is, if the order only gets a partial fill before exceeding the limit price (say only 56 shares get filled), can the Trailing SL be fitted to sell the entire position, regardless of the share size?

Looking at the sample for limit orders included in this repo's docs, and this webpage, I would envision it should look something like:

[
    // STOP-LIMIT BUY ORDER, ENTER A POSITION.
    {
        "conid": 251962528, # <- or whatever AAPL's conid is
        "secType": "362673777:STK",
        "cOID": "limit-buy-order-1",
        "orderType": "STP LMT",
        "lmtPrice": 30.00,
        "auxPrice": 25.00
        "side": "BUY",
        "quantity": 100,
        "tif": "DAY"
    },
    // TRAILING STOP, EXIT A POSITION.
    {
        "conid": 251962528,
        "secType": "362673777:STK",
        "cOID": "market-sell-order-1",
        "orderType": "TRAIL",
        "trailStopPrice": 20.00,
        "trailingPercent": 0.01, # <- 1%? Is there a way to make this $1.00 as described above?
        "side": "BUY",
        "quantity": 100,  # <- is there a way to make this 100% of position?
        "tif": "DAY"
    }
]

I noticed there might be something to do with defining the transmit attribute too, which is not included above (as I have no clue what I'm doing yet XD)

areed1192 commented 3 years ago

I just added some more examples of order types in the samples folders. Hopefully, they help you out. :)

As for your order, I think that would work. Your best bet is to try it with the paper account and see if the behavior is what you expect.

windowshopr commented 3 years ago

Wicked! I think I can work with the added samples! Thanks a lot!