3.3Trading

The framework provides two types of trading requests:

Standard requests for an individual order: opening, closing, or modifying it

Aggregated actions on a market or on the whole account. For example, you can issue a request of type FXB.OrderTypes.FLATTEN. This closes all orders and trades on the account, rather than your code having to iterate though the orders in Framework.Orders and issue its own separate requests to close each one.

In addition, you can either send a single request or an array containing a batch of requests. If you issue a batch, then you get a response either after all requests succeed or when the first one fails. (You don't get a response from a batch listing each individual request and whether it succeeded or failed.)

You issue trading requests using Framework.SendOrder(). This has two compulsory parameters and one optional parameter:

Parameter

Description

request

Definition of an individual request, or an array of requests

callback

Callback function which asynchronously receives success of the request or (first) failure

settings

Optional settings controlling how the request is processed. For example, the default is that the framework will automatically display an error message if a trading request fails, but you can use the settings to say that the framework should not display its own error dialog, and that you will do this yourself.

For example:

Framework.SendOrder({

// Buy 10K (0.10 lots) EUR/USD

tradingAction: FXB.OrderTypes.BUY,

instrumentId: "EUR/USD",

volume: 10000

}, function (MsgResult) {

// Asynchronously called with the result of the request

});

Or, using a batch of multiple requests, with optional settings specifying forced confirmation by the user before the batch is started:

Framework.SendOrder([

{

// Close all EUR/USD orders and trades

tradingAction: FXB.OrderTypes.CLOSEPOSITION,

instrumentId: "EUR/USD"

},{

// Buy 5K GBP/USD, specifying the volume in lots

tradingAction: FXB.OrderTypes.BUY,

instrumentId: "GBP/USD",

volume: {lots: 0.05}

}

]}, function (MsgResult) {

// Asynchronously called when the whole batch has succeeded or on the first failure

}, {

// Force confirmation by the user before the batch is processed

confirm: true

});

The asynchronous MsgResult from SendOrder() contains a result object which has isOkay and isError properties. If the request was unsuccessful, result will contain a non-zero code property specifying the error. For example:

Framework.SendOrder( … , function (MsgResult) {

if (MsgResult.result.isOkay) {

// Succeeded

} else {

// Failed

// For example, can get a textual description of the error using:

var errText = Framework.Translation.TranslateError(Msg.result);

}

});