Stop Cbot at Target Equity
Stop Cbot at Target Equity
27 Jan 2022, 20:00
Hello , can anybody give me a piece of code I can put into my cbot to stop it completely when a certain equity target is reached, let's say when equity has increased by $100. Thank you
Replies
umadrat2012
28 Jan 2022, 11:05
RE: thank you, how would the code look like if my cbot trades multiple currencies at the same time?
amusleh said:
Hi,
Here is an example:
using cAlgo.API; namespace cAlgo.Robots { [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)] public class DrawIcon : Robot { private double _initialEquity; [Parameter("Equity Increase Amount", DefaultValue = 100)] public double EquityIncreaseAmount { get; set; } protected override void OnStart() { _initialEquity = Account.Equity; } protected override void OnTick() { if (Account.Equity - _initialEquity >= EquityIncreaseAmount) { Stop(); } } } }
It works properly only if your cBot trades the current chart symbol, if it trades multiple symbols then you have to use all symbols Tick events and check equity every time a symbol tick is received.
@umadrat2012
amusleh
28 Jan 2022, 12:02
Hi,
Here is a multi symbol example:
using System;
using System.Linq;
using cAlgo.API;
namespace cAlgo.Robots
{
[Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
public class PartialCloseSample : Robot
{
private double _initialEquity;
[Parameter("Equity Increase Amount", DefaultValue = 100)]
public double EquityIncreaseAmount { get; set; }
protected override void OnStart()
{
_initialEquity = Account.Equity;
foreach (var group in Positions.GroupBy(position => position.SymbolName))
{
var positionSymbol = Symbols.GetSymbol(group.Key);
positionSymbol.Tick -= PositionSymbol_Tick;
}
Positions.Opened += Positions_Opened;
Positions.Closed += Positions_Closed;
}
private void Positions_Closed(PositionClosedEventArgs obj)
{
// If there are other positions from same symbol then don't remove the symbol Tick event handler
if (Positions.Any(position => position.SymbolName.Equals(obj.Position.SymbolName, StringComparison.Ordinal)))
{
return;
}
// If there is no other position from the closed position symbol then remove the Tick event handler
var positionSymbol = Symbols.GetSymbol(obj.Position.SymbolName);
positionSymbol.Tick -= PositionSymbol_Tick;
}
private void Positions_Opened(PositionOpenedEventArgs obj)
{
// If there are other positions from same symbol then don't add the symbol Tick event handler
// Because we already have one
if (Positions.Count(position => position.SymbolName.Equals(obj.Position.SymbolName, StringComparison.Ordinal)) > 1)
{
return;
}
// Add position symbol tick event handler
var positionSymbol = Symbols.GetSymbol(obj.Position.SymbolName);
positionSymbol.Tick += PositionSymbol_Tick;
}
private void PositionSymbol_Tick(SymbolTickEventArgs obj)
{
if (Account.Equity - _initialEquity >= EquityIncreaseAmount)
{
Stop();
}
}
}
}
@amusleh
iandelmar305
26 Jan 2023, 22:42
Hello, could you add a "close all cbots" and all open orders? that would be nice" thanks
amusleh said:
Hi,
Here is an example:
using cAlgo.API; namespace cAlgo.Robots { [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)] public class DrawIcon : Robot { private double _initialEquity; [Parameter("Equity Increase Amount", DefaultValue = 100)] public double EquityIncreaseAmount { get; set; } protected override void OnStart() { _initialEquity = Account.Equity; } protected override void OnTick() { if (Account.Equity - _initialEquity >= EquityIncreaseAmount) { Stop(); } } } }
It works properly only if your cBot trades the current chart symbol, if it trades multiple symbols then you have to use all symbols Tick events and check equity every time a symbol tick is received.
@iandelmar305
amusleh
28 Jan 2022, 08:31
Hi,
Here is an example:
It works properly only if your cBot trades the current chart symbol, if it trades multiple symbols then you have to use all symbols Tick events and check equity every time a symbol tick is received.
@amusleh