try to get a macd triggered trailing... Help!!
try to get a macd triggered trailing... Help!!
29 Apr 2018, 02:40
hello,
i'm a little bit stuck here..
i'm trying to build a second trailing stoploss that will be triggered by macd crossover.
if a buy trade is open: GetMacdIsShort should trigger my second trailing.
if a sell trade is open: GetMacdIsLong should trigger my second trailing.
if this trailing is triggered but the macd trigger signal falls away: this trailing should reset and wait for the next macd signal
private bool getMacdIsShort(int index)
{
return _MACD.MACD.Last(0) < _MACD.Signal.Last(0);
}
private bool getMacdIsLong(int index)
{
return _MACD.MACD.Last(0) > _MACD.Signal.Last(0);
}
private void MacTrailing()
{
if (MacdTrailing > 0 && Trigger2 > 0)
{
Position[] positions = Positions.FindAll(label, Symbol);
foreach (Position position in positions)
{
if (position.TradeType == TradeType.Sell)
{
double distance = position.EntryPrice - Symbol.Ask;
if (distance >= Trigger2 * Symbol.PipSize)
{
double newStopLossPrice = Symbol.Ask + MacdTrailing * Symbol.PipSize;
if (position.StopLoss == null || newStopLossPrice < position.StopLoss)
{
ModifyPosition(position, newStopLossPrice, position.TakeProfit);
}
}
}
else if (position.TradeType == TradeType.Buy)
{
double distance = Symbol.Bid - position.EntryPrice;
if (distance >= Trigger2 * Symbol.PipSize)
{
double newStopLossPrice = Symbol.Bid - MacdTrailing * Symbol.PipSize;
if (position.StopLoss == null || newStopLossPrice > position.StopLoss)
{
ModifyPosition(position, newStopLossPrice, position.TakeProfit);
}
}
}
}
}
}
private long VolumeInUnits
{
get { return Symbol.QuantityToVolume(Quantity); }
}
}
}