找回密码
 注册
搜索
楼主: xinxing

打算开发一个股票自动交易系统(Algorithmic trading)

[复制链接]
 楼主| 发表于 2019-3-28 02:18 AM | 显示全部楼层


mikeqc 发表于 2019-3-27 08:29 AM
谈趋势和盘整,一定在级别之下谈。比如,最近跌成狗屎的TSLA,在日线上看是下跌,小时图就可能是下跌尾声 ...

太棒了,你讲的许多东西值得学习,细化,以便自动化处理数据。
三个不同的时间尺度:小时、日线和月线,三个特征。通过日线下跌,小时岛形反转成立,加上月线上的 histogram 在某个价位出现峰值 来判断盘整,有意思。
自动化首先就是规则的量化,量化的规则就可以程序化。
“自动化交易系统”是“交易规则”的量化和程序化,同样,“自动选股”是选股标准的量化和自动化。
很高兴能跟你合作,把你的宝贵经验,转化成计算机能够操作的自动化系统。我在Boston地区,你在附近吗?增加联系。
回复 鲜花 鸡蛋

使用道具 举报

 楼主| 发表于 2019-3-28 09:20 PM | 显示全部楼层
接下来,准备使用 移动平均线[moving average (MA)], 一个技术分析中广泛使用的指标。

平均的目的是滤除“噪声”,使得价格平滑,便于处理。

计算公式就是简单的在一个移动窗口内作平均 [Simple Moving Average (SMA)] 。

$$ SMA = \frac{A_1 + A_2 + ... + A_n}{n} $$ $A_n$是窗口中最后一个价格,在即时跟踪时,它就是股票的当前现价,$A_1$, $A_2$ ... 是过去的价格,所以SMA是一个滞后指标。

为了缓解滞后, 人们倾向于提高当前价格所占的比重,把过去的价格的比重按照指数降低,这就是[Exponential Moving Average (EMA)]

EMA可以递推计算: $$ EMA_n = f*ClosingPrice_n + \left(1-f\right)EMA_{n-1}; $$ where $f = \frac{2}{T+1}$ 是倍数, and $T$ 是周期.

有了EMA就可以计算MACD (Moving average convergence divergence)。

普通软件使用的MACD 是12-day EMA,减去 26-day EMA, 再用9天的EMA(信号线)算交叉。其实不必拘泥于这三个数,再者说,换成分钟不是更精确么。

交叉有两种类型,上叉和下叉 (金叉和死叉) 代表牛和熊区间的开始和结束。在牛的一侧做多,在熊的一侧做空,按理也可以挣钱。既然计算不复杂,何不算一下呢。

更何况,计算MACD可以和之前的over-bought-over-sold模型相结合。

回复 鲜花 鸡蛋

使用道具 举报

 楼主| 发表于 2019-3-31 12:15 AM | 显示全部楼层
本帖最后由 xinxing 于 2019-3-31 12:39 AM 编辑

MACD的上叉是bullish的开始,买进股票SVXY。

MACD的下叉是bullish的结束,卖出清空。该模型给出下列交易, 一年可盈利13.67%

这个表格只考虑了bullish交易,如果考虑bearish一侧的做空,净利润要乘二,等于27.34%,跑赢大盘了。

SNDate TimeActCurrent PriceRealized Rate%Net Profit Rate%
12018-Mar-5 Mon 1:29 PMBought at $49.4025
22018-Mar-13 Tue 2:07 PMSold at $51.32013.8823.882
32018-Mar-16 Fri 11:44 AMBought at $52.08
42018-Mar-19 Mon 10:28 AMSold at $50.72-2.6111.27
52018-Mar-21 Wed 12:36 PMBought at $51.034
62018-Mar-22 Thu 10:38 AMSold at $48.6476-4.676-3.406
72018-Mar-27 Tue 12:44 PMBought at $47.0212
82018-Mar-27 Tue 3:52 PMSold at $45.608-3.005-6.411
92018-Mar-29 Thu 11:11 AMBought at $46.6116
102018-Apr-2 Mon 2:09 PMSold at $44-5.603-12.01
112018-Apr-3 Tue 11:48 AMBought at $45.1598
122018-Apr-19 Thu 11:38 AMSold at $49.32259.218-2.797
132018-Apr-26 Thu 2:24 PMBought at $48.9178
142018-May-3 Thu 10:47 AMSold at $48.317-1.228-4.025
152018-May-4 Fri 1:49 PMBought at $50.04
162018-May-8 Tue 2:07 PMSold at $50-0.07994-4.105
172018-May-9 Wed 9:39 AMBought at $50.5654
182018-May-15 Tue 11:14 AMSold at $52.1863.205-0.8997
192018-May-21 Mon 10:50 AMBought at $54.6
202018-May-22 Tue 3:02 PMSold at $53.8796-1.319-2.219
212018-May-25 Fri 11:09 AMBought at $54.5194
222018-May-29 Tue 9:30 AMSold at $52.84-3.08-5.3
232018-Jun-1 Fri 10:35 AMBought at $52.9563
242018-Jun-8 Fri 10:56 AMSold at $54.66453.226-2.074
252018-Jun-11 Mon 12:53 PMBought at $55.29
262018-Jun-12 Tue 3:44 PMSold at $55.560.4883-1.586
272018-Jun-13 Wed 9:35 AMBought at $55.84
282018-Jun-13 Wed 1:21 PMSold at $55.6798-0.2869-1.873
292018-Jun-14 Thu 1:44 PMBought at $56.2796
302018-Jun-14 Thu 3:12 PMSold at $55.981-0.5307-2.403
312018-Jun-27 Wed 12:06 PMBought at $51.5669
322018-Jun-27 Wed 1:08 PMSold at $50.3948-2.273-4.676
332018-Jun-29 Fri 9:43 AMBought at $51.4389
342018-Jul-11 Wed 1:30 PMSold at $53.87324.7320.05629
352018-Jul-16 Mon 1:12 PMBought at $55.3215
362018-Jul-16 Mon 2:42 PMSold at $55.0488-0.493-0.4367
372018-Jul-17 Tue 11:57 AMBought at $55.799
382018-Jul-18 Wed 10:16 AMSold at $55.3676-0.7732-1.21
392018-Jul-25 Wed 2:14 PMBought at $55.8487
402018-Jul-26 Thu 1:32 PMSold at $55.6806-0.301-1.511
412018-Jul-26 Thu 2:51 PMBought at $55.9939
422018-Jul-27 Fri 11:49 AMSold at $55.84-0.2748-1.786
432018-Aug-1 Wed 10:24 AMBought at $55.62
442018-Aug-2 Thu 10:50 AMSold at $54.4759-2.057-3.843
452018-Aug-2 Thu 1:00 PMBought at $55.3133
462018-Aug-10 Fri 9:39 AMSold at $56.60872.342-1.501
472018-Aug-16 Thu 12:17 PMBought at $55.9737
482018-Aug-23 Thu 12:02 PMSold at $56.60671.131-0.3699
492018-Aug-24 Fri 10:52 AMBought at $57.32
502018-Aug-24 Fri 2:48 PMSold at $57.08-0.4187-0.7886
512018-Sep-10 Mon 11:57 AMBought at $55.8101
522018-Sep-18 Tue 9:31 AMSold at $57.783.532.741
532018-Sep-19 Wed 1:03 PMBought at $58.7327
542018-Sep-21 Fri 3:30 PMSold at $59.010.47213.213
552018-Oct-1 Mon 11:07 AMBought at $59.7294
562018-Oct-2 Tue 3:59 PMSold at $59.2466-0.80852.405
572018-Oct-3 Wed 9:31 AMBought at $59.6729
582018-Oct-3 Wed 2:54 PMSold at $59.2038-0.78611.618
592018-Oct-9 Tue 3:05 PMBought at $56.5091
602018-Oct-10 Wed 9:47 AMSold at $55.2954-2.148-0.5294
612018-Oct-15 Mon 1:06 PMBought at $51.28
622018-Oct-22 Mon 12:20 PMSold at $50.1318-2.239-2.768
632018-Oct-22 Mon 1:36 PMBought at $50.5411
642018-Oct-23 Tue 9:57 AMSold at $48.854-3.338-6.107
652018-Oct-29 Mon 11:10 AMBought at $48.0905
662018-Nov-9 Fri 12:18 PMSold at $50.98116.011-0.09586
672018-Nov-16 Fri 12:16 PMBought at $49.1004
682018-Nov-20 Tue 9:54 AMSold at $46.738-4.811-4.907
692018-Nov-21 Wed 3:10 PMBought at $47.8947
702018-Dec-4 Tue 1:25 PMSold at $49.0092.327-2.581
712018-Dec-11 Tue 2:52 PMBought at $46.0126
722018-Dec-17 Mon 12:40 PMSold at $45.3893-1.355-3.935
732018-Dec-19 Wed 12:32 PMBought at $45.3721
742018-Dec-20 Thu 11:14 AMSold at $44.0725-2.864-6.8
752018-Dec-28 Fri 10:30 AMBought at $41.1682
762019-Jan-16 Wed 3:20 PMSold at $46.969114.097.291
772019-Jan-25 Fri 10:33 AMBought at $46.9429
782019-Jan-28 Mon 1:04 PMSold at $45.56-2.9464.345
792019-Jan-29 Tue 10:07 AMBought at $46.4692
802019-Feb-6 Wed 2:48 PMSold at $49.7156.98511.33
812019-Feb-12 Tue 9:59 AMBought at $49.8975
822019-Feb-14 Thu 10:02 AMSold at $49.0959-1.6069.723
832019-Feb-15 Fri 1:15 PMBought at $50.0845
842019-Feb-26 Tue 9:56 AMSold at $51.12.02811.75
852019-Mar-1 Fri 1:31 PMBought at $51.7256
862019-Mar-4 Mon 12:56 PMSold at $50.39-2.5829.169
872019-Mar-11 Mon 11:36 AMBought at $51.0023
882019-Mar-18 Mon 1:14 PMSold at $53.29694.49913.67
回复 鲜花 鸡蛋

使用道具 举报

 楼主| 发表于 2019-3-31 07:50 AM | 显示全部楼层
RSI交易是单一指数模型,MACD交易也是单一指数模型。 它们各自独立,没有线性关联,那么,二者就可以组合,产生新一代模型。

新模型简单到只有一句话就可以描述:只在牛市做多。

这里的“牛市”是MACD算出来的bullish,从上叉开始,到下叉结束,也就是上表中单数开始,双数结束。

这里的“做多”是指RSI算出来的oversold状态买进,overbought卖出。

新模型得出的交易记录如下:(单数买进,双数卖出)

SNDate TimeActCurrent PriceRealized rateNet Profit Rate
12018-Mar-7 Wed 12:15 PMBought at $48.36
22018-Mar-8 Thu 11:17 AMSold at $50.364.1364.136
32018-Mar-12 Mon 11:01 AMBought at $51.5977
42018-Mar-12 Mon 3:41 PMSold at $51.95810.69864.834
52018-Mar-16 Fri 2:02 PMBought at $51.92
62018-Mar-16 Fri 2:48 PMSold at $52.080.30825.142
72018-Mar-19 Mon 9:50 AMBought at $50.76
82018-Mar-19 Mon 10:29 AMSold at $50.6836-0.15044.992
92018-Mar-22 Thu 9:39 AMBought at $48.8717
102018-Mar-22 Thu 10:39 AMSold at $48.7055-0.34014.652
112018-Apr-2 Mon 11:14 AMBought at $44.9967
122018-Apr-2 Mon 2:10 PMSold at $43.96-2.3042.348
132018-Apr-6 Fri 1:59 PMBought at $45.44
142018-Apr-12 Thu 9:51 AMSold at $46.40162.1164.464
152018-Apr-19 Thu 9:40 AMBought at $49.5991
162018-Apr-19 Thu 11:39 AMSold at $49.282-0.63923.825
172018-Apr-26 Thu 3:34 PMBought at $48.76
182018-Apr-27 Fri 9:36 AMSold at $49.25711.024.845
192018-May-3 Thu 10:42 AMBought at $48.1794
202018-May-3 Thu 10:48 AMSold at $48.40.4585.303
212018-May-15 Tue 9:51 AMBought at $52.5588
222018-May-15 Tue 11:15 AMSold at $52.1145-0.84534.457
232018-May-21 Mon 3:40 PMBought at $54.056
242018-May-22 Tue 3:03 PMSold at $53.9147-0.26144.196
252018-May-25 Fri 2:51 PMBought at $54.1839
262018-May-29 Tue 9:31 AMSold at $52.8724-2.421.775
272018-Jun-1 Fri 12:37 PMBought at $52.9529
282018-Jun-4 Mon 9:55 AMSold at $53.49421.0222.798
292018-Jun-7 Thu 1:45 PMBought at $53.9839
302018-Jun-7 Thu 2:56 PMSold at $54.681.2894.087
312018-Jun-11 Mon 2:48 PMBought at $55.2143
322018-Jun-11 Mon 3:44 PMSold at $55.480.48124.568
332018-Jun-12 Tue 2:47 PMBought at $55.32
342018-Jun-12 Tue 3:44 PMSold at $55.560.43385.002
352018-Jun-27 Wed 12:47 PMBought at $50.7336
362018-Jun-27 Wed 1:09 PMSold at $50.3316-0.79244.21
372018-Jul-2 Mon 9:38 AMBought at $49.984
382018-Jul-2 Mon 2:04 PMSold at $50.2280.48814.698
392018-Jul-3 Tue 12:58 PMBought at $50.6
402018-Jul-6 Fri 9:48 AMSold at $51.92072.617.308
412018-Jul-10 Tue 1:52 PMBought at $54.1084
422018-Jul-11 Wed 1:31 PMSold at $53.9445-0.30287.005
432018-Aug-1 Wed 11:59 AMBought at $55.12
442018-Aug-1 Wed 12:58 PMSold at $55.240.21777.223
452018-Aug-2 Thu 9:44 AMBought at $54.3588
462018-Aug-2 Thu 10:51 AMSold at $54.50820.27477.497
472018-Aug-9 Thu 3:40 PMBought at $57.96
482018-Aug-10 Fri 9:40 AMSold at $56.5348-2.4595.038
492018-Aug-17 Fri 12:41 PMBought at $55.7915
502018-Aug-17 Fri 2:11 PMSold at $56.120.58875.627
512018-Aug-23 Thu 11:28 AMBought at $56.679
522018-Aug-23 Thu 12:03 PMSold at $56.5688-0.19445.433
532018-Sep-11 Tue 9:37 AMBought at $55.3553
542018-Sep-11 Tue 10:21 AMSold at $56.00771.1786.611
552018-Sep-17 Mon 1:17 PMBought at $57.9592
562018-Sep-18 Tue 9:32 AMSold at $57.8095-0.25836.353
572018-Sep-19 Wed 3:05 PMBought at $58.4698
582018-Sep-20 Thu 9:47 AMSold at $59.17981.2147.567
592018-Oct-2 Tue 3:24 PMBought at $59.0159
602018-Oct-2 Tue 4:00 PMSold at $59.250.39677.964
612018-Oct-3 Wed 2:43 PMBought at $59.2669
622018-Oct-3 Wed 2:55 PMSold at $59.1706-0.16257.801
632018-Oct-10 Wed 9:44 AMBought at $55.4493
642018-Oct-10 Wed 9:48 AMSold at $55.2822-0.30137.5
652018-Oct-17 Wed 10:51 AMBought at $51.3744
662018-Oct-17 Wed 3:01 PMSold at $52.55242.2939.793
672018-Oct-19 Fri 12:43 PMBought at $50.54
682018-Oct-22 Mon 12:21 PMSold at $50.1676-0.73689.056
692018-Oct-23 Tue 9:42 AMBought at $48.7584
702018-Oct-23 Tue 9:58 AMSold at $48.77570.035329.092
712018-Oct-29 Mon 12:17 PMBought at $47.5282
722018-Nov-1 Thu 11:33 AMSold at $48.993.07612.17
732018-Nov-2 Fri 10:46 AMBought at $49.0891
742018-Nov-5 Mon 2:00 PMSold at $49.30050.430612.6
752018-Nov-5 Mon 3:39 PMBought at $49.318
762018-Nov-6 Tue 10:01 AMSold at $49.70180.778113.38
772018-Nov-6 Tue 2:37 PMBought at $49.8964
782018-Nov-7 Wed 9:52 AMSold at $51.35252.91816.29
792018-Nov-7 Wed 1:58 PMBought at $51.6076
802018-Nov-7 Wed 2:42 PMSold at $51.870.508616.8
812018-Nov-9 Fri 10:21 AMBought at $51.2654
822018-Nov-9 Fri 12:19 PMSold at $50.9884-0.540316.26
832018-Nov-20 Tue 9:39 AMBought at $47.1697
842018-Nov-20 Tue 9:55 AMSold at $46.79-0.80515.46
852018-Nov-26 Mon 12:50 PMBought at $48.2097
862018-Nov-28 Wed 12:13 PMSold at $49.71053.11318.57
872018-Nov-30 Fri 9:38 AMBought at $49.3364
882018-Nov-30 Fri 10:48 AMSold at $50.04151.42920
892018-Dec-4 Tue 12:37 PMBought at $49.4088
902018-Dec-4 Tue 1:26 PMSold at $48.8986-1.03318.97
912018-Dec-12 Wed 2:15 PMBought at $46.7598
922018-Dec-17 Mon 12:41 PMSold at $45.4061-2.89516.07
932018-Dec-19 Wed 3:09 PMBought at $44.2423
942018-Dec-20 Thu 11:15 AMSold at $44.0316-0.476315.6
952018-Dec-28 Fri 10:40 AMBought at $41.0321
962018-Dec-28 Fri 12:49 PMSold at $41.46171.04716.64
972018-Dec-31 Mon 1:57 PMBought at $42.1155
982019-Jan-2 Wed 1:15 PMSold at $42.71781.4318.07
992019-Jan-3 Thu 10:48 AMBought at $41.5994
1002019-Jan-3 Thu 11:52 AMSold at $42.48072.11920.19
1012019-Jan-7 Mon 2:02 PMBought at $44.1679
1022019-Jan-7 Mon 3:31 PMSold at $44.28320.26120.45
1032019-Jan-10 Thu 9:51 AMBought at $44.5327
1042019-Jan-11 Fri 10:16 AMSold at $45.37451.8922.34
1052019-Jan-14 Mon 9:39 AMBought at $45.5791
1062019-Jan-14 Mon 1:48 PMSold at $46.19481.35123.69
1072019-Jan-16 Wed 10:49 AMBought at $46.9493
1082019-Jan-16 Wed 3:21 PMSold at $46.95440.0109123.7
1092019-Jan-28 Mon 9:54 AMBought at $45.67
1102019-Jan-28 Mon 1:05 PMSold at $45.58-0.197123.51
1112019-Jan-29 Tue 11:07 AMBought at $45.9376
1122019-Jan-29 Tue 12:04 PMSold at $46.3920.98924.5
1132019-Jan-29 Tue 2:31 PMBought at $46.2866
1142019-Jan-31 Thu 10:22 AMSold at $47.87233.42627.92
1152019-Jan-31 Thu 12:33 PMBought at $47.6793
1162019-Feb-1 Fri 9:37 AMSold at $48.32471.35429.28
1172019-Feb-4 Mon 2:20 PMBought at $49.0955
1182019-Feb-4 Mon 3:01 PMSold at $49.23360.281429.56
1192019-Feb-6 Wed 10:18 AMBought at $49.3718
1202019-Feb-6 Wed 2:49 PMSold at $49.71040.685930.24
1212019-Feb-13 Wed 11:39 AMBought at $49.6058
1222019-Feb-13 Wed 1:34 PMSold at $49.87580.544330.79
1232019-Feb-14 Thu 9:42 AMBought at $49.1176
1242019-Feb-14 Thu 10:03 AMSold at $49.13370.032730.82
1252019-Feb-20 Wed 9:29 AMBought at $50.31
1262019-Feb-20 Wed 11:33 AMSold at $50.640.655931.48
1272019-Feb-21 Thu 2:59 PMBought at $50.6393
1282019-Feb-25 Mon 9:49 AMSold at $52.583.83235.31
1292019-Feb-26 Tue 9:39 AMBought at $50.93
1302019-Feb-26 Tue 9:57 AMSold at $51.140.412335.72
1312019-Mar-4 Mon 11:58 AMBought at $50.87
1322019-Mar-4 Mon 12:57 PMSold at $50.39-0.943634.78
1332019-Mar-14 Thu 2:05 PMBought at $52.5224
1342019-Mar-15 Fri 9:58 AMSold at $53.2941.46936.25
1352019-Mar-15 Fri 2:10 PMBought at $53.39
1362019-Mar-15 Fri 2:53 PMSold at $53.50420.213936.46
1372019-Mar-18 Mon 11:41 AMBought at $52.8502
1382019-Mar-18 Mon 1:08 PMSold at $53.26030.77637.24
总共69次买卖,其中,47次获利,22次亏损, 
Probability Of Winning: 68.12%,
Profit Factor: 2.95385,
Maximum Drawdown: 4.40%
Total Net Profit Rate: 37.24% in 280 days,
Average year return: 33.51%
回复 鲜花 鸡蛋

使用道具 举报

 楼主| 发表于 2019-3-31 08:10 AM | 显示全部楼层
本帖最后由 xinxing 于 2019-3-31 08:16 AM 编辑

把上表中最后一列作图。相结合的好处是稳定盈利。“稳定”不是瞎说,是用“峰谷跌幅”定量化过的描述。
netProfitIn280daysSince2018-Feb-20.png

点评

恭喜,思路终于正确了。如果这个系统操作 SPY, QQQ, 或他们的2X ETF 是什么样的收益。SVXY 大约是最危险的ETN了,看看XIV在2/5/2018发生了什么就知道了。哪怕对了99次,只要有一次黑天鹅没很躲开,就外婆了  发表于 2019-3-31 07:05 PM
回复 鲜花 鸡蛋

使用道具 举报

 楼主| 发表于 2019-3-31 10:30 AM | 显示全部楼层
同一时间段,股票价格走势 与 净利润曲线 的 对比:下跌时期能正常盈利。
同一时间段股票价格走势vs净利润曲线.png
回复 鲜花 鸡蛋

使用道具 举报

 楼主| 发表于 2019-4-1 08:06 AM | 显示全部楼层
mikeqc 恭喜,思路终于正确了。如果这个系统操作 SPY, QQQ, 或他们的2X ETF 是什么样的收益。SVXY 大约是最危险的ETN了,看看XIV在2/5/2018发生了什么就知道了。哪怕对了99次,只要有一次黑天鹅没很躲开,就外婆了 发表于 2019-3-31 07:05 PM

黑天鹅有先兆(见图),之前两天,也就是Feb 2,MACD就已经下叉到熊的一侧了,所以安全。

很感兴趣让这个系统操作 SPY, QQQ,你有这几年SPY和QQQ一分钟 (1-minute interval)数据吗?私信、电邮给我,我可以让这个系统计算结果。

SVXY_flash_crash.png
回复 鲜花 鸡蛋

使用道具 举报

发表于 2019-4-1 11:18 AM | 显示全部楼层
xinxing 发表于 2019-3-31 08:10 AM
把上表中最后一列作图。相结合的好处是稳定盈利。“稳定”不是瞎说,是用“峰谷跌幅”定量化过的描述。

回复 鲜花 鸡蛋

使用道具 举报

 楼主| 发表于 2019-4-7 07:08 PM | 显示全部楼层
svca 发表于 2019-4-1 11:18 AM
谢谢习仲勋 的鼓励
回复 鲜花 鸡蛋

使用道具 举报

 楼主| 发表于 2019-4-7 09:36 PM | 显示全部楼层
比如,从11/8/2018 接连5天下降。下降期,RSI多次触及 over-sold 状态,如果,接连买进,势必造成亏损,如何避免?您是如何利用RSI指标进行交易的?
下降势中,RSI多次触及OverSold 状态,如果,接连买进,势必造成亏损,如何避免?.png
回复 鲜花 鸡蛋

使用道具 举报

 楼主| 发表于 2019-4-12 07:38 AM | 显示全部楼层
技术分析支持自昨天开始至少一周的上涨(from 4/11 to 4/18). 在超卖状态购买任何下跌都是安全的。
回复 鲜花 鸡蛋

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

手机版|小黑屋|www.hutong9.net

GMT-5, 2024-11-23 10:30 AM , Processed in 0.118579 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表