SetStopLoss()
Set a stop loss to exit a trade if price decreases by specified amount.
Parameters
Example
// Close the position once the price decreases by 5
SetStopLoss(5);using Signals.DataSeries.Bars;
using Signals.Framework;
using Signals.Indicators.Bollinger;
using Signals.Indicators.ATR;
public class BollingerBands : SingleMarketStrategy
{
private Bars twoHoursBars;
private Bars minuteBars;
private Bollinger bb;
private ATR atr;
private double entryPrice;
private double breakEven = double.MaxValue;
private const double multiplier1 = 5;
private const double multiplier2 = 3;
private const double multiplier3 = 3;
public override void Setup(DataMarketplace data, IndicatorsMarketplace indicators)
{
twoHoursBars = data.Bars(BarPeriodType.Hour, 2).WithOffset(25);
minuteBars = data.Bars(BarPeriodType.Minute, 1).WithOffset(25);
bb = indicators.Bollinger(2.0, 20).Keep(2).OnSeries(twoHoursBars.Close);
atr = indicators.ATR(14).OnSeries(twoHoursBars);
}
public override void RegisterActions()
{
OnUpdateOf(twoHoursBars).Do(() =>
{
if (!Position.InPosition)
{
CancelAllPendingOrders();
var atrValue = atr.Value;
entryPrice = bb.Value.Upper;
breakEven = entryPrice + atrValue * multiplier3;
EnterLongStop(entryPrice);
SetProfitTarget(atrValue * multiplier1);
SetStopLoss(atrValue * multiplier2);
}
});
OnUpdateOf(minuteBars).Do(() =>
{
if ((Position.InPosition) && (minuteBars.High[0] > breakEven))
{
SetStopLoss(0);
}
});
}
}Last updated