Skip to content

TradeOperation

Summary

Provides access to the properties describing an asynchronous trade operation.

Signature

1
public sealed class TradeOperation

Namespace

cAlgo.API

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
 TradeOperation operation = ExecuteMarketOrderAsync(TradeType.Buy, Symbol.Name, 10000, "asynchronous");
 if (operation.IsExecuting)
 {
     Print("Trade is executing");
 }
 else
 {
     if (operation.TradeResult.IsSuccessful)
         Print("Trade executed");
 }
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
 protected override void OnStart()
 {
     Positions.Opened += OnPositionOpened;
     TradeOperation operation = ExecuteMarketOrderAsync(TradeType.Buy, Symbol.Name, 10000, "asynchronous");
     ExecuteMarketOrder(TradeType.Buy, Symbol.Name, 10000, "synchronous", 10, 10);
     if (operation.IsExecuting)
     {
         Print("Trade is executing");
     }
     else
     {
         if (operation.TradeResult.IsSuccessful)
             Print("Trade executed");
     }
 }
 private void OnPositionOpened(PositionOpenedEventArgs args)
 {
     var position = args.Position;
     Print($"Position {position.Label} opened at {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
 using cAlgo.API;
 namespace cAlgo.Robots
 {
     // This sample cBot shows how to use TradeOperation to monitor an async order execution/placement operation
     [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
     public class Test : Robot
     {
         protected override void OnStart()
         {
             var tradeOperation = ExecuteMarketOrderAsync(TradeType.Buy, SymbolName, Symbol.VolumeInUnitsMin, OnTradeResult);
             if (tradeOperation.IsExecuting)
             {
                 Print("Executing");
             }
             else
             {
                 Print("Completed");
             }
         }
         private void OnTradeResult(TradeResult args)
         {
             Print("Was Trade Operation Successful: ", args.IsSuccessful);
         }
     }
 }
1
2
3
4
5
6
 operation = api.ExecuteMarketOrderAsync(TradeType.Buy, api.Symbol.Name, 10000, "asynchronous")
 if operation.IsExecuting:
     print("Trade is executing")
 else:
     if operation.TradeResult.IsSuccessful:
         print("Trade executed")
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
 def on_start(self):
     api.Positions.Opened += self.on_position_opened
     operation = api.ExecuteMarketOrderAsync(TradeType.Buy, api.Symbol.Name, 10000, "asynchronous")
     api.ExecuteMarketOrder(TradeType.Buy, api.Symbol.Name, 10000, "synchronous", 10, 10)
     if operation.IsExecuting:
         print("Trade is executing")
     else:
         if operation.TradeResult.IsSuccessful:
             print("Trade executed")
 def on_position_opened(self, args):
     position = args.Position
     print(f"Position {position.Label} opened at {position.EntryPrice}")
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
 import clr
 clr.AddReference("cAlgo.API")
 from cAlgo.API import *
 from System import Action
 class Test():
     def on_start(self):
         tradeOperation = api.ExecuteMarketOrderAsync(TradeType.Buy, api.SymbolName, api.Symbol.VolumeInUnitsMin, Action[TradeResult](self.on_trade_result))
         if tradeOperation.IsExecuting:
             print("Executing")
         else:
             print("Completed")
     def on_trade_result(self, args):
         print(f"Was Trade Operation Successful: {args.IsSuccessful}")

Methods

ToString

Summary

The description of a trade operation

Signature

1
public string ToString()

Return Value

string

Examples

1
2
 TradeOperation operation = ExecuteMarketOrderAsync(TradeType.Buy, Symbol.Name, 10000, "asynchronous");
 Print(operation.ToString());
1
2
 operation = api.ExecuteMarketOrderAsync(TradeType.Buy, api.Symbol.Name, 10000, "asynchronous")
 print(operation.ToString())

Properties

IsExecuting

Summary

True if a trade operation is being executed, false if it completed

Signature

1
public bool IsExecuting {get;}

Return Value

bool

Examples

1
2
3
4
5
 TradeOperation operation = ExecuteMarketOrderAsync(TradeType.Buy, Symbol.Name, 20000, "myLabel");
 if (!operation.IsExecuting)
 {
     Print("Trade executed");
 }
1
2
3
 operation = api.ExecuteMarketOrderAsync(TradeType.Buy, api.Symbol.Name, 20000, "myLabel")
 if operation.IsExecuting == False:
     print("Trade executed")

TradeResult

Summary

The result of a trade operation

Signature

1
public TradeResult TradeResult {get;}

Return Value

TradeResult

Examples

1
2
3
4
5
 TradeOperation operation = ExecuteMarketOrderAsync(TradeType.Buy, Symbol.Name, 20000, "myLabel");
 if (!operation.IsExecuting && operation.TradeResult.IsSuccessful)
 {
     Print("Trade {0} executed", operation.TradeResult.Position.Label);
 }
1
2
3
 operation = api.ExecuteMarketOrderAsync(TradeType.Buy, api.Symbol.Name, 20000, "myLabel")
 if operation.IsExecuting == False and operation.TradeResult.IsSuccessful:
     print(f"Trade {operation.TradeResult.Position.Label} executed")