hans

hans

【ファイナンス】【Python】外国為替バックテストR-break ---- 【二】売買戦略


前言#

今回は 2 つのバージョンがあります。最初のバージョンは 1 日に 1 回だけ売買を実行します。2 つ目のバージョンは 1 日に複数回取引を行い、最終的に収益状況を集計します。最初のバージョンは検証済みで問題ないはずですが、2 つ目のバージョンは 1 回検証しただけで、完全に問題がないとは言えません。コードはまだ多くの最適化が可能ですが、まずは機能を実現し、後で必要に応じて最適化します。

Excel 形式については前回の記事を参照してください。アドレス: here

第一版コード:#

#!/usr/bin/env python2
# -*- coding: utf-8 -*-

import xlrd
import matplotlib.pyplot as plt

currency = str(raw_input("通貨の種類を入力してください: "))
if currency == "eur":
    cur_index = 0
elif currency == "xau":
    cur_index = 1
elif currency == "GBP":
    cur_index = 2
elif currency == "jpy":
    cur_index = 3
elif currency == "cad":
    cur_index = 4
else:
    print "オプス、間違ったタイプを入力しました"
    exit()

# 停止ラインを設定
sl1 = float(raw_input("最初の停止ラインを入力してください: "))
sl2 = float(raw_input("2番目の停止ラインを入力してください: "))
if sl2 <= sl1:
    print "オプス、停止ライン2は停止ライン1より大きくする必要があります"
    exit()
sl3 = float(raw_input("3番目の停止ラインを入力してください: "))
if sl3 <= sl2:
    print "オプス、停止ライン3は停止ライン2より大きくする必要があります"
    exit()

charge = 0.0005 # 手数料
profit_high = []
profit_low = []
date_high = []
date_low = []
buy_price = []
sell_price = []
buy_sl = []
sell_sl = []
buy_break = []
sell_break = []
buy_stop = []
sell_stop = []

data_day = xlrd.open_workbook('./data/data_day.xlsx')
table_day = data_day.sheets()[cur_index]
nrows_day = table_day.nrows

data_hour = xlrd.open_workbook('./data/jpy_5_min.xlsx')
table_hour = data_hour.sheets()[cur_index]
nrows_hour = table_hour.nrows

def getdate(table, row):
    date = xlrd.xldate_as_tuple(table.cell(row, 2).value, 0)
    short_date = date[0:3]
    return short_date

def getbenchmark(row):
    Close = table_day.cell(row, 6).value
    Low = table_day.cell(row, 5).value
    High = table_day.cell(row, 4).value
    f1 = table_day.cell(row, 7).value
    f3 = table_day.cell(row, 9).value
    Bsetup = Low - f1 * (High - Close)
    Ssetup = High + f1 * (Close - Low)
    Bbreak = Ssetup + f3 * (Ssetup - Bsetup)
    Sbreak = Bsetup - f3 * (Ssetup - Bsetup)
    return Bbreak, Sbreak

i = 0
for r_day in range(nrows_day-1, 1, -1): 
    temp_high = 0
    buy_high = 0
    temp_low = 0
    sell_low = 0
    bool_b = [True, True, True, True, True] # 5つのブールスイッチを設定。停止ラインの更新を制御します。
    bool_s = [True, True, True, True, True]
    short_date_day = getdate(table_day,r_day) 

    for r_hour in range(nrows_hour-1-i, 1, -1): 
        short_date_hour = getdate(table_hour, r_hour)

        if (short_date_day[0] > short_date_hour[0]): 
            continue
        elif (short_date_day[0] == short_date_hour[0]) and \
        (short_date_day[1] > short_date_hour[1]):
            continue
        elif (short_date_day[0] == short_date_hour[0]) and \
        (short_date_day[1] == short_date_hour[1]) and \
        (short_date_day[2] > short_date_hour[2]):
            continue
        elif (short_date_day[0] == short_date_hour[0]) and \
        (short_date_day[1] == short_date_hour[1]) and \
        (short_date_day[2] == short_date_hour[2]):
            i += 1
            if r_day+1 >= nrows_day: 
                continue
            else:
                Bbreak, Sbreak = getbenchmark(r_day+1)

                curr_high = table_hour.cell(r_hour, 4).value
                curr_low = table_hour.cell(r_hour, 5).value

                while bool_b[4]:
                    if (curr_high - Bbreak >= sl3) and bool_b[3]: 
                        if buy_high == 0:
                            buy_high = curr_high
                            temp_high = curr_high
                        else:
                            temp_high = Bbreak + sl3
                        bool_b[0:4] = [False, False, False,  False] 
                        break
                    elif (curr_high - Bbreak >= sl2) and bool_b[2]:
                        if buy_high == 0:
                            buy_high = curr_high
                            temp_high = curr_high
                        else:
                            temp_high = Bbreak + sl2
                        bool_b[0:3] = [False, False, False]
                        break
                    elif (curr_high - Bbreak >= sl1) and bool_b[1]:
                        if buy_high == 0:
                            buy_high = curr_high
                            temp_high = curr_high
                        else:
                            temp_high = Bbreak + sl1
                        bool_b[0:2] = [False, False]
                        break
                    elif (curr_high - Bbreak >= 0) and bool_b[0]:
                        if buy_high == 0:
                            buy_high = curr_high
                            temp_high = curr_high
                        bool_b[0] = False
                        break
                    elif temp_high != 0 and (curr_high - temp_high <= charge):
                        last_high = table_hour.cell(r_hour+1,4).value 
                        if curr_high < last_high: 
                            profit_high.append(curr_high - buy_high)
                            date_high.append('%s/%s' %(short_date_hour[1], \
                                                       short_date_hour[2]))
                            buy_price.append(buy_high)
                            buy_sl.append(temp_high)
                            buy_break.append(Bbreak)
                            buy_stop.append(curr_high)
                            bool_b[4] = False 
                            break
                        else:
                            break
                    else:
                        break
                

                while bool_s[4]:
                    if (Sbreak - curr_low >= sl3) and bool_s[3]:
                        if sell_low == 0:
                            sell_low = curr_low
                            temp_low = curr_low
                        else:
                            temp_low = Sbreak - sl3
                        bool_s[0:4] = [False, False, False,  False]
                        break
                    elif (Sbreak - curr_low >= sl2) and bool_s[2]:
                        if sell_low == 0:
                            sell_low = curr_low
                            temp_low = curr_low
                        else:
                            temp_low = Sbreak - sl2
                        bool_s[0:3] = [False, False, False]
                        break
                    elif (Sbreak - curr_low >= sl1) and bool_s[1]:
                        if sell_low == 0:
                            sell_low = curr_low
                            temp_low = curr_low
                        else:
                            temp_low = Sbreak - sl1
                        bool_s[0:2] = [False, False]
                        break
                    elif (Sbreak - curr_low >= 0) and bool_s[0]:
                        if sell_low == 0:
                            sell_low = curr_low
                            temp_low = curr_low
                        bool_s[0] = False
                        break
                    elif temp_low != 0 and (temp_low - curr_low <= charge):
                        last_low = table_hour.cell(r_hour+1,5).value
                        if last_low < curr_low:
                            profit_low.append(sell_low - curr_low)
                            date_low.append('%s/%s' %(short_date_hour[1], \
                                                      short_date_hour[2]))
                            sell_price.append(sell_low)
                            sell_sl.append(temp_low)
                            sell_break.append(Sbreak)
                            sell_stop.append(curr_low)
                            bool_s[4] = False
                            break
                        else:
                            break
                    else:
                        break
                
        else:
            break
        
def getxaxis(profit):
    x = []
    for i in range(len(profit)):
        x.append(i)
    return x

if len(profit_high) !=0 :
    print "------------------------------------------------"
    print ">>>>>買いポイントの処理を開始します。\n"
    for i in range(len(date_high)):
        print "番号:%d  日付:       [%s]" %(i+1, date_high[i])
        print "       買い価格:  [%f]"  %buy_price[i]
        print "       買いブレーク:  [%f]"  %buy_break[i]
        print "       停止ライン:  [%f]"  %buy_sl[i]
        print "       売り価格: [%f]"  %buy_stop[i]
        print "       利益:     [%f]"  %profit_high[i]
        print "------------------------------------------------"
    plt.figure()
    x_high = getxaxis(profit_high)
    high_hist = plt.bar(tuple(x_high), tuple(profit_high), color = ('red'), \
                        label = ('買いポイントの利益'), width = 0.3, align = 'center')
    plt.xticks(tuple(x_high), tuple(date_high))
    plt.legend()
    plt.grid()
    plt.show()

if len(profit_low) != 0:
    print ">>>>>売りポイントの処理を開始します。\n"
    for i in range(len(date_low)):
        print "番号:%d  日付:       [%s]" %(i+1, date_low[i])
        print "       売り価格: [%f]"  %sell_price[i]
        print "       売りブレーク: [%f]"  %sell_break[i]
        print "       停止ライン:  [%f]"  %sell_sl[i]
        print "       買い価格:  [%f]"  %sell_stop[i]
        print "       利益:     [%f]"  %profit_low[i]
        print "------------------------------------------------"
    plt.figure()
    x_low = getxaxis(profit_low)
    low_hist = plt.bar(tuple(x_low), tuple(profit_low), color = ('green'), \
                       label = ('売りポイントの利益'), width = 0.3, align = 'center')
    plt.xticks(tuple(x_low), tuple(date_low))
    plt.legend()
    plt.grid()
    plt.show()

第二版コード:#

#!/usr/bin/env python2
# -*- coding: utf-8 -*-

import xlrd
import matplotlib.pyplot as plt

currency = str(raw_input("通貨の種類を入力してください: "))
if currency == "eur":
    cur_index = 0
elif currency == "xau":
    cur_index = 1
elif currency == "GBP":
    cur_index = 2
elif currency == "jpy":
    cur_index = 3
elif currency == "cad":
    cur_index = 4
else:
    print "オプス、間違ったタイプを入力しました"
    exit()

sl1 = float(raw_input("最初の停止ラインを入力してください: "))
sl2 = float(raw_input("2番目の停止ラインを入力してください: "))
if sl2 <= sl1:
    print "オプス、停止ライン2は停止ライン1より大きくする必要があります"
    exit()
sl3 = float(raw_input("3番目の停止ラインを入力してください: "))
if sl3 <= sl2:
    print "オプス、停止ライン3は停止ライン2より大きくする必要があります"
    exit()
sl4 = float(raw_input("4番目の停止ラインを入力してください: "))
if sl4 <= sl3:
    print "オプス、停止ライン4は停止ライン3より大きくする必要があります"
    exit()
sl5 = float(raw_input("5番目の停止ラインを入力してください: "))
if sl5 <= sl4:
    print "オプス、停止ライン5は停止ライン4より大きくする必要があります"
    exit()

charge = 0.01
profit_high = []
profit_low = []
date_high = []
date_low = []

data_day = xlrd.open_workbook('./data/data_day.xlsx')
table_day = data_day.sheets()[cur_index]
nrows_day = table_day.nrows

data_hour = xlrd.open_workbook('./data/jpy_5_min.xlsx')
table_hour = data_hour.sheets()[cur_index]
nrows_hour = table_hour.nrows

def getdate(table, row):
    date = xlrd.xldate_as_tuple(table.cell(row, 2).value, 0)
    short_date = date[0:3]
    return short_date

def getbenchmark(row):
    Close = table_day.cell(row, 6).value
    Low = table_day.cell(row, 5).value
    High = table_day.cell(row, 4).value
    f1 = table_day.cell(row, 7).value
    f3 = table_day.cell(row, 9).value
    Bsetup = Low - f1 * (High - Close)
    Ssetup = High + f1 * (Close - Low)
    Bbreak = Ssetup + f3 * (Ssetup - Bsetup)
    Sbreak = Bsetup - f3 * (Ssetup - Bsetup)
    return Bbreak, Sbreak

def addProfit(profit):
    result = 0
    for i in profit:
        result += float(i)
    return result

i = 0
for r_day in range(nrows_day-1, 1, -1):
    temp_high = 0
    buy_high = 0
    temp_low = 0
    sell_low = 0
    curr_profit_high = []
    curr_profit_low = []
    bool_high = True
    bool_low = True
    bool_b = [True, True, True, True, True, True]
    bool_s = [True, True, True, True, True, True]
    short_date_day = getdate(table_day,r_day)

    for r_hour in range(nrows_hour-1-i, 1, -1):
        short_date_hour = getdate(table_hour, r_hour)

        if (short_date_day[0] > short_date_hour[0]):
            continue
        elif (short_date_day[0] == short_date_hour[0]) and \
        (short_date_day[1] > short_date_hour[1]):
            continue
        elif (short_date_day[0] == short_date_hour[0]) and \
        (short_date_day[1] == short_date_hour[1]) and \
        (short_date_day[2] > short_date_hour[2]):
            continue
        elif (short_date_day[0] == short_date_hour[0]) and \
        (short_date_day[1] == short_date_hour[1]) and \
        (short_date_day[2] == short_date_hour[2]):
            i += 1
            if r_day+1 >= nrows_day:
                continue
            else:
                Bbreak, Sbreak = getbenchmark(r_day+1)

                curr_high = table_hour.cell(r_hour, 4).value
                curr_low = table_hour.cell(r_hour, 5).value
                while True:
                    if (curr_high - Bbreak >= 0) and bool_b[0]: 
                        buy_high = curr_high
                        temp_high = curr_high
                        bool_b[0] = False
                        break
                    if (curr_high - buy_high >= sl1) and bool_b[1]:
                        if buy_high == 0:
                            break
                        temp_high = buy_high + sl1
                        bool_b[1] = [False]
                    if (curr_high - buy_high >= sl2) and bool_b[2]:
                        if buy_high == 0:
                            break
                        temp_high = buy_high + sl2
                        bool_b[2] = [False]
                    if (curr_high - buy_high >= sl3) and bool_b[3]:
                        if buy_high == 0:
                            break
                        temp_high = buy_high + sl3
                        bool_b[3] = [False]
                    if (curr_high - buy_high >= sl4) and bool_b[4]:
                        if buy_high == 0:
                            break
                        temp_high = buy_high + sl4
                        bool_b[4] = [False]
                    if (curr_high - buy_high >= sl5) and bool_b[5]:
                        if buy_high == 0:
                            break
                        temp_high = buy_high + sl5
                        bool_b[5] = [False]
                        
                    if buy_high != 0 and curr_high - temp_high <= charge:
                        last_high = table_hour.cell(r_hour+1,4).value
                        if curr_high < last_high and bool_high: 
                            curr_profit_high.append(curr_high - buy_high)
                            bool_b = [False, False, False, False, False, False]
                            bool_high = False
                        if curr_high <= Bbreak: 
                            buy_high = 0
                            temp_high = 0
                            bool_b = [True, True, True, True, True, True]
                            bool_high = True
                    break

                while True:
                    if (Sbreak - curr_low >= 0) and bool_s[0]:
                        sell_low = curr_low
                        temp_low = curr_low
                        bool_s[0] = False
                        break
                    if (sell_low - curr_low >= sl1) and bool_s[1]:
                        if sell_low == 0:
                            break
                        temp_low = sell_low - sl1
                        bool_s[1] = False
                    if (sell_low - curr_low >= sl2) and bool_s[2]:
                        if sell_low == 0:
                            break
                        temp_low = sell_low - sl2
                        bool_s[2] = False
                    if (sell_low - curr_low >= sl3) and bool_s[3]:
                        if sell_low == 0:
                            break
                        temp_low = sell_low - sl3
                        bool_s[3] = False
                    if (sell_low - curr_low >= sl4) and bool_s[4]:
                        if sell_low == 0:
                            break
                        temp_low = sell_low - sl4
                        bool_s[4] = False
                    if (sell_low - curr_low >= sl5) and bool_s[5]:
                        if sell_low == 0:
                            break
                        temp_low = sell_low - sl5
                        bool_s[5] = False

                    if sell_low != 0 and temp_low - curr_low <= charge:
                        last_low = table_hour.cell(r_hour+1, 5).value
                        if last_low < curr_low and bool_low:
                            curr_profit_low.append(sell_low - curr_low)
                            bool_s = [False, False, False, False, False, False]
                            bool_low = False
                        if curr_low >= Sbreak:
                            sell_low = 0
                            temp_low = 0
                            bool_s = [True, True, True, True, True, True]
                            bool_low = True
                    break
                
        else:
            break
    if len(curr_profit_high) != 0:
        profit_high.append(addProfit(curr_profit_high)) 
        date_high.append('%s/%s' %(short_date_day[1], short_date_day[2]))
    if len(curr_profit_low) != 0:
        profit_low.append(addProfit(curr_profit_low))
        date_low.append('%s/%s' %(short_date_hour[1], short_date_hour[2]))

def getxaxis(profit):
    x = []
    for i in range(len(profit)):
        x.append(i)
    return x

if len(profit_high) !=0 :
    print "------------------------------------------------"
    print ">>>>>買いポイント。\n"
    for i in range(len(date_high)):
        print "番号:%d  日付:       [%s]" %(i+1, date_high[i])
        print "       利益:     [%f]"  %profit_high[i]
        print "------------------------------------------------"
    plt.figure()
    x_high = getxaxis(profit_high)
    high_hist = plt.bar(tuple(x_high), tuple(profit_high), color = ('red'), \
                        label = ('買いポイントの利益'), width = 0.3, align = 'center')
    plt.xticks(tuple(x_high), tuple(date_high))
    plt.legend()
    plt.grid()
    plt.show()

if len(profit_low) != 0:
    print ">>>>>売りポイント。\n"
    for i in range(len(date_low)):
        print "番号:%d  日付:       [%s]" %(i+1, date_low[i])
        print "       利益:     [%f]"  %profit_low[i]
        print "------------------------------------------------"
    plt.figure()
    x_low = getxaxis(profit_low)
    low_hist = plt.bar(tuple(x_low), tuple(profit_low), color = ('green'), \
                       label = ('売りポイントの利益'), width = 0.3, align = 'center')
    plt.xticks(tuple(x_low), tuple(date_low))
    plt.legend()
    plt.grid()
    plt.show()

第三版コード:#

#!/usr/bin/env python2
# -*- coding: utf-8 -*-

import xlrd
import matplotlib.pyplot as plt
from prettytable import PrettyTable

currency = str(raw_input("通貨の種類を入力してください: "))
if currency == "eur":
    cur_index = 0
elif currency == "xau":
    cur_index = 1
elif currency == "GBP":
    cur_index = 2
elif currency == "jpy":
    cur_index = 3
elif currency == "cad":
    cur_index = 4
else:
    print "オプス、間違ったタイプを入力しました"
    exit()

gap = float(raw_input("ギャップを入力してください: "))

sl1 = float(raw_input("最初の停止ラインを入力してください: "))
sl2 = float(raw_input("2番目の停止ラインを入力してください: "))
if sl2 <= sl1:
    print "オプス、停止ライン2は停止ライン1より大きくする必要があります"
    exit()
sl3 = float(raw_input("3番目の停止ラインを入力してください: "))
if sl3 <= sl2:
    print "オプス、停止ライン3は停止ライン2より大きくする必要があります"
    exit()
sl4 = float(raw_input("4番目の停止ラインを入力してください: "))
if sl4 <= sl3:
    print "オプス、停止ライン4は停止ライン3より大きくする必要があります"
    exit()
sl5 = float(raw_input("5番目の停止ラインを入力してください: "))
if sl5 <= sl4:
    print "オプス、停止ライン5は停止ライン4より大きくする必要があります"
    exit()
sl6 = float(raw_input("6番目の停止ラインを入力してください: "))
if sl6 <= sl5:
    print "オプス、停止ライン6は停止ライン5より大きくする必要があります"
    exit()
sl7 = float(raw_input("7番目の停止ラインを入力してください: "))
if sl7 <= sl6:
    print "オプス、停止ライン7は停止ライン6より大きくする必要があります"
    exit()
sl8 = float(raw_input("8番目の停止ラインを入力してください: "))
if sl8 <= sl7:
    print "オプス、停止ライン8は停止ライン7より大きくする必要があります"
    exit()

charge = 0
profit_high = []
profit_low = []
date_high = []
date_low = []
total_day = 1
total_day_high = 0
total_day_low = 0
total_num_high = 0
total_num_low = 0

data_day = xlrd.open_workbook('./data/data_day.xlsx')
table_day = data_day.sheets()[cur_index]
nrows_day = table_day.nrows

data_hour = xlrd.open_workbook('./data/jpy_min/jpy_2015_min.xlsx')
table_hour = data_hour.sheets()[cur_index]
nrows_hour = table_hour.nrows

def getdate(table, row):
    date = xlrd.xldate_as_tuple(table.cell(row, 2).value, 0)
    short_date = date[0:3]
    return short_date

def getbenchmark(row):
    Close = table_day.cell(row, 6).value
    Low = table_day.cell(row, 5).value
    High = table_day.cell(row, 4).value
    f1 = table_day.cell(row, 7).value
    f3 = table_day.cell(row, 9).value
    Bsetup = Low - f1 * (High - Close)
    Ssetup = High + f1 * (Close - Low)
    Bbreak = Ssetup + f3 * (Ssetup - Bsetup)
    Sbreak = Bsetup - f3 * (Ssetup - Bsetup)
    return Bbreak, Sbreak

def addProfit(profit):
    result = 0
    for i in profit:
        result += float(i)
    return result

i = 0
for r_day in range(nrows_day-1, 1, -1):
    temp_high = 0
    buy_high = 0
    temp_low = 0
    sell_low = 0
    curr_profit_high = []
    curr_profit_low = []
    bool_high = True
    bool_low = True
    bool_b = [True, True, True, True, True, True, True, True, True]
    bool_s = [True, True, True, True, True, True, True, True, True]
    short_date_day = getdate(table_day,r_day)

    for r_hour in range(nrows_hour-1-i, 1, -1):
        short_date_hour = getdate(table_hour, r_hour)

        if (short_date_day[0] > short_date_hour[0]):
            i += 1
            continue
        elif (short_date_day[0] == short_date_hour[0]) and \
        (short_date_day[1] > short_date_hour[1]):
            i += 1
            continue
        elif (short_date_day[0] == short_date_hour[0]) and \
        (short_date_day[1] == short_date_hour[1]) and \
        (short_date_day[2] > short_date_hour[2]):
            i += 1
            continue
        elif (short_date_day[0] == short_date_hour[0]) and \
        (short_date_day[1] == short_date_hour[1]) and \
        (short_date_day[2] == short_date_hour[2]):
            i += 1
            if r_day+1 >= nrows_day:
                continue
            else:
                Bbreak, Sbreak = getbenchmark(r_day+1)

                curr_high = table_hour.cell(r_hour, 4).value
                curr_low = table_hour.cell(r_hour, 5).value

                while True:
                    if r_hour+1 >= nrows_hour:
                        break
                    last_high = table_hour.cell(r_hour+1,4).value
                    if (curr_high - Bbreak >= 0) and (curr_high - last_high < \
                    gap) and bool_b[0]:
                        total_num_high += 1
                        buy_high = curr_high
                        temp_high = curr_high
                        bool_b[0] = False
                        break
                    if (curr_high - buy_high >= sl1) and bool_b[1]:
                        if buy_high == 0:
                            break
                        temp_high = buy_high + sl1
                        bool_b[1] = [False]
                    if (curr_high - buy_high >= sl2) and bool_b[2]:
                        if buy_high == 0:
                            break
                        temp_high = buy_high + sl2
                        bool_b[2] = [False]
                    if (curr_high - buy_high >= sl3) and bool_b[3]:
                        if buy_high == 0:
                            break
                        temp_high = buy_high + sl3
                        bool_b[3] = [False]
                    if (curr_high - buy_high >= sl4) and bool_b[4]:
                        if buy_high == 0:
                            break
                        temp_high = buy_high + sl4
                        bool_b[4] = [False]
                    if (curr_high - buy_high >= sl5) and bool_b[5]:
                        if buy_high == 0:
                            break
                        temp_high = buy_high + sl5
                        bool_b[5] = [False]
                    if (curr_high - buy_high >= sl6) and bool_b[6]:
                        if buy_high == 0:
                            break
                        temp_high = buy_high + sl6
                        bool_b[6] = [False]
                    if (curr_high - buy_high >= sl7) and bool_b[7]:
                        if buy_high == 0:
                            break
                        temp_high = buy_high + sl7
                        bool_b[7] = [False]
                    if (curr_high - buy_high >= sl8) and bool_b[8]:
                        if buy_high == 0:
                            break
                        temp_high = buy_high + sl8
                        bool_b[8] = [False]
                        
                    if buy_high != 0 and curr_high - temp_high <= charge:
                        if curr_high < last_high and bool_high:
                            curr_profit_high.append(curr_high - buy_high)
                            bool_b = [False, False, False, False, False, False,\
                                      False, False, False]
                            bool_high = False
                        if curr_high <= Bbreak:
                            buy_high = 0
                            temp_high = 0
                            bool_b = [True, True, True, True, True, True,\
                                      True, True, True]
                            bool_high = True
                    break

                while True:
                    if r_hour+1 >= nrows_hour:
                        break
                    last_low = table_hour.cell(r_hour+1, 5).value
                    if (Sbreak - curr_low >= 0) and (last_low - curr_low < gap) \
                       and bool_s[0]:
                        total_num_low += 1
                        sell_low = curr_low
                        temp_low = curr_low
                        bool_s[0] = False
                        break
                    if (sell_low - curr_low >= sl1) and bool_s[1]:
                        if sell_low == 0:
                            break
                        temp_low = sell_low - sl1
                        bool_s[1] = False
                    if (sell_low - curr_low >= sl2) and bool_s[2]:
                        if sell_low == 0:
                            break
                        temp_low = sell_low - sl2
                        bool_s[2] = False
                    if (sell_low - curr_low >= sl3) and bool_s[3]:
                        if sell_low == 0:
                            break
                        temp_low = sell_low - sl3
                        bool_s[3] = False
                    if (sell_low - curr_low >= sl4) and bool_s[4]:
                        if sell_low == 0:
                            break
                        temp_low = sell_low - sl4
                        bool_s[4] = False
                    if (sell_low - curr_low >= sl5) and bool_s[5]:
                        if sell_low == 0:
                            break
                        temp_low = sell_low - sl5
                        bool_s[5] = False
                    if (sell_low - curr_low >= sl6) and bool_s[6]:
                        if sell_low == 0:
                            break
                        temp_low = sell_low - sl6
                        bool_s[6] = False
                    if (sell_low - curr_low >= sl7) and bool_s[7]:
                        if sell_low == 0:
                            break
                        temp_low = sell_low - sl7
                        bool_s[7] = False
                    if (sell_low - curr_low >= sl8) and bool_s[8]:
                        if sell_low == 0:
                            break
                        temp_low = sell_low - sl8
                        bool_s[8] = False

                    if sell_low != 0 and temp_low - curr_low <= charge:
                        if last_low < curr_low and bool_low:
                            curr_profit_low.append(sell_low - curr_low)
                            bool_s = [False, False, False, False, False, False,\
                                      False, False, False]
                            bool_low = False
                        if curr_low >= Sbreak:
                            sell_low = 0
                            temp_low = 0
                            bool_s = [True, True, True, True, True, True,\
                                      True, True, True]
                            bool_low = True
                    break
                
        else:
            break
    if len(curr_profit_high) != 0:
        total_day_high += 1
        profit_high.append(addProfit(curr_profit_high))
        date_high.append('%s/%s' %(short_date_day[1], short_date_day[2]))
    if len(curr_profit_low) != 0:
        total_day_low += 1
        profit_low.append(addProfit(curr_profit_low))
        date_low.append('%s/%s' %(short_date_hour[1], short_date_hour[2]))

def getxaxis(profit):
    x = []
    for i in range(len(profit)):
        x.append(i)
    return x

if len(profit_high) !=0 :
    print "------------------------------------------------"
    print ">>>>>買いポイント。\n"
    for i in range(len(date_high)):
        print "番号:%d  日付:       [%s]" %(i+1, date_high[i])
        print "       利益:     [%f]"  %profit_high[i]
        print "------------------------------------------------"
    plt.figure()
    x_high = getxaxis(profit_high)
    high_hist = plt.bar(tuple(x_high), tuple(profit_high), color = ('red'), \
                        label = ('買いポイントの利益'), width = 0.3, align = 'center')
    plt.xticks(tuple(x_high), tuple(date_high))
    plt.legend()
    plt.grid()
    plt.show()

if len(profit_low) != 0:
    print ">>>>>売りポイント。\n"
    for i in range(len(date_low)):
        print "番号:%d  日付:       [%s]" %(i+1, date_low[i])
        print "       利益:     [%f]"  %profit_low[i]
        print "------------------------------------------------"
    plt.figure()
    x_low = getxaxis(profit_low)
    low_hist = plt.bar(tuple(x_low), tuple(profit_low), color = ('green'), \
                       label = ('売りポイントの利益'), width = 0.3, align = 'center')
    plt.xticks(tuple(x_low), tuple(date_low))
    plt.legend()
    plt.grid()
    plt.show()
    
plt.figure()
x = [len(profit_high), len(profit_low)]
y = ['ロング', 'ショート']
plt.pie(x, labels = y, autopct = '%1.2f%%', colors = ('r', 'b'))
plt.show()

total_high = 0
total_low = 0
for i in range(len(profit_high)):
    total_high += profit_high[i]
for i in range(len(profit_low)):
    total_low += profit_low[i]

avg_day_num = (total_num_high + total_num_low) / total_day
table = PrettyTable(['', '合計', '買い', '売り'])
table.padding_width = 1
table.add_row(['総利益', total_high+total_low, total_high, total_low])
table.add_row(['サンプル時間', total_day, total_day_high, total_day_low])
table.add_row(['総数', total_num_high+total_num_low, total_num_high, total_num_low])
table.add_row(['平均日数', avg_day_num, '', ''])
print table
読み込み中...
文章は、創作者によって署名され、ブロックチェーンに安全に保存されています。