検証結果のunits.csvをグラフ化し検討する

1. まず基本のパッケージとデータを読み込む

In [1]:
import pandas as pd
import numpy as np
import time
import plotly
import plotly.graph_objs as go
import plotly.figure_factory as ff
plotly.offline.init_notebook_mode(connected=True)

raw = pd.read_csv(
    '/home/ubuntu/workspace/analysis/1711/[171126-4] Units.tsv'
    ,delimiter   = '\t'
    ,index_col   = 'Date'
    ,parse_dates = True
).sort_index()


layout = dict(
    autosize = False
    ,legend  = dict(
        xanchor  = "left"
        ,y = "1.01"
        ,orientation = "h"
    )
    ,margin = go.Margin(l=5 ,r=80 ,b=100 ,t=50)
    ,width  = 1200
    ,height = 900
    ,xaxis = dict(
         title = ""
        ,type  = "Date"
        ,showgrid  = True
        ,tickangle = 45
#         ,tickformat = "%Y-%m"
    )
    ,yaxis = dict(
        domain = [0 ,.65]
        ,title = ""
        ,side  = "right"
#         ,exponentformat = "none"
#         ,separatethousands = False
#         ,tickprefix = ""
#         ,ticksuffix = "万円"
#         ,type = "log"
    )
    ,yaxis2 = dict(
        domain = [.65 ,.95]
        ,title = ""
        ,side  = "right"
#         ,overlaying = "y"
    )
    ,font = dict(
        family = 'Courier New, monospace'
        ,size  = 20
        ,color = '#7f7f7f'
    )
)
/home/ubuntu/.pyenv/versions/anaconda3-4.0.0/lib/python3.5/site-packages/IPython/core/interactiveshell.py:2698: DtypeWarning:

Columns (29,30,31,39,43) have mixed types. Specify dtype option on import or set low_memory=False.

2. データのチェック

In [2]:
raw.columns
Out[2]:
Index(['SymNum', 'Symbol', 'Lot', 'Open', 'High', 'Low', 'Close',
       'Open_forChart', 'High_forChart', 'Low_forChart', 'Close_forChart',
       'Currency', 'ATR20', 'High10', 'Low10', 'High20', 'Low20', 'EMA6',
       'EMA19', 'EMA50', 'EMA100', 'EMA300', 'EMA350', 'Filter_50_300',
       'MACD_6_19', 'MACD2_6_19', 'plus_DI_14', 'minus_DI_14', 'DX', 'ADX_14',
       'EntrySig', 'ExitSig', 'SO_Sig', 'ExitSig_N', 'TimedSig',
       'ReversingEntrySig', 'PrevProfitSig', 'LO2_Sig', 'LO3_Sig', 'LO4_Sig',
       'EntryDate', 'EntryPrice', 'LongShort', 'N_Status', 'Pos', 'NumDays',
       'N_Size', 'N', 'RedRisk', 'YellowRisk', 'PL', 'Comm', 'MTM',
       'ChgPctATR', 'ChgPctN_Value', 'LO2', 'LO3', 'LO4', 'SO', 'ExitPrice',
       'Log', 'TradingPos'],
      dtype='object')

2-1. Lowの確認

In [3]:
raw[raw.High_forChart < raw.Open_forChart]
Out[3]:
SymNum Symbol Lot Open High Low Close Open_forChart High_forChart Low_forChart ... MTM ChgPctATR ChgPctN_Value LO2 LO3 LO4 SO ExitPrice Log TradingPos
Date

0 rows × 62 columns

変だった場合は以下のコードで修正

In [197]:
raw.loc[raw.High_forChart < raw.Open_forChart, "High_forChart"] = raw.loc[raw.High_forChart < raw.Open_forChart, "Open_forChart"]

2-2. Highの確認

In [4]:
raw[raw.Low_forChart > raw.Open_forChart]
Out[4]:
SymNum Symbol Lot Open High Low Close Open_forChart High_forChart Low_forChart ... MTM ChgPctATR ChgPctN_Value LO2 LO3 LO4 SO ExitPrice Log TradingPos
Date

0 rows × 62 columns

変だった場合は以下のコードで修正

In [198]:
raw.loc[raw.Low_forChart > raw.Open_forChart, "Low_forChart"] = raw.loc[raw.Low_forChart > raw.Open_forChart, "Open_forChart"]

3. ループで全銘柄のグラフを出力

In [8]:
start = 11
end   = 16

symNums = sorted(list(raw.drop_duplicates(['SymNum'])['SymNum']))
s       = str(2000 +start) +"/1/1"
e       = str(2000 +end)   +"/12/31"
cnt     = 0

for symNum in symNums:
    cnt = cnt +1

    raw_for = raw[raw.SymNum==symNum][s:e]
    fig_increasing = ff.create_candlestick(
#         raw_for.Open ,raw_for.High ,raw_for.Low ,raw_for.Close ,dates=raw_for.index
        raw_for.Open_forChart ,raw_for.High_forChart ,raw_for.Low_forChart ,raw_for.Close_forChart ,dates=raw_for.index
        ,direction='increasing' ,marker=go.Marker(color='#CCCCCC') ,line=go.Line(color='#CCCCCC',width=1)
    )
    fig_decreasing = ff.create_candlestick(
#         raw_for.Open ,raw_for.High ,raw_for.Low ,raw_for.Close ,dates=raw_for.index
        raw_for.Open_forChart ,raw_for.High_forChart ,raw_for.Low_forChart ,raw_for.Close_forChart ,dates=raw_for.index
        ,direction='decreasing' ,marker=go.Marker(color='#777777') ,line=go.Line(color='#777777',width=1)
    )
    fig = fig_increasing
    fig['data'].extend(fig_decreasing['data'])

    fig['data'].extend([
#          go.Scatter(x=raw_for.index ,y=raw_for.SMA350       ,name= 'SMA350'   ,line=dict(color='#1A237E'))
         go.Scatter(x=raw_for.index ,y=raw_for.EntryPrice    ,name= 'Entry' ,line=dict(color='#1A237E' ,width=1))
#         ,go.Scatter(x=raw_for.index ,y=raw_for.EntryAvgPrice ,name= 'Avg'   ,line=dict(color='#1A237E' ,dash="dot" ,width=1))
        ,go.Scatter(x=raw_for.index ,y=raw_for.LO2           ,name= 'LO2'   ,line=dict(color='#1976D2' ,width=1))
        ,go.Scatter(x=raw_for.index ,y=raw_for.LO3           ,name= 'LO3'   ,line=dict(color='#2196F3' ,width=1))
        ,go.Scatter(x=raw_for.index ,y=raw_for.LO4           ,name= 'LO4'   ,line=dict(color='#29B6F6' ,width=1))
#         ,go.Scatter(x=raw_for.index ,y=raw_for.EMA5          ,name= 'EMA5'  ,line=dict(color='#F44336' ,width=1))
#         ,go.Scatter(x=raw_for.index ,y=raw_for.EMA20         ,name= 'EMA20' ,line=dict(color='#4CAF50' ,width=1))
#         ,go.Scatter(x=raw_for.index ,y=raw_for.EMA40         ,name= 'EMA40' ,line=dict(color='#303F9F' ,width=1) ,fill="tonexty" ,fillcolor="rgba(170,170,170,.2)")
#         ,go.Scatter(x=raw_for.index ,y=raw_for.EMA50        ,name= 'EMA50'   ,line=dict(color='#777777'))
        ,go.Scatter(x=raw_for.index ,y=raw_for.EMA100       ,name= 'EMA100'  ,line=dict(color='#F44336'))
#         ,go.Scatter(x=raw_for.index ,y=raw_for.EMA250       ,name= 'EMA250' ,line=dict(color='#777777'))
        ,go.Scatter(x=raw_for.index ,y=raw_for.EMA350       ,name= 'EMA350' ,line=dict(color='#4CAF50'))
#         ,go.Scatter(x=raw_for.index ,y=raw_for.High20       ,name= 'High20' ,line=dict(color='#2196F3' ,width=1 ,dash="dot"))
#         ,go.Scatter(x=raw_for.index ,y=raw_for.Low20        ,name= 'Low20'  ,line=dict(color='#2196F3' ,width=1 ,dash="dot"))
#         ,go.Scatter(x=raw_for.index ,y=raw_for.High10       ,name= 'High10' ,line=dict(color='#29B6F6' ,width=1 ,dash="dot"))
#         ,go.Scatter(x=raw_for.index ,y=raw_for.Low10        ,name= 'Low10'  ,line=dict(color='#29B6F6' ,width=1 ,dash="dot"))
#         ,go.Scatter(x=raw_for.index ,y=raw_for.SO           ,name= 'SO'        ,line=dict(color='#29B6F6'))
#         ,go.Scatter(x=raw_for.index ,y=raw_for.ExitPrice    ,name= 'ExitPrice' ,line=dict(color='#FF6F00' ,dash="dot"))# ,shape="hv"))
        ,go.Scatter(yaxis="y2" ,x=raw_for.index ,y=raw_for.MTM ,name= '値洗い' ,line=dict(color='#FFD54F' ,width=1 ,shape="hvh") ,fill="tozeroy")
        ,go.Bar(    yaxis="y2" ,x=raw_for.index ,y=raw_for.PL  ,name= '損益'   ,marker=dict(color='#FF6F00'))
    ])

    fig['layout'].update(layout)
    fig['layout'].update(dict(title=raw_for.Symbol[0]))
    
    print("2-"+str(cnt)+". "+raw_for.Symbol[0])
    plotly.offline.iplot(fig)
    time.sleep(0.5) #sleep(秒指定)
2-1. Nikkei225
2-2. DJI