Can macd crossover signal be the trigger of trailingstoploss?
Can macd crossover signal be the trigger of trailingstoploss?
23 Apr 2018, 16:30
Hello,
I would like to know if my macd signal can be the trigger for a second trailingstoploss..
for example: I have a few signals for a buy trade, when a buy trade is open and my macd signals fals away (by Crossover to sell) i would like my second trailing stoploss to be triggered.. when the signal comes back up (by crossover to buy) this second trailing stoploss does'nt count anymore till the next macd crossover to sell, so i only have my first trailingstoploss.
below is how i have my macd.
using System;
using System.Linq;
using cAlgo.API;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;
using cAlgo.Indicators;
namespace cAlgo
{
[Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
public class macdcross : Robot
{
[Parameter()]
public DataSeries SourceSeries { get; set; }
[Parameter("Quantity (Lots)", DefaultValue = 1, MinValue = 0.01, Step = 0.01)]
public double Quantity { get; set; }
[Parameter("MACD LongCycle", DefaultValue = 26, MinValue = 1)]
public int LongCycle { get; set; }
[Parameter("MACD ShortCycle", DefaultValue = 12, MinValue = 1)]
public int ShortCycle { get; set; }
[Parameter("MACD Period", DefaultValue = 9, MinValue = 1)]
public int MACDPeriod { get; set; }
[Parameter("Stop Loss (pips)", DefaultValue = 0, MinValue = 0)]
public int StopLossInPips { get; set; }
[Parameter("Take Profit (pips)", DefaultValue = 0, MinValue = 0)]
public int TakeProfitInPips { get; set; }
[Parameter("trigger", DefaultValue = 0)]
public int Trigger { get; set; }
[Parameter("Trailing", DefaultValue = 0)]
public int Trailing { get; set; }
private MacdCrossOver _MACD;
private const string label = "MacdCross";
protected override void OnStart()
{
_MACD = Indicators.MacdCrossOver(LongCycle, ShortCycle, MACDPeriod);
}
protected override void OnBar()
{
TRAILING();
{
var longPosition = Positions.Find(label, Symbol, TradeType.Buy);
var shortPosition = Positions.Find(label, Symbol, TradeType.Sell);
if (_MACD.MACD.Last(0) > _MACD.Signal.Last(0) && _MACD.MACD.Last(0) > 0)
{
if (shortPosition != null)
ClosePosition(shortPosition);
ExecuteMarketOrder(TradeType.Buy, Symbol, VolumeInUnits, label, StopLossInPips, TakeProfitInPips);
}
else if (_MACD.MACD.Last(0) < _MACD.Signal.Last(0) && _MACD.MACD.Last(0) < 0)
{
if (longPosition != null)
ClosePosition(longPosition);
ExecuteMarketOrder(TradeType.Sell, Symbol, VolumeInUnits, label, StopLossInPips, TakeProfitInPips);
}
}
}
private void TRAILING()
{
if (Trailing > 0 && Trigger > 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 >= Trigger * Symbol.PipSize)
{
double newStopLossPrice = Symbol.Ask + Trailing * Symbol.PipSize;
if (position.StopLoss == null || newStopLossPrice < position.StopLoss)
{
ModifyPosition(position, newStopLossPrice, position.TakeProfit);
}
}
}
else
{
double distance = Symbol.Bid - position.EntryPrice;
if (distance >= Trigger * Symbol.PipSize)
{
double newStopLossPrice = Symbol.Bid - Trailing * Symbol.PipSize;
if (position.StopLoss == null || newStopLossPrice > position.StopLoss)
{
ModifyPosition(position, newStopLossPrice, position.TakeProfit);
}
}
}
}
}
}
private long VolumeInUnits
{
get { return Symbol.QuantityToVolume(Quantity); }
}
}
}