4.6.2 Special Orders

The signal can generate special exit order types, besides price and market orders such as: ProfitTarget, StopLoss, BreakEven, DollarTrailing, PercentTrailing, ExitOnClose.

Special order types can work in one of two modes (if the mode is set in several signals, then the mode that was set last is used for all special orders):

CurSpecOrdersMode = PerContract – special exits are attached to each entry that opened the position. Average entry order fill price is used to calculate the special order.The Amount should be specified in currency per one contract in the special order settings. CurSpecOrdersMode = PerPosition – one exit is generated per position which closes it completely. The average open position price is used for the calculation of special order prices. In the special order settings, the Amount should be specified in currency per entire position.

NOTE: Amount is one of the parameters used for special orders generation. It sets potential profit value after reaching which the special order becomes active.

Commands to generate special order types are:

  • GenerateExitOnClose() – to generate a market order which closes the current position upon the session close. Please note, that during automated trading, this order may not be executed, because at time the order is sent the trading session may already be closed and orders may not be accepted. To use this type of order in automated trading correctly, it is recommended to set the sessions so they end, for example, a minute before the exchange trading session closes.
  • GenerateProfitTarget(double amount) – to generate a Limit order with a price where the profit (excluding commission) from a position (CurSpecOrdersMode = PerPosition) or from a contract for each entry (CurSpecOrdersMode = PerContract), should not be lower than the amount set in the Amount parameter.
Example:

The long position was opened at price 12,25 for 100 contracts. BigPointValue=10, MinMove=25, PriceScale=0,01.

Signal code:

CurSpecOrdersMode = ESpecOrdersMode.PerPosition;
GenerateProfitTarget(5);

Sell Limit order will be generated at price = EntryPrice + (Amount/Contracts)/ BigPointValue = 12,25 + 5/100/10 = 12,255;

Because the price does not correspond to the minimum price increment, it will be rounded to BETTER price, according to the step MinMove* PriceScale = 25*0,01 = 0,25 до 12,50. The final ProfitTarget order: Sell 100 contracts at 12,50 Limit;

  • GenerateStopLoss(double amount) – to generate a Stop order with the price to set the loss (excluding commission) from a position (CurSpecOrdersMode = PerPosition) or a contract for each entry (CurSpecOrdersMode = PerContract) specified in the Amount parameter.
Example:

The symbol has the following parameters: BigPointValue=100, MinMove=2, PriceScale=0,01. Assume the short position is opened at the average price 0,982 for 10000 contracts by the two orders SellShort1 1000 at 1,00 и SellShort2 9000 at 0,98;

Signal code:

CurSpecOrdersMode = ESpecOrdersMode.PerContract;
GenerateStopLoss(5);

GenerateStopLoss(5) in PerContract mode will generate 2 Stop orders (one exit per each entry).

StopLoss order price for the first entry = EntyPrice + Amount / BigPointValue = 1,00 + 5/100 = 1,00 + 0,05 = 1,05; considering the price step (0,02) is rounded to the WORSE = 1,06. StopLoss order price for the second entry = EntyPrice + Amount / BigPointValue = 0,98 + 5/100 = 0,98 + 0,05 = 1,03; considering the price step (0,02) is rounded to the WORSE = 1,04.

Final sending orders: BuyToCover 1000 contracts from SellShort1 at 1,06 Stop; BuyToCover 9000 contracts from SellShort1 at 1,04 Stop;

  • GenerateBreakEven(double amount) – to generate a stop order at the break-even level (calculated excluding commission) after achieving profit per position (in PerPosition mode) or per contract (PerContract), including commission. Break-even level is calculated excluding the commission and is equal to the execution of the entry order in PerContract mode or the average open price of the position in PerPosition mode.

In PerPosition mode, the stop order sending condition is:

Positions[0].MaxRunUp >= amount;

In PerContract mode, the stop order sending condition for the entry is:

TradeRunUp >= amount;
  • GenerateDollarTrailing(double amount) – to generate a trailing stop order with the price to close the position (RepPosition/entry(PerContract), when the potential profit decreases from the maximum value by a particular amount.

Stop order(s) price calculation is executed tick by tick.

Example:

The symbol has the following parameters: BigPointValue=1, MinMove=1, PriceScale=0,01. The long position entry was at price 34,14 for 100 contracts. The price after entry has reached maximum MaxProfitPrice = 35,01.

Signal code:

CurSpecOrdersMode = ESpecOrdersMode.PerPosition;
GenerateDollarTrailing(5);

TralingStop price = MaxPositionProfitPrice – amount/contracts/BigPointValue = 35,01 – 5/100/1 = 35,01 – 0,05 = 34,96. After rounding to the minimum price step to the WORST, we get the price 34,96.

The final order: Sell 100 contracts at 34,96 Stop.
  • GeneratePercentTrailing(double amount, double percentage) – to generate a trailing stop order that closes the position (trade) after the position(PerPosition)/trade(PerContract) reaches the profit that is greater or equal to the price set in the amount parameter. The price will close the position/trade when the profit goes down by a percentage% from the maximum value.

Stop order(s) price calculation is executed tick by tick.

Example:

The symbol has the following parameters: BigPointValue=1, MinMove=1, PriceScale=0,01.

The long position entries were at price 24,10 for 100 contracts and 24,56 for 50 contracts. Maximum price after the first entry was 24,60, after the second one was 24,58.

Signal code:

CurSpecOrdersMode = ESpecOrdersMode.PerContract;
GeneratePercentTrailing(0.30, 25);

Sending of trailing stop for the first entry possibility is verified:

MaxProfitPerContract = (MaxPrice – EntryPrice)*BigPointValue = (24,60 - 24,10)*1 = 0,50;

Yes, the condition for stop sending is executed MaxProfitPerContract >= amount. Let’s count the price:

StopPrice = MaxPrice – (MaxPrice- EntryPrice)*percentage/100 = 24,60 - (24,60 - 24,10)*25/100 = 24,60 – 0,50*0,25 = 24,475.

Let’s round the price to the minimum step to the WORST, we get the price = 24,47.

Sending of trailing stop for the first entry possibility is verified:

MaxProfitPerContract = (MaxPrice – EntryPrice)*BigPointValue = (24,58 - 24,56)*1 = 0,02; MaxProfitPerContract = (MaxPrice – EntryPrice)*BigPointValue = (24,58 - 24,56)*1 = 0,02;

MaxProfitPerContract condition >= amount for this entry was not executed, so the stop order for it will not be sent at this calculation. It will be generated at the moment, when the price is reached = EntryPrice + amount/BigPointValue = 24,56 + 0,30/1 = 24,86.

The final order will be only one: Sell 100 contracts from Entry1 at 24,47 Stop;

CurSpecOrdersMode – affects all special orders in the strategy. The last set value of this marker is always used for price calculations.