The idea is to buy, exit with profit or stop loss target, wait and hour and start over again.
From the log it seems:
- The stop order never logged (although they have been filled)
- Repeating buy orders on 2020-01-29 and 2020-02-26 even I used hourly data and set if not self.Portfolio[self.symbol].Invested to prevent repeating
- There are Order Error: id: 88, Insufficient buying power to complete order. Are these the result of repeating orders?
Thanks
Ruming
Ruming Jiang
I thought every time order gets generated(placed), filled or cancelled by SetHoldings or Liquidate method in OnData, OnOrderEvent should be called. However by setting breakpoints at def OnOrderEvent, the algo doesn't seem to go there ever. What went wrong?
Thanks
Louis Szeto
Hi Ruming
self.fillPrice will not be the filled price you sent out the market order by SetHoldings, it takes time to get the true fill price but you already sent the stop order out. Same for other metrics. Also the filledQuantity will not be valid as the order could be splitted in SetHoldings. Usually we use Limit Order to better control our entry and exit in profit taking/stop loss. It is ok for liquid stocks like AAPL. Of course you can still use Market Order and Update the profit/stop order's limit/stop price after the entry order is filled.
Your order ticket is NOT equal to order ID. You need to use method .OrderId.
Your order is sent out does not mean it is filled. You can still have multiple orders sent in your way. So you might get insufficient margin.
Cheers
Louis
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.
Ruming Jiang
Hi Louis,
Many thanks! Let me try to summarize my errors and corrections here:
6. The attached backtest looks more sensible as the limit order to enter long never gets filled after Mar 24 the lowest point through Jun 30.
7. Where to find the sequence of OnData and OnOrderEvent? Does status of orders places last hour come in before or after current hour data?
Thanks
Louis Szeto
Hi Ruming
In my humble opinion:
6. Stated in 1. & 5. for preliminary solution. Also use the framework in 4. may helps. Or you may set some condition to force trade.
7. That will need software engineer of LEAN to answer.
I include these in the backtest for your need to update stop/exit price after filled primary trade, but personal recommendation is not to have them for price action strategies.
Louis
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.
Louis Szeto
But basically OnData runs after you received a slice of data, and OnOrderEvent runs after orders are initiated and their status changes.
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.
Ruming Jiang
Hi Louis,
Thanks for the insights. The algo itself is no important. I used hourly data to reduce resource and help me see through the timing of all these events easier. From my questions I do see a good framework would be extremely valuable.. Everything looks much clear except for three:
- Adding log after liquidation, I found liquidation orders being placed throughout a whole hour from day to day like following. Adding self.stat in front of liquidation solved it but I still wondered how it could happen. Similar problem on Jan 28 where orders are placed at 9:31 and 15:01. As OnData is only triggered by hourly data, I only expected all actions happen at hourly edges. Where do these 1min addtions come from?
2020-04-06 10:17:00 :LIQUIDATION ORDER PLACED2020-04-06 10:18:00 :LIQUIDATION ORDER PLACED2020-04-06 10:19:00 :LIQUIDATION ORDER PLACED+2020-01-28 09:31:00AAPLBuy LimitLimit: $76.33226877625 USD
1333CanceledLiquidated+2020-01-28 09:31:00AAPLSell Stop MarketStop: $75.57 USD
-1333CanceledLiquidated+2020-01-28 15:01:00AAPLBuy LimitFill: $78.647314442 USD
Limit: $78.647314442 USD
1294FilledLimit Price: ¤78.65+2020-01-28 15:01:00AAPLSell Stop MarketFill: $77.86 USD
Stop: $77.86 USD
-1294FilledStop Price: ¤77.862. I added logs in OnOrderEvent trying to see timing of all order events (New, Submitted, Filled & Cancelled). I can only go through after commenting out the Submitted section in the end otherwise it keeps giving following error, although same code were used lines above with no issue.
Runtime Error: AttributeError : 'GeekyYellowGreenArmadillo' object has no attribute 'stopOrder' at OnOrderEvent elif orderEvent.OrderId == self.stopOrder.OrderId: === at Python.Runtime.PyObject.Invoke(PyTuple args in main.py:line 95 AttributeError : 'GeekyYellowGreenArmadillo' object has no attribute 'stopOrder'
Even if I comment out the last section, the New and Submitted order events do not seem to be logged:
2020-01-02 11:00:00 :LONG ORDER FILLED @ 73.616969751 STOP UPDATED @ 72.88 TARGET UPDATED @ 75.832020-01-06 10:00:00 :STOP ORDER FILLED @ 72.88 NEXT ENTRY TIME 2020-01-06 11:00:002020-01-06 12:00:00 :LONG ORDER FILLED @ 73.636735349 STOP UPDATED @ 72.9 TARGET UPDATED @ 75.852020-01-09 10:00:00 :STOP ORDER CANCELLED2020-01-09 10:00:00 :LIQUIDATION ORDER FILLED @ 76.176614692 STOP ORDER CANCELLED NEXT ENTRY TIME 2020-01-09 11:00:002020-01-09 12:00:00 :LONG ORDER FILLED @ 76.3693292725 STOP UPDATED @ 75.61 TARGET UPDATED @ 78.662020-01-17 16:00:00 :STOP ORDER CANCELLED3. There is one error in the log says zero quantity
2020-01-17 16:00:00 :STOP ORDER CANCELLED2020-01-21 09:31:00 :Unable to submit order with id -10 that has zero quantity.2020-01-21 10:00:00 :LIQUIDATION ORDER FILLED @ 78.38294956875 STOP ORDER CANCELLED NEXT ENTRY TIME 2020-01-21 11:00:00Thanks
Louis Szeto
Hi Ruming
Cheers
Louis
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.
Ruming Jiang
Hi Louis,
I do appreciate your help with so many questions. There is value to see the challenges dealing with order mechanism but more importantly help me realize the value of the framework which I didn't when saw it in boot camp.
Thanks,
Ruming
Louis Szeto
welcome ;)
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.
Ruming Jiang
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!