Sl and Tp and Breakeven Bot HELP!
Sl and Tp and Breakeven Bot HELP!
12 Apr 2024, 09:00
This bot is just a copy and paste of other bots combined. I'm not sure why its not working but when I open a position I am getting an “Order Execution Error Nothing to change”. Any help would be greatly appreciated
using System;
using cAlgo.API;
namespace cAlgo.Robots
{
[Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
public class CombinedTradingBot : Robot
{
// Parameters from the first script
[Parameter("Modify TP", DefaultValue = true)]
public bool TpOption { get; set; }
[Parameter("Take Profit Pips", DefaultValue = 3)]
public double TpPips { get; set; }
[Parameter("Modify SL", DefaultValue = true)]
public bool SlOption { get; set; }
[Parameter("Stop Loss Pips", DefaultValue = 3)]
public double SlPips { get; set; }
// Parameters from the second script
[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()
{
Positions.Modified += OnPositionsModified;
Positions.Opened += OnPositionsOpened;
Positions.Closed += OnPositionsClosed;
PendingOrders.Filled += OnPendingOrdersFilled;
}
protected override void OnTick()
{
Settpsl();
if (IncludeBreakEven)
{
BreakEvenAdjustment();
}
}
private void OnPositionsModified(PositionModifiedEventArgs args)
{
Settpsl();
}
private void OnPositionsOpened(PositionOpenedEventArgs args)
{
Settpsl();
}
private void OnPositionsClosed(PositionClosedEventArgs args)
{
Settpsl();
}
private void OnPendingOrdersFilled(PendingOrderFilledEventArgs args)
{
Settpsl();
}
private void Settpsl()
{
foreach (var position in Positions)
{
var tp = position.TradeType == TradeType.Buy ? position.EntryPrice + TpPips * Symbol.PipSize : position.EntryPrice - TpPips * Symbol.PipSize;
var sl = position.TradeType == TradeType.Buy ? position.EntryPrice - SlPips * Symbol.PipSize : position.EntryPrice + SlPips * Symbol.PipSize;
// Only modify if the changes are significant (more than the symbol's pip size)
if (TpOption && Math.Abs(position.TakeProfit ?? 0 - tp) > Symbol.PipSize)
{
ModifyPosition(position, position.StopLoss, tp);
}
if (SlOption && Math.Abs(position.StopLoss ?? 0 - sl) > Symbol.PipSize)
{
ModifyPosition(position, sl, position.TakeProfit);
}
}
}
private void BreakEvenAdjustment()
{
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;
var newSL = position.TradeType == TradeType.Buy ? entryPrice + BreakEvenExtraPips * Symbol.PipSize : entryPrice - BreakEvenExtraPips * Symbol.PipSize;
// Ensure significant change before modifying
if (distance >= BreakEvenPips * Symbol.PipSize && Math.Abs(position.StopLoss ?? 0 - newSL) > Symbol.PipSize)
{
if (IncludeTrail && position.Pips >= TrailingStopPips)
{
position.ModifyTrailingStop(true);
}
else
{
ModifyPosition(position, newSL, position.TakeProfit);
}
Print($"Stop Loss adjusted for {position.TradeType} position {position.Id} to {newSL}");
}
}
}
protected override void OnStop()
{
// Clean-up logic here, if necessary
}
}
}
Replies
Jonathangong12
12 Apr 2024, 20:58
( Updated at: 14 Apr 2024, 08:02 )
RE: Sl and Tp and Breakeven Bot HELP!
PanagiotisCharalampous said:
Hi there,
“Order Execution Error Nothing to change” means that the property you are trying to modify has the same value as the previous one.
Best regards,
Panagiotis
How can I fix it?
@Jonathangong12
firemyst
14 Apr 2024, 10:18
RE: RE: Sl and Tp and Breakeven Bot HELP!
Jonathangong12 said:
PanagiotisCharalampous said:
Hi there,
“Order Execution Error Nothing to change” means that the property you are trying to modify has the same value as the previous one.
Best regards,
Panagiotis
How can I fix it?
Don't modify your position if nothing has changed. So you have to write code to see if the current value is the same as it was before you try to modify it. You have to ask yourself if the time and effort you'll spend on that is really worth it just to remove the cTrader popup that happens.
@firemyst
PanagiotisCharalampous
12 Apr 2024, 11:52
Hi there,
“Order Execution Error Nothing to change” means that the property you are trying to modify has the same value as the previous one.
Best regards,
Panagiotis
@PanagiotisCharalampous