Description
Updated
//OrglobalFx_BreakEven cBOT
// orglobalng@gmail.com
// Will move stoploss to breakeven and trail the price.
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.EasternStandardTime, AccessRights = AccessRights.None)]
public class _OFX_CBOT_032421032022_BREAKEVEN_v2 : Robot
{
[Parameter("Instance Name", DefaultValue = "")]
public string InstanceName { get; set; }
[Parameter("Include Break-Even", DefaultValue = true, Group = "Protection")]
public bool IncludeBreakEven { get; set; }
[Parameter("Break-Even Trigger (pips)", DefaultValue = 100, MinValue = 1, Group = "Protection")]
public int BreakEvenPips { get; set; }
[Parameter("Break-Even Extra (pips)", DefaultValue = 100, MinValue = 1, Group = "Protection")]
public int BreakEvenExtraPips { get; set; }
[Parameter("Trail after Break-Even", DefaultValue = true, Group = "Protection")]
public bool Includetrail { get; set; }
[Parameter("Trailing Stop (pips)", DefaultValue = 100, MinValue = 1, Group = "Protection")]
public int trailingstoppips { get; set; }
protected override void OnStart()
{
// Put your initialization logic here
}
protected override void OnTick()
{
if (IncludeBreakEven)
{
BreakEvenAdjustment();
}
}
#region Break Even
// code from clickalgo.com
private void BreakEvenAdjustment()
{
var positn = Positions.Find(InstanceName, SymbolName);
var allPositions = Positions.FindAll(InstanceName, SymbolName);
foreach (Position position in allPositions)
{
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)
{
// && position.Pips >= trailingstoppips)
if (Includetrail)
{
//ModifyPosition(position, position.EntryPrice);
position.ModifyStopLossPrice(position.EntryPrice + (Symbol.PipSize * BreakEvenExtraPips));
Print("Stop Loss to Break Even set for BUY position {0}", position.Id);
if (position.Pips >= trailingstoppips)
position.ModifyTrailingStop(true);
}
else if (!Includetrail)
{
//ModifyPosition(position, position.EntryPrice + (Symbol.PipSize * BreakEvenExtraPips), position.TakeProfit);
position.ModifyStopLossPrice(position.EntryPrice + (Symbol.PipSize * BreakEvenExtraPips));
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)
{
// && position.Pips >= trailingstoppips)
if (Includetrail)
{
ModifyPosition(position, entryPrice - (Symbol.PipSize * BreakEvenExtraPips), position.TakeProfit);
Print("Stop Loss to Break Even set for SELL position {0}", position.Id);
if (position.Pips >= trailingstoppips)
position.ModifyTrailingStop(true);
}
else if (!Includetrail)
{
ModifyPosition(position, entryPrice - (Symbol.PipSize * BreakEvenExtraPips), position.TakeProfit);
Print("Stop Loss to Break Even set for SELL position {0}", position.Id);
}
}
}
}
}
}
#endregion
protected override void OnStop()
{
// Put your deinitialization logic here
}
}
}

orglobalng
Joined 03.03.2021
- Type: Free
- Language: C#
- Trading Platform: cTrader Automate
- Filename: _OFX_CBOT_032421032022_BREAKEVEN_v2.algo
- Rating: 0
- Downloads: 971
Warning! Executing cBots downloaded from this section may result in loss of funds. Use them at your own risk.
Notification Publishing copyrighted material is strictly prohibited. If you believe there is copyrighted material in this section you may use the Copyright Infringement Notification form to submit a claim.
Comments
Only logged in users can post a comment
Comments not found