Skip to content

Position

Summary

Taking or opening a position means buying or selling a trading pair.

Signature

1
public abstract interface Position

Namespace

cAlgo.API

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
 protected override void OnStart()
 {
     foreach (var position in Positions)
     {
         Print("Position Label {0}", position.Label);
         Print("Position ID {0}", position.Id);
         Print("Profit {0}", position.GrossProfit);
         Print("Entry Price {0}", position.EntryPrice);
     }
 }
 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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
 using cAlgo.API;
 using cAlgo.API.Internals;
 namespace cAlgo.Robots
 {
     /// 
     /// This sample shows how to execute a position or market order
     /// 
     [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
     public class PositionExecutionSample : Robot
     {
         [Parameter("Direction", DefaultValue = TradeType.Buy)]
         public TradeType Direction { get; set; }
         [Parameter("Volume (Lots)", DefaultValue = 0.01)]
         public double VolumeInLots { get; set; }
         [Parameter("Distance (Pips)", DefaultValue = 20, MinValue = 1)]
         public double DistanceInPips { get; set; }
         [Parameter("Stop (Pips)", DefaultValue = 10, MinValue = 0)]
         public double StopInPips { get; set; }
         [Parameter("Target (Pips)", DefaultValue = 10, MinValue = 0)]
         public double TargetInPips { get; set; }
         [Parameter("Label")]
         public string Label { get; set; }
         [Parameter("Comment")]
         public string Comment { get; set; }
         [Parameter("Trailing Stop", DefaultValue = false)]
         public bool HasTrailingStop { get; set; }
         [Parameter("Stop Loss Trigger Method", DefaultValue = StopTriggerMethod.Trade)]
         public StopTriggerMethod StopLossTriggerMethod { get; set; }
         [Parameter("Async", DefaultValue = false)]
         public bool IsAsync { get; set; }
         protected override void OnStart()
         {
             var volumeInUnits = Symbol.QuantityToVolumeInUnits(VolumeInLots);
             DistanceInPips *= Symbol.PipSize;
             var stopLoss = StopInPips == 0 ? null : (double?)StopInPips;
             var takeProfit = TargetInPips == 0 ? null : (double?)TargetInPips;
             TradeResult result = null;
             if (IsAsync)
                 ExecuteMarketOrderAsync(Direction, SymbolName, volumeInUnits, Label, stopLoss, takeProfit, Comment, HasTrailingStop, StopLossTriggerMethod, OnCompleted);
             else
                 result = ExecuteMarketOrder(Direction, SymbolName, volumeInUnits, Label, stopLoss, takeProfit, Comment, HasTrailingStop, StopLossTriggerMethod);
             if (!IsAsync) OnCompleted(result);
         }
         private void OnCompleted(TradeResult result)
         {
             if (!result.IsSuccessful) Print("Error: ", result.Error);
             Stop();
         }
     }
 }
 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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
 using cAlgo.API;
 using System;
 using System.Linq;
 namespace cAlgo.Robots
 {
     /// 
     /// This sample shows how to close a position
     /// 
     [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
     public class PositionClosingSample : Robot
     {
         [Parameter("Position Comment")]
         public string PositionComment { get; set; }
         [Parameter("Position Label")]
         public string PositionLabel { get; set; }
         protected override void OnStart()
         {
             Position position = null;
             if (!string.IsNullOrWhiteSpace(PositionComment) && !string.IsNullOrWhiteSpace(PositionLabel))
             {
                 position = Positions.FindAll(PositionLabel).FirstOrDefault(iOrder => string.Equals(iOrder.Comment, PositionComment, StringComparison.OrdinalIgnoreCase));
             }
             else if (!string.IsNullOrWhiteSpace(PositionComment))
             {
                 position = Positions.FirstOrDefault(iOrder => string.Equals(iOrder.Comment, PositionComment, StringComparison.OrdinalIgnoreCase));
             }
             else if (!string.IsNullOrWhiteSpace(PositionLabel))
             {
                 position = Positions.Find(PositionLabel);
             }
             if (position == null)
             {
                 Print("Couldn't find the position, please check the comment and label");
                 Stop();
             }
             ClosePosition(position);
         }
     }
 }
 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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
 using cAlgo.API;
 using System;
 using System.Linq;
 namespace cAlgo.Robots
 {
     /// 
     /// This sample shows how to modify a position
     /// 
     [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
     public class PositionModificationSample : Robot
     {
         [Parameter("Position Comment")]
         public string PositionComment { get; set; }
         [Parameter("Position Label")]
         public string PositionLabel { get; set; }
         [Parameter("Stop Loss (Pips)", DefaultValue = 10)]
         public double StopLossInPips { get; set; }
         [Parameter("Stop Loss Trigger Method", DefaultValue = StopTriggerMethod.Trade)]
         public StopTriggerMethod StopLossTriggerMethod { get; set; }
         [Parameter("Take Profit (Pips)", DefaultValue = 10)]
         public double TakeProfitInPips { get; set; }
         [Parameter("Volume (Lots)", DefaultValue = 0.01)]
         public double VolumeInLots { get; set; }
         [Parameter("Has Trailing Stop", DefaultValue = false)]
         public bool HasTrailingStop { get; set; }
         protected override void OnStart()
         {
             Position position = null;
             if (!string.IsNullOrWhiteSpace(PositionComment) && !string.IsNullOrWhiteSpace(PositionLabel))
             {
                 position = Positions.FindAll(PositionLabel).FirstOrDefault(iOrder => string.Equals(iOrder.Comment, PositionComment, StringComparison.OrdinalIgnoreCase));
             }
             else if (!string.IsNullOrWhiteSpace(PositionComment))
             {
                 position = Positions.FirstOrDefault(iOrder => string.Equals(iOrder.Comment, PositionComment, StringComparison.OrdinalIgnoreCase));
             }
             else if (!string.IsNullOrWhiteSpace(PositionLabel))
             {
                 position = Positions.Find(PositionLabel);
             }
             if (position == null)
             {
                 Print("Couldn't find the position, please check the comment and label");
                 Stop();
             }
             var positionSymbol = Symbols.GetSymbol(position.SymbolName);
             var stopLossInPrice = position.StopLoss;
             if (StopLossInPips > 0)
             {
                 var stopLossInPipsPrice = StopLossInPips * positionSymbol.PipSize;
                 stopLossInPrice = position.TradeType == TradeType.Buy ? position.EntryPrice - stopLossInPipsPrice : position.EntryPrice + stopLossInPipsPrice;
             }
             var takeProfitInPrice = position.TakeProfit;
             if (TakeProfitInPips > 0)
             {
                 var takeProfitInPipsPrice = TakeProfitInPips * positionSymbol.PipSize;
                 takeProfitInPrice = position.TradeType == TradeType.Buy ? position.EntryPrice + takeProfitInPipsPrice : position.EntryPrice - takeProfitInPipsPrice;
             }
             ModifyPosition(position, stopLossInPrice, takeProfitInPrice, HasTrailingStop, StopLossTriggerMethod);
             if (VolumeInLots > 0)
             {
                 var volumeInUnits = positionSymbol.QuantityToVolumeInUnits(VolumeInLots);
                 ModifyPosition(position, volumeInUnits);
             }
         }
     }
 }
 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
26
27
28
29
30
 using cAlgo.API;
 namespace cAlgo.Robots
 {
     /// 
     /// This sample shows how to handle position events
     /// 
     [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
     public class PositionEventsSample : Robot
     {
         protected override void OnStart()
         {
             Positions.Opened += Positions_Opened;
             Positions.Closed += Positions_Closed;
             Positions.Modified += Positions_Modified;
         }
         private void Positions_Modified(PositionModifiedEventArgs obj)
         {
             var modifiedPosition = obj.Position;
         }
         private void Positions_Closed(PositionClosedEventArgs obj)
         {
             var closedPosition = obj.Position;
             var closeReason = obj.Reason;
         }
         private void Positions_Opened(PositionOpenedEventArgs obj)
         {
             var openedPosition = obj.Position;
         }
     }
 }

Methods

ModifyStopLossPrice

Summary

Shortcut for Robot.ModifyPosition method to change the Stop Loss.

Signature

1
public abstract TradeResult ModifyStopLossPrice(double? stopLoss)

Parameters

Name Type Description
stopLoss double? New Stop Loss price

Return Value

TradeResult

ModifyTakeProfitPrice

Summary

Shortcut for Robot.ModifyPosition method to change the Take Profit.

Signature

1
public abstract TradeResult ModifyTakeProfitPrice(double? takeProfit)

Parameters

Name Type Description
takeProfit double? New Take Profit price

Return Value

TradeResult

ModifyStopLossPips

Summary

Shortcut for the Robot.ModifyPosition method to change the Stop Loss pips

Signature

1
public abstract TradeResult ModifyStopLossPips(double? stopLossPips)

Parameters

Name Type Description
stopLossPips double? New Stop Loss in Pips

Return Value

TradeResult

ModifyTakeProfitPips

Summary

Shortcut for the Robot.ModifyPosition method to change the Take Profit pips

Signature

1
public abstract TradeResult ModifyTakeProfitPips(double? takeProfitPips)

Parameters

Name Type Description
takeProfitPips double? New Take Profit in Pips

Return Value

TradeResult

ModifyTrailingStop

Summary

Shortcut for the Robot.ModifyPosition method to change the Trailing Stop.

Signature

1
public abstract TradeResult ModifyTrailingStop(bool hasTrailingStop)

Parameters

Name Type Description
hasTrailingStop bool If true position will have trailing stop loss

Return Value

TradeResult

ModifyVolume

Summary

Shortcut for the Robot.ModifyPosition method to change the VolumeInUnits.

Signature

1
public abstract TradeResult ModifyVolume(double volume)

Parameters

Name Type Description
volume double New Volume in Units

Return Value

TradeResult

Reverse (2)

Reverse (1 of 2)

Summary

Shortcut for the Robot.ReversePosition method to change the direction of the trade.

Signature

1
public abstract TradeResult Reverse()

Return Value

TradeResult

Reverse (2 of 2)

Summary

Shortcut for the Robot.ReversePosition method to change the direction of trade and the volume.

Signature

1
public abstract TradeResult Reverse(double volume)

Parameters

Name Type Description
volume double New Volume in Units

Return Value

TradeResult

Close

Summary

Shortcut for the Robot.ClosePosition method.

Signature

1
public abstract TradeResult Close()

Return Value

TradeResult

Properties

SymbolName

Summary

Gets the symbol name.

Signature

1
public abstract string SymbolName {get;}

Return Value

string

TradeType

Summary

Trade type (Buy/Sell) of the position.

Signature

1
public abstract TradeType TradeType {get;}

Return Value

TradeType

Examples

1
2
 ExecuteMarketOrder(TradeType.Buy, Symbol, 10000, "myLabel", 10,10);
 Print(LastResult.Position.TradeType);

VolumeInUnits

Summary

The amount traded by the position.

Signature

1
public abstract double VolumeInUnits {get;}

Return Value

double

Examples

1
2
 ExecuteMarketOrder(TradeType.Buy, Symbol, 10000, "myLabel", 10,10);
 Print(LastResult.Position.VolumeInUnits);

Id

Summary

The position's unique identifier.

Signature

1
public abstract int Id {get;}

Return Value

int

Examples

1
2
 ExecuteMarketOrder(TradeType.Buy, Symbol, 10000, "myLabel", 10,10);
 Print(LastResult.Position.Id);

GrossProfit

Summary

Gross profit accrued by the order associated with the position.

Signature

1
public abstract double GrossProfit {get;}

Return Value

double

Examples

1
2
 ExecuteMarketOrder(TradeType.Buy, Symbol, 10000, "myLabel", 10,10);
 Print(LastResult.Position.GrossProfit);

EntryPrice

Summary

Entry price of the position.

Signature

1
public abstract double EntryPrice {get;}

Return Value

double

Examples

1
2
 ExecuteMarketOrder(TradeType.Buy, Symbol, 10000, "myLabel", 10,10);
 Print(LastResult.Position.EntryPrice);

StopLoss

Summary

The Stop Loss level of the position.

Signature

1
public abstract double? StopLoss {get;}

Return Value

double?

Examples

1
2
 ExecuteMarketOrder(TradeType.Buy, Symbol, 10000, "myLabel", 10,10);
 Print(LastResult.Position.StopLoss);

TakeProfit

Summary

The take profit level of the position.

Signature

1
public abstract double? TakeProfit {get;}

Return Value

double?

Examples

1
2
 ExecuteMarketOrder(TradeType.Buy, Symbol, 10000, "myLabel", 10,10);
 Print(LastResult.Position.TakeProfit);

NetProfit

Summary

The Net profit of the position.

Signature

1
public abstract double NetProfit {get;}

Return Value

double

Examples

1
2
 ExecuteMarketOrder(TradeType.Buy, Symbol, 10000, "myLabel", 10,10);
 Print(LastResult.Position.NetProfit);

Swap

Summary

Swap is the overnight interest rate if any, accrued on the position.

Signature

1
public abstract double Swap {get;}

Return Value

double

Examples

1
 Print(LastResult.Position.Swap);

Commissions

Summary

Commission Amount of the request to trade one way (Buy/Sell) associated with this position.

Signature

1
public abstract double Commissions {get;}

Return Value

double

Examples

1
2
 ExecuteMarketOrder(TradeType.Buy, Symbol, 10000, "myLabel", 10,10);
 Print(LastResult.Position.Commissions);

EntryTime

Summary

Entry time of trade associated with the position.The Timezone used is set in the cBot attribute.

Signature

1
public abstract DateTime EntryTime {get;}

Return Value

DateTime

Examples

1
2
 ExecuteMarketOrder(TradeType.Buy, Symbol, 10000, "myLabel", 10,10);
 Print(LastResult.Position.EntryTime);

Pips

Summary

Represents the winning or loosing pips of the position.

Signature

1
public abstract double Pips {get;}

Return Value

double

Examples

1
2
 ExecuteMarketOrder(TradeType.Buy, Symbol, 10000, "myLabel", 10,10);
 Print(LastResult.Position.Pips);

Label

Summary

Label can be used to represent the order.

Signature

1
public abstract string Label {get;}

Return Value

string

Examples

1
2
3
 var result = ExecuteMarketOrder(TradeType.Sell, Symbol, 10000, "myLabel");
 if(result.IsSuccessful)
     Print("Position {0} is open", result.Position.Label);

Comment

Summary

Comment can be used as a note for the order.

Signature

1
public abstract string Comment {get;}

Return Value

string

Examples

1
2
3
 ExecuteMarketOrder(TradeType.Buy, Symbol, 5000, "myLabel", 10, 10, 2, "this is a comment");
 if(result.IsSuccessful)
     Print("Position is open: {0}", result.Position.Comment);

Quantity

Summary

Quantity of lots traded by the position.

Signature

1
public abstract double Quantity {get;}

Return Value

double

HasTrailingStop

Summary

When HasTrailingStop set to true, the server updates the Stop Loss every time the position moves in your favor.

Signature

1
public abstract bool HasTrailingStop {get;}

Return Value

bool

Examples

1
2
 ExecuteMarketOrder(TradeType.Buy, Symbol, 10000, "myLabel", 10, 10, 2, "comment", true);
 Print("Position was opened, has Trailing Stop = {0}", result.Position.HasTrailingStop);

StopLossTriggerMethod

Summary

Trigger method for the position's Stop Loss.

Signature

1
public abstract StopTriggerMethod? StopLossTriggerMethod {get;}

Return Value

StopTriggerMethod?

Margin

Summary

The amount of used margin by the position.

Signature

1
public abstract double Margin {get;}

Return Value

double

CurrentPrice

Summary

Gets the position current market price.If Position's TradeType is Buy it returns Symbol current Bid price.If position's TradeType is Sell it returns Symbol current Ask price.

Signature

1
public abstract double CurrentPrice {get;}

Return Value

double

Symbol

Summary

Gets the position symbol.

Signature

1
public abstract Symbol Symbol {get;}

Return Value

Symbol

SymbolCode

Signature

1
public abstract string SymbolCode {get;}

Return Value

string

Volume

Signature

1
public abstract long Volume {get;}

Return Value

long

Profit

Signature

1
public abstract decimal Profit {get;}

Return Value

decimal