Exit from a position with multiple orders simultaneously through multiple openings.

Exit from a position with multiple orders simultaneously through multiple openings.

Let’s consider an example of how to place several closing orders on a position at the same time by opening multiple positions upon entry.

We remind you that the architecture of OsEngine prohibits placing more than one closing order in the market, and this setup allows us to bypass that limitation.

 

1. Open the example robot PriceChannelCounterTrend.

On GitHub, in the OsEngine repository, it can be found here.

Inside the project here:

2. Constructor and service code.

1. A field for storing the source BotTabSimple. Later, we will place the source object in this field so we can access it from any part of the robot. This makes it more convenient.

2. A field for storing the indicator.

3. Parameters. We will discuss each one shortly.

4. Creation of the source BotTabSimple.

5. Creation of the parameters.

6. Creation of the PriceChannel indicator. Set its length from the parameter.

7. Subscription to the CandleFinishedEvent (candle completion) event. All trading logic will be in the handler of this event.

8. Subscription to the parameter change event. In this handler, we will set the new length value for the indicator.

9. Disable automatic position management.

The settings are:

Responsibilities of the parameters:

1. Regime – operating mode.

a. Off – Disabled.

b. On – Enabled and will enter both long and short positions.

c. OnlyLong – opening only long positions.

d. OnlyShort – opening only short positions.

e. OnlyClosePosition – only closing positions is available.

2. Volume type – method for selecting volume.

a. Contracts – number of contracts for the instrument.

b. Contract currency – currency of the contract.

c. Deposit percent – percentage of the deposit.

3. Volume – value of the volume. What it represents depends on the previous point. In the case of Contracts, the volume of the instrument is specified here. In the case of Contract currency, the amount in rubles or dollars to enter is specified here. In the case of Deposit percent, the percentage of the total deposit to enter into the contract is specified here.

4. Asset in portfolio – here you should indicate the name of the currency that will be used to calculate the volume if you chose the volume type “Deposit percent.” In the tester, we leave it as "Prime." In crypto, this is usually "USDT."

5. Price channel length – length of the PriceChannel indicator.

6. Stop percent – size of the stop for closing the position.

7. Profit order one percent – distance from entry to the first closing order.

8. Profit order two percent – distance from entry to the second closing order.

 

3. Entering the logic in the candle completion event.

1. If the robot's mode is Off, exit the method.

2. If there is no data from the indicator, exit the method.

3. If the data in the indicator is less than its calculation length, exit the method.

4. Retrieve all positions from the source.

5. If there are no positions, but the mode does not allow opening new positions, exit the method.

6. If there are no positions and the mode allows it, proceed to the position opening method.

7. If there are positions, proceed to the position closing method. Note that this is done through a loop – sending all existing positions for closure.

 

4. Logic for opening positions.

1. Take the necessary variables for calculations. The last closing price of the candle. The upper and lower channel boundaries. Note that for PriceChannel, the previous values are taken, as this is the only way to determine the channel breakout.

2. If for some reason the channel boundaries are equal to zero, exit the method.

3. If the candle closing price crossed the upper channel boundary, enter short.

4. If the candle closing price crossed the lower channel boundary, enter long.

5. IMPORTANT! Note that two positions are immediately opened in each direction, and each is given a name for the position. The first is First, the second is Second.

 

5. Logic for closing positions.

1. If the stop order for the position is not set, we enter this logic branch and set it separately for Long and Short.

2. If this is position number one (First) and there are no active closing orders for it, we enter and set a closing order for it at a specific percentage from the entry.

3. If this is position number two (Second) and there are no active closing orders for it, we enter and set a closing order for it at a specific percentage from the entry.

 

6. Upon exit.

Counter-Trend robot based on the PriceChannel indicator, which closes its position with two orders simultaneously:

Wishing you successful algorithms!

Support for OsEngine