Closed by robot or manually
27 Dec 2013, 22:20
Is there a way to test if a trade was closed by the robot, or manually?
Replies
jeex
27 Dec 2013, 23:19
But no...
No, things are not that simple. Cerunnos' way checks if the closed position was opened by hand. I want to check if a recently closed position is closed by hand.
So i want to activate a method
- whenever any of the many positions is closed ( OnPositionsClosed )
and the method must determine:
- if the position belongs to that robot (p.Label...)
- if the position was closed
- by hand, or
- by the robot, or
- because of a stoploss or takeprofit
Is there a way?
@jeex
Cerunnos
27 Dec 2013, 23:52
RE: But no...
jeex said:
No, things are not that simple. Cerunnos' way checks if the closed position was opened by hand. I want to check if a recently closed position is closed by hand.
So i want to activate a method
- whenever any of the many positions is closed ( OnPositionsClosed )
and the method must determine:
- if the position belongs to that robot (p.Label...)
- if the position was closed
- by hand, or
- by the robot, or
- because of a stoploss or takeprofit
Is there a way?
Maybe it works with a callback function: ClosePositionAsync(Position position, Action callback). The callback function is called if the position was closed by the robot (closed by hand -> no callback function is called).
@Cerunnos
Cerunnos
28 Dec 2013, 10:37
That could be a simple solution for a semi-automatic system. But I'm not sure if the following code with the new API functions is working properly. I will test it next week ...
Note: SL / TP hits were not considered (/forum/cbot-support/1655).
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.UTC)]
public class NewRobot : Robot
{
private bool flag_robot_close = false;
private bool breakeven_flag = false;
private bool distance_1 = false;
[Parameter(DefaultValue = 0.0)]
public double Parameter { get; set; }
private void PositionClosedByRobot(TradeResult tradeResult)
{
if(tradeResult.IsSuccessful)
flag_robot_close = true;
}
private void OnPositionsClosed(PositionClosedEventArgs args)
{
if (!flag_robot_close) // only manual positions
{
Print("Position #{0} was closed by hand", args.Position.Id);
// code
//
}
else
{
Print("Position #{0} was closed by robot", args.Position.Id);
// code
//
flag_robot_close = false;
}
}
protected override void OnStart()
{
Positions.Closed += OnPositionsClosed;
}
protected override void OnTick()
{
//......................
//.......................
Position myPosition = Positions.Find("myLabel");
if (myPosition != null && breakeven_flag && distance_1) // some condition...
ClosePositionAsync(myPosition,PositionClosedByRobot); //callback -> position closed by robot
}
protected override void OnStop()
{
}
}
}
@Cerunnos
fzlogic
30 Dec 2013, 12:51
OnPositionsClosed will be triggered before PositionClosedByRobot and the result will be that flag_robot_close will not serve the intended purpose.
Try setting the flag_robot_close to true before calling ClosePositionAsync.
if (myPosition != null && breakeven_flag && distance_1)
{
flag_robot_close = true;
ClosePositionAsync(myPosition, PositionClosedByRobot);
}
@fzlogic
Cerunnos
30 Dec 2013, 14:08
Yes thank you, that's what I supposed. But actually you do not need a callback function, or not?
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.UTC)]
public class NewRobot : Robot
{
private bool flag_robot_close = false;
private bool breakeven_flag = false;
private bool distance_1 = false;
[Parameter(DefaultValue = 0.0)]
public double Parameter { get; set; }
private void OnPositionsClosed(PositionClosedEventArgs args)
{
if (!flag_robot_close) // only manual positions
{
Print("Position #{0} was closed by hand", args.Position.Id);
// code
//
}
else
{
Print("Position #{0} was closed by robot", args.Position.Id);
// code
//
flag_robot_close = false;
}
}
protected override void OnStart()
{
Positions.Closed += OnPositionsClosed;
}
protected override void OnTick()
{
//......................
//.......................
Position myPosition = Positions.Find("myLabel");
if (myPosition != null && breakeven_flag && distance_1) // some condition...
{
flag_robot_close = true;
ClosePosition(myPosition); //no callback -> position closed by robot
}
}
}
}
@Cerunnos

Cerunnos
27 Dec 2013, 22:42
RE:
jeex said:
Use labels in your robot:
private void OnPositionsClosed(PositionClosedEventArgs args) { if (args.Position.Label == string.Empty) // only manual positions { Print("Manual Position #{0} was closed", args.Position.Id); } else (args.Position.Label == "myRobot") { Print("Robot Position #{0} was closed", args.Position.Id); } }@Cerunnos