CloseTrade Function

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 (will be used in the future releases 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';

 

var

History: TCandleHistory;

Account: TAccount;

Amount: Double;

MyTrade: TTrade;

TraderRangeInteger;

 

procedure OnCreate;

begin

AddCandleHistorySetting(@History, 'Candle History', '', CI_1_Hour, 100);

History.OnNewRateEvent := @OnNewRate;

AddAccountSetting(@Account, 'Account', '');

AddFloatSetting(@Amount, 'Amount(Lots)', 5);

AddIntegerSetting(@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

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: TSystemEvent

The script tries to modify a triggered order

 

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

 

System event actions

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';

 

var

TraderRange: Integer;

 

procedure OnCreate;

begin

AddIntegerSetting(@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;