generated from databricks-industry-solutions/industry-solutions-blueprints
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy path02_damd_crossed_markets.scala
196 lines (133 loc) · 7.11 KB
/
02_damd_crossed_markets.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
// Databricks notebook source
var bboDf = spark.table("main.ndlda.ndlda_bbo")
var tradesDf = spark.table("main.ndlda.ndlda_trades")
// COMMAND ----------
bboDf.printSchema
// COMMAND ----------
tradesDf.printSchema
// COMMAND ----------
display(bboDf.limit(1000))
// COMMAND ----------
display(tradesDf.limit(1000))
// COMMAND ----------
val distinctExchangesDf = bboDf.select($"exchange").distinct.sort($"exchange")
display(distinctExchangesDf)
// COMMAND ----------
val distinctQuotePairsDf = bboDf.select($"pair").distinct
display(distinctQuotePairsDf.sort($"pair"))
// COMMAND ----------
print(distinctQuotePairsDf.count)
// COMMAND ----------
val quotesCrossedDf = bboDf.filter($"bid" >= $"ask")
/* Display the exchanges with crossed quotes in their data */
display(quotesCrossedDf.select($"exchange").distinct.sort($"exchange"))
// COMMAND ----------
val quotesCrossedCountedDf = quotesCrossedDf.groupBy($"exchange", $"pair").count.sort($"exchange", $"pair")
display(quotesCrossedCountedDf)
// COMMAND ----------
import org.apache.spark.sql.functions.{ sum }
display(quotesCrossedCountedDf.agg(sum($"count")))
// COMMAND ----------
/* Please see, and search, LAST_WIN: https://spark.apache.org/docs/latest/sql-migration-guide.html */
/* Code: https://github.com/apache/spark/blob/0fa9c554fc0b3940a47c3d1c6a5a17ca9a8cee8e/sql/catalyst/src/main/scala/org/apache/spark/sql/internal/SQLConf.scala, Lines: 3505 - 3518 */
spark.conf.set("spark.sql.mapKeyDedupPolicy", "LAST_WIN")
// COMMAND ----------
import org.apache.spark.sql._
import org.apache.spark.sql.catalyst.expressions.NamedExpression
import org.apache.spark.sql.expressions.{ Window }
import org.apache.spark.sql.functions._
import org.apache.spark.sql.functions.{ abs, col, greatest, last, least, lit, regexp_replace, struct, when }
import scala.collection.mutable
var bboDf = spark.table("main.ndlda.ndlda_bbo")
val distinctExchanges = bboDf.select($"exchange").distinct.sort($"exchange").as[String].collect.toList
val pairTimeOrderedWindow = Window.partitionBy($"pair").orderBy($"exchange_timestamp".asc, $"exchange_timestamp_nanoseconds".asc)
val mapExchangeBidColumns = distinctExchanges.map(exchange => col(s"${exchange}_bid")).toSeq
val mapExchangeBidStrings = distinctExchanges.map(exchange => s"${exchange}_bid").toSeq
val mapExchangeAskColumns = distinctExchanges.map(exchange => col(s"${exchange}_ask")).toSeq
val mapExchangeAskStrings = distinctExchanges.map(exchange => s"${exchange}_ask").toSeq
// Explode out columns based on the exchange and populate the column
val explodedExchangesBBODf = distinctExchanges.foldLeft(bboDf)(
(df, c) => df
.withColumn(s"${c}_bid", when($"exchange" === c, $"bid").otherwise(null))
.withColumn(s"${c}_ask", when($"exchange" === c, $"ask").otherwise(null))
)
// Last over the window for all the exchange specific columns getting the previous value for the column if the value is null.
val lastBBODf = distinctExchanges.foldLeft(explodedExchangesBBODf)(
(df, c) => df
.withColumn(s"${c}_bid", last(s"${c}_bid", true).over(pairTimeOrderedWindow))
.withColumn(s"${c}_ask", last(s"${c}_ask", true).over(pairTimeOrderedWindow))
)
// Create a map from the non-null values/prices with the key being the exchange
def mapExchangesQuotesSide(columns: Seq[String]): Column = {
def getPairs(columnName: String): Seq[Column] = Seq(lit(columnName), when(col(columnName).isNotNull, col(columnName)).otherwise(lit(null)))
map_filter(map(columns.flatMap(getPairs): _*), (k, v) => !(v.isNull || v < 0))
}
// Return a collection of the high and low map entries
val bestWorstPrice = udf[(String, Double, String, Double), Map[String, Double]]((m: Map[String, Double]) => {
var high_name, low_name: String = null;
var high_val, low_val: Double = 0d;
m.foreach { case (k,v) => {
if (v > high_val || high_name == null || high_name.isEmpty) {
high_val = v
high_name = k
}
if (v < low_val || low_name == null || low_name.isEmpty) {
low_val = v
low_name = k
}
}}
(high_name, high_val, low_name, low_val)
})
val marketSystemQuoteValuesDf = lastBBODf
.withColumn("bid_exchanges", mapExchangesQuotesSide(mapExchangeBidStrings))
.withColumn("high_low_bids", bestWorstPrice($"bid_exchanges"))
.withColumn("best_bid_exchange", regexp_replace($"high_low_bids._1", "_bid", ""))
.withColumn("best_bid_price", $"high_low_bids._2")
.withColumn("worst_bid_exchange", regexp_replace($"high_low_bids._3", "_bid", ""))
.withColumn("worst_bid_price", $"high_low_bids._4")
.withColumn("ask_exchanges", mapExchangesQuotesSide(mapExchangeAskStrings))
.withColumn("high_low_asks", bestWorstPrice($"ask_exchanges"))
.withColumn("best_ask_exchange", regexp_replace($"high_low_asks._3", "_ask", ""))
.withColumn("best_ask_price", $"high_low_asks._4")
.withColumn("worst_ask_exchange", regexp_replace($"high_low_asks._1", "_ask", ""))
.withColumn("worst_ask_price", $"high_low_asks._2")
.withColumn("difference_bid", $"best_bid_price" - $"worst_bid_price")
.withColumn("abs_difference_bid", abs($"difference_bid"))
.withColumn("difference_ask", $"best_ask_price" - $"worst_ask_price")
.withColumn("abs_difference_ask", abs($"difference_ask"))
.withColumn("best_quote_spread", $"best_ask_price" - $"best_bid_price")
.withColumn("abs_best_quote_spread", abs($"best_quote_spread"))
.withColumn("worst_quote_spread", $"worst_ask_price" - $"worst_bid_price")
.withColumn("abs_worst_quote_spread", abs($"worst_quote_spread"))
.drop(mapExchangeBidStrings: _*)
.drop($"bid_exchanges")
.drop($"high_low_bids")
.drop(mapExchangeAskStrings: _*)
.drop($"ask_exchanges")
.drop($"high_low_asks")
// COMMAND ----------
display(marketSystemQuoteValuesDf)
// COMMAND ----------
val bboMarketsCrossedDf = marketSystemQuoteValuesDf.filter($"best_bid_price" >= $"best_ask_price")
// COMMAND ----------
display(bboMarketsCrossedDf)
// COMMAND ----------
val bboMarketsBidsCrossedCountedDf = bboMarketsCrossedDf.groupBy($"best_bid_exchange", $"pair").count.sort($"best_bid_exchange", $"pair")
display(bboMarketsBidsCrossedCountedDf)
// COMMAND ----------
val bboMarketsAsksCrossedCountedDf = bboMarketsCrossedDf.groupBy($"best_ask_exchange", $"pair").count.sort($"best_ask_exchange", $"pair")
display(bboMarketsAsksCrossedCountedDf)
// COMMAND ----------
val exchangesBboMarketsQuotesCrossedCountedDf = bboMarketsCrossedDf.groupBy($"exchange").count.sort($"exchange")
display(exchangesBboMarketsQuotesCrossedCountedDf)
// COMMAND ----------
val bboMarketsQuotesCrossedCountedDf = bboMarketsCrossedDf.groupBy($"best_bid_exchange", $"pair").count.sort($"exchange", $"pair")
display(bboMarketsQuotesCrossedCountedDf)
// COMMAND ----------
import org.apache.spark.sql.functions.mean
val bboMarketsQuotesCrossedMeanDf = bboMarketsCrossedDf.groupBy($"best_bid_exchange", $"pair").agg(mean("abs_best_quote_spread")).sort($"exchange", $"pair")
display(bboMarketsQuotesCrossedMeanDf)
// COMMAND ----------
import org.apache.spark.sql.functions.stddev
val bboMarketsQuotesCrossedStdDevDf = bboMarketsCrossedDf.groupBy($"best_bid_exchange", $"pair").agg(stddev("abs_best_quote_spread")).sort($"exchange", $"pair")
display(bboMarketsQuotesCrossedStdDevDf)