Add stoploss
15 Jul 2021, 11:06
Hello i using a cbot. But want to add a stoploss in it
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 AdvancedHedgingandScalpingcBot : Robot
{
[Parameter("Volume", DefaultValue = 1000, MinValue = 1, Step = 1)]
public int Volume { get; set; }
private double _equity;
private int noOfPositions = 4;
private int pips = 2;
protected override void OnStart()
{
_equity = Account.Balance;
ExecuteMarketOrder(TradeType.Buy, Symbol, Volume, "BUY");
ExecuteMarketOrder(TradeType.Sell, Symbol, Volume, "SELL");
}
protected override void OnBar()
{
foreach (var position in Positions)
{
if (position.Pips > pips)
{
ClosePosition(position);
}
}
if (Positions.Count < noOfPositions)
{
ExecuteMarketOrder(TradeType.Buy, Symbol, Volume, "BUY");
ExecuteMarketOrder(TradeType.Sell, Symbol, Volume, "SELL");
}
int buyCount = 0;
int sellCount = 0;
foreach (var position in Positions)
{
if (position.TradeType == TradeType.Buy)
buyCount++;
if (position.TradeType == TradeType.Sell)
sellCount++;
}
if (buyCount == 0 || sellCount == 0)
{
Volume += 1000;
noOfPositions += 2;
pips++;
ExecuteMarketOrder(TradeType.Buy, Symbol, Volume, "BUY");
ExecuteMarketOrder(TradeType.Sell, Symbol, Volume, "SELL");
}
if (Account.Equity > _equity + Account.Equity / 100)
{
foreach (var position in Positions)
{
ClosePosition(position);
}
_equity = Account.Equity;
Volume = 1000;
noOfPositions = 4;
pips = 2;
ExecuteMarketOrder(TradeType.Buy, Symbol, Volume, "BUY");
ExecuteMarketOrder(TradeType.Sell, Symbol, Volume, "SELL");
}
}
}

PanagiotisCharalampous
15 Jul 2021, 11:12
Hi rickbeeke84,
You can pass the stop loss as a parameter in the ExecuteMarketOrder() method. You can find all the overloads of the method here.
Best Regards,
Panagiotis
Join us on Telegram and Facebook
@PanagiotisCharalampous