Hi Mike, the bot still trades well in test however I don't think the trailing stop loss is working. The PrintLog was an issue so I changed to just Print which seemed to be the solution though no matter what Stop Loss or Trailing Stop Loss settings I use, there is nothing in the log. Any suggestions on the below would be greatly appreciated.
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 BreakoutSix : Robot
{
[Parameter("Percentage", Group = "Volume", DefaultValue = 98, MinValue = 1, Step = 1)]
public double Percentage { get; set; }
[Parameter("Lower", Group = "Volume", DefaultValue = 10, MinValue = 1, Step = 0.01)]
public double Lower { get; set; }
[Parameter("Upper", Group = "Volume", DefaultValue = 100, MinValue = 1, Step = 0.01)]
public double Upper { get; set; }
[Parameter("Source", Group = "Bollinger Bands")]
public DataSeries Source { get; set; }
[Parameter("Band Height (pips)", Group = "Bollinger Bands", DefaultValue = 40.0, MinValue = 0)]
public double BandHeightPips { get; set; }
[Parameter("Bollinger Bands Deviations", Group = "Bollinger Bands", DefaultValue = 2)]
public double Deviations { get; set; }
[Parameter("Bollinger Bands Periods", Group = "Bollinger Bands", DefaultValue = 20)]
public int Periods { get; set; }
[Parameter("Bollinger Bands MA Type", Group = "Bollinger Bands")]
public MovingAverageType MAType { get; set; }
[Parameter("Consolidation Periods", Group = "Bollinger Bands", DefaultValue = 2)]
public int ConsolidationPeriods { get; set; }
[Parameter("Initial Stop (pips)", Group = "Protection", DefaultValue = 20.0)]
public int TrailingStopTrigger { get; set; }
[Parameter("Trailling Stop (pips)", Group = "Protection", DefaultValue = 8.0)]
public int TrailingStopStep { get; set; }
[Parameter("Take Profit (pips)", Group = "Protection", DefaultValue = 20.0)]
public int TakeProfit { get; set; }
BollingerBands bollingerBands;
string label = "Sample Breakout cBot";
int consolidation;
protected override void OnStart()
{
bollingerBands = Indicators.BollingerBands(Source, Periods, Deviations, MAType);
}
protected override void OnBar()
{
var top = bollingerBands.Top.Last(1);
var bottom = bollingerBands.Bottom.Last(1);
if (top - bottom <= BandHeightPips * Symbol.PipSize)
{
consolidation = consolidation + 1;
}
else
{
consolidation = 0;
}
if (consolidation >= ConsolidationPeriods)
{
var volumeInUnits = Math.Floor(Account.FreeMargin - (Account.Balance * (Percentage / 100)));
double tradelots = Symbol.NormalizeVolumeInUnits(volumeInUnits, RoundingMode.ToNearest);
if (Ask < (bottom + (((top - bottom) / 100) * Upper)))
ExecuteMarketOrder(TradeType.Buy, SymbolName, tradelots, label, TrailingStopTrigger, null, "", true);
if (Bid > (top - (((top - bottom) / 100) * Lower)))
ExecuteMarketOrder(TradeType.Sell, SymbolName, tradelots, label, TrailingStopTrigger, null, "", true);
consolidation = 0;
}
}
public void SetTrailingStop(Position p)
{
double newStopLossPrice = 0;
try
{
if (p.TradeType == TradeType.Sell)
{
double distance = p.EntryPrice - Symbol.Ask;
if (distance < TrailingStopTrigger * Symbol.PipSize)
return;
newStopLossPrice = Symbol.Ask + TrailingStopStep * Symbol.PipSize;
if (p.StopLoss == null || newStopLossPrice < p.StopLoss)
{
if (p.TakeProfit == null)
{
ModifyPosition(p, newStopLossPrice, null);
}
else
{
ModifyPosition(p, newStopLossPrice, p.TakeProfit);
}
}
}
if (p.TradeType == TradeType.Buy)
{
double distance = Symbol.Bid - p.EntryPrice;
if (distance < TrailingStopTrigger * Symbol.PipSize)
return;
newStopLossPrice = Symbol.Bid - TrailingStopStep * Symbol.PipSize;
if (p.StopLoss == null || newStopLossPrice > p.StopLoss)
{
if (p.TakeProfit == null)
{
ModifyPosition(p, newStopLossPrice, null);
}
else
{
ModifyPosition(p, newStopLossPrice, p.TakeProfit);
}
}
}
} catch (Exception ex)
{
Print("SetTrailingStop: " + ex.Message);
Print("Entry price: " + p.EntryPrice.ToString());
Print("New stop loss price: " + newStopLossPrice.ToString());
}
}
}
}