How to add Stop Loss to CBot Code
How to add Stop Loss to CBot Code
02 Jul 2020, 17:28
Hi all,
I was wondering if anyone is able to help me with adding a Stop Loss to my code below please? I have tried myself numerous times and can't seem to get the Stop Loss function to work. Below is the original code (which I haven't messed around with). Any help would be very much appreciated!
Thanks.
using System;
using System.Linq;
using cAlgo.API;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;
using cAlgo.Indicators;
namespace cAlgo.Robots
{
[Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
public class MA_Crossing_cBot : Robot
{
[Parameter(DefaultValue = "MA_Crossing_Renko_cBot")]
public string cBotLabel { get; set; }
[Parameter("Currency pair", DefaultValue = "EURUSD")]
public string TradeSymbol { get; set; }
[Parameter("Lot Size", DefaultValue = 0.1, MinValue = 0.01, Step = 0.01)]
public double LotSize { get; set; }
[Parameter("MA Type", DefaultValue = MovingAverageType.Exponential)]
public MovingAverageType MAType { get; set; }
[Parameter("MA Period", DefaultValue = 14)]
public int MAPeriod { get; set; }
[Parameter("Signal candle,in bars", DefaultValue = 1, MinValue = 0)]
public int SignalCandle { get; set; }
[Parameter("Fixed TP", DefaultValue = false)]
public bool UseTP { get; set; }
[Parameter("TP Level", DefaultValue = 40.0)]
public double TakeProfit { get; set; }
[Parameter("TrailingStop", DefaultValue = false)]
public bool UseTS { get; set; }
[Parameter("Trailing Trigger", DefaultValue = 15.0)]
public double TrailingTrigger { get; set; }
[Parameter("Trailing Pips", DefaultValue = 4.0)]
public double TrailingPips { get; set; }
private MovingAverage MA;
protected override void OnStart()
{
//check symbol
Symbol CurrentSymbol = Symbols.GetSymbol(TradeSymbol);
if (CurrentSymbol == null)
{
Print("Currency pair is not supported,please check!");
OnStop();
}
MA = Indicators.MovingAverage(MarketSeries.Close, MAPeriod, MAType);
}
protected override void OnBar()
{
if (MA.Result.Last(SignalCandle) < MarketSeries.Close.Last(SignalCandle) && MA.Result.Last(SignalCandle) > MarketSeries.Low.Last(SignalCandle) && Positions.FindAll(cBotLabel, TradeSymbol, TradeType.Buy).Length == 0)
{
ClosePosition(TradeType.Sell);
OpenMarketOrder(TradeType.Buy, TradeSymbol, LotSize);
}
else if (MA.Result.Last(SignalCandle) > MarketSeries.Close.Last(SignalCandle) && MA.Result.Last(SignalCandle) < MarketSeries.High.Last(SignalCandle) && Positions.FindAll(cBotLabel, TradeSymbol, TradeType.Sell).Length == 0)
{
ClosePosition(TradeType.Buy);
OpenMarketOrder(TradeType.Sell, TradeSymbol, LotSize);
}
}
protected override void OnTick()
{
if (UseTS == true && Positions.FindAll(cBotLabel, TradeSymbol).Length > 0)
DoTrailingStop();
}
private void ClosePosition(TradeType tradeType)
{
foreach (var position in Positions.FindAll(cBotLabel, TradeSymbol, tradeType))
{
var result = ClosePosition(position);
if (!result.IsSuccessful)
{
Print("Closing market order error: {0}", result.Error);
OnStop();
}
}
}
private void OpenMarketOrder(TradeType tradeType, string strSymbol, double dLots)
{
var volumeInUnits = Symbol.QuantityToVolumeInUnits(dLots);
volumeInUnits = Symbol.NormalizeVolumeInUnits(volumeInUnits, RoundingMode.Down);
double TP_in_pips = 0.0;
if (UseTP == true)
TP_in_pips = TakeProfit;
var result = ExecuteMarketOrder(tradeType, strSymbol, volumeInUnits, cBotLabel, 0, TP_in_pips);
if (!result.IsSuccessful)
{
Print("Execute Market Order Error: {0}", result.Error.Value);
OnStop();
}
}
private void DoTrailingStop()
{
var cBotPositions = Positions.FindAll(cBotLabel, TradeSymbol);
foreach (var position in cBotPositions)
{
if (position.TradeType == TradeType.Buy && position.Pips >= TrailingTrigger && position.HasTrailingStop == false)
{
var NewSL = position.TradeType == TradeType.Buy ? (position.EntryPrice + (TrailingPips * Symbol.PipSize)) : (position.EntryPrice - (TrailingPips * Symbol.PipSize));
ModifyPosition(position, NewSL, position.TakeProfit, true);
}
}
}
protected override void OnStop()
{
}
}
}
Replies
phil.weekes
03 Jul 2020, 19:00
RE:
PanagiotisCharalampous said:
Hi Phil,
You can add your stop loss in the following method
ExecuteMarketOrder(tradeType, strSymbol, volumeInUnits, cBotLabel, 0, TP_in_pips);
there were 0 is at the moment. You just need to replace it with the stop loss you want.
Best Regards,
Panagiotis
Thanks Panagiotis, I really appreciate the help. I added in a couple of parameters for the stop loss then wrote a piece of code in for the stop loss like I have already have for my take profit as below:
double SL_in_pips = 0.0;
if (UseSL == true)
SL_in_pips = StopLoss;
I then adjusted the line of code that you suggested and it works perfectly.
Kind regards,
Phil.
@phil.weekes
PanagiotisCharalampous
03 Jul 2020, 09:00
Hi Phil,
You can add your stop loss in the following method
there were 0 is at the moment. You just need to replace it with the stop loss you want.
Best Regards,
Panagiotis
Join us on Telegram
@PanagiotisCharalampous