I'm trying to translate an algorithm I wrote in MultiCharts/Tradestation's Power/EasyLanguage into C#. I'm new to C# and coding in general, so this has proved rather difficult, but I'm pretty close to done now. The last remaining issue I have is I need to implement a trailing stop that functions in the same way that PercentTrailing works:
https://www.multicharts.com/trading-software/index.php/SetPercentTrailing
I take it I need to calculate the maximum profit somehow, but due to my inexperience with C# I'm not sure how I'd go about doing this. I also noticed that in the documentation there is a TrailingStop Field: https://www.quantconnect.com/lean/documentation/topic5664.html
How could I go about using this? I don't see what parameters it takes/how to actually use it as an order.
Thanks in advance!
Jing Wu
Hi Philip, sorry we don't support the trailing stop order at this time. You can find all the available order types in documentation
Douglas Stridsberg
If you want, you can emulate your own trailing stop loss by something akin to the below:
Philip Grossman
I actually implemented something similar to that, but I used a decimal list and clearing it upon exit instead of using a RollingWindow. Haven't gotten it working yet but I'm going to keep working at it. Perhaps I'll try the RollingWindow - the advantages to that would be that it runs faster/uses less memory, right?
Alexandre Catarino
Hi Philip Grossman,
As Jing pointed out, we don't support this kind of orders yet. Therefore you will be emulating the behavior or a trailing stop order. I would suggest you look at how we have implemented the Trailing Stop Risk Management Model.
Please try that approach and share your algorithm for further directions if you have other questions.
Emilio Freire
Hi Alex,
Great to see the additional Risk Management modules! I've been trying to understand the Trailing Stop one, but cannot fully see how that logic emulates a trailing stop. Would you mind briefly explaining the logic?
No worries if no time!
Thanks,
Emilio
Alexandre Catarino
In the Trailing Stop Risk Management, we record the security maximum unrealized profit point that is reached on a new maximum of highs. If the current bar high is not a new maximum, we check whether the low was far enough from the recorded maximum to trigger a liquidate order.
Say we buy ABC for $100 and the sequence of highs is $105, $106, $107, $105. At this moment, the maximum unrealized profit was $7. At this point, the last low was $102 which is a 5% drawdown from $107, If the maximum drawdown is 4%, we create a PortfolioTarget for ABC with zero quantity.
Emilio Freire
Thanks for taking the time Alex. Does this only work for Long positions then?
Alexandre Catarino
Emilio Freire, yes, unfortunately so.
I have added a new GitHub issue to fix this problem:
Extends Trailing Stop Risk Management Model for short positions #2716
The community (and you) are welcome to propose a solution.
Emiliano Fraticelli
Hi!
I'm trying to implement it in C#, a technology that I unfortunately don't know well.
Â
This is giving me error for now:Â
Can someone help me in implementing the solution proposed by Douglas above?
Varad Kabade
Hi Emiliano Fraticelli,
The above algorithm gives an error because of the input parameters in the GetRange method. The first parameter you pass in the GetRange method is the index it will start the copy, and the second parameter the number of elements that will be copied.
 Refer to the attached backtest for reference.
Best,
Varad Kabade
Emiliano Fraticelli
It works…but if you increase the length of the RollingWindow, it does not seem to work anymore.
Â
For instance the code belowe gives :
552 | 17:01:33:
Runtime Error: Offset and length were out of bounds for the array or count is greater than the number of elements from index to the end of the source collection. (Open Stacktrace)
Varad Kabade
Hi Emiliano Fraticelli,
The Rolling Window has 20 size in the above algorithm, but we are trying to access the first 200 elements, so increasing the Rolling window size fixes the error. Refer to the attached backtest for reference.
Best,
Varad kabade
Emiliano Fraticelli
Lol what a stupid…I tought about something like it but I couldn't find the other reference in the code…my bad…sorry
Newoptionz
So, just out of interest, how would we do this in python, say for the attached algo, this is copy ofÂ
, I just changed updated SetEndDate. Â The backtest does not seem to want to attach?
Newoptionz
Maybe not so much out of interest - how does one do this in python?
Thanks
Newoptionz
Well, I got a version going using self.AddRiskManagement(MaximumDrawdownPercentPerSecurity(0.05)). Â
Thanks
Philip Grossman
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!