Hello,
I've been searching for a cTrader cBot that will automatically move my SL to my entry price plus a certain amount of pips once the position is a certain amount of pips in profit. I think it would be fantastic not to have to manually open the advanced protection window and set the values for every position. I then found that you guys have a page relating to this:
https://clickalgo.com/how-to-code-a-break-even-stop-loss-with-ctrader
So I downloaded the 'Complete Algo with Source Code' txt file and copy and pasted it into my cTrader desktop (version 4.2.16.7409) code editor/builder where I thought it best to place it (I have never made a cBot before) and the code looks like this:
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;
namespace cAlgo.Robots
{
[Robot(AccessRights = AccessRights.None)]
public class NewcBot : 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; }
if (IncludeBreakEven)
{
BreakEvenAdjustment(allPositions);
}
/// <summary>
/// Adjusts the break even plus (x) pips
/// </summary>
/// <param name="tradeType"></param>
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);
}
}
}
}
}
}
}
As I expected, the build failed and has 25 errors but I would just like to ask how near or far is this code away from being a complete and fully functional 'move SL to breakeven' cBot? any help with this would be greatly appreciated.
Thank you in advance.