Tick Trading Python
===
Taiwan R User Group / MLDM Monday
@2017-05-09
---
關於我
===
- 生魚片@永豐金證券
---
```flow
st=>start: 資料源
e=>end: $$$
op=>operation: 指標(1..*)
op2=>operation: 計算
cond=>condition: 下單
st->op->op2->cond
cond(yes)->e
cond(no)->op
```
---
# Demo-1 均線操作法
## [黃金交叉]([https://www.google.com.tw/search?q=%E5%85%A9%E6%A2%9D%E5%9D%87%E7%B7%9A&oq=%E5%85%A9%E6%A2%9D%E5%9D%87%E7%B7%9A&aqs=chrome..69i57.3098j0j4&sourceid=chrome&ie=UTF-8)
## 以 5ma 穿越 20ma 為例子
----
## 買進
```python=
cross(ma(c,5),ma(c,20))
```
## 賣出
```python=
cross(ma(c,20),ma(c,5))
```
----
# 技術分析
## 1. 容易上手 <!-- .element: class="fragment" data-fragment-index="1" -->
- Easy Language Like <!-- .element: class="fragment" data-fragment-index="2" -->
- 缺少彈性 <!-- .element: class="fragment" data-fragment-index="3" -->
----
## 2. 指標工具箱
- 提供解決 <!-- .element: class="fragment" data-fragment-index="1" -->
- 黑箱:為何你們的跟OO的數值不一樣 <!-- .element: class="fragment" data-fragment-index="2" -->
- 解決問題 or 理解問題 <!-- .element: class="fragment" data-fragment-index="3" -->
![](https://i.imgur.com/tLkGrAz.jpg =600x400) <!-- .element: class="fragment" data-fragment-index="4" -->
----
所以改用 ![](http://www.sinotrade.com.tw/Images/tradecenter-list-pic_1.png)
===
[2004年12月推出](http://www.sinopac.com/news/release01/news_931207.asp)
[官方操作文件](http://www.sinotrade.com.tw/ec/eleader1/pro-trade.pdf)
## 有甚麼不一樣 <!-- .element: class="fragment" data-fragment-index="1" -->
![](https://i.imgur.com/fkGonRm.jpg)
----
# PYTHON
![](https://i.imgur.com/OC2gq9c.png =600x) <!-- .element: class="fragment" data-fragment-index="1" -->
---
# Demo-2 均線操作法
* 自己的指標自己刻 <!-- .element: class="fragment" data-fragment-index="1" -->
* 無法解決,至少理解 <!-- .element: class="fragment" data-fragment-index="2" -->
----
```python=
def pure_running_mean(data, N):
"""Simply MA using pure python"""
if len(data) <= N:
return
_sum = 0
result = list(0 for x in data)
data = data[::-1]
for i in range(0, N):
_sum = _sum + data[i]
result[i] = _sum/(i+1)
for i in range(N, len(data)):
_sum = _sum - data[i - N] + data[i]
result[i] = _sum / N
return result[::-1][:-N]
```
* c = 收盤價
* c[0] 當下最新
* c[1] 前一個Tick
* c[-1] 最後一筆資料
----
```python=
def pure_cross(list_a, list_b):
two_list = zip(list_a, list_b)
result = list(0 for _ in two_list)
for idx, item in enumerate(two_list[:-1]):
prev_item = two_list[idx + 1]
if prev_item[0] < prev_item[1] and item[0] > item[1]:
result[idx] = 1
else:
result[idx] = 0
return result
```
## 穿越的定義
前一個Tick LineA < LineB
and
現在的Tick LineA > LineB
---
# 當
# 數學方程式 <!-- .element: class="fragment" data-fragment-index="1" -->
# 遇見 <!-- .element: class="fragment" data-fragment-index="2" -->
# 電腦程式 <!-- .element: class="fragment" data-fragment-index="3" -->
----
## c3h3
![](https://i.imgur.com/46jfFOR.png =600x) <!-- .element: class="fragment" data-fragment-index="1" -->
----
# Demo-3 均線操作法
## Numpy <!-- .element: class="fragment" data-fragment-index="1" -->
```python=
import numpy
def np_running_mean(data, N):
cumsum = numpy.cumsum(numpy.insert(data, 0, 0))
return (cumsum[N:] - cumsum[:-N]) / N
```
<!-- .element: class="fragment" data-fragment-index="1" -->
[StackOverflow](https://stackoverflow.com/questions/13728392/moving-average-or-running-mean/27681394#27681394) <!-- .element: class="fragment" data-fragment-index="1" -->
---
# 自己的指標自己刻
# ohlcv <!-- .element: class="fragment" data-fragment-index="1" -->
# 自己的資料自己餵 <!-- .element: class="fragment" data-fragment-index="2" -->
----
我的策略 - Demo4
==
## 只想在沒人賣的時候進場 <!-- .element: class="fragment" data-fragment-index="1" -->
* 委賣筆數為10天內最小的時候進場 <!-- .element: class="fragment" data-fragment-index="2" -->
## 在一堆人賣的時候出場 <!-- .element: class="fragment" data-fragment-index="3" -->
* 委賣筆數為10天內最大的時候出場 <!-- .element: class="fragment" data-fragment-index="4" -->
:secret: 委賣筆數 :secret:
<!-- .element: class="fragment" data-fragment-index="5" -->
----
[證券交易所 - 每5秒委託成交統計](http://www.tse.com.tw/ch/trading/exchange/MI_5MINS/MI_5MINS.php)
江波圖
![](https://i.imgur.com/BBePDCI.png ) <!-- .element: class="fragment" data-fragment-index="1" -->
----
自給自足的資料庫
==
![](https://i.imgur.com/oKgWw6i.png =600x)
----
委賣筆數為N天內最小 進場
==
```python=
def at_low_sell_power(N):
sell_power = load_csv_list('ask')
asklist = map(float, sell_power)
ask = array(0)
ask.data = asklist[:len(c)]
cond = lowest(ask, N) == ask[0]
return cond
```
----
委賣筆數為N天內最大 出場
==
```python=
def at_high_sell_power(N):
sell_power = load_csv_list('ask')
asklist = map(float, sell_power)
ask = array(0)
ask.data = asklist[:len(c)]
cond = highest(ask, N) == ask[0]
return cond
```
----
成果分析
==
![](https://i.imgur.com/IdID9U2.png)
---
資源
==
[Pandas/Numpy on eLeader](http://ypochien.sytes.net:8080/share.cgi?ssid=0Rv4g3F)
## 解壓縮至bin目錄之下
## import 自己的模組,記得reload
```python=
import mypy
reload(mypy)
mypy.at_low_sell_power(10)
```
----
資料不多,加減用
==
畫面看到的皆可用....
## **item.data** is python list(double)
---
下單API
==
Python版本 - Windows
[GitHub - Sinopac Order API](https://github.com/ypochien/Sinopac-Order-API) <!-- .element: class="fragment" data-fragment-index="1" -->
## Live Demo <!-- .element: class="fragment" data-fragment-index="1" -->
---
報價
==
## [Demo-Web](http://alvin.tw/quote)
## Demo-Python <!-- .element: class="fragment" data-fragment-index="1" -->
## MQTT / Web socket <!-- .element: class="fragment" data-fragment-index="2" -->
## 希望未來能開放 Sinopac Quote API <!-- .element: class="fragment" data-fragment-index="3" -->
---
![](https://i.imgur.com/kEGVsjA.png)
==
# 加入我們
## 永豐金證券
---
:clock9::question:
有問題:
[加入LINE群組](http://line.me/ti/g/4b6wkylcz6)
![](https://i.imgur.com/or4eLDB.png =500x)
---
END
{"metaMigratedAt":"2023-06-14T12:13:13.000Z","metaMigratedFrom":"Content","title":"Tick Trading Python","breaks":true,"contributors":"[{\"id\":\"c931cdad-28d3-4748-999c-67579370fed2\",\"add\":6,\"del\":6}]"}