iStock 1.0.3
This package is specially designed for Taiwan stock market.
License: MIT
Requires Python: >=3.7.0

# 使用教學

以下文件將幫助使用者快速上手`iStock`，功能包含**快速建立臺股資料庫**、**金融數據爬蟲**、**即時數據回測**，提供量化交易的最佳Python套件!

```py
__init__.py
data.py
crawler.py
plotting.py
backtest.py
tradingview.py
linebot.py
```

## Demo 
#### 建立資料庫
```py
import iStock

# 自動建立DB & 手動下載雲端數據
iStock.setup()
```

#### 多線程爬蟲
```py
from iStock import crawler

crawler.auto_update()
```

#### 使用資料庫
```py
from iStock import data

# 取得臺股上市櫃收盤價
close = data.get_twstock('收盤價')

# 取得臺股上市櫃外資買賣超股數
foreign_investors = data.get_twstock('外資買賣超股數')

# 取得臺股上市櫃融資餘額增減股數
margin_trading = data.get_twstock('融資餘額增減股數')

# 取得臺股上市櫃當月營收
rev = data.get_twstock('當月營收')

# 取得加密貨幣數據
btc = data.get_crypto_coin('BTCUSDT','1d')
eth = data.get_crypto_coin('ETHUSDT','1d')

# 取得個股開高低收量
# 選擇股票代號(全球股市+加密市場)，如:0050,APPL,BTCUSDT
stock_id = '2330'
ohlcv = data.get_klines(stock_id,'d')
```

#### 取得即時報價
```py
from iStock import data

stock_id = '2330'
current_price = data.get_current_price(stock_id)
```

#### 股市Kline
```py
from iStock import plotting

# 繪製互動式K線圖
plotting.kline_chart(ohlcv,stock_id)

# 繪製靜態式K線圖
plotting.candlestick(ohlcv,stock_id)
```

#### 策略回測
回測分為針對單一商品及多商品組合(portfolio)，以下使用常見的均線策略作範本:

##### **單一商品**
```py
from iStock import data
from iStock import backtest

stock_id = '2330'
ohlcv = data.get_klines(stock_id)

# 建立技術指標
def ma(n):
    return ohlcv.close.rolling(n,min_periods=1).mean()

# 黃金交叉時買進;死亡交叉時賣出
entry = (ma(5)>=ma(20))&(ma(5).shift()<ma(20).shift())
exit = (ma(5)<ma(20))&(ma(5).shift()>=ma(20).shift())

# 虧損達6%時停損
stop_loss = -6

# 開始回測
strategy = backtest.individual_stock(ohlcv,stock_id,entry,exit,stop_loss=stop_loss)
```

##### **多商品組合(portfolio)**
```py
from iStock import data
from iStock import backtest

c = data.get_twstock('收盤價')
o = data.get_twstock('開盤價')
h = data.get_twstock('最高價')
v = data.get_twstock('成交股數')/1000

# 建立技術指標
def ma(n):
    return c.rolling(n,min_periods=1).mean()

def pressure(n):
    return (h/h.shift().rolling(n,min_periods=5).max()-1)*100
    
buy1 = (c>10)&(c<200)&(v.rolling(20).mean()>=100)
buy2 = (pressure(120)>=0)&(pressure(120).shift()<0)
buy3 = c>o*1.03

sell1 = c<ma(5)
sell2 = c<o*0.97

entry = buy1&buy2&buy3
exit = sell1&sell2

freq_days = 10
start_date = '2010-10-26'
end_date = c.index[-1]
stop_loss = -5

# 開始回測
strategy = backtest.portfolio_stocks(entry,exit,freq_days,start_date,end_date,stop_loss=stop_loss)
```

##### **金字塔交易(pyramid)**
```py
from iStock import data
from iStock import backtest
import pandas as pd
import talib

c = data.get_twstock('收盤價')
stock_id = '0050'
ohlcv = data.get_klines(stock_id)

def ma(n):
    return ohlcv.close.rolling(n,min_periods=1).mean()

buy1 = (ma(5)>=ma(20))&(ma(5).shift()<ma(20).shift())
buy2 = (talib.RSI(ohlcv.close)<30)&(talib.RSI(ohlcv.close).shift()>=30)
buy3 = (talib.RSI(ohlcv.close)<20)&(talib.RSI(ohlcv.close).shift()>=20)
buy4 = (talib.RSI(ohlcv.close)<15)&(talib.RSI(ohlcv.close).shift()>=15)

bv1 = 1000
bv2 = 1000
bv3 = 2000
bv4 = 3000

sell1 = (ma(5)<=ma(20))&(ma(5).shift()>ma(20).shift())
sell2 = (talib.RSI(ohlcv.close)>=80)&(talib.RSI(ohlcv.close).shift()<80)
sell3 = (talib.RSI(ohlcv.close)>=85)&(talib.RSI(ohlcv.close).shift()<85)
sell4 = (talib.RSI(ohlcv.close)>=90)&(talib.RSI(ohlcv.close).shift()<90)

sv1 = 2000
sv2 = 1000
sv3 = 2000
sv4 = 3000

entry = pd.DataFrame({
    'GOLD_CROSSOVER':buy1*bv1,
    'RSI<30':buy2*bv2,
    'RSI<20':buy3*bv3,
    'RSI<10':buy4*bv4
})

exit = pd.DataFrame({
    'DEATH_CROSSOVER':sell1*-sv1,
    'RSI>80':sell2*-sv2,
    'RSI>85':sell3*-sv3,
    'RSI>90':sell4*-sv4
})

start_date = c.index[0]
end_date = c.index[-1]
init_asset = 1000000

# 開始回測
strategy = backtest.pyramid_trade(ohlcv,stock_id,entry,exit,init_asset)
```