If you are looking into adding a simple breakeven stop loss to your cTrader cBot then the following code example will help you integrate it into your existing automated trading robot (cBot). This code example is for the popular cTrader Trading Platform.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using cAlgo.API;
using cAlgo.API.Collections;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;
// This sample code was created by ClickAlgo Limited and is open source.
// Visit us at https://clickalgo.com
// This cBot will adjust the stop loss when target (trigger) is x pips in profit and add a fewe extra pips to cover fees.
// Commmisions and swaps are not calculated in thne breakeven.
// feel free to use this code in your own projects.
// The breakeven code is run on every incomming tick of data (price chnage), if you wish to run on candle close move the code that is in the onTick to OnBar.
namespace cAlgo.Robots
{
[Robot(AccessRights = AccessRights.None)]
public class ClickAlgoSampleBreakeven : Robot
{
[Parameter("Include Break-Even", DefaultValue = false)]
public bool IncludeBreakEven { get; set; }
[Parameter("Break-Even Trigger (pips)", DefaultValue = 10, MinValue = 1)]
public int BreakEvenPips { get; set; }
[Parameter("Break-Even Extra (pips)", DefaultValue = 2, MinValue = 1)]
public int BreakEvenExtraPips { get; set; }
protected override void OnStart()
{
}
protected override void OnTick()
{
// called on each tick of incomming data.
if (IncludeBreakEven)
{
var allPositionsBySymbol = Positions.FindAll(string.Empty, SymbolName);
BreakEvenAdjustment(allPositionsBySymbol);
}
}
protected override void OnBar()
{
// called when the candle (bar) closes
}
protected override void OnStop()
{
// Handle cBot stop here
}
private void BreakEvenAdjustment(IList<Position> allPositions)
{
foreach (Position position in allPositions)
{
if (position.StopLoss != null)
return;
var entryPrice = position.EntryPrice;
var distance = position.TradeType == TradeType.Buy ? Symbol.Bid - entryPrice : entryPrice - Symbol.Ask;
// move stop loss to break even plus and additional (x) pips
if (distance >= BreakEvenPips * Symbol.PipSize)
{
if (position.TradeType == TradeType.Buy)
{
if (position.StopLoss <= position.EntryPrice + (Symbol.PipSize * BreakEvenExtraPips) || position.StopLoss == null)
{
ModifyPosition(position, position.EntryPrice + (Symbol.PipSize * BreakEvenExtraPips), position.TakeProfit);
Print("Stop Loss to Break Even set for BUY position {0}", position.Id);
}
}
else
{
if (position.StopLoss >= position.EntryPrice - (Symbol.PipSize * BreakEvenExtraPips) || position.StopLoss == null)
{
ModifyPosition(position, entryPrice - (Symbol.PipSize * BreakEvenExtraPips), position.TakeProfit);
Print("Stop Loss to Break Even set for SELL position {0}", position.Id);
}
}
}
}
}
}
}
You can also download the source code below just unzip and double-click on the file to install it into cTrader.
Breakeven Sample + Fees
The example code below is provided as standard with the cTrader platform and includes commissions, swaps and fees, just unzip and double click to install into cTrader.