Hey guys. Looking for some help on something. In interactive brokers you can place a futures contract order to purchase a contract and at the exact same time place a bracket order which is a corresponding sell Order at a certain value and a stop loss at a certain value. You guys know anyway to do this in quantconnect? Without rolling back into ondata? I want it to be pretty much instant.
I could let the order go through and save the futures purchase price and then on the next run of ondata place a sell order and stop loss for the purchase price plus or minus a certain ATR value for example but I don’t want to wait until the next minute. Is there a way to do something like that or place orders in the section the provides order data when orders execute? Like the order execution section could immediately run orders to bracket an order only when an order goes through and have it immediately done? Problem with that is that when one of the bracket orders executes it may try to bracket again? I might make a list function to let the algo know if I’m in an order and needs brackets and then brackets will fire, and a separate list function that says I’ve got an order in place so if a bracket order fires to reset the list function and not bracket again…
Big thanks in advance!
David
Mia Alissi
Hey David,
You can achieve this by using a combination of limit orders and stop limit orders. When one of the orders fills, you can cancel the other orders in the bracket using the
OnOrderEvent
event handler. Here's an example in Python:This code snippet will create a stop loss and take profit order when your initial order is filled. When either the stop loss or take profit order is filled, it will cancel the other order.
Make sure to set the brokerage model to a brokerage that supports these order types, such as:
For more information on order types, you can refer to the QuantConnect documentation on trading and orders.
I hope this helps!
The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.
Nico Xenox
Hey David Eldringhoff,
I copied and pasted a sample code below on how I did this:
This part gets triggered as soon as a position is filled.
If you're using limit orders just tag your buy order with a name and then check if the limit order that got filled also has the same tag. You would do this so that you dont get any problems with limit order from the sell side.
Hope it helps.
Best,
Nico
David E
Thanks to both of you. I've got it close to working but the backtests seem to either have a loop in the onorderevent section where it just buys and sells the limit and stop loss orders and 10000 orders a day occur or both the limit and stop loss fire at the exact same time as the initial order placement which doesnt make sense to me.
Nico Xenox
Hey David Eldringhoff,
I forgot a crucial part in my code that cancels the rest of the bracket when one order gets triggered. This is the rest of the code:
Again, if you're using limit orders to enter the market and not a market order you will have to add an if statement to the limit order that checks that there is no tag like you used in the limit order for entering the market.
This should solve the problem you're having with the numerous orders you're having.
Best,
Nico
David E
Do you have issues in backtesting where the purchase order is placed and then the limit or stop loss fill at the exact same time as the initial order? it happens on every order.
+2023-06-01 00:22:00NQ16M23Sell MarketFill: $14,286.50 USD
-1Filled +2023-06-01 00:22:00NQ16M23Buy LimitFill: $14,284.00 USD
Limit: $14,284.00 USD
1FilledLimit Price: ¤14,284.00+2023-06-01 00:22:00NQ16M23Buy Stop MarketStop: $14,289.00 USD
1CanceledStop Price: ¤14,289.00+2023-06-01 02:16:00NQ16M23Sell MarketFill: $14,300.00 USD
-1Filled +2023-06-01 02:16:00NQ16M23Buy LimitFill: $14,296.75 USD
Limit: $14,296.75 USD
1FilledLimit Price: ¤14,296.75+2023-06-01 02:16:00NQ16M23Buy Stop MarketStop: $14,303.25 USD
1CanceledStop Price: ¤14,303.25Nico Xenox
Hey David Eldringhoff,
I'm guessing that you're using tick resolution? a few months ago I made a algo which seemed to work really well. The results where amazing and it also worked with futures, the backtests where extremly good and it seemed like I could get rich really fast. Now everytime this happens something can't be right. I had similar execution times like you and I even tried it live with the paper trading node. Sometimes I was winning 6k-10k a day by doing absolutly nothing. When putting the same algo with IB to the test the results were nowhere near. They were even negative!
So be careful when playing around with tick data. I guess that in the backtest and in live trading the algo will be able to reach every upward tick while in the real market it will fail misserably
There were times in real market trading where the algo would buy and sell in the same second but didn't happen often
David E
Can anyone with Quantconnect chime in? The real main issue here I think is that we need bracket order support with Interactive brokers and a correct representation of bracket orders on backtesting. It looks like its not supported correctly with Quantconnect.
If I place a futures order manually on Interactive brokers , prior to submitting the order, I have the ability to attach a bracket order. I can bracket the order with a limit order and stop loss. Importantly, when doing so it does not increase margin requirements.
I can try to reproduce this by using the techniques above (and I built a manual bracket like this in the ondata section which actually works much better than the OnOrderEvent section but still increases margin requirements when it shouldn't) but they do not work for a few reasons. One reason, is that by doing so the orders are submitted separately and therefore require additional margin. The original order for the underlying plus extra margin for the extra orders. I've tested this on IB, if an order for a futures contract is placed followed by a separate limit and stop order it will use additional margin, whereas if you place the original order with a bracket attached the additional margin is not required. With bracket orders the additional order is automatically cancelled when either the limit or stop is triggered. These are reasons why we could benefit from bracket order functionality.
Additionally, this doesn't work because the orders are filled immediately for some reason. I've adjusted the stop distances and they still fill instantly. I initially suspected that it was filling because the stops were within range of the current bar but even if I extend the stops out, they still fill instantly. There is something incorrectly processing here.
Thanks
David E
OK couldn't fix the OnOrderEvent bracket orders. They are still closing the exact same minute that they are placed which doesn't make sense. However, if I switch to place the bracket orders in the OnData section they work more normally. However, it caused another issue. Most orders work as planned but some have both the limit order and the stop market order fire in the same minute. I suspect this is a data problem and here is why. I'm trying to scalp for an ATR value in futures contract in only the overnight hours, where volatility is low. The ATR is about 2-5 points on a minute time frame. Below you will see a few normal orders followed by one where both bracket orders fill at once. The stop market order was placed for 1 ATR outside the fill price of the contract I'm holding but the stopmarket order filled like 50 points outside the value of the order. On trading view, the price did not fluctuate anymore than a few points in that minute of the day. It definitely did not move >50 points in that minute.
2023-01-03 00:04:00NQ17H23Sell Stop MarketNORMAL orders with brackets:
Stop: $11,024.25 USD
-1Canceled +2023-01-03 00:42:00NQ17H23Sell MarketFill: $11,045.75 USD
-1Filled +2023-01-03 00:42:00NQ17H23Buy LimitFill: $11,041.75 USD
Limit: $11,041.75 USD
1FilledLimit Price: ¤11,041.75+2023-01-03 00:42:00NQ17H23Buy Stop MarketStop: $11,051.00 USD
1Canceled +2023-01-03 01:33:00NQ17H23Sell MarketFill: $11,060.00 USD
-1Filled +2023-01-03 01:33:00NQ17H23Buy LimitFill: $11,057.00 USD
Limit: $11,057.00 USD
1FilledLimit Price: ¤11,057.00+2023-01-03 01:33:00NQ17H23Buy Stop MarketStop: $11,063.75 USD
1Canceled +2023-01-03 01:43:00NQ17H23Buy MarketFill: $11,042.25 USD
1Filled +2023-01-03 01:43:00NQ17H23Sell LimitFill: $11,045.75 USD
Limit: $11,045.75 USD
-1FilledLimit Price: ¤11,045.75INCORRECT order with brackets:
2023-01-03 02:45:00NQ17H23Sell MarketFill: $11,062.50 USD
-1Filled +2023-01-03 02:45:00NQ17H23Buy LimitFill: $11,057.75 USD
Limit: $11,057.75 USD
1FilledLimit Price: ¤11,057.75+2023-01-03 02:45:00NQ17H23Buy Stop MarketFill: $11,119.50 USD
Stop: $11,068.00 USD
1FilledStop Price: ¤11,068.00David E
actually I just noticed these aren't working right either. The limit orders to close for profit are still firing the same minute that the initial order is placed…. Not every single order I place wins the exact minute I place the order. Something isn't working right.
David E
The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.
To unlock posting to the community forums please complete at least 30% of Boot Camp.
You can continue your Boot Camp training progress from the terminal. We hope to see you in the community soon!