CreateStopLimitOnEntry

The CreateStopLimitOnEntry() Function allows you to set stops and limits on a placed entry order (in case the order was created without predefined stop and limit). It has 4 parameters:

 

1.EntryOrder: TOrder; – the entry order, on which the conditional order is placed.
2.Rate: Double – the stop or limit rate.
3.OrderType: TOrderType — the order type. Values: otStop, otLimit.
4.OperationTag: String — the custom order tag.

Example: Let us create a strategy that will place an Entry Stop Buy Order 40 points above the market rate when it is started, without a predefined stop and limit.

Then it will set a stop 25 points below the entry order rate, and a limit 40 points above the entry order rate, once the difference between the close

and the open rate of the last finished candle is bigger than 20 points.

 

We  will use the candle history for this strategy. The procedure OnNewCandle will check the conditions every time a new candle appears.

We will also use the procedure OnOrderChange to identify the order which was placed when the strategy started. After the Stop and Limit orders are created, the strategy will stop (terminate).

 

The script will be as follows:

 

const

  StrategyName='My Strategy';

  StrategyVersion = '1.0';

 

var

  History: TCandleHistory;

  Account: TAccount;

  Amount, Point: Double;

  Stop, Limit, ESRate: Integer;

  MyOrder: TOrder;

 

procedure OnCreate;

begin

  Settings.AddCandleHistory(@History, 'Candle History''', CI_1_Minute, 100);

  History.OnNewCandleEvent := @OnNewCandle;

  Settings.AddAccount(@Account,'Account''');

  Settings.AddFloat(@Amount, 'Amount(Lots)', 5);

  Settings.AddInteger(@Stop, 'Stop in pips', 25);

  Settings.AddInteger(@Limit, 'Limit in pips', 40);

  Settings.AddInteger(@ESRate, 'Entry Stop Distance in pips', 40);

end;

 

procedure OnStart;

begin

  Point := History.Instrument.PointSize;

  CreateEntryOrder(History.Instrument,  Account,  Amount, bsBuy,

                   History.Instrument.Buy + ESRate*Point,

                   NullRate, NullRate, otEStop, 'EntryStopBuy');

end;

 

// this procedure is used to identify the order when it is inserted

procedure OnOrderChange(const Action: TDataModificationType; const Order: TOrder);

begin

  if (Action = dmtInsert)  and (Order.OrderType = otEStop) and

     (Order.Tag='EntryStopBuy'then

    MyOrder := Order; 

end;

 

procedure OnNewCandle;

begin

  if History.Last(1).Close - History.Last(1).Open > 20*Point  then

    begin

      CreateStopLimitOnEntry(MyOrder, MyOrder.Rate-(History.Instrument.Buy-History.Instrument.Sell)

                                                  -Stop*Point,otStop,'Stop');

      CreateStopLimitOnEntry(MyOrder, MyOrder.Rate-(History.Instrument.Buy-History.Instrument.Sell)

                                                  +Limit*Point,otLimit,'Limit');

      Terminate;

    end;

end;