search Nothing found
Main Algotrading documentation Trade operations Positions

Positions.Оpen

We send a request to open a position with the specified parameters.

There are two ways to call a method:

int Positions.Open(symbolName, positionType, volume, takeProfit, stopLoss, takeProfitByPips,
stopLossByPips, label, out positionId, isJustCheck = false, isAsync = false)


Input parameters:

Parameter
Type
Description
symbolName          
string
Symbol name
positionType
Buy or Sell
volume
double
Position volume in lots
takeProfit
double
Take Profit - the level of the market price, upon reaching which you should close the position to take profits. Indicated in quoted currency.
stopLoss
double
Stop loss - the level of the market price, upon reaching which you should close the position to fix losses. Indicated in quoted currency.
takeProfitByPips
Int
Take profit. Price level in points for profit fixing.
stopLossByPips
Int
Stop Loss. Price level in points for fixing losses.
positionId
outint
In the parameter, you can set a variable into which, after a successful opening of a position, a unique identifier of the open position will be recorded. The parameter works only for synchronous mode.
isJustCheck
bool
Check mode (enabled or disabled). When the mode is enabled, a request to open a position is not sent, but only the possibility of opening with such parameters is checked. Disabled by default
 = false
isAsync
bool
Asynchronous mode (enabled or disabled).
Disabled by default
= false


int Positions.Open(ref position, isJustCheck = false, isAsync = false)

Input parameters:

Parameter
Type
Description
position
An object that contains all operation parameters for opening a position
isJustCheck
bool
Check mode (enabled or disabled). When the mode is enabled, a request to open a position is not sent, but only the possibility of opening with such parameters is checked. Disabled by default
 = false
isAsync
bool
Asynchronous mode (enabled or disabled).
Disabled by default
= false


Return value:

If isJustCheck= true

It returns the code of the operation execution result:

0
Check for the possibility of opening was successful. Check occurs on the client side
-1
Check failed


If isAsync= false (synchronous mode by default), it returns the code of the operation execution result:

0
Position is successfully opened
If an error occurs, returns the server error code

If the name of the variable is passed to the out parameter positionId and the position is successfully opened, then the unique identifier of the open position will be recorded to this variable.


If isAsync= true (asynchronous mode)

0
The request to open a position was successfully sent to the server. It does not mean that the position has already been executed or will definitely be executed.
If an error occurs, it returns the error code received on the client. The codes match the server error codes.

Using the out parameter positionId in asynchronous mode does not make sense. It will always return -1

Example:

/*
We open a buy position on the main symbol of the robot, with a volume of 0.1 lots, without setting TP/SL, with the value of the MyLabel position label set.
*/
string MyLabel="MyPosition";
int PositionId;
int err = -1;
err = Positions.Open(Symbol.SymbolName, PositionType.Sell, Lot, 0, 0, 0, 0, MyLabel, out positionId);
if (err != 0) {
msg= DateTime.Now.ToString("dd.MM.yyyy HH:mm:ss:fff") + " Failed, ServerErrorCode = " + err.ToString();
}