1
0
Files
leopard-analysis/backtest.ipynb

238 lines
7.0 KiB
Plaintext
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
{
"cells": [
{
"metadata": {
"ExecuteTime": {
"end_time": "2026-01-19T09:22:09.640369Z",
"start_time": "2026-01-19T09:22:07.517191Z"
}
},
"cell_type": "code",
"source": [
"import pandas as pd\n",
"\n",
"host = '81.71.3.24'\n",
"port = 6785\n",
"database = 'leopard_dev'\n",
"username = 'leopard'\n",
"password = '9NEzFzovnddf@PyEP?e*AYAWnCyd7UhYwQK$pJf>7?ccFiN^x4$eKEZ5~E<7<+~X'"
],
"id": "d815a3591c4f9463",
"outputs": [],
"execution_count": 7
},
{
"cell_type": "code",
"id": "initial_id",
"metadata": {
"collapsed": true,
"ExecuteTime": {
"end_time": "2026-01-19T09:42:25.068816Z",
"start_time": "2026-01-19T09:42:18.026869Z"
}
},
"source": [
"import psycopg2\n",
"\n",
"dailies_df = pd.DataFrame()\n",
"\n",
"with psycopg2.connect(host=host, port=port, database=database, user=username, password=password) as connection:\n",
" with connection.cursor() as cursor:\n",
" # language=PostgreSQL\n",
" cursor.execute(\n",
" \"\"\"select trade_date, open, close, high, low, factor\n",
"from leopard_daily daily\n",
" left join leopard_stock stock on stock.id = daily.stock_id\n",
"where stock.code = '000001.SZ'\n",
" and daily.trade_date between '2025-01-01 00:00:00' and '2025-12-31 23:59:59'\n",
"order by daily.trade_date\"\"\"\n",
" )\n",
" rows = cursor.fetchall()\n",
"\n",
" df = pd.DataFrame.from_records(rows, columns=['trade_date', 'open', 'close', 'high', 'low', 'factor'])\n",
"\n",
"df"
],
"outputs": [
{
"data": {
"text/plain": [
" trade_date open close high low factor\n",
"0 2025-01-02 11.73 11.43 11.77 11.39 127.7841\n",
"1 2025-01-03 11.44 11.38 11.54 11.36 127.7841\n",
"2 2025-01-06 11.38 11.44 11.48 11.22 127.7841\n",
"3 2025-01-07 11.42 11.51 11.53 11.37 127.7841\n",
"4 2025-01-08 11.50 11.50 11.63 11.40 127.7841\n",
".. ... ... ... ... ... ...\n",
"183 2025-10-09 11.33 11.40 11.41 11.27 131.7878\n",
"184 2025-10-10 11.37 11.43 11.49 11.36 131.7878\n",
"185 2025-10-13 11.32 11.40 11.46 11.28 131.7878\n",
"186 2025-10-14 11.39 11.57 11.60 11.36 131.7878\n",
"187 2025-10-15 11.34 11.40 11.42 11.26 134.5794\n",
"\n",
"[188 rows x 6 columns]"
],
"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>trade_date</th>\n",
" <th>open</th>\n",
" <th>close</th>\n",
" <th>high</th>\n",
" <th>low</th>\n",
" <th>factor</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>2025-01-02</td>\n",
" <td>11.73</td>\n",
" <td>11.43</td>\n",
" <td>11.77</td>\n",
" <td>11.39</td>\n",
" <td>127.7841</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>2025-01-03</td>\n",
" <td>11.44</td>\n",
" <td>11.38</td>\n",
" <td>11.54</td>\n",
" <td>11.36</td>\n",
" <td>127.7841</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>2025-01-06</td>\n",
" <td>11.38</td>\n",
" <td>11.44</td>\n",
" <td>11.48</td>\n",
" <td>11.22</td>\n",
" <td>127.7841</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>2025-01-07</td>\n",
" <td>11.42</td>\n",
" <td>11.51</td>\n",
" <td>11.53</td>\n",
" <td>11.37</td>\n",
" <td>127.7841</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>2025-01-08</td>\n",
" <td>11.50</td>\n",
" <td>11.50</td>\n",
" <td>11.63</td>\n",
" <td>11.40</td>\n",
" <td>127.7841</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",
" </tr>\n",
" <tr>\n",
" <th>183</th>\n",
" <td>2025-10-09</td>\n",
" <td>11.33</td>\n",
" <td>11.40</td>\n",
" <td>11.41</td>\n",
" <td>11.27</td>\n",
" <td>131.7878</td>\n",
" </tr>\n",
" <tr>\n",
" <th>184</th>\n",
" <td>2025-10-10</td>\n",
" <td>11.37</td>\n",
" <td>11.43</td>\n",
" <td>11.49</td>\n",
" <td>11.36</td>\n",
" <td>131.7878</td>\n",
" </tr>\n",
" <tr>\n",
" <th>185</th>\n",
" <td>2025-10-13</td>\n",
" <td>11.32</td>\n",
" <td>11.40</td>\n",
" <td>11.46</td>\n",
" <td>11.28</td>\n",
" <td>131.7878</td>\n",
" </tr>\n",
" <tr>\n",
" <th>186</th>\n",
" <td>2025-10-14</td>\n",
" <td>11.39</td>\n",
" <td>11.57</td>\n",
" <td>11.60</td>\n",
" <td>11.36</td>\n",
" <td>131.7878</td>\n",
" </tr>\n",
" <tr>\n",
" <th>187</th>\n",
" <td>2025-10-15</td>\n",
" <td>11.34</td>\n",
" <td>11.40</td>\n",
" <td>11.42</td>\n",
" <td>11.26</td>\n",
" <td>134.5794</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"<p>188 rows × 6 columns</p>\n",
"</div>"
]
},
"execution_count": 17,
"metadata": {},
"output_type": "execute_result"
}
],
"execution_count": 17
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.6"
}
},
"nbformat": 4,
"nbformat_minor": 5
}