CloseTrade

The CloseTrade() function allows you to close positions. In order to close a position, you need to indicate the object from the TTrade class. The function has 4 parameters:

 

1.Trade: TTrade – the open position which should be closed.
2.Amount: Double — the number of lots.
3.TraderRange: Integer — Trader Range.
4.OperationTag: String — the custom order tag (may be used to identify closed positions).

     This is an optional parameter. It does not have to be the same as the position tag.

Example: Let us create a strategy that will open a buy position on start. Then it will close the position when the Net P/L of the position is lower than or equal to -100 points.

 

We will use the OnNewRate procedure for this strategy, as it runs every time a feed update is received. Remember that you must set this function in the procedure OnCreate().

The procedure OnTradeChange will be used to identify the position that was opened when the strategy started. After the position is closed, the strategy will stop (terminate).

 

const

 StrategyName='My Strategy';

 StrategyVersion = '1.0';

 

var

 History: TCandleHistory;

 Account: TAccount;

 Amount: Double;

 MyTrade: TTrade;

 TraderRange: Integer;

 

procedure OnCreate;

begin

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

 History.OnNewRateEvent := @OnNewRate;

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

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

 Settings.AddInteger(@TraderRange, 'Trader Range', 2);

end;

 

procedure OnStart;

begin

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

             NullRate, NullRate, TraderRange, 'MyPosition');

end;

 

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

procedure OnTradeChange(const Action: TDataModificationType; const Trade: TTrade);

begin

if (Action = dmtInsert) and (Trade.Tag = 'MyPosition') then MyTrade:=Trade;

end;

 

procedure OnNewRate;

begin

if ((MyTrade.OpenRate-MyTrade.CloseRate)>=100*History.Instrument.PointSize) then

  begin

     CloseTrade(MyTrade, MyTrade.Amount, TraderRange, 'MyPosition');

     Terminate;

  end;

end;

 

Note: when the strategy is simultaneously closing a large number of open positions, a situation may occur when the script will attempt to close a previously closed  position.

Also, your strategy might send a close order on a position regardless of the fact that such order may already exist.

Each such situation constitutes an event. There are 4 system events which can be managed (TSystemEvent class objects):

 

System events

System event

Description

EventTradeNotFound

The script tries to close a non-existent position

EventDuplicateCloseOrder

The script tries to place a close order regardless of the fact that such order already exists

EventOrderCantBeRemoved

The script tries to remove a triggered order

EventOrderCantBeModified

The script tries to modify a triggered order

EventOrderIsCloseToMarket

The script tries to place order so close to the market price

 

To set the same reaction to all System events (described in the table below) use

 

SetSystemEventForAll(const Reaction: TSystemEventReaction;

 

You can set an appropriate action for each of those events. There are 4 types of action (TSystemEventReaction class objects):

 

System event actions

System event action

Description

serException

An exception will be raised. The strategy will be terminated.

serMessage

The message will be output on the screen. The strategy will continue to run.

serLog

The message will be output in the log. The strategy will continue to run.

serNothing

Nothing will happen. The strategy will continue to run.

 

By default, the action to all the system events is set to serMessage. So if any of them takes place, you will receive a message.

However, if you want some other action to be performed in any of these cases, you can set it as desired.

 

Example:

 

EventTradeNotFound.Reaction := serLog;

 

Example: Let us create a strategy that will close all the open positions when started, and output a message in the log every time when a position cannot be closed:

 

const

 StrategyName = 'MyStrategy';

 StrategyVersion = '1.0';

 

var

 TraderRange: Integer;

 

procedure OnCreate;

begin

 Settings.AddInteger(@TraderRange, 'Trader Range', 0);

end;

 

procedure OnStart;

var

 i: Integer;

begin

 EventTradeNotFound.Reaction := serLog;

 EventDuplicateCloseOrder.Reaction := serLog;

for i:=Tradelist.Count-1 downto 0 do

   CloseTrade(TradeList.Get(i), TradeList.Get(i).Amount,

              TraderRange, TradeList.Get(i).Tag);

end;