BuyAtMarketToPosition

BuyAtMarketToPosition
public void BuyAtMarketToPosition(Position position, decimal volume)

Unlike the previous one, the BuyAtMarketToPosition method modifies a position by adding a market order to the list of opening orders. It accepts the following parameters:

1. position – the position that needs to be modified;

2. volume – the volume for the new market order;

This method suits well for scaling into a position. Let's outline the following task: in a trend-following strategy, we want to build a position in two parts. If the price deviates from the moving average by a certain percentage, we buy the first part, and if it continues to rise and reaches a value of x2 from the current value of the SMA, we buy the second part.

Let's do this:

1. If the price exceeded the deviation level, we buy at the market.

2. We calculate the second level of deviation.

3. If the price exceeded the second level, and also if the robot has not yet collected the required volume (conditionally 100 lots), we enter the body of the conditional construction.

4. We call the method BuyAtMarketToPosition in order to buy the missing volume.

And in the log, we also see two market opening orders in the position:

public void BuyAtMarketToPosition(Position position, decimal volume, string signalType)

The second version of the method performs the same actions and accepts an additional parameter signalType.

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