generated from user_client2024/58
System save at 15/10/2024 17:08 by user_client2024
This commit is contained in:
parent
52c1bfb416
commit
9e67cf6d7a
@ -2,7 +2,7 @@
|
|||||||
"cells": [
|
"cells": [
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 21,
|
"execution_count": 10,
|
||||||
"id": "e706cfb0-2234-4c4c-95d8-d1968f656aa0",
|
"id": "e706cfb0-2234-4c4c-95d8-d1968f656aa0",
|
||||||
"metadata": {
|
"metadata": {
|
||||||
"tags": []
|
"tags": []
|
||||||
@ -30,7 +30,7 @@
|
|||||||
" o.order_price,\n",
|
" o.order_price,\n",
|
||||||
" o.side AS order_side\n",
|
" o.side AS order_side\n",
|
||||||
" FROM \n",
|
" FROM \n",
|
||||||
" {trade_data_1b} t\n",
|
" {trade_10m_v3} t\n",
|
||||||
" LEFT JOIN \n",
|
" LEFT JOIN \n",
|
||||||
" order_10m o ON o.date_time BETWEEN t.date_time - INTERVAL '{spoofing_time_window_s}' SECOND \n",
|
" order_10m o ON o.date_time BETWEEN t.date_time - INTERVAL '{spoofing_time_window_s}' SECOND \n",
|
||||||
" AND t.date_time\n",
|
" AND t.date_time\n",
|
||||||
@ -76,7 +76,7 @@
|
|||||||
" t.trader_id,\n",
|
" t.trader_id,\n",
|
||||||
" t.trade_id,\n",
|
" t.trade_id,\n",
|
||||||
" SUM(t.trade_volume) AS total_trade_volume\n",
|
" SUM(t.trade_volume) AS total_trade_volume\n",
|
||||||
" FROM {trade_data_1b} t\n",
|
" FROM {trade_10m_v3} t\n",
|
||||||
" WHERE \n",
|
" WHERE \n",
|
||||||
" t.date_time BETWEEN t.date_time - INTERVAL '{trade_time_window_s}' SECOND\n",
|
" t.date_time BETWEEN t.date_time - INTERVAL '{trade_time_window_s}' SECOND\n",
|
||||||
" AND t.date_time\n",
|
" AND t.date_time\n",
|
||||||
@ -107,11 +107,11 @@
|
|||||||
" net_order_volume_all_cte a ON n.trade_id = a.trade_id\n",
|
" net_order_volume_all_cte a ON n.trade_id = a.trade_id\n",
|
||||||
"WHERE \n",
|
"WHERE \n",
|
||||||
" n.net_order_volume > 0 -- Only consider positive net order volumes (potential spoofing);\n",
|
" n.net_order_volume > 0 -- Only consider positive net order volumes (potential spoofing);\n",
|
||||||
" limit 1000\n",
|
" limit 100\n",
|
||||||
"\"\"\"\n",
|
"\"\"\"\n",
|
||||||
"\n",
|
"\n",
|
||||||
"class Scenario:\n",
|
"class Scenario:\n",
|
||||||
" seq = SQLQueryInterface(schema=\"internal\")\n",
|
" seq = SQLQueryInterface(schema=\"trade_schema\")\n",
|
||||||
"\n",
|
"\n",
|
||||||
" def logic(self, **params):\n",
|
" def logic(self, **params):\n",
|
||||||
" spoofing_time_window = params.get('spoofing_time_window', 300000) # default to 300,000 ms (5 minutes)\n",
|
" spoofing_time_window = params.get('spoofing_time_window', 300000) # default to 300,000 ms (5 minutes)\n",
|
||||||
@ -130,7 +130,7 @@
|
|||||||
"\n",
|
"\n",
|
||||||
" # Execute the query with the parameters passed from `params`\n",
|
" # Execute the query with the parameters passed from `params`\n",
|
||||||
" row_list = self.seq.execute_raw(query.format(\n",
|
" row_list = self.seq.execute_raw(query.format(\n",
|
||||||
" trade_data_1b=\"trade_10m_v3\", # Replace with actual table name\n",
|
" trade_10m_v3=\"trade_10m_v3\", # Replace with actual table name\n",
|
||||||
" spoofing_time_window_s=spoofing_time_window_s,\n",
|
" spoofing_time_window_s=spoofing_time_window_s,\n",
|
||||||
" trade_time_window_s=trade_time_window_s,\n",
|
" trade_time_window_s=trade_time_window_s,\n",
|
||||||
" spoofing_side=spoofing_side\n",
|
" spoofing_side=spoofing_side\n",
|
||||||
@ -156,7 +156,7 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 22,
|
"execution_count": null,
|
||||||
"id": "b5c4307f-bc35-47e2-b680-fd1df2168d6c",
|
"id": "b5c4307f-bc35-47e2-b680-fd1df2168d6c",
|
||||||
"metadata": {
|
"metadata": {
|
||||||
"tags": []
|
"tags": []
|
||||||
@ -166,242 +166,18 @@
|
|||||||
"name": "stdout",
|
"name": "stdout",
|
||||||
"output_type": "stream",
|
"output_type": "stream",
|
||||||
"text": [
|
"text": [
|
||||||
"Query start time : 2024-10-14 07:40:43.846637\n"
|
"Query start time: 2024-10-15 08:26:06.758480\n"
|
||||||
]
|
]
|
||||||
},
|
|
||||||
{
|
|
||||||
"data": {
|
|
||||||
"text/html": [
|
|
||||||
"<div>\n",
|
|
||||||
"<style scoped>\n",
|
|
||||||
" .dataframe tbody tr th:only-of-type {\n",
|
|
||||||
" vertical-align: middle;\n",
|
|
||||||
" }\n",
|
|
||||||
"\n",
|
|
||||||
" .dataframe tbody tr th {\n",
|
|
||||||
" vertical-align: top;\n",
|
|
||||||
" }\n",
|
|
||||||
"\n",
|
|
||||||
" .dataframe thead th {\n",
|
|
||||||
" text-align: right;\n",
|
|
||||||
" }\n",
|
|
||||||
"</style>\n",
|
|
||||||
"<table border=\"1\" class=\"dataframe\">\n",
|
|
||||||
" <thead>\n",
|
|
||||||
" <tr style=\"text-align: right;\">\n",
|
|
||||||
" <th></th>\n",
|
|
||||||
" <th>focal_ID</th>\n",
|
|
||||||
" <th>trade_time_window</th>\n",
|
|
||||||
" <th>net_volume</th>\n",
|
|
||||||
" <th>order_count</th>\n",
|
|
||||||
" <th>total_trade_volume</th>\n",
|
|
||||||
" <th>order_trade_ratio</th>\n",
|
|
||||||
" <th>volume_percentage</th>\n",
|
|
||||||
" <th>Segment</th>\n",
|
|
||||||
" <th>SAR_FLAG</th>\n",
|
|
||||||
" <th>Risk</th>\n",
|
|
||||||
" </tr>\n",
|
|
||||||
" </thead>\n",
|
|
||||||
" <tbody>\n",
|
|
||||||
" <tr>\n",
|
|
||||||
" <th>0</th>\n",
|
|
||||||
" <td>3097728207</td>\n",
|
|
||||||
" <td>2024-01-01 00:03:00</td>\n",
|
|
||||||
" <td>-92.0</td>\n",
|
|
||||||
" <td>1</td>\n",
|
|
||||||
" <td>92</td>\n",
|
|
||||||
" <td>-1.0</td>\n",
|
|
||||||
" <td>0.0</td>\n",
|
|
||||||
" <td>Default</td>\n",
|
|
||||||
" <td>N</td>\n",
|
|
||||||
" <td>Low Risk</td>\n",
|
|
||||||
" </tr>\n",
|
|
||||||
" <tr>\n",
|
|
||||||
" <th>1</th>\n",
|
|
||||||
" <td>3228645322</td>\n",
|
|
||||||
" <td>2024-01-01 00:06:00</td>\n",
|
|
||||||
" <td>-689.0</td>\n",
|
|
||||||
" <td>1</td>\n",
|
|
||||||
" <td>689</td>\n",
|
|
||||||
" <td>-1.0</td>\n",
|
|
||||||
" <td>0.0</td>\n",
|
|
||||||
" <td>Default</td>\n",
|
|
||||||
" <td>N</td>\n",
|
|
||||||
" <td>Low Risk</td>\n",
|
|
||||||
" </tr>\n",
|
|
||||||
" <tr>\n",
|
|
||||||
" <th>2</th>\n",
|
|
||||||
" <td>2701872727</td>\n",
|
|
||||||
" <td>2024-01-01 00:09:00</td>\n",
|
|
||||||
" <td>-42.0</td>\n",
|
|
||||||
" <td>1</td>\n",
|
|
||||||
" <td>42</td>\n",
|
|
||||||
" <td>-1.0</td>\n",
|
|
||||||
" <td>0.0</td>\n",
|
|
||||||
" <td>Default</td>\n",
|
|
||||||
" <td>N</td>\n",
|
|
||||||
" <td>Low Risk</td>\n",
|
|
||||||
" </tr>\n",
|
|
||||||
" <tr>\n",
|
|
||||||
" <th>3</th>\n",
|
|
||||||
" <td>1659056655</td>\n",
|
|
||||||
" <td>2024-01-01 00:11:00</td>\n",
|
|
||||||
" <td>-167.0</td>\n",
|
|
||||||
" <td>1</td>\n",
|
|
||||||
" <td>167</td>\n",
|
|
||||||
" <td>-1.0</td>\n",
|
|
||||||
" <td>0.0</td>\n",
|
|
||||||
" <td>Default</td>\n",
|
|
||||||
" <td>N</td>\n",
|
|
||||||
" <td>Low Risk</td>\n",
|
|
||||||
" </tr>\n",
|
|
||||||
" <tr>\n",
|
|
||||||
" <th>4</th>\n",
|
|
||||||
" <td>1661288887</td>\n",
|
|
||||||
" <td>2024-01-01 00:13:00</td>\n",
|
|
||||||
" <td>-756.0</td>\n",
|
|
||||||
" <td>1</td>\n",
|
|
||||||
" <td>756</td>\n",
|
|
||||||
" <td>-1.0</td>\n",
|
|
||||||
" <td>0.0</td>\n",
|
|
||||||
" <td>Default</td>\n",
|
|
||||||
" <td>N</td>\n",
|
|
||||||
" <td>Low Risk</td>\n",
|
|
||||||
" </tr>\n",
|
|
||||||
" <tr>\n",
|
|
||||||
" <th>...</th>\n",
|
|
||||||
" <td>...</td>\n",
|
|
||||||
" <td>...</td>\n",
|
|
||||||
" <td>...</td>\n",
|
|
||||||
" <td>...</td>\n",
|
|
||||||
" <td>...</td>\n",
|
|
||||||
" <td>...</td>\n",
|
|
||||||
" <td>...</td>\n",
|
|
||||||
" <td>...</td>\n",
|
|
||||||
" <td>...</td>\n",
|
|
||||||
" <td>...</td>\n",
|
|
||||||
" </tr>\n",
|
|
||||||
" <tr>\n",
|
|
||||||
" <th>95</th>\n",
|
|
||||||
" <td>1945772682</td>\n",
|
|
||||||
" <td>2024-01-01 00:43:00</td>\n",
|
|
||||||
" <td>-854.0</td>\n",
|
|
||||||
" <td>1</td>\n",
|
|
||||||
" <td>854</td>\n",
|
|
||||||
" <td>-1.0</td>\n",
|
|
||||||
" <td>0.0</td>\n",
|
|
||||||
" <td>Default</td>\n",
|
|
||||||
" <td>N</td>\n",
|
|
||||||
" <td>Low Risk</td>\n",
|
|
||||||
" </tr>\n",
|
|
||||||
" <tr>\n",
|
|
||||||
" <th>96</th>\n",
|
|
||||||
" <td>2137478041</td>\n",
|
|
||||||
" <td>2024-01-01 00:43:00</td>\n",
|
|
||||||
" <td>-926.0</td>\n",
|
|
||||||
" <td>1</td>\n",
|
|
||||||
" <td>926</td>\n",
|
|
||||||
" <td>-1.0</td>\n",
|
|
||||||
" <td>0.0</td>\n",
|
|
||||||
" <td>Default</td>\n",
|
|
||||||
" <td>N</td>\n",
|
|
||||||
" <td>Low Risk</td>\n",
|
|
||||||
" </tr>\n",
|
|
||||||
" <tr>\n",
|
|
||||||
" <th>97</th>\n",
|
|
||||||
" <td>7138329164</td>\n",
|
|
||||||
" <td>2024-01-01 00:43:00</td>\n",
|
|
||||||
" <td>-433.0</td>\n",
|
|
||||||
" <td>1</td>\n",
|
|
||||||
" <td>433</td>\n",
|
|
||||||
" <td>-1.0</td>\n",
|
|
||||||
" <td>0.0</td>\n",
|
|
||||||
" <td>Default</td>\n",
|
|
||||||
" <td>N</td>\n",
|
|
||||||
" <td>Low Risk</td>\n",
|
|
||||||
" </tr>\n",
|
|
||||||
" <tr>\n",
|
|
||||||
" <th>98</th>\n",
|
|
||||||
" <td>1867007441</td>\n",
|
|
||||||
" <td>2024-01-01 00:43:00</td>\n",
|
|
||||||
" <td>-626.0</td>\n",
|
|
||||||
" <td>1</td>\n",
|
|
||||||
" <td>626</td>\n",
|
|
||||||
" <td>-1.0</td>\n",
|
|
||||||
" <td>0.0</td>\n",
|
|
||||||
" <td>Default</td>\n",
|
|
||||||
" <td>N</td>\n",
|
|
||||||
" <td>Low Risk</td>\n",
|
|
||||||
" </tr>\n",
|
|
||||||
" <tr>\n",
|
|
||||||
" <th>99</th>\n",
|
|
||||||
" <td>2347906349</td>\n",
|
|
||||||
" <td>2024-01-01 00:43:00</td>\n",
|
|
||||||
" <td>-69.0</td>\n",
|
|
||||||
" <td>1</td>\n",
|
|
||||||
" <td>69</td>\n",
|
|
||||||
" <td>-1.0</td>\n",
|
|
||||||
" <td>0.0</td>\n",
|
|
||||||
" <td>Default</td>\n",
|
|
||||||
" <td>N</td>\n",
|
|
||||||
" <td>Low Risk</td>\n",
|
|
||||||
" </tr>\n",
|
|
||||||
" </tbody>\n",
|
|
||||||
"</table>\n",
|
|
||||||
"<p>100 rows × 10 columns</p>\n",
|
|
||||||
"</div>"
|
|
||||||
],
|
|
||||||
"text/plain": [
|
|
||||||
" focal_ID trade_time_window net_volume order_count \\\n",
|
|
||||||
"0 3097728207 2024-01-01 00:03:00 -92.0 1 \n",
|
|
||||||
"1 3228645322 2024-01-01 00:06:00 -689.0 1 \n",
|
|
||||||
"2 2701872727 2024-01-01 00:09:00 -42.0 1 \n",
|
|
||||||
"3 1659056655 2024-01-01 00:11:00 -167.0 1 \n",
|
|
||||||
"4 1661288887 2024-01-01 00:13:00 -756.0 1 \n",
|
|
||||||
".. ... ... ... ... \n",
|
|
||||||
"95 1945772682 2024-01-01 00:43:00 -854.0 1 \n",
|
|
||||||
"96 2137478041 2024-01-01 00:43:00 -926.0 1 \n",
|
|
||||||
"97 7138329164 2024-01-01 00:43:00 -433.0 1 \n",
|
|
||||||
"98 1867007441 2024-01-01 00:43:00 -626.0 1 \n",
|
|
||||||
"99 2347906349 2024-01-01 00:43:00 -69.0 1 \n",
|
|
||||||
"\n",
|
|
||||||
" total_trade_volume order_trade_ratio volume_percentage Segment \\\n",
|
|
||||||
"0 92 -1.0 0.0 Default \n",
|
|
||||||
"1 689 -1.0 0.0 Default \n",
|
|
||||||
"2 42 -1.0 0.0 Default \n",
|
|
||||||
"3 167 -1.0 0.0 Default \n",
|
|
||||||
"4 756 -1.0 0.0 Default \n",
|
|
||||||
".. ... ... ... ... \n",
|
|
||||||
"95 854 -1.0 0.0 Default \n",
|
|
||||||
"96 926 -1.0 0.0 Default \n",
|
|
||||||
"97 433 -1.0 0.0 Default \n",
|
|
||||||
"98 626 -1.0 0.0 Default \n",
|
|
||||||
"99 69 -1.0 0.0 Default \n",
|
|
||||||
"\n",
|
|
||||||
" SAR_FLAG Risk \n",
|
|
||||||
"0 N Low Risk \n",
|
|
||||||
"1 N Low Risk \n",
|
|
||||||
"2 N Low Risk \n",
|
|
||||||
"3 N Low Risk \n",
|
|
||||||
"4 N Low Risk \n",
|
|
||||||
".. ... ... \n",
|
|
||||||
"95 N Low Risk \n",
|
|
||||||
"96 N Low Risk \n",
|
|
||||||
"97 N Low Risk \n",
|
|
||||||
"98 N Low Risk \n",
|
|
||||||
"99 N Low Risk \n",
|
|
||||||
"\n",
|
|
||||||
"[100 rows x 10 columns]"
|
|
||||||
]
|
|
||||||
},
|
|
||||||
"execution_count": 22,
|
|
||||||
"metadata": {},
|
|
||||||
"output_type": "execute_result"
|
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"source": [
|
"source": [
|
||||||
"scenario = Scenario()\n",
|
"scenario = Scenario()\n",
|
||||||
"scenario.logic(validation_window=300000)"
|
"scenario.logic(spoofing_time_window = 300000,\n",
|
||||||
|
" spoofing_side = 'buy',\n",
|
||||||
|
" use_volume_for_order_trade_ratio = True,\n",
|
||||||
|
" trade_time_window = 300000,\n",
|
||||||
|
" ignore_trade_after_spoofing = True,\n",
|
||||||
|
" ignore_price_improvement = True)"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
|||||||
252
main.ipynb
252
main.ipynb
@ -2,7 +2,7 @@
|
|||||||
"cells": [
|
"cells": [
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 21,
|
"execution_count": 12,
|
||||||
"id": "e706cfb0-2234-4c4c-95d8-d1968f656aa0",
|
"id": "e706cfb0-2234-4c4c-95d8-d1968f656aa0",
|
||||||
"metadata": {
|
"metadata": {
|
||||||
"tags": []
|
"tags": []
|
||||||
@ -30,7 +30,7 @@
|
|||||||
" o.order_price,\n",
|
" o.order_price,\n",
|
||||||
" o.side AS order_side\n",
|
" o.side AS order_side\n",
|
||||||
" FROM \n",
|
" FROM \n",
|
||||||
" {trade_data_1b} t\n",
|
" {trade_10m_v3} t\n",
|
||||||
" LEFT JOIN \n",
|
" LEFT JOIN \n",
|
||||||
" order_10m o ON o.date_time BETWEEN t.date_time - INTERVAL '{spoofing_time_window_s}' SECOND \n",
|
" order_10m o ON o.date_time BETWEEN t.date_time - INTERVAL '{spoofing_time_window_s}' SECOND \n",
|
||||||
" AND t.date_time\n",
|
" AND t.date_time\n",
|
||||||
@ -76,7 +76,7 @@
|
|||||||
" t.trader_id,\n",
|
" t.trader_id,\n",
|
||||||
" t.trade_id,\n",
|
" t.trade_id,\n",
|
||||||
" SUM(t.trade_volume) AS total_trade_volume\n",
|
" SUM(t.trade_volume) AS total_trade_volume\n",
|
||||||
" FROM {trade_data_1b} t\n",
|
" FROM {trade_10m_v3} t\n",
|
||||||
" WHERE \n",
|
" WHERE \n",
|
||||||
" t.date_time BETWEEN t.date_time - INTERVAL '{trade_time_window_s}' SECOND\n",
|
" t.date_time BETWEEN t.date_time - INTERVAL '{trade_time_window_s}' SECOND\n",
|
||||||
" AND t.date_time\n",
|
" AND t.date_time\n",
|
||||||
@ -107,11 +107,11 @@
|
|||||||
" net_order_volume_all_cte a ON n.trade_id = a.trade_id\n",
|
" net_order_volume_all_cte a ON n.trade_id = a.trade_id\n",
|
||||||
"WHERE \n",
|
"WHERE \n",
|
||||||
" n.net_order_volume > 0 -- Only consider positive net order volumes (potential spoofing);\n",
|
" n.net_order_volume > 0 -- Only consider positive net order volumes (potential spoofing);\n",
|
||||||
" limit 1000\n",
|
" limit 100\n",
|
||||||
"\"\"\"\n",
|
"\"\"\"\n",
|
||||||
"\n",
|
"\n",
|
||||||
"class Scenario:\n",
|
"class Scenario:\n",
|
||||||
" seq = SQLQueryInterface(schema=\"internal\")\n",
|
" seq = SQLQueryInterface(schema=\"trade_schema\")\n",
|
||||||
"\n",
|
"\n",
|
||||||
" def logic(self, **params):\n",
|
" def logic(self, **params):\n",
|
||||||
" spoofing_time_window = params.get('spoofing_time_window', 300000) # default to 300,000 ms (5 minutes)\n",
|
" spoofing_time_window = params.get('spoofing_time_window', 300000) # default to 300,000 ms (5 minutes)\n",
|
||||||
@ -130,7 +130,7 @@
|
|||||||
"\n",
|
"\n",
|
||||||
" # Execute the query with the parameters passed from `params`\n",
|
" # Execute the query with the parameters passed from `params`\n",
|
||||||
" row_list = self.seq.execute_raw(query.format(\n",
|
" row_list = self.seq.execute_raw(query.format(\n",
|
||||||
" trade_data_1b=\"trade_10m_v3\", # Replace with actual table name\n",
|
" trade_10m_v3=\"trade_10m_v3\", # Replace with actual table name\n",
|
||||||
" spoofing_time_window_s=spoofing_time_window_s,\n",
|
" spoofing_time_window_s=spoofing_time_window_s,\n",
|
||||||
" trade_time_window_s=trade_time_window_s,\n",
|
" trade_time_window_s=trade_time_window_s,\n",
|
||||||
" spoofing_side=spoofing_side\n",
|
" spoofing_side=spoofing_side\n",
|
||||||
@ -156,7 +156,7 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 22,
|
"execution_count": null,
|
||||||
"id": "b5c4307f-bc35-47e2-b680-fd1df2168d6c",
|
"id": "b5c4307f-bc35-47e2-b680-fd1df2168d6c",
|
||||||
"metadata": {
|
"metadata": {
|
||||||
"tags": []
|
"tags": []
|
||||||
@ -166,242 +166,18 @@
|
|||||||
"name": "stdout",
|
"name": "stdout",
|
||||||
"output_type": "stream",
|
"output_type": "stream",
|
||||||
"text": [
|
"text": [
|
||||||
"Query start time : 2024-10-14 07:40:43.846637\n"
|
"Query start time: 2024-10-15 09:25:10.320161\n"
|
||||||
]
|
]
|
||||||
},
|
|
||||||
{
|
|
||||||
"data": {
|
|
||||||
"text/html": [
|
|
||||||
"<div>\n",
|
|
||||||
"<style scoped>\n",
|
|
||||||
" .dataframe tbody tr th:only-of-type {\n",
|
|
||||||
" vertical-align: middle;\n",
|
|
||||||
" }\n",
|
|
||||||
"\n",
|
|
||||||
" .dataframe tbody tr th {\n",
|
|
||||||
" vertical-align: top;\n",
|
|
||||||
" }\n",
|
|
||||||
"\n",
|
|
||||||
" .dataframe thead th {\n",
|
|
||||||
" text-align: right;\n",
|
|
||||||
" }\n",
|
|
||||||
"</style>\n",
|
|
||||||
"<table border=\"1\" class=\"dataframe\">\n",
|
|
||||||
" <thead>\n",
|
|
||||||
" <tr style=\"text-align: right;\">\n",
|
|
||||||
" <th></th>\n",
|
|
||||||
" <th>focal_ID</th>\n",
|
|
||||||
" <th>trade_time_window</th>\n",
|
|
||||||
" <th>net_volume</th>\n",
|
|
||||||
" <th>order_count</th>\n",
|
|
||||||
" <th>total_trade_volume</th>\n",
|
|
||||||
" <th>order_trade_ratio</th>\n",
|
|
||||||
" <th>volume_percentage</th>\n",
|
|
||||||
" <th>Segment</th>\n",
|
|
||||||
" <th>SAR_FLAG</th>\n",
|
|
||||||
" <th>Risk</th>\n",
|
|
||||||
" </tr>\n",
|
|
||||||
" </thead>\n",
|
|
||||||
" <tbody>\n",
|
|
||||||
" <tr>\n",
|
|
||||||
" <th>0</th>\n",
|
|
||||||
" <td>3097728207</td>\n",
|
|
||||||
" <td>2024-01-01 00:03:00</td>\n",
|
|
||||||
" <td>-92.0</td>\n",
|
|
||||||
" <td>1</td>\n",
|
|
||||||
" <td>92</td>\n",
|
|
||||||
" <td>-1.0</td>\n",
|
|
||||||
" <td>0.0</td>\n",
|
|
||||||
" <td>Default</td>\n",
|
|
||||||
" <td>N</td>\n",
|
|
||||||
" <td>Low Risk</td>\n",
|
|
||||||
" </tr>\n",
|
|
||||||
" <tr>\n",
|
|
||||||
" <th>1</th>\n",
|
|
||||||
" <td>3228645322</td>\n",
|
|
||||||
" <td>2024-01-01 00:06:00</td>\n",
|
|
||||||
" <td>-689.0</td>\n",
|
|
||||||
" <td>1</td>\n",
|
|
||||||
" <td>689</td>\n",
|
|
||||||
" <td>-1.0</td>\n",
|
|
||||||
" <td>0.0</td>\n",
|
|
||||||
" <td>Default</td>\n",
|
|
||||||
" <td>N</td>\n",
|
|
||||||
" <td>Low Risk</td>\n",
|
|
||||||
" </tr>\n",
|
|
||||||
" <tr>\n",
|
|
||||||
" <th>2</th>\n",
|
|
||||||
" <td>2701872727</td>\n",
|
|
||||||
" <td>2024-01-01 00:09:00</td>\n",
|
|
||||||
" <td>-42.0</td>\n",
|
|
||||||
" <td>1</td>\n",
|
|
||||||
" <td>42</td>\n",
|
|
||||||
" <td>-1.0</td>\n",
|
|
||||||
" <td>0.0</td>\n",
|
|
||||||
" <td>Default</td>\n",
|
|
||||||
" <td>N</td>\n",
|
|
||||||
" <td>Low Risk</td>\n",
|
|
||||||
" </tr>\n",
|
|
||||||
" <tr>\n",
|
|
||||||
" <th>3</th>\n",
|
|
||||||
" <td>1659056655</td>\n",
|
|
||||||
" <td>2024-01-01 00:11:00</td>\n",
|
|
||||||
" <td>-167.0</td>\n",
|
|
||||||
" <td>1</td>\n",
|
|
||||||
" <td>167</td>\n",
|
|
||||||
" <td>-1.0</td>\n",
|
|
||||||
" <td>0.0</td>\n",
|
|
||||||
" <td>Default</td>\n",
|
|
||||||
" <td>N</td>\n",
|
|
||||||
" <td>Low Risk</td>\n",
|
|
||||||
" </tr>\n",
|
|
||||||
" <tr>\n",
|
|
||||||
" <th>4</th>\n",
|
|
||||||
" <td>1661288887</td>\n",
|
|
||||||
" <td>2024-01-01 00:13:00</td>\n",
|
|
||||||
" <td>-756.0</td>\n",
|
|
||||||
" <td>1</td>\n",
|
|
||||||
" <td>756</td>\n",
|
|
||||||
" <td>-1.0</td>\n",
|
|
||||||
" <td>0.0</td>\n",
|
|
||||||
" <td>Default</td>\n",
|
|
||||||
" <td>N</td>\n",
|
|
||||||
" <td>Low Risk</td>\n",
|
|
||||||
" </tr>\n",
|
|
||||||
" <tr>\n",
|
|
||||||
" <th>...</th>\n",
|
|
||||||
" <td>...</td>\n",
|
|
||||||
" <td>...</td>\n",
|
|
||||||
" <td>...</td>\n",
|
|
||||||
" <td>...</td>\n",
|
|
||||||
" <td>...</td>\n",
|
|
||||||
" <td>...</td>\n",
|
|
||||||
" <td>...</td>\n",
|
|
||||||
" <td>...</td>\n",
|
|
||||||
" <td>...</td>\n",
|
|
||||||
" <td>...</td>\n",
|
|
||||||
" </tr>\n",
|
|
||||||
" <tr>\n",
|
|
||||||
" <th>95</th>\n",
|
|
||||||
" <td>1945772682</td>\n",
|
|
||||||
" <td>2024-01-01 00:43:00</td>\n",
|
|
||||||
" <td>-854.0</td>\n",
|
|
||||||
" <td>1</td>\n",
|
|
||||||
" <td>854</td>\n",
|
|
||||||
" <td>-1.0</td>\n",
|
|
||||||
" <td>0.0</td>\n",
|
|
||||||
" <td>Default</td>\n",
|
|
||||||
" <td>N</td>\n",
|
|
||||||
" <td>Low Risk</td>\n",
|
|
||||||
" </tr>\n",
|
|
||||||
" <tr>\n",
|
|
||||||
" <th>96</th>\n",
|
|
||||||
" <td>2137478041</td>\n",
|
|
||||||
" <td>2024-01-01 00:43:00</td>\n",
|
|
||||||
" <td>-926.0</td>\n",
|
|
||||||
" <td>1</td>\n",
|
|
||||||
" <td>926</td>\n",
|
|
||||||
" <td>-1.0</td>\n",
|
|
||||||
" <td>0.0</td>\n",
|
|
||||||
" <td>Default</td>\n",
|
|
||||||
" <td>N</td>\n",
|
|
||||||
" <td>Low Risk</td>\n",
|
|
||||||
" </tr>\n",
|
|
||||||
" <tr>\n",
|
|
||||||
" <th>97</th>\n",
|
|
||||||
" <td>7138329164</td>\n",
|
|
||||||
" <td>2024-01-01 00:43:00</td>\n",
|
|
||||||
" <td>-433.0</td>\n",
|
|
||||||
" <td>1</td>\n",
|
|
||||||
" <td>433</td>\n",
|
|
||||||
" <td>-1.0</td>\n",
|
|
||||||
" <td>0.0</td>\n",
|
|
||||||
" <td>Default</td>\n",
|
|
||||||
" <td>N</td>\n",
|
|
||||||
" <td>Low Risk</td>\n",
|
|
||||||
" </tr>\n",
|
|
||||||
" <tr>\n",
|
|
||||||
" <th>98</th>\n",
|
|
||||||
" <td>1867007441</td>\n",
|
|
||||||
" <td>2024-01-01 00:43:00</td>\n",
|
|
||||||
" <td>-626.0</td>\n",
|
|
||||||
" <td>1</td>\n",
|
|
||||||
" <td>626</td>\n",
|
|
||||||
" <td>-1.0</td>\n",
|
|
||||||
" <td>0.0</td>\n",
|
|
||||||
" <td>Default</td>\n",
|
|
||||||
" <td>N</td>\n",
|
|
||||||
" <td>Low Risk</td>\n",
|
|
||||||
" </tr>\n",
|
|
||||||
" <tr>\n",
|
|
||||||
" <th>99</th>\n",
|
|
||||||
" <td>2347906349</td>\n",
|
|
||||||
" <td>2024-01-01 00:43:00</td>\n",
|
|
||||||
" <td>-69.0</td>\n",
|
|
||||||
" <td>1</td>\n",
|
|
||||||
" <td>69</td>\n",
|
|
||||||
" <td>-1.0</td>\n",
|
|
||||||
" <td>0.0</td>\n",
|
|
||||||
" <td>Default</td>\n",
|
|
||||||
" <td>N</td>\n",
|
|
||||||
" <td>Low Risk</td>\n",
|
|
||||||
" </tr>\n",
|
|
||||||
" </tbody>\n",
|
|
||||||
"</table>\n",
|
|
||||||
"<p>100 rows × 10 columns</p>\n",
|
|
||||||
"</div>"
|
|
||||||
],
|
|
||||||
"text/plain": [
|
|
||||||
" focal_ID trade_time_window net_volume order_count \\\n",
|
|
||||||
"0 3097728207 2024-01-01 00:03:00 -92.0 1 \n",
|
|
||||||
"1 3228645322 2024-01-01 00:06:00 -689.0 1 \n",
|
|
||||||
"2 2701872727 2024-01-01 00:09:00 -42.0 1 \n",
|
|
||||||
"3 1659056655 2024-01-01 00:11:00 -167.0 1 \n",
|
|
||||||
"4 1661288887 2024-01-01 00:13:00 -756.0 1 \n",
|
|
||||||
".. ... ... ... ... \n",
|
|
||||||
"95 1945772682 2024-01-01 00:43:00 -854.0 1 \n",
|
|
||||||
"96 2137478041 2024-01-01 00:43:00 -926.0 1 \n",
|
|
||||||
"97 7138329164 2024-01-01 00:43:00 -433.0 1 \n",
|
|
||||||
"98 1867007441 2024-01-01 00:43:00 -626.0 1 \n",
|
|
||||||
"99 2347906349 2024-01-01 00:43:00 -69.0 1 \n",
|
|
||||||
"\n",
|
|
||||||
" total_trade_volume order_trade_ratio volume_percentage Segment \\\n",
|
|
||||||
"0 92 -1.0 0.0 Default \n",
|
|
||||||
"1 689 -1.0 0.0 Default \n",
|
|
||||||
"2 42 -1.0 0.0 Default \n",
|
|
||||||
"3 167 -1.0 0.0 Default \n",
|
|
||||||
"4 756 -1.0 0.0 Default \n",
|
|
||||||
".. ... ... ... ... \n",
|
|
||||||
"95 854 -1.0 0.0 Default \n",
|
|
||||||
"96 926 -1.0 0.0 Default \n",
|
|
||||||
"97 433 -1.0 0.0 Default \n",
|
|
||||||
"98 626 -1.0 0.0 Default \n",
|
|
||||||
"99 69 -1.0 0.0 Default \n",
|
|
||||||
"\n",
|
|
||||||
" SAR_FLAG Risk \n",
|
|
||||||
"0 N Low Risk \n",
|
|
||||||
"1 N Low Risk \n",
|
|
||||||
"2 N Low Risk \n",
|
|
||||||
"3 N Low Risk \n",
|
|
||||||
"4 N Low Risk \n",
|
|
||||||
".. ... ... \n",
|
|
||||||
"95 N Low Risk \n",
|
|
||||||
"96 N Low Risk \n",
|
|
||||||
"97 N Low Risk \n",
|
|
||||||
"98 N Low Risk \n",
|
|
||||||
"99 N Low Risk \n",
|
|
||||||
"\n",
|
|
||||||
"[100 rows x 10 columns]"
|
|
||||||
]
|
|
||||||
},
|
|
||||||
"execution_count": 22,
|
|
||||||
"metadata": {},
|
|
||||||
"output_type": "execute_result"
|
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"source": [
|
"source": [
|
||||||
"scenario = Scenario()\n",
|
"scenario = Scenario()\n",
|
||||||
"scenario.logic(validation_window=300000)"
|
"scenario.logic(spoofing_time_window = 300000,\n",
|
||||||
|
" spoofing_side = 'buy',\n",
|
||||||
|
" use_volume_for_order_trade_ratio = True,\n",
|
||||||
|
" trade_time_window = 300000,\n",
|
||||||
|
" ignore_trade_after_spoofing = True,\n",
|
||||||
|
" ignore_price_improvement = True)"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
|||||||
21
main.py
21
main.py
@ -1,7 +1,7 @@
|
|||||||
#!/usr/bin/env python
|
#!/usr/bin/env python
|
||||||
# coding: utf-8
|
# coding: utf-8
|
||||||
|
|
||||||
# In[21]:
|
# In[12]:
|
||||||
|
|
||||||
|
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
@ -25,7 +25,7 @@ trade_window AS (
|
|||||||
o.order_price,
|
o.order_price,
|
||||||
o.side AS order_side
|
o.side AS order_side
|
||||||
FROM
|
FROM
|
||||||
{trade_data_1b} t
|
{trade_10m_v3} t
|
||||||
LEFT JOIN
|
LEFT JOIN
|
||||||
order_10m o ON o.date_time BETWEEN t.date_time - INTERVAL '{spoofing_time_window_s}' SECOND
|
order_10m o ON o.date_time BETWEEN t.date_time - INTERVAL '{spoofing_time_window_s}' SECOND
|
||||||
AND t.date_time
|
AND t.date_time
|
||||||
@ -71,7 +71,7 @@ opposite_trade_volume_cte AS (
|
|||||||
t.trader_id,
|
t.trader_id,
|
||||||
t.trade_id,
|
t.trade_id,
|
||||||
SUM(t.trade_volume) AS total_trade_volume
|
SUM(t.trade_volume) AS total_trade_volume
|
||||||
FROM {trade_data_1b} t
|
FROM {trade_10m_v3} t
|
||||||
WHERE
|
WHERE
|
||||||
t.date_time BETWEEN t.date_time - INTERVAL '{trade_time_window_s}' SECOND
|
t.date_time BETWEEN t.date_time - INTERVAL '{trade_time_window_s}' SECOND
|
||||||
AND t.date_time
|
AND t.date_time
|
||||||
@ -102,11 +102,11 @@ LEFT JOIN
|
|||||||
net_order_volume_all_cte a ON n.trade_id = a.trade_id
|
net_order_volume_all_cte a ON n.trade_id = a.trade_id
|
||||||
WHERE
|
WHERE
|
||||||
n.net_order_volume > 0 -- Only consider positive net order volumes (potential spoofing);
|
n.net_order_volume > 0 -- Only consider positive net order volumes (potential spoofing);
|
||||||
limit 1000
|
limit 100
|
||||||
"""
|
"""
|
||||||
|
|
||||||
class Scenario:
|
class Scenario:
|
||||||
seq = SQLQueryInterface(schema="internal")
|
seq = SQLQueryInterface(schema="trade_schema")
|
||||||
|
|
||||||
def logic(self, **params):
|
def logic(self, **params):
|
||||||
spoofing_time_window = params.get('spoofing_time_window', 300000) # default to 300,000 ms (5 minutes)
|
spoofing_time_window = params.get('spoofing_time_window', 300000) # default to 300,000 ms (5 minutes)
|
||||||
@ -125,7 +125,7 @@ class Scenario:
|
|||||||
|
|
||||||
# Execute the query with the parameters passed from `params`
|
# Execute the query with the parameters passed from `params`
|
||||||
row_list = self.seq.execute_raw(query.format(
|
row_list = self.seq.execute_raw(query.format(
|
||||||
trade_data_1b="trade_10m_v3", # Replace with actual table name
|
trade_10m_v3="trade_10m_v3", # Replace with actual table name
|
||||||
spoofing_time_window_s=spoofing_time_window_s,
|
spoofing_time_window_s=spoofing_time_window_s,
|
||||||
trade_time_window_s=trade_time_window_s,
|
trade_time_window_s=trade_time_window_s,
|
||||||
spoofing_side=spoofing_side
|
spoofing_side=spoofing_side
|
||||||
@ -149,11 +149,16 @@ class Scenario:
|
|||||||
return final_scenario_df
|
return final_scenario_df
|
||||||
|
|
||||||
|
|
||||||
# In[22]:
|
# In[ ]:
|
||||||
|
|
||||||
|
|
||||||
scenario = Scenario()
|
scenario = Scenario()
|
||||||
scenario.logic(validation_window=300000)
|
scenario.logic(spoofing_time_window = 300000,
|
||||||
|
spoofing_side = 'buy',
|
||||||
|
use_volume_for_order_trade_ratio = True,
|
||||||
|
trade_time_window = 300000,
|
||||||
|
ignore_trade_after_spoofing = True,
|
||||||
|
ignore_price_improvement = True)
|
||||||
|
|
||||||
|
|
||||||
# In[ ]:
|
# In[ ]:
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user