Skip to content

PositionClosedEventArgs

Summary

Provides data for the position closing event.

Signature

1
public class PositionClosedEventArgs

Namespace

cAlgo.API

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
 using cAlgo.API;
 using cAlgo.API.Internals;
 namespace cAlgo.Robots;
 [Robot(AccessRights = AccessRights.None, AddIndicators = true)]
 public class Test : Robot
 {
     protected override void OnStart()
     {
         Positions.Closed += OnPositionClosed;
     }
     private void OnPositionClosed(PositionClosedEventArgs args)
     {
         var position = args.Position;
         Print("Position closed with {0} profit", position.GrossProfit);
     }
 }
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
 import clr
 clr.AddReference("cAlgo.API")
 # Import cAlgo API types
 from cAlgo.API import *
 # Import trading wrapper functions
 from robot_wrapper import *
 class Test():
     def on_start(self):
         api.Positions.Closed += self.on_position_closed
     def on_position_closed(self, args):
         position = args.Position
         print(f"Position closed with {position.GrossProfit} profit")

See Also

Properties

Position

Summary

Gets the position being closed.

Signature

1
public Position Position {get;}

Return Value

Position

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
 protected override void OnStart()
 {
     ExecuteMarketOrder(TradeType.Buy, Symbol.Name, 10000, "myLabel");
     Positions.Closed += PositionsClosed;
 }
 private void PositionsClosed(PositionClosedEventArgs args)
 {
     var position = args.Position;
     if(position.Label == "myLabel")
     {
         var tradeType = position.TradeType;
         var symbol = MarketData.GetSymbol(position.SymbolName);
         var volume = position.VolumeInUnits;
         var label = position.Label;
         if(position.GrossProfit > 0)
             ExecuteMarketOrder(tradeType, symbol, volume, label);
         else
         {
             var oppositeTrade = tradeType == TradeType.Buy
                         ? TradeType.Sell
                         : TradeType.Buy;
             ExecuteMarketOrder(oppositeTrade, symbol, volume, label);
         }
     }
 }

Reason

Summary

Gets the reason of the position being closed.

Signature

1
public PositionCloseReason Reason {get;}

Return Value

PositionCloseReason