BreakEven crashing optimization even with few steps!!!
BreakEven crashing optimization even with few steps!!!
23 Apr 2023, 21:32
Hi, I am a c# beginner and I hope I can get some help. I recently adapted and include BE function in my cbot and I am having some issues. The function works just fine in visual mode tests and in live mode.. But optmization crashes if I include any steps from the BE parameters. If I use any BE parameters into optimizer, It crashes even with a 20 steps optimization. Here is the BE related parts of my code:
using System;
using System.Linq;
using cAlgo.API;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;
using cAlgo.Indicators;
using System.Globalization;
namespace cAlgo.Robots
{
[Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
public class THEULTIMATE : Robot
{
[Parameter(DefaultValue = "001", Group = "Basic Settings")]
public string InstanceName { get; set; }
[Parameter("Lot Size", DefaultValue = 1, MinValue = 0.01, Step = 0.01, Group = "Basic Settings") ]
public double LotSize { get; set; }
[Parameter("StopLoss in pips", DefaultValue = 40.0, Group = "Protection")]
public double StopLoss { get; set; }
[Parameter("TakeProfit in pips", DefaultValue = 40.0, Group = "Protection")]
public double TakeProfit { get; set; }
[Parameter("Use Break-Even", DefaultValue = true, Group = "Protection")]
public bool UseBreakEven { get; set; }
[Parameter("Break-Even Trigger (pips)", DefaultValue = 10, MinValue = 1, Group = "Protection")]
public int BETrigger { get; set; }
[Parameter("Break-Even Extra (pips)", DefaultValue = 10, MinValue = 1, Group = "Protection")]
public int BEExtraPips { get; set; }
protected override void OnTick()
{
if (UseBreakEven == true)
{
BreakEvenFunction();
}
}
private void BreakEvenFunction()
{
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 >= BETrigger * Symbol.PipSize)
{
if (position.TradeType == TradeType.Buy)
{
if (position.StopLoss <= position.EntryPrice + (Symbol.PipSize * BEExtraPips) || position.StopLoss == null)
{
ModifyPosition(position, position.EntryPrice + (Symbol.PipSize * BEExtraPips), position.TakeProfit);
}
}
else
{
if (position.StopLoss >= position.EntryPrice - (Symbol.PipSize * BEExtraPips) || position.StopLoss == null)
{
ModifyPosition(position, entryPrice - (Symbol.PipSize * BEExtraPips), position.TakeProfit);
}
}
}
}
}
}
}
firemyst
25 Apr 2023, 15:16
You may need to check if the SL or TP value is null along with changing the order of comparisons in the "if" statements.
@firemyst