Hi Team, I'm trying to create my first algo and have an issue. Could you please check what I'm doing wrong? Algo only open Short positions. I know logic regarding checking where moving is before determining direction seems to be ugly, and hope you can give a hint for better solution.
Ryan Riordon
Hi Leu,
You are placing 3 orders at the same time: MarketOrder, LimitOrder and StopMarketOrder. With the "-positionSize" you are telling it to open a short position.
Leu Bar
Hi Ryan Riordon, could you please explain how to make Take profit for short position? Maybe you have some example,?Thank you!
Ryan Riordon
Leu,
Firstly I would suggest reading the documentation and getting familiar with order types:
https://www.quantconnect.com/docs/algorithm-reference/trading-and-ordersIf you are trying to control losses you could just put this in the "def Initialize": self.SetRiskManagement(MaximumDrawdownPercentPerSecurity(0.2)) where .2 is a stop loss at 20%. It doesn't matter if you are long or short, it will liquidate either one. Then you could just set up a simple limit order for the entry:
for symbol in selected_symbols:
price = self.Securities[symbol].Price
self.LimitOrder(symbol, self.cashused/price, (price * 0.99)) #put a "-" in front of the self if you want to short
You could set up a fixed amount of $ used if you put in initialize self.cashused = xxxxx.xx
There are also examples on how to update the order on that link I provided.
If you want more help you will need to very clearly say if you are invested 100% of the time, if you want market or limit orders, what your stop loss will be and wheather you are going to let the EMA crossovers determine your exit when winning or if you are going to take profit before the EMA crossovers.
Derek Melchin
Hi Leu,
In the algorithm above, since `isLong` is set in OnOrderEvent, it remains True in OnData when
self.emaFastValue > self.emaSlowValue
no longer holds. This can lead to missing entries.
In addition, the CancelOpenOrders conditional needed to be extended to include all the TP/SL order types.
if order.Type == OrderType.Limit or order.Type == OrderType.StopLimit or order.Type == OrderType.StopMarket:
These changes have been made in the attached backtest.
Best,
Derek Melchin
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.
Leu Bar
Hi Derek, thank you for your answer. But with that logic algorithm execute marked order every time when previous stop loss or take profit was executed. What I wanted to write is, that after stop loss/take profit was executed, algorithm should check how ema's positioned and make a trade only after crossover.
For exemple:
1) we hade long and stop loss was executed.
2) Checking ema's. Ema fast < ema slow. It means that we are waiting cross and will take Long (now it will take short)
That why added this
isLong = self.emaFastValue > self.emaSlowValue on OnOrderEvent. Probably should better way to express this.
Derek Melchin
Hi Leu,
Thanks for clarifying. The algorithm I previously attached was designed to trade immediately instead of waiting for cross-overs. A consequence of this is that it can take the same position after a stop-loss or take profit order is filled.
Best,
Derek Melchin
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.
Leu Bar
Hi Derek, I finally got result which I wanted to achieve or come close to it :D The issue was a stupid in
self.firstRun = False (was self.firstRun == False)
I leave changed algo here. I gues it's good first algo for beginers like me it cover a lot of QC concepts.
Leu Bar
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!