# Implementing Cover Trades

The Cover Trades feature, available from version 2.32 of FX Sales, provides an alternative means of trading to the standard RFS ticket. In a time-critical sales opportunity, the sales dealer can agree a price with a client and use Cover Trades to execute a trade immediately at a zero-margin rate. After execution, the dealer amends the margin on the trade so that the all-in rate matches the price quoted to the client.

This page describes how to implement Cover Trades in your integration adapter.

## Feature overview

When an FX Sales user selects a TOBO user for whom Cover Trades is enabled, the Pending tile set is shown in the UI:

![CoverTrades1](../images/CoverTrades1.png)

Cover Trade tiles display executable, zero-margin rates. You can customise how many mouse clicks are required to execute a trade (single-click, double-click, or single-click with confirmation dialog). See [Implement execution of Cover Trades](#implement-execution-of-cover-trades).

![CoverTrades2](../images/CoverTrades2.png)

Following execution of a cover trade, the trade is available in the blotter for immediate amendment. The sales dealer amends the margin so that the all-in rate matches the price agreed with the client:

![CoverTrades3](../images/CoverTrades3.png)

## Implementation requirements

To implement FX Sales Cover Trades, you require the following:

* FX Sales 2.32
* Integration adapters implementing the following FX Integration API (3.68.0+) features:
  * [User Config Service](../fx-integration-api/fxapi-user-config.md)
  * [ESP Trade Model](../fx-integration-api/fxapi-trade-model-esp.md)
  * [Amend Trade Model](../fx-integration-api/fxapi-trade-model-amend.md)

The implementations of the User Config Service, ESP Trade Model, and the Amend Trade Model can be in the same or separate adapters.

## Implementation overview

Implementation requires three steps:

1. User configuration: update your [User Config Service](../fx-integration-api/fxapi-user-config.md) implementation to enable the Cover Trades feature for TOBO users.
2. Pricing: update your trading adapter’s ESP pricing subscription handler to supply zero-margin rates for Cover Trade pricing subscriptions.
3. Trading: update your trading adapter’s ESP trade model Submit handler

## Implement user configuration for Cover Trades

In your User Config Adapter’s implementation of `CachedObjectProvider<TOBOConfig, SalesTOBOConfigSubjectInfo>.onRequest`, perform the following steps:

1. Refer to your backend customer profile to determine if the TOBO user represented by the [`SalesTOBOConfigSubjectInfo`,opts="nofollow"](https://docs.caplin.com/developer/api/fxintegration/3.68/com/caplin/motif/fx/config/SalesTOBOConfigSubjectInfo.html) parameter should have Cover Trades enabled.
2. If Cover Trades should be enabled for the TOBO user, register an enabled [`CoverTrade`,opts="nofollow"](https://docs.caplin.com/developer/api/fxintegration/3.68/com/caplin/motif/fx/config/definitions/CoverTrade.html) object with the [`Features`,opts="nofollow"](https://docs.caplin.com/developer/api/fxintegration/3.68/com/caplin/motif/fx/config/definitions/Features.html) object:

```java
public class ToboConfigProvider implements
        CachedObjectProvider<TOBOConfig, SalesTOBOConfigSubjectInfo> {

    @Override
    public void onRequest(
            final SalesTOBOConfigSubjectInfo salesToboConfigSubjectInfo,
            final SubjectConsumer<TOBOConfig> objectPublisherConsumer) {
        ...
        Features features = new Features();
        features.coverTrade(new CoverTrade().enabled(true));
        ...
        ToboConfig toboConfig = new ToboConfig();
        toboConfig.setFeatures(features);
        objectPublisherConsumer.accept(toboConfig);
    }

    ...
}
```

## Implement pricing for Cover Trades

In your trading adapter, use the [`RateSubjectInfo.isCoverTrade()`,opts="nofollow"](https://docs.caplin.com/developer/api/fxintegration/3.68/com/caplin/motif/fx/rates/RateSubjectInfo.html#isCoverTrade()) method to identify the margin requirements of a pricing subscription. 

When `RateSubjectInfo.isCoverTrade()` returns true, your trading adapter should provide rates with no TOBO user margin applied:

```plantuml
@startuml
start
:Adapter receives pricing subscription:\n /FXSALES/<CCyPair>/<Tenor>/<Dealt>/<Amount>/<TOBOUser>;
if (""RateSubjectInfo.isCoverTrade()"") then (true)
  :Adapter sends rates with __no margin__ applied;
else (false)
  :Adapter sends rates with __TOBO user margin__ applied;
endif
stop
@enduml
```

## Implement execution of Cover Trades

Sales dealers execute a cover trade by clicking a rate in a Cover Trade tile. To configure how many mouse clicks are required to execute a trade, set the AppConfig option `FX_TILE_ESP_TRADING_CLICK_TYPE` to one of the strings below:

* `ONE_CLICK`: single click
* `DOUBLE_CLICK` : double click
* `TWO_CLICK`: single click with a confirmation dialog (OK/Cancel)

When your trading adapter receives an ESP [Submit](../fx-integration-api/fxapi-trade-model-esp.md) message from a Cover Trade tile, the message includes the field `IsCoverTrade`. This field is retrievable in your trading adapter using the [SubmitTradeEvent.getIsCoverTrade(),opts="nofollow"](https://docs.caplin.com/developer/api/fxintegration/3.68/com/caplin/generated/motif/fx/trading/esp/events/client/SubmitTradeEvent.html) method.

Handle cover trades as you would any other ESP trade, with one exception: add the `IsAmendable` field to the execution blotter record. The TradeConfirmation event for a cover trade does not require any extra fields.

```plantuml
@startuml
start
:Adapter receives ESP Submit message;
:Adapter submits trade to the back end;
if (Submission successful?) then (yes)
  if (""SubmitTradeEvent.getIsCoverTrade()"") then (true)
    :Adapter adds a record\nto the Execution Blotter,\nincluding an IsAmendable field;
  else (false)
    :Adapter adds a record\nto the Execution Blotter;
  endif
  :Adapter raises\na TradeConfirmation event;
else (no)
  if (Rejection or error?) then (error)
    :Adapter raises\nan ErrorEvent;
  else (rejection)
    :Adapter raises\na RejectEvent;
  endif
endif
stop
@enduml
```

This is the minimum that is required to implement Cover Trades. You may wish to extend this implementation to make it easier to identify unamended cover trades in the execution blotter. For example, in addition to adding the `IsAmendable` field to the Execution Blotter record, you could also set a custom status, such as "Pending".
