Trailing Stop Loss
Trailing Stop Loss
17 Aug 2022, 20:42
Hi,
My trailing stop loss works very well, however, I am unsure how to get it to keep the take profit set to the current pip distance.
every time it triggers the trailing stop it deletes the take profit and only begins trailing the stop loss until eventually it comes down and hits it.
I would like it so it can still take profit if price reaches it, but in an event that it comes just short of the take profit, it will have that safety net of the trailing stop.
here is the code currently:
[Parameter("Include Trailing Stop", DefaultValue = true, Group = "Risk Management")]
public bool IncludeTrailingStop { get; set; }
[Parameter("Trailing Stop Trigger (pips)", MinValue = 1, MaxValue = 500, Step = 1, Group = "Risk Management")]
public int TrailingStopTrigger { get; set; }
[Parameter("Trailing Stop Step (pips)", MinValue = 1, MaxValue = 500, Step = 1, Group = "Risk Management")]
public int TrailingStopStep { get; set; }
protected override void OnTick()
{
if (IncludeTrailingStop)
{
SetTrailingStop();
}
}
private void SetTrailingStop()
{
var sellPositions = Positions.FindAll(InstanceName, SymbolName, TradeType.Sell);
foreach (var position in sellPositions)
{
double distance = position.EntryPrice - Symbol.Ask;
if (distance < TrailingStopTrigger * Symbol.PipSize)
continue;
double newStopLossPrice = Symbol.Ask + TrailingStopStep * Symbol.PipSize;
if (position.StopLoss == null || newStopLossPrice < position.StopLoss)
{
ModifyPosition(position, newStopLossPrice, null);
}
}
var buyPositions = Positions.FindAll(InstanceName, SymbolName, TradeType.Buy);
foreach (var position in buyPositions)
{
double distance = Symbol.Bid - position.EntryPrice;
if (distance < TrailingStopTrigger * Symbol.PipSize)
continue;
double newStopLossPrice = Symbol.Bid - TrailingStopStep * Symbol.PipSize;
if (position.StopLoss == null || newStopLossPrice > position.StopLoss)
{
ModifyPosition(position, newStopLossPrice, null);
}
}
}
Replies
PanagiotisCharalampous
18 Aug 2022, 08:52
Hi kurtisnauss,
I would suggest to use ModifyStopLossPrice() instead.
Best Regards,
Panagiotis
Join us on Telegram and Facebook
@PanagiotisCharalampous
kurtisnauss
18 Aug 2022, 21:02
RE:
PanagiotisCharalampous said:
Hi kurtisnauss,
I would suggest to use ModifyStopLossPrice() instead.
Best Regards,
Panagiotis
The first reply actually did work when I added that to the final modify position line.
What would be the difference or benefit of doing it that way instead?
thanks
@kurtisnauss
firemyst
19 Aug 2022, 03:34
RE: RE:
kurtisnauss said:
PanagiotisCharalampous said:
Hi kurtisnauss,
I would suggest to use ModifyStopLossPrice() instead.
Best Regards,
Panagiotis
The first reply actually did work when I added that to the final modify position line.
What would be the difference or benefit of doing it that way instead?
thanks
It doesn't adjust your take profit at all. It only affects the stoploss price, hence the method name.
1) It's simpler.
2) you don't have to worry about putting code like this:
(position.TakeProfit.HasValue ? position.TakeProfit.GetValueOrDefault() : (double?)null)
everywhere you call "ModifyPosition".
3) your code would be a nanoseconds faster because it's one less conditional statement that has to be evaluated and values retrieved.
@firemyst
Shares4UsDevelopment
05 Dec 2023, 08:16
Just use:
Position..ModifyStopLossPrice(newStopLossPrice);
in stead of
ModifyPosition(position, newStopLossPrice, null);
@Shares4UsDevelopment
firemyst
18 Aug 2022, 04:35
RE:
kurtisnauss said:
Your take profit disappears because you set it to "null" every time you "ModifyPosition".
So if you don't want to change your TakeProfit, set the value:
ModifyPosition(position, newStopLossPrice,
(position.TakeProfit.HasValue ? position.TakeProfit.GetValueOrDefault() : (double?)null) )@firemyst