Robot Positions. Class Position.

Robot Positions. Class Position.

Most robots in OsEngine see and manage only their own positions. Those that are stored only in the specific robot's journal. Robots open and close positions, manage them, and see them only in their own journal.

In the picture above, the difference between the position on the exchange (PositionOnBoard) and the positions (Position) of the robots is depicted. From this image, you should understand that robots can have multiple positions (even in different directions) for one instrument, while in the exchange portfolio, there is always just one.

 

1. What is a position for a robot?

When a robot wants to buy or sell an instrument on the exchange, it opens a position that only it can see, and which can be seen in its journal.

When a robot wants to exit some asset, it must close the position.

For those who have traded for many years only in Quik, this concept can be painful. However, this came to OsEngine from WealthLab, which has been a world standard for writing robots for many years. You will have to work closely with this class and get used to such an abstraction.

In the robot's journal, the positions look like a table:

1. Open the general journal in the terminal.

2. Go to the "Open Positions" tab.

3. A corresponding table will appear before you.

 

2. Class Position in OsEngine.

You can view it on GitHub here.

In the project, it is located here:

Let's consider the members of the Position class.

OpenOrders – public property of type List<Order>. Returns a list of all orders involved in opening this position.

AddNewOpenOrder – public method that adds an incoming order that was involved in opening the position.

CloseOrders – public property of type List<Order>. Returns a list of all orders involved in closing this position.

MyTrades – public property of type List<MyTrade>. Returns a list of all trades belonging to the position.

AddNewCloseOrder – public method. Adds an incoming order that was involved in closing the position.

OpenActiv – public property of type bool. Checks if there are active orders to open in the position.

CloseActiv – public property of type bool. Checks if there are active orders to close in the position.

StopOrderIsActiv – public field of type bool. Flag indicating the activity of the stop-loss for the position.

StopOrderPrice – public field of type decimal. Contains the price of the stop-loss level.

StopOrderRedLine – public field of type decimal. Contains the price at which a real order will be sent to the trading system at the StopOrderPrice level.

ProfitOrderIsActiv – public field of type bool. Flag indicating the activity of the take-profit for the position.

ProfitOrderPrice – public field of type decimal. Contains the price of the take-profit level.

ProfitOrderRedLine – public field of type decimal. Contains the price at which a real order will be sent to the trading system at the ProfitOrderPrice level.

Direction – public field of type Side. Indicates the direction of the position. The used values of the enumeration Side:

None - used when creating objects when the direction is not defined.

Buy - purchase, long position, long.

Sell - sale, short position, short.

State – public field of type PositionStateType. Displays the current state of the position. Let's take a closer look at all options of the enumeration:

None - the object has just been created by the program, and its state has not yet been defined.

Opening - the position is in the process of being opened. This state indicates the presence of an active order involved in opening the position.

Done - closed. Indicates successful execution of the operation.

OpeningFail - error opening the position. Set if the opening order was canceled or rejected by the trading system.

Open - successful opening of the position. It should be noted that if the opening order was partially executed, the position will still be considered opened.

Closing - in the process of being closed. This state indicates the presence of an active or partially executed order.

ClosingFail - there was an unsuccessful attempt to close. Action must be taken to close it.

ClosingSurplus - the position was closed with a surplus, meaning the executed volume in the orders that closed the position exceeds the corresponding figure in the opening orders.

Deleted - there was a request to delete the position.

Number – public field of type int. Stores a unique number within the program.

SecurityName – public string property. Provides access to the field that stores the name of the trading instrument for which trades are made within this position.

NameBot – public string field. Stores the name of the robot to which this position belongs.

ProfitOperationPersent – public field of type decimal. Used to store the trading result in percentage terms relative to the opening price of the position.

ProfitOperationPunkt – public field of type decimal. Used to store the trading result expressed in price steps of the instrument.

Comment – public string field. Used to store additional information provided by the user.

SignalTypeOpen – public field of type string. Contains a description of the signal to open a position.

SignalTypeClose – public field of type string. Contains a description of the signal to close a position.

MaxVolume – public property of type decimal, read-only. Returns the maximum volume executed in the position.

OpenVolume – public property of type decimal. Read-only. Returns the open volume.

WaitVolume – public property of type decimal. Read-only. Returns the volume set on the exchange and waiting to be opened.

EntryPrice – public property of type decimal. Calculates and returns the weighted average price of all trades involved in the opening of the position.

ClosePrice – public property of type decimal. Calculates and returns the weighted average price of all trades involved in the closing of the position.

MultToJournal – multiplier for position analysis. Used for platform needs. IMPORTANT. Do not change the value.

SetOrder – public method. Calculates the necessary state of the position using the incoming order.

CalculateProfitToPosition – private method. Calculates the values of the fields ProfitOperationPersent and ProfitOperationPunkt.

SetTrade – public method. Calculates the necessary state of the position using the incoming trade.

SetBidAsk – public method. Calculates the necessary state of the position using the incoming prices of the best levels for buying and selling.

GetStringForSave – public method that does not take parameters. Returns a string that contains all fields of the object for further storage.

SetDealFromString – public method. Accepts a string containing data for all fields, parses it, and initializes the object's state.

TimeCreate – public property of type DateTime. Returns the creation time of the position.

TimeClose – public property of type DateTime. Returns the closing time of the position.

TimeOpen – public property of type DateTime. Returns the opening time of the position.

ProfitPortfolioPersent – public property of type decimal. Returns the size of the profit/loss of the position relative to the portfolio in percentage terms.

ComissionType – public field of type ComissionType. Defines the method of calculating the commission. The ComissionType enumeration contains the following values:

None - method not set.

Percent - in percentage terms.

OneLotFix - fixed amount per lot.

ComissionValue – public field of type decimal. Contains the value used for calculating the commission.

ProfitPortfolioPunkt – public property of type decimal. Returns the amount of profit/loss relative to the portfolio in absolute terms, calculated including the commission.

IsLotServer – private method. Determines whether the exchange supports multiple papers in one lot.

CommissionTotal – public method. Calculates the total commission for the position.

Lots – public field of type decimal. Contains the number of instrument papers in one lot.

PriceStepCost – public field of type decimal. Contains the cost of the minimum price movement for the instrument.

PriceStep – public field of type decimal. Contains the size of the minimum possible price movement.

PortfolioValueOnOpenPosition – public field of type decimal. Contains the size of the portfolio at the moment of position creation.

If you have any difficulties or questions, please write to the support chat. Link