From 425a6e3ddec32ac31dfaf487199c53892bc3896e Mon Sep 17 00:00:00 2001 From: Madalina Raicu Date: Tue, 5 Mar 2024 08:58:27 +0000 Subject: [PATCH] fix: add reference --- .../components/deal-ticket/deal-ticket.tsx | 7 +- libs/deal-ticket/src/hooks/use-form-values.ts | 8 +- .../utils/map-form-values-to-submission.ts | 84 +++++++++++++------ 3 files changed, 67 insertions(+), 32 deletions(-) diff --git a/libs/deal-ticket/src/components/deal-ticket/deal-ticket.tsx b/libs/deal-ticket/src/components/deal-ticket/deal-ticket.tsx index 32d021e56..3971deeab 100644 --- a/libs/deal-ticket/src/components/deal-ticket/deal-ticket.tsx +++ b/libs/deal-ticket/src/components/deal-ticket/deal-ticket.tsx @@ -81,6 +81,7 @@ import { KeyValue } from './key-value'; import { DocsLinks } from '@vegaprotocol/environment'; import { useT } from '../../use-t'; import { DealTicketPriceTakeProfitStopLoss } from './deal-ticket-price-tp-sl'; +import uniqueId from 'lodash/uniqueId'; export const REDUCE_ONLY_TOOLTIP = '"Reduce only" will ensure that this order will not increase the size of an open position. When the order is matched, it will only trade enough volume to bring your open volume towards 0 but never change the direction of your position. If applied to a limit order that is not instantly filled, the order will be stopped.'; @@ -390,9 +391,11 @@ export const DealTicket = ({ return; } if (formValues.tpSl) { + const reference = `${pubKey}-${now}-${uniqueId()}`; const batchMarketInstructions = mapFormValuesToTakeProfitAndStopLoss( formValues, - market + market, + reference ); submit({ batchMarketInstructions, @@ -407,7 +410,7 @@ export const DealTicket = ({ submit({ orderSubmission }); lastSubmitTime.current = now; }, - [market, submit] + [market, pubKey, submit] ); useController({ name: 'type', diff --git a/libs/deal-ticket/src/hooks/use-form-values.ts b/libs/deal-ticket/src/hooks/use-form-values.ts index 93952d599..aeb7c09be 100644 --- a/libs/deal-ticket/src/hooks/use-form-values.ts +++ b/libs/deal-ticket/src/hooks/use-form-values.ts @@ -31,13 +31,13 @@ export interface StopOrderFormValues { oco?: boolean; - ocoTriggerType: 'price' | 'trailingPercentOffset'; + ocoTriggerType?: 'price' | 'trailingPercentOffset'; ocoTriggerPrice?: string; ocoTriggerTrailingPercentOffset?: string; - ocoType: OrderType; - ocoSize: string; - ocoTimeInForce: OrderTimeInForce; + ocoType?: OrderType; + ocoSize?: string; + ocoTimeInForce?: OrderTimeInForce; ocoPrice?: string; } diff --git a/libs/deal-ticket/src/utils/map-form-values-to-submission.ts b/libs/deal-ticket/src/utils/map-form-values-to-submission.ts index 0d37aeae2..bf17bbb17 100644 --- a/libs/deal-ticket/src/utils/map-form-values-to-submission.ts +++ b/libs/deal-ticket/src/utils/map-form-values-to-submission.ts @@ -16,8 +16,10 @@ export const mapFormValuesToOrderSubmission = ( order: OrderFormValues, marketId: string, decimalPlaces: number, - positionDecimalPlaces: number + positionDecimalPlaces: number, + reference?: string ): OrderSubmission => ({ + reference, marketId: marketId, type: order.type, side: order.side, @@ -82,7 +84,8 @@ export const mapFormValuesToStopOrdersSubmission = ( data: StopOrderFormValues, marketId: string, decimalPlaces: number, - positionDecimalPlaces: number + positionDecimalPlaces: number, + reference?: string ): StopOrdersSubmission => { const submission: StopOrdersSubmission = {}; const stopOrderSetup: StopOrderSetup = { @@ -97,7 +100,8 @@ export const mapFormValuesToStopOrdersSubmission = ( }, marketId, decimalPlaces, - positionDecimalPlaces + positionDecimalPlaces, + reference ), }; setTrigger( @@ -108,7 +112,13 @@ export const mapFormValuesToStopOrdersSubmission = ( decimalPlaces ); let oppositeStopOrderSetup: StopOrderSetup | undefined = undefined; - if (data.oco) { + if ( + data.oco && + data.ocoType && + data.ocoPrice && + data.ocoSize && + data.ocoTimeInForce + ) { oppositeStopOrderSetup = { orderSubmission: mapFormValuesToOrderSubmission( { @@ -163,39 +173,61 @@ export const mapFormValuesToStopOrdersSubmission = ( export const mapFormValuesToTakeProfitAndStopLoss = ( formValues: OrderFormValues, - market: MarketFieldsFragment + market: MarketFieldsFragment, + reference: string ) => { - const ocoType = - formValues.type === Schema.OrderType.TYPE_LIMIT - ? Schema.OrderType.TYPE_MARKET - : Schema.OrderType.TYPE_LIMIT; const orderSubmission = mapFormValuesToOrderSubmission( formValues, market.id, market.decimalPlaces, - market.positionDecimalPlaces + market.positionDecimalPlaces, + reference ); + const oppositeSide = + formValues.side === Schema.Side.SIDE_BUY + ? Schema.Side.SIDE_SELL + : Schema.Side.SIDE_BUY; + // For direction it needs to be implied + // If position is LONG (BUY) + // TP is SHORT and trigger is RISES ABOVE + // If position is SHORT + // TP is LONG and trigger is FALLS BELOW + const takeProfitTriggerDirection = + formValues.side === Schema.Side.SIDE_BUY + ? Schema.StopOrderTriggerDirection.TRIGGER_DIRECTION_RISES_ABOVE + : Schema.StopOrderTriggerDirection.TRIGGER_DIRECTION_FALLS_BELOW; + // For direction it needs to be implied + // If position is LONG (BUY) + // SL is SHORT and trigger is FALLS BELOW + // If position is SHORT + // SL is LONG and trigger is RISES ABOVE + const stopLossTriggerDirection = + formValues.side === Schema.Side.SIDE_BUY + ? Schema.StopOrderTriggerDirection.TRIGGER_DIRECTION_FALLS_BELOW + : Schema.StopOrderTriggerDirection.TRIGGER_DIRECTION_RISES_ABOVE; + const takeProfitStopOrderSubmission = formValues.takeProfit && mapFormValuesToStopOrdersSubmission( { ...formValues, - ocoPrice: formValues.takeProfit, - triggerDirection: - formValues.side === Schema.Side.SIDE_SELL - ? Schema.StopOrderTriggerDirection.TRIGGER_DIRECTION_FALLS_BELOW - : Schema.StopOrderTriggerDirection.TRIGGER_DIRECTION_RISES_ABOVE, + price: formValues.takeProfit, + triggerDirection: takeProfitTriggerDirection, triggerType: 'price', - ocoTriggerType: 'price', + triggerPrice: formValues.takeProfit, + side: oppositeSide, expire: false, - ocoType, + timeInForce: Schema.OrderTimeInForce.TIME_IN_FORCE_FOK, + ocoTriggerType: 'price', + ocoType: Schema.OrderType.TYPE_MARKET, ocoSize: formValues.size, ocoTimeInForce: Schema.OrderTimeInForce.TIME_IN_FORCE_FOK, }, market.id, market.decimalPlaces, - market.positionDecimalPlaces + market.positionDecimalPlaces, + reference ); const stopLossStopOrderSubmission = @@ -203,21 +235,21 @@ export const mapFormValuesToTakeProfitAndStopLoss = ( mapFormValuesToStopOrdersSubmission( { ...formValues, - ocoPrice: formValues.stopLoss, - triggerDirection: - formValues.side === Schema.Side.SIDE_BUY - ? Schema.StopOrderTriggerDirection.TRIGGER_DIRECTION_FALLS_BELOW - : Schema.StopOrderTriggerDirection.TRIGGER_DIRECTION_RISES_ABOVE, + triggerPrice: formValues.stopLoss, + price: formValues.stopLoss, + triggerDirection: stopLossTriggerDirection, triggerType: 'price', - ocoTriggerType: 'price', + side: oppositeSide, expire: false, - ocoType, + ocoTriggerType: 'price', + ocoType: Schema.OrderType.TYPE_MARKET, ocoSize: formValues.size, ocoTimeInForce: Schema.OrderTimeInForce.TIME_IN_FORCE_FOK, }, market.id, market.decimalPlaces, - market.positionDecimalPlaces + market.positionDecimalPlaces, + reference ); const stopOrdersSubmission = []; if (takeProfitStopOrderSubmission) {