0

I am attempting to implement a trailing stop loss functionality in C# similar to what is described here:

https://www.multicharts.com/trading-software/index.php/SetPercentTrailing

Basically, if the profit rises over a certain amount and then drops by a certain percentage of that amount, it closes the order.

Unfortunately, for whatever reason, it doesn't seem to be working. It never exits when there's a profit, but the stop-loss works. I am inexperienced with C# and after spending quite a while on this I'm stumped as to what could be going wrong - my thought is that I may be using lists incorrectly. This is being written with QuantConnect/LEAN. Here is what I've written:

// 0.00025m is the conversion factor for profitPercentage into recognizable integers

var profitPercentage = Portfolio[_symbol].UnrealizedProfitPercent / 0.00025m;
var percentages = new List<decimal>();
var profitThreshold = 10;
decimal maxProfit;
decimal trailingPercent = 10;
decimal stopLoss = -10;

if (profitPercentage > profitThreshold) 
{
    percentages.Add(profitPercentage);
    percentages.Sort();
    maxProfit = percentages[percentages.Count - 1];
    if (profitPercentage < (maxProfit - (maxProfit * trailingPercent)))
    {
        SetHoldings(_symbol, 0);
        percentages.Clear();
        position = "none";
     }
} 
else if (profitPercentage < stopLoss)
{
     Console.WriteLine("Profit:" + profitPercentage);
     SetHoldings(_symbol, 0);
     percentages.Clear();
     position = "none";
} 

The stop loss seems to work fine, so profitPercentage appears to be outputting the right values. The issue seems to lie in the logic related to the list. Similarly, if I simplify the first if statement thus:

if (profitPercentage > profitThreshold) 
{
    SetHoldings(_symbol, 0);
}

This also works fine.

orad
  • 15,272
  • 23
  • 77
  • 113
Phil
  • 3
  • 2
  • 4
    nothing obvious on first sight. "it doesn't seem to be working" is not a useful error description. are the values just off, or do you get a runtime exception? it would be great if you could substitute values for dependencies like `Portfolio` so we get a minimal, complete example to debug. – Cee McSharpface Nov 20 '18 at 01:20
  • I'm not sure if the values are off, but it never exists in profit, even when it goes past the profit threshold and then back down. The stop loss seems to work fine, however. I'm not sure what values I can substitute for `Portfolio`, unfortunately. What I can say for sure though is that the `Console.WriteLine` in the `else if` statement outputs values like `-40.149284802348`. – Phil Nov 20 '18 at 01:58
  • Seemingly `profitPercentage` is returning the expected values, but isn't exiting properly when it goes above `profitThreshold`. So I'm assuming something must be going wrong with the list or the logic related to it. When I simplify the example and make it simply exit once `profitPercentage > profitThreshold` then it exits properly. For example: `if (profitPercentage > profitThreshold) { SetHoldings(_symbol, 0); }` This works fine. – Phil Nov 20 '18 at 02:04
  • you probably want trailingpercent to have a value of 0.1 then – Cee McSharpface Nov 20 '18 at 02:13
  • I've tried that - still hasn't worked unfortunately. Looking at the equity curve, there are trades that were entered, went up over 100%, and then still exited at a 10% loss. – Phil Nov 20 '18 at 02:53
  • 1
    Watch a 10 minute YouTube video on Debugging in Visual Studio, you need to step through the code, line by line and work out whats wrong with the logic. – Jeremy Thompson Nov 20 '18 at 03:28
  • Thanks for the suggestion - seems I've isolated the problem, but still unsure what's causing it. It seems that values aren't being added properly to `percentages`. As I step through the program, `profitPercentage` updates, but instead of adding those new values to `percentages` and then sorting them, the count of `percentages` stays at 1 and just updates with the newest value for `profitPercentage`. Because of this, `maxProfit` and `profitPercentage` are always equal, and the second if statement is never executed. What could be causing this? – Phil Nov 20 '18 at 19:27

0 Answers0