Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

v1.6.53: add analytics for SL/TP #325

Merged
merged 14 commits into from
May 1, 2024

Conversation

moo-onthelawn
Copy link
Contributor

@moo-onthelawn moo-onthelawn commented Apr 30, 2024

Summary of Logging Added

  1. User clicks submit button on SL/TP
    • We log a TriggerOrderClick event with
      • marketId
      • positionSize: Double
      • [takeProfit/stopLoss]OrderAction: 'CREATE' | 'CANCEL' | 'REPLACE' | null (if no action)
      • [takeProfit/stopLoss]Order[Cancel/Place]ClientId: Int
  2. Trade[Cancel/Place]Order logged with existing params and fromSlTpDialog: true
  3. If successful:
    • Trade[Cancel/Place]OrderConfirmed logged with existing params and fromSlTpDialog: true
    • Trade[Cancel/Place]OrderSubmissionConfirmed logged with existing params and fromSlTpDialog: true
  4. If unsuccessful, Trade[Cancel/Place]OrderFailed logged with existing params and fromSlTpDialog: true

Testing

Built locally on web and logged output.

2x create 2x cancel 2x replace 1x replace, 1x no action
Screenshot 2024-04-30 at 5 07 01 PM Screenshot 2024-04-30 at 5 06 38 PM Screenshot 2024-04-30 at 5 07 47 PM Screenshot 2024-04-30 at 5 08 22 PM

Code

  1. Add more thorough logging for SL/TP
  2. Distinguish trade log events from SL/TP log events:
    • Action: Click "place orders" on SL/TP dialog
      New behavior: single TriggerOrderClick fired
      Old behavior: TradePlaceOrder and TradeCancelOrder fired
    • Action: Trade submit (for logging times)
      New behavior: TradePlaceOrder/TradeCancelOrder with fromSlTp: true
    • Action: Trade successfully submitted (for logging times)
      New behavior: TradePlaceOrderSubmissionConfirmed /TradeCancelOrderSubmissionConfirmed with fromSlTp: true
    • Action: Trade failed to submit (for logging times)
      New behavior: TradePlaceOrderSubmissionFailed /TradeCancelOrderSubmissionFailed with fromSlTp: true
    • Action: parsing place/cancel order records
      New behavior: TradeCancelOrderConfirmed /TradePlaceOrderConfirmed with fromSlTp: true
  3. Create a new triggerOrdersAnalyticsPayload
    • Only used in the TriggerOrderClick event; other events continue to use placeOrderAnalyticsPayload/cancelOrderAnalyticsPayload
    • Tracks marketId, (total)PositionSize, stopLossOrderAction/takeProfitOrderAction (REPLACE / CANCEL / CREATE), stopLossOrder[Cancel/Place]ClientId, takeProfitOrder[Cancel/Place]ClientId
  4. Update existing payloads with more data for analytics
    • PlaceOrderRecord/CancelOrderRecord: added fromSlTp to pass to Trade[Cancel/Place]OrderConfirmed
    • HumanReadableTriggerOrdersPayload: add marketId, positionSize for triggerOrdersAnalyticsPayload
    • HumanReadableCancelOrderPayload: add type

Copy link

linear bot commented Apr 30, 2024

@moo-onthelawn moo-onthelawn marked this pull request as draft April 30, 2024 13:53
@moo-onthelawn moo-onthelawn force-pushed the mulan/ct-783-new-analytics-clickevent-for-sltp branch from 4fa7d8e to 72873a3 Compare April 30, 2024 15:56
@moo-onthelawn moo-onthelawn force-pushed the mulan/ct-783-new-analytics-clickevent-for-sltp branch 5 times, most recently from c843288 to a47d64e Compare April 30, 2024 17:30
@moo-onthelawn moo-onthelawn force-pushed the mulan/ct-783-new-analytics-clickevent-for-sltp branch from a47d64e to 2d5c692 Compare April 30, 2024 18:05
@moo-onthelawn moo-onthelawn marked this pull request as ready for review April 30, 2024 18:05
@moo-onthelawn moo-onthelawn force-pushed the mulan/ct-783-new-analytics-clickevent-for-sltp branch from cf8e211 to 6e5458c Compare April 30, 2024 18:07
@moo-onthelawn moo-onthelawn changed the title draft v1.6.51: add analytics for SL/TP Apr 30, 2024
@moo-onthelawn moo-onthelawn changed the title v1.6.51: add analytics for SL/TP v1.6.53: add analytics for SL/TP Apr 30, 2024
@@ -1880,10 +1878,15 @@ open class StateManagerAdaptor(
val cancelOrderPayloads = mutableListOf<HumanReadableCancelOrderPayload>()
val triggerOrders = stateMachine.state?.input?.triggerOrders

val marketId = triggerOrders?.marketId ?: throw Exception("marketId is null")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

kotlin tip: we can instead enforce triggerOrders is non-null by doing:
val triggerOrders = requireNotNull(stateMachine.state?.input?.triggerOrders) { "triggerOrders input was null" }

then we don't need to do null checks on it later.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

part of me wants to write a codemod to go through the whole repo to replace all of the throwExceptions

var takeProfitOrderAction: TriggerOrderAction? = null

placeOrderPayloads.forEach { placePayload ->
val orderType = OrderType.invoke(placePayload.type)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

.invoke() can be called like a constructor: OrderType(placePayload.type)

"positionSize" to payload.positionSize,
"stopLossOrderAction" to stopLossOrderAction?.rawValue,
"takeProfitOrderAction" to takeProfitOrderAction?.rawValue,
) as IMap<String, Any>?
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why is this cast necessary?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah good point - should be fine if I just update the return type to be IMap<String, Any?>

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh actually I see why we have all these casts in the file - it's because ParsingHelper.merge expects and returns Map<String, Any>? instead of Map<String, Any?>. Given how widely this is used, it seems like a pretty beefy change to fix, so I might just keep it like this for now 😢

Comment on lines 1157 to 1158
val submitTimeMs = Clock.System.now().toEpochMilliseconds().toDouble()
val uiDelayTimeMs = submitTimeMs - uiClickTimeMs
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I missed this before, why are we converting these timestamps to Double? Shouldn't we be using Long?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm unsure - this is just the existing pattern; cc @aforaleka who may have more context (I think you added these values for analytics?)

Copy link

@yang-dydx yang-dydx left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i only looked at the screenshots and the output looks good from analytics perspective

@moo-onthelawn moo-onthelawn merged commit b5703c7 into main May 1, 2024
3 checks passed
@moo-onthelawn moo-onthelawn deleted the mulan/ct-783-new-analytics-clickevent-for-sltp branch May 1, 2024 14:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

3 participants