Skip to content

ModifyTakeProfitPips Method

ModifyTakeProfitPips

Summary

Shortcut for Robot.ModifyPendingOrder method to change Take Profit

Signature

1
public abstract TradeResult ModifyTakeProfitPips(double? takeProfitPips)

Parameters

Name Type Description
takeProfitPips double? New Take Profit value in Pips

Return Value

TradeResult

Declaring Type

cAlgo.API.PendingOrder

Examples

1
2
3
 PlaceLimitOrder(TradeType.Buy, Symbol, 10000,Symbol.Bid);
 var order = LastResult.PendingOrder;
 Print("The pending order's ID: {0}", order.Id);
 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
 using cAlgo.API;
 using System;
 using System.Globalization;
 namespace cAlgo.Robots
 {
     // This sample bot shows how to place different types of pending orders
     [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
     public class PendingOrderPlacingSample : Robot
     {
         [Parameter("Type", DefaultValue = PendingOrderType.Limit)]
         public PendingOrderType OrderType { get; set; }
         [Parameter("Direction", DefaultValue = TradeType.Buy)]
         public TradeType OrderTradeType { 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("Limit Range (Pips)", DefaultValue = 10, MinValue = 1)]
         public double LimitRangeInPips { get; set; }
         [Parameter("Expiry", DefaultValue = "00:00:00")]
         public string Expiry { 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 Method", DefaultValue = StopTriggerMethod.Trade)]
         public StopTriggerMethod StopLossTriggerMethod { get; set; }
         [Parameter("Stop Order Method", DefaultValue = StopTriggerMethod.Trade)]
         public StopTriggerMethod StopOrderTriggerMethod { 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;
             TimeSpan expiry;
             if (!TimeSpan.TryParse(Expiry, CultureInfo.InvariantCulture, out expiry))
             {
                 Print("Invalid expiry");
                 Stop();
             }
             var expiryTime = expiry != TimeSpan.FromSeconds(0) ? (DateTime?)Server.Time.Add(expiry) : null;
             TradeResult result = null;
             switch (OrderType)
             {
                 case PendingOrderType.Limit:
                     var limitPrice = OrderTradeType == TradeType.Buy ? Symbol.Ask - DistanceInPips : Symbol.Ask + DistanceInPips;
                     if (IsAsync)
                         PlaceLimitOrderAsync(OrderTradeType, SymbolName, volumeInUnits, limitPrice, Label, stopLoss, takeProfit, expiryTime, Comment, HasTrailingStop, StopLossTriggerMethod, OnCompleted);
                     else
                         result = PlaceLimitOrder(OrderTradeType, SymbolName, volumeInUnits, limitPrice, Label, stopLoss, takeProfit, expiryTime, Comment, HasTrailingStop, StopLossTriggerMethod);
                     break;
                 case PendingOrderType.Stop:
                     var stopPrice = OrderTradeType == TradeType.Buy ? Symbol.Ask + DistanceInPips : Symbol.Ask - DistanceInPips;
                     if (IsAsync)
                         PlaceStopOrderAsync(OrderTradeType, SymbolName, volumeInUnits, stopPrice, Label, stopLoss, takeProfit, expiryTime, Comment, HasTrailingStop, StopLossTriggerMethod, StopOrderTriggerMethod, OnCompleted);
                     else
                         result = PlaceStopOrder(OrderTradeType, SymbolName, volumeInUnits, stopPrice, Label, stopLoss, takeProfit, expiryTime, Comment, HasTrailingStop, StopLossTriggerMethod, StopOrderTriggerMethod);
                     break;
                 case PendingOrderType.StopLimit:
                     var stopLimitPrice = OrderTradeType == TradeType.Buy ? Symbol.Ask + DistanceInPips : Symbol.Ask - DistanceInPips;
                     if (IsAsync)
                         PlaceStopLimitOrderAsync(OrderTradeType, SymbolName, volumeInUnits, stopLimitPrice, LimitRangeInPips, Label, stopLoss, takeProfit, expiryTime, Comment, HasTrailingStop, StopLossTriggerMethod, StopOrderTriggerMethod, OnCompleted);
                     else
                         result = PlaceStopLimitOrder(OrderTradeType, SymbolName, volumeInUnits, stopLimitPrice, LimitRangeInPips, Label, stopLoss, takeProfit, expiryTime, Comment, HasTrailingStop, StopLossTriggerMethod, StopOrderTriggerMethod);
                     break;
                 default:
                     Print("Invalid order type");
                     throw new ArgumentOutOfRangeException("OrderType");
             }
             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
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
 using cAlgo.API;
 using System;
 using System.Globalization;
 using System.Linq;
 namespace cAlgo.Robots
 {
     // This sample shows how to modify a pending order
     // It uses order comment to find the order, you can use order label instead if you want to
     // Set stop loss and take profit to 0 if you don't want to change it
     // Leave expiry parameter empty if you don't want to change it or 0 if you want to remove it
     // If you don't want to change the target price set it to 0
     // If you don't want to change the volume set it to 0
     [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
     public class PendingOrderModificationSample : Robot
     {
         [Parameter("Order Comment")]
         public string OrderComment { get; set; }
         [Parameter("Order Label")]
         public string OrderLabel { get; set; }
         [Parameter("Target Price", DefaultValue = 0.0)]
         public double TargetPrice { 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("Expiry (HH:mm:ss)")]
         public string Expiry { get; set; }
         [Parameter("Volume (Lots)", DefaultValue = 0.01)]
         public double VolumeInLots { get; set; }
         [Parameter("Has Trailing Stop", DefaultValue = false)]
         public bool HasTrailingStop { get; set; }
         [Parameter("Order Trigger Method", DefaultValue = StopTriggerMethod.Trade)]
         public StopTriggerMethod OrderTriggerMethod { get; set; }
         [Parameter("Limit Range (Pips)", DefaultValue = 10)]
         public double LimitRangeInPips { get; set; }
         protected override void OnStart()
         {
             PendingOrder order = null;
             if (!string.IsNullOrWhiteSpace(OrderComment) && !string.IsNullOrWhiteSpace(OrderComment))
             {
                 order = PendingOrders.FirstOrDefault(iOrder => string.Equals(iOrder.Comment, OrderComment, StringComparison.OrdinalIgnoreCase) && string.Equals(iOrder.Label, OrderLabel, StringComparison.OrdinalIgnoreCase));
             }
             else if (!string.IsNullOrWhiteSpace(OrderComment))
             {
                 order = PendingOrders.FirstOrDefault(iOrder => string.Equals(iOrder.Comment, OrderComment, StringComparison.OrdinalIgnoreCase));
             }
             else if (!string.IsNullOrWhiteSpace(OrderLabel))
             {
                 order = PendingOrders.FirstOrDefault(iOrder => string.Equals(iOrder.Label, OrderLabel, StringComparison.OrdinalIgnoreCase));
             }
             if (order == null)
             {
                 Print("Couldn't find the order, please check the comment and label");
                 Stop();
             }
             var targetPrice = TargetPrice == 0 ? order.TargetPrice : TargetPrice;
             var orderSymbol = Symbols.GetSymbol(order.SymbolName);
             var stopLossInPips = StopLossInPips == 0 ? order.StopLossPips : (double?)StopLossInPips;
             var takeProfitInPips = TakeProfitInPips == 0 ? order.TakeProfitPips : (double?)TakeProfitInPips;
             DateTime? expiryTime;
             if (string.IsNullOrWhiteSpace(Expiry))
             {
                 expiryTime = order.ExpirationTime;
             }
             else if (Expiry.Equals("0", StringComparison.OrdinalIgnoreCase))
             {
                 expiryTime = null;
             }
             else
             {
                 var expiryTimeSpan = default(TimeSpan);
                 if (!TimeSpan.TryParse(Expiry, CultureInfo.InvariantCulture, out expiryTimeSpan))
                 {
                     Print("Your provided value for expiry is not valid, please use HH:mm:ss format");
                     Stop();
                 }
                 expiryTime = expiryTimeSpan == default(TimeSpan) ? null : (DateTime?)Server.Time.Add(expiryTimeSpan);
             }
             var volumeInUnits = VolumeInLots == 0 ? order.VolumeInUnits : orderSymbol.QuantityToVolumeInUnits(VolumeInLots);
             if (order.OrderType == PendingOrderType.Limit)
             {
                 ModifyPendingOrder(order, targetPrice, stopLossInPips, takeProfitInPips, expiryTime, volumeInUnits, HasTrailingStop, StopLossTriggerMethod);
             }
             else if (order.OrderType == PendingOrderType.Stop)
             {
                 ModifyPendingOrder(order, targetPrice, stopLossInPips, takeProfitInPips, expiryTime, volumeInUnits, HasTrailingStop, StopLossTriggerMethod, OrderTriggerMethod);
             }
             else if (order.OrderType == PendingOrderType.StopLimit)
             {
                 ModifyPendingOrder(order, targetPrice, stopLossInPips, takeProfitInPips, expiryTime, volumeInUnits, HasTrailingStop, StopLossTriggerMethod, OrderTriggerMethod, LimitRangeInPips);
             }
         }
     }
 }
 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
 using cAlgo.API;
 using System;
 using System.Linq;
 namespace cAlgo.Robots
 {
     // This sample shows how to cancel a pending order
     [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
     public class PendingOrderCancelationSample : Robot
     {
         [Parameter("Order Comment")]
         public string OrderComment { get; set; }
         [Parameter("Order Label")]
         public string OrderLabel { get; set; }
         protected override void OnStart()
         {
             PendingOrder order = null;
             if (!string.IsNullOrWhiteSpace(OrderComment) && !string.IsNullOrWhiteSpace(OrderLabel))
             {
                 order = PendingOrders.FirstOrDefault(iOrder => string.Equals(iOrder.Comment, OrderComment, StringComparison.OrdinalIgnoreCase) && string.Equals(iOrder.Label, OrderLabel, StringComparison.OrdinalIgnoreCase));
             }
             else if (!string.IsNullOrWhiteSpace(OrderComment))
             {
                 order = PendingOrders.FirstOrDefault(iOrder => string.Equals(iOrder.Comment, OrderComment, StringComparison.OrdinalIgnoreCase));
             }
             else if (!string.IsNullOrWhiteSpace(OrderLabel))
             {
                 order = PendingOrders.FirstOrDefault(iOrder => string.Equals(iOrder.Label, OrderLabel, StringComparison.OrdinalIgnoreCase));
             }
             if (order == null)
             {
                 Print("Couldn't find the order, please check the comment and label");
                 Stop();
             }
             CancelPendingOrder(order);
         }
     }
 }
 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
 using cAlgo.API;
 namespace cAlgo.Robots
 {
     // This sample shows how to use PendingOrders events
     [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
     public class PendingOrderEvents : Robot
     {
         protected override void OnStart()
         {
             PendingOrders.Cancelled += PendingOrders_Cancelled;
             PendingOrders.Modified += PendingOrders_Modified;
             PendingOrders.Filled += PendingOrders_Filled;
         }
         private void PendingOrders_Filled(PendingOrderFilledEventArgs obj)
         {
             var pendingOrderThatFilled = obj.PendingOrder;
             var filledPosition = obj.Position;
         }
         private void PendingOrders_Modified(PendingOrderModifiedEventArgs obj)
         {
             var modifiedOrder = obj.PendingOrder;
         }
         private void PendingOrders_Cancelled(PendingOrderCancelledEventArgs obj)
         {
             var cancelledOrder = obj.PendingOrder;
             var cancellationReason = obj.Reason;
         }
     }
 }

Last update: December 5, 2022

Comments