Hi, i have a problem updating all trailing stops for my portfolio.
First, the inital stop loss is made during the order event:
def OnOrderEvent(self, event):
# fetch the order of the event
#INITAL STOPLOSS
order = self.Transactions.GetOrderById(event.OrderId)
if order.Type == OrderType.Market and orderEvent.Status == OrderStatus.Filled:
self.stopMarketTicket = self.StopMarketOrder(event.Symbol, -event.Quantity, event.FillPrice*0.90)
In the OnData part, i want to update the Stoploss as a Trailing stop like this:
#Trailing Stop################################################
for kvp in self.Portfolio: # loop through portfolio
self.StockWatermark = -1 # init
self.StockClosePrice = self.GetPrice(kvp.Key)
if self.StockClosePrice > self.StockWatermark: #and not(self.stopMarketTicket == none):
self.StockWatermark = self.StockClosePrice # set new watermark high for this stock
updateFields = UpdateOrderFields()
updateFields.StopPrice = self.StockWatermark * 0.8
self.stopMarketTicket.Update(updateFields) ###
How do I use an Array of “self.stopMarketTicket” for all stocks in my portfolio and use it in the OnData part?
Should i pass it through the function like def OnOrderEvent(self, event, Array): , and same in Ondata def OnData(self, data, Array) ?
Thanks for your help, Regards Chris
Varad Kabade
Hi Chris,
We recommend creating a dictionary keyed by symbol to store the tickets inside the initialize method:
then:
To save the ticket and use that in the OnOrderEvent.When the order is filled or canceled, we can remove the object from the dict.
Alternatively, you can use :
to get the list of all open orders, and you can check the order type to iterate only through the StopMarketOrders.
Best,
Varad Kabade
Toaster
Thanks for the answer, so i solved problem Nr.1 but not Nr.2:
I assign the Array in OnOrderEvent with event.Symbol
In Ondata, i dont have event.Symbol, so i want to iterate like this:
kvp.Key is the wrong “loop through” variable, but i dont know why?
Toaster
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!