Positions.Modify
We send a request to change an open position.
There are two options for calling a method:
int Positions.Modify(positionId, takeProfit, stopLoss, isJustCheck = false, isAsync = false)
Input parameters:
Parameters | Type | Description |
positionId | int | Position unique identifier |
takeProfit | double | Take profit. Price level in quoted currency for profit fixing. |
stopLoss | double | Stop Loss. Price level in quoted currency for fixing losses. |
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.Modify(ref position, isJustCheck = false, isAsync = false)
Input parameters:
Parameters |
Type |
Description |
position |
An object that contains all the parameters of the operation to change the 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 result code of the operation:
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 result code of the operation:
0 |
Position has been successfully modified |
If an error occurs, it returns the server error code |
If isAsync = true (asynchronous mode)
0 |
Position has been successfully modified |
If an error occurs, it returns the server error code |
Example:
//-----------------------------------------------------------
// We modify stoploss and takeprofit of the position
//-----------------------------------------------------------
private void ModifyPosition(int positionId)
{
string line = "ModifyPosition: " + DateTime.Now.ToString("dd.MM.yyyy HH:mm:ss:fff");
Position pos = Positions.Find(positionId);
if (pos != null) {
double takeProfit = Math.Round(pos.OpenPrice + 4000 / Math.Pow(10, Symbol.Digits), Symbol.Digits);
double stopLoss = Math.Round(pos.OpenPrice - 4000 / Math.Pow(10, Symbol.Digits), Symbol.Digits);
int err = Positions.Modify(pos.PositionId, takeProfit, stopLoss);
if (err == 0) {
line = line + Environment.NewLine +
" Symbol = " + Symbol.SymbolName + Environment.NewLine +
" Commission = " + pos.Commission.ToString() + Environment.NewLine +
" Margin = " + pos.Margin.ToString() + Environment.NewLine +
" OpenDate = " + pos.OpenDate.ToString("dd.MM.yyyy HH:mm:ss:fff") + Environment.NewLine +
" OpenPrice = " + pos.OpenPrice.ToString() + Environment.NewLine +
" Label = " + pos.Label + Environment.NewLine +
" TakeProfit = " + takeProfit.ToString() + Environment.NewLine +
" StopLoss = " + stopLoss.ToString() + Environment.NewLine +
" Swap = " + pos.Swap.ToString() + Environment.NewLine +
" PositionId = " + pos.PositionId.ToString() + Environment.NewLine +
" PositionType = " + pos.PositionType.ToString() + Environment.NewLine +
" Profit = " + pos.Profit.ToString() + Environment.NewLine;
} else {
line = line + " Failed, ServerErrorCode = " + err.ToString() + Environment.NewLine;
}
} else {
line = line + " Position not found " + Environment.NewLine;
}
using (StreamWriter sw = new StreamWriter(mOnTickLog, true))
sw.WriteLine(line);