Position


Summary

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

Syntax

public interface Position

Members

NameTypeSummary
Close MethodShortcut for the Robot.ClosePosition method.
Comment PropertyComment can be used as a note for the order.
Commissions PropertyCommission Amount of the request to trade one way (Buy/Sell) associated with this position.
EntryPrice PropertyEntry price of the position.
EntryTime PropertyEntry time of trade associated with the position.
The Timezone used is set in the cBot attribute.
GrossProfit PropertyGross profit accrued by the order associated with the position.
HasTrailingStop PropertyWhen HasTrailingStop set to true, the server updates the Stop Loss every time the position moves in your favor.
Id PropertyThe position's unique identifier.
Label PropertyLabel can be used to represent the order.
ModifyStopLossPips MethodShortcut for the Robot.ModifyPosition method to change the Stop Loss pips
ModifyStopLossPrice MethodShortcut for Robot.ModifyPosition method to change the Stop Loss.
ModifyTakeProfitPips MethodShortcut for the Robot.ModifyPosition method to change the Take Profit pips
ModifyTakeProfitPrice MethodShortcut for Robot.ModifyPosition method to change the Take Profit.
ModifyTrailingStop MethodShortcut for the Robot.ModifyPosition method to change the Trailing Stop.
ModifyVolume MethodShortcut for the Robot.ModifyPosition method to change the VolumeInUnits.
NetProfit PropertyThe Net profit of the position.
Pips PropertyRepresents the winning or loosing pips of the position.
Quantity PropertyQuantity of lots traded by the position.
Reverse MethodShortcut for the Robot.ReversePosition method to change the direction of the trade.
StopLoss PropertyThe Stop Loss level of the position.
StopLossTriggerMethod PropertyTrigger method for the position's Stop Loss.
Swap PropertySwap is the overnight interest rate if any, accrued on the position.
SymbolName PropertyGets the symbol name.
TakeProfit PropertyThe take profit level of the position.
TradeType PropertyTrade type (Buy/Sell) of the position.
VolumeInUnits PropertyThe amount traded by the position.

Example 1

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);
    }
}

Example 2

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();
        }
    }
}

Example 3

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);
        }
    }
}

Example 4

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);
            }
        }
    }
}

Example 5

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;
        }
    }
}
Reference