diff --git a/.ipynb_checkpoints/Untitled-checkpoint.ipynb b/.ipynb_checkpoints/Untitled-checkpoint.ipynb new file mode 100644 index 0000000..94c2fe7 --- /dev/null +++ b/.ipynb_checkpoints/Untitled-checkpoint.ipynb @@ -0,0 +1,213 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": null, + "id": "d0f8357f-babc-4b20-bc31-e2b675e3ad4c", + "metadata": {}, + "outputs": [], + "source": [ + "from datetime import datetime, timedelta\n", + "\n", + "import pandas as pd\n", + "\n", + "from tms_data_interface import SQLQueryInterface\n", + " \n", + "query = \"\"\"\n", + "\n", + "WITH time_windows AS (\n", + "\n", + " SELECT\n", + "\n", + " -- End time is the current trade time\n", + "\n", + " date_time AS end_time,\n", + " \n", + " -- Subtract seconds from the end_time using date_add() with negative integer interval\n", + "\n", + " date_add('second', -{time_window_s}, date_time) AS start_time,\n", + " \n", + " -- Trade details\n", + "\n", + " trade_price,\n", + "\n", + " trade_volume,\n", + "\n", + " trader_id,\n", + " \n", + " -- Calculate minimum price within the time window\n", + "\n", + " MIN(trade_price) OVER (\n", + "\n", + " ORDER BY date_time \n", + "\n", + " RANGE BETWEEN INTERVAL '{time_window_s}' SECOND PRECEDING AND CURRENT ROW\n", + "\n", + " ) AS min_price,\n", + " \n", + " -- Calculate maximum price within the time window\n", + "\n", + " MAX(trade_price) OVER (\n", + "\n", + " ORDER BY date_time \n", + "\n", + " RANGE BETWEEN INTERVAL '{time_window_s}' SECOND PRECEDING AND CURRENT ROW\n", + "\n", + " ) AS max_price,\n", + " \n", + " -- Calculate total trade volume within the time window\n", + "\n", + " SUM(trade_volume) OVER ( \n", + "\n", + " ORDER BY date_time \n", + "\n", + " RANGE BETWEEN INTERVAL '{time_window_s}' SECOND PRECEDING AND CURRENT ROW\n", + "\n", + " ) AS total_volume,\n", + " \n", + " -- Calculate participant's trade volume within the time window\n", + "\n", + " SUM(CASE WHEN trader_id = trader_id THEN trade_volume ELSE 0 END) OVER (\n", + "\n", + " PARTITION BY trader_id \n", + "\n", + " ORDER BY date_time \n", + "\n", + " RANGE BETWEEN INTERVAL '{time_window_s}' SECOND PRECEDING AND CURRENT ROW\n", + "\n", + " ) AS participant_volume\n", + "\n", + " FROM\n", + "\n", + " {trade_data_1b}\n", + "\n", + ")\n", + "\n", + "SELECT\n", + "\n", + " -- Select the time window details\n", + "\n", + " start_time,\n", + "\n", + " end_time,\n", + " \n", + " -- Select the participant (trader) ID\n", + "\n", + " trader_id AS \"Participant\",\n", + " \n", + " -- Select the calculated min and max prices\n", + "\n", + " min_price,\n", + "\n", + " max_price,\n", + " \n", + " -- Calculate the price change percentage\n", + "\n", + " (max_price - min_price) / NULLIF(min_price, 0) * 100 AS \"Price Change (%)\",\n", + " \n", + " -- Calculate the participant's volume as a percentage of total volume\n", + "\n", + " (participant_volume / NULLIF(total_volume, 0)) * 100 AS \"Volume (%)\",\n", + " \n", + " -- Participant volume\n", + "\n", + " participant_volume,\n", + " \n", + " -- Select the total volume within the window\n", + "\n", + " total_volume AS \"Total Volume\"\n", + "\n", + "FROM\n", + "\n", + " time_windows\n", + "\n", + "\"\"\"\n", + " \n", + " \n", + "from tms_data_interface import SQLQueryInterface\n", + " \n", + "class Scenario:\n", + "\n", + " seq = SQLQueryInterface(schema=\"trade_schema\")\n", + "\n", + " def logic(self, **kwargs):\n", + "\n", + " validation_window = kwargs.get('validation_window')\n", + "\n", + " time_window_s = int(validation_window/1000)\n", + "\n", + " query_start_time = datetime.now()\n", + "\n", + " print(\"Query start time :\",query_start_time)\n", + "\n", + " row_list = self.seq.execute_raw(query.format(trade_data_1b=\"trade_10m_v3\",\n", + "\n", + " time_window_s = time_window_s)\n", + "\n", + " )\n", + "\n", + " cols = [\n", + "\n", + " 'START_DATE_TIME',\n", + "\n", + " 'END_DATE_TIME',\n", + "\n", + " 'Focal_id',\n", + "\n", + " 'MIN_PRICE',\n", + "\n", + " 'MAX_PRICE',\n", + "\n", + " 'PRICE_CHANGE_PCT',\n", + "\n", + " 'PARTICIPANT_VOLUME_PCT',\n", + "\n", + " 'PARTICIPANT_VOLUME',\n", + "\n", + " 'TOTAL_VOLUME',\n", + "\n", + " ]\n", + "\n", + " final_scenario_df = pd.DataFrame(row_list, columns = cols)\n", + "\n", + " final_scenario_df['PARTICIPANT_VOLUME_PCT'] = final_scenario_df['PARTICIPANT_VOLUME']/\\\n", + "\n", + " final_scenario_df['TOTAL_VOLUME'] * 100\n", + "\n", + " final_scenario_df['Segment'] = 'Default'\n", + "\n", + " final_scenario_df['SAR_FLAG'] = 'N'\n", + "\n", + " final_scenario_df['Risk'] = 'Low Risk'\n", + "\n", + " final_scenario_df.dropna(inplace=True)\n", + "\n", + " # final_scenario_df['RUN_DATE'] = final_scenario_df['END_DATE']\n", + "\n", + " return final_scenario_df\n", + " " + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3 (ipykernel)", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.11.8" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/Untitled.ipynb b/Untitled.ipynb new file mode 100644 index 0000000..94c2fe7 --- /dev/null +++ b/Untitled.ipynb @@ -0,0 +1,213 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": null, + "id": "d0f8357f-babc-4b20-bc31-e2b675e3ad4c", + "metadata": {}, + "outputs": [], + "source": [ + "from datetime import datetime, timedelta\n", + "\n", + "import pandas as pd\n", + "\n", + "from tms_data_interface import SQLQueryInterface\n", + " \n", + "query = \"\"\"\n", + "\n", + "WITH time_windows AS (\n", + "\n", + " SELECT\n", + "\n", + " -- End time is the current trade time\n", + "\n", + " date_time AS end_time,\n", + " \n", + " -- Subtract seconds from the end_time using date_add() with negative integer interval\n", + "\n", + " date_add('second', -{time_window_s}, date_time) AS start_time,\n", + " \n", + " -- Trade details\n", + "\n", + " trade_price,\n", + "\n", + " trade_volume,\n", + "\n", + " trader_id,\n", + " \n", + " -- Calculate minimum price within the time window\n", + "\n", + " MIN(trade_price) OVER (\n", + "\n", + " ORDER BY date_time \n", + "\n", + " RANGE BETWEEN INTERVAL '{time_window_s}' SECOND PRECEDING AND CURRENT ROW\n", + "\n", + " ) AS min_price,\n", + " \n", + " -- Calculate maximum price within the time window\n", + "\n", + " MAX(trade_price) OVER (\n", + "\n", + " ORDER BY date_time \n", + "\n", + " RANGE BETWEEN INTERVAL '{time_window_s}' SECOND PRECEDING AND CURRENT ROW\n", + "\n", + " ) AS max_price,\n", + " \n", + " -- Calculate total trade volume within the time window\n", + "\n", + " SUM(trade_volume) OVER ( \n", + "\n", + " ORDER BY date_time \n", + "\n", + " RANGE BETWEEN INTERVAL '{time_window_s}' SECOND PRECEDING AND CURRENT ROW\n", + "\n", + " ) AS total_volume,\n", + " \n", + " -- Calculate participant's trade volume within the time window\n", + "\n", + " SUM(CASE WHEN trader_id = trader_id THEN trade_volume ELSE 0 END) OVER (\n", + "\n", + " PARTITION BY trader_id \n", + "\n", + " ORDER BY date_time \n", + "\n", + " RANGE BETWEEN INTERVAL '{time_window_s}' SECOND PRECEDING AND CURRENT ROW\n", + "\n", + " ) AS participant_volume\n", + "\n", + " FROM\n", + "\n", + " {trade_data_1b}\n", + "\n", + ")\n", + "\n", + "SELECT\n", + "\n", + " -- Select the time window details\n", + "\n", + " start_time,\n", + "\n", + " end_time,\n", + " \n", + " -- Select the participant (trader) ID\n", + "\n", + " trader_id AS \"Participant\",\n", + " \n", + " -- Select the calculated min and max prices\n", + "\n", + " min_price,\n", + "\n", + " max_price,\n", + " \n", + " -- Calculate the price change percentage\n", + "\n", + " (max_price - min_price) / NULLIF(min_price, 0) * 100 AS \"Price Change (%)\",\n", + " \n", + " -- Calculate the participant's volume as a percentage of total volume\n", + "\n", + " (participant_volume / NULLIF(total_volume, 0)) * 100 AS \"Volume (%)\",\n", + " \n", + " -- Participant volume\n", + "\n", + " participant_volume,\n", + " \n", + " -- Select the total volume within the window\n", + "\n", + " total_volume AS \"Total Volume\"\n", + "\n", + "FROM\n", + "\n", + " time_windows\n", + "\n", + "\"\"\"\n", + " \n", + " \n", + "from tms_data_interface import SQLQueryInterface\n", + " \n", + "class Scenario:\n", + "\n", + " seq = SQLQueryInterface(schema=\"trade_schema\")\n", + "\n", + " def logic(self, **kwargs):\n", + "\n", + " validation_window = kwargs.get('validation_window')\n", + "\n", + " time_window_s = int(validation_window/1000)\n", + "\n", + " query_start_time = datetime.now()\n", + "\n", + " print(\"Query start time :\",query_start_time)\n", + "\n", + " row_list = self.seq.execute_raw(query.format(trade_data_1b=\"trade_10m_v3\",\n", + "\n", + " time_window_s = time_window_s)\n", + "\n", + " )\n", + "\n", + " cols = [\n", + "\n", + " 'START_DATE_TIME',\n", + "\n", + " 'END_DATE_TIME',\n", + "\n", + " 'Focal_id',\n", + "\n", + " 'MIN_PRICE',\n", + "\n", + " 'MAX_PRICE',\n", + "\n", + " 'PRICE_CHANGE_PCT',\n", + "\n", + " 'PARTICIPANT_VOLUME_PCT',\n", + "\n", + " 'PARTICIPANT_VOLUME',\n", + "\n", + " 'TOTAL_VOLUME',\n", + "\n", + " ]\n", + "\n", + " final_scenario_df = pd.DataFrame(row_list, columns = cols)\n", + "\n", + " final_scenario_df['PARTICIPANT_VOLUME_PCT'] = final_scenario_df['PARTICIPANT_VOLUME']/\\\n", + "\n", + " final_scenario_df['TOTAL_VOLUME'] * 100\n", + "\n", + " final_scenario_df['Segment'] = 'Default'\n", + "\n", + " final_scenario_df['SAR_FLAG'] = 'N'\n", + "\n", + " final_scenario_df['Risk'] = 'Low Risk'\n", + "\n", + " final_scenario_df.dropna(inplace=True)\n", + "\n", + " # final_scenario_df['RUN_DATE'] = final_scenario_df['END_DATE']\n", + "\n", + " return final_scenario_df\n", + " " + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3 (ipykernel)", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.11.8" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/main.py b/main.py new file mode 100644 index 0000000..f220144 --- /dev/null +++ b/main.py @@ -0,0 +1,14 @@ +#!/usr/bin/env python +# coding: utf-8 + +# In[ ]: + + +from tms_data_interface import SQLQueryInterface + +class Scenario: + seq = SQLQueryInterface() + + def logic(self, **kwargs): + # Write your code here +