hans

hans

【金融】【Python】外汇回测R-break ---- 【二】买卖策略


前言#

这次有两个版本,第一个版本是一天只执行一次买卖交易。第二个版本是一天执行多次交易,最后汇总收益情况。第一版检验过应该没问题,第二版检验了一次没问题,还不敢确定是否完全没问题。代码应该还可以优化很多,先实现功能,后期有需要再优化吧。

excel 格式看我上一篇文章。地址: 这里

第一版代码:#

#!/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("请输入第二条停止线: "))
if sl2 <= sl1:
    print "错误,你的第二条停止线应该大于第一条停止线"
    exit()
sl3 = float(raw_input("请输入第三条停止线: "))
if sl3 <= sl2:
    print "错误,你的第三条停止线应该大于第二条停止线"
    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个boolean开关。用于控制更新停止线。
    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("请输入第二条停止线: "))
if sl2 <= sl1:
    print "错误,你的第二条停止线应该大于第一条停止线"
    exit()
sl3 = float(raw_input("请输入第三条停止线: "))
if sl3 <= sl2:
    print "错误,你的第三条停止线应该大于第二条停止线"
    exit()
sl4 = float(raw_input("请输入第四条停止线: "))
if sl4 <= sl3:
    print "错误,你的第四条停止线应该大于第三条停止线"
    exit()
sl5 = float(raw_input("请输入第五条停止线: "))
if sl5 <= sl4:
    print "错误,你的第五条停止线应该大于第四条停止线"
    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("请输入第二条停止线: "))
if sl2 <= sl1:
    print "错误,你的第二条停止线应该大于第一条停止线"
    exit()
sl3 = float(raw_input("请输入第三条停止线: "))
if sl3 <= sl2:
    print "错误,你的第三条停止线应该大于第二条停止线"
    exit()
sl4 = float(raw_input("请输入第四条停止线: "))
if sl4 <= sl3:
    print "错误,你的第四条停止线应该大于第三条停止线"
    exit()
sl5 = float(raw_input("请输入第五条停止线: "))
if sl5 <= sl4:
    print "错误,你的第五条停止线应该大于第四条停止线"
    exit()
sl6 = float(raw_input("请输入第六条停止线: "))
if sl6 <= sl5:
    print "错误,你的第六条停止线应该大于第五条停止线"
    exit()
sl7 = float(raw_input("请输入第七条停止线: "))
if sl7 <= sl6:
    print "错误,你的第七条停止线应该大于第六条停止线"
    exit()
sl8 = float(raw_input("请输入第八条停止线: "))
if sl8 <= sl7:
    print "错误,你的第八条停止线应该大于第七条停止线"
    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
加载中...
此文章数据所有权由区块链加密技术和智能合约保障仅归创作者所有。