How to self.Liquidate() in a StopMarketOrder

Back

Hello, I'm new to QuantConnect and pretty new to Python in general; I'm trying to build a simple strategy that uses the RSI as a signal and the PSAR indicator for where the Stop Order should be placed. I know that in order to add a corresponding Stop Market Order, we need to add the following line after the MarketOrder line:

self.StopMarketTicket = self.StopMarketOrder("symbol", Quantity, stopPrice)

How do I make it so that if this Order is executed then the entire position should be liquidated? I'm not sure how to accomplish that if I can only set a fixed quantity, maybe I'm missing something important. 

I also have another question about using indicator values in if statements for when to open orders. I understand you can say something like this if the indicator only outputs 1 value such as the RSI:

self.RSI_OB = 70

self.RSI_OS = 30

if not self.Portfolio.Invested:

    if self.RSI.Current.Value < self.RSI_OS:

        self.SetHoldings("SPY", 1)

 But what if I want to refer to an indicator such as the Aroon indicator that outputs 2 values? How do I only refer to one of the values? I assumed it would've been something like self.AROON.downPeriod or self.AROON.downPeriod.Current.Value but that didn't seem to work, I would greatly appreciate it if anyone could help me out, thanks!

 

Update Backtest







 
0

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.


Hi Vincent,

If we wish to use a stop market order to liquidate our entire position, we need to set the quantity of the order to the size of our position. If we increase our position size before the order is executed, we will update the quantity of our stop market order. This can be accomplished using the OrderTicket for our stop market order.

order_ticket = self.StopMarketOrder(symbol, quantity, stop_price)order_ticket.UpdateQuantity(new_quantity)

Learn more about updating orders in the documentation on trading and orders.

We can access the two components of the Aroon Oscillator with self.AROON.AroonUp and self.AROON.AroonDown. The details for what components make up an indicator are in each indicator's API file. For example, here is the one for the AroonOscillator. You can find the API file for an indicator by searching in the Lean repository.

Best
Rahul

0

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.


Okay I got the part about updating the Order Ticket with the updated Quantity, but what do i put exactly in the "quantity" parameter? Because I enter into positions using SetHoldings() so the sizing will be dynamic. 

What if I did something like, as soon as the Order Ticker for the position is made, I save the Quantity of that order to a variable and then use that variable in the place of the "quantity" parameter, or something like:

self.SetHoldings("SPY", 1)

spynum = self.Portfolio["SPY"].Quantity

self.stopMarketTicket = self.StopMarketOrder("SPY", spynum, 0.90 * self.Securities["SPY"].Close)

 

Would this work? Or was this what you were trying to explain to me?

 

0

Hey Vincent,

That's a very good solution! However, it takes one time step for the order to be completed, which means your portfolio will not be immediately updated. To work around this, we can calculate the order size with

spynum = self.CalculateOrderQuantity("SPY", 1)

This calculates the number of shares you can buy given the portion of your margin you want to dedicate to that order. Make sure to also use a negative quantity for your stop loss in order to signal it is a sell order.

self.SetHoldings("SPY", 1)
spynum = self.CalculateOrderQuantity("SPY", 1)
self.stopMarketTicket = self.StopMarketOrder("SPY", spynum, 0.90 * self.Securities["SPY"].Close)

Best
Rahul

1

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.


This is super helpful, thank you Rahul!

0

Update Backtest





0

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.


Loading...

This discussion is closed