这次有两个版本,第一个版本是一天只执行一次买卖交易。第二个版本是一天执行多次交易,最后汇总收益情况。第一版检验过应该没问题,第二版检验了一次没问题,还不敢确定是否完全没问题。代码应该还可以优化很多,先实现功能,后期有需要再优化吧。
excel格式看我上一篇文章。地址:http://blog.csdn.net/renhanchi/article/details/73436644
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
import xlrd
import matplotlib.pyplot as plt
currency = str(raw_input("Please enter currency type: "))
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 "Ops, you enter a wrong type"
exit()
# 设置停止线
sl1 = float(raw_input("Please enter first stop line: "))
sl2 = float(raw_input("Please enter second stop line: "))
if sl2 <= sl1:
print "Ops, you stop line 2 should greater than stop line 1"
exit()
sl3 = float(raw_input("Please enter third stop line: "))
if sl3 <= sl2:
print "Ops, you stop line 3 should greater than stop line 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]
#table_day = data_day.sheet_by_name(u'currency') # load sheet by name
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
# f2 = table_day.cell(row, 8).value
f3 = table_day.cell(row, 9).value
Bsetup = Low - f1 * (High - Close)
Ssetup = High + f1 * (Close - Low)
# Benter = (1 + f2) / 2 * (High + Low) - f2 * High
# Senter = (1 + f2) / 2 * (High + Low) - f2 * 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): # i的作用是读取过一行分钟表格数据后,就抛弃这一行,再不读取。
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: # 因为当天的Break是在天表格中上一天基础上算出来的
# 所以如果天表格没有上一天,分钟表格走一天。
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 ">>>>>Start to deal with BUY POINT.\n"
for i in range(len(date_high)):
print "num:%d date: [%s]" %(i+1, date_high[i])
print " buy price: [%f]" %buy_price[i]
print " buy break: [%f]" %buy_break[i]
print " stop line: [%f]" %buy_sl[i]
print " sell price: [%f]" %buy_stop[i]
print " profit: [%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 = ('buy point profit'), 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 ">>>>>Start to deal with SELL POINT.\n"
for i in range(len(date_low)):
print "num:%d date: [%s]" %(i+1, date_low[i])
print " sell price: [%f]" %sell_price[i]
print " sell break: [%f]" %sell_break[i]
print " stop line: [%f]" %sell_sl[i]
print " buy price: [%f]" %sell_stop[i]
print " profit: [%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 = ('sell point profit'), 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("Please enter currency type: "))
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 "Ops, you enter a wrong type"
exit()
sl1 = float(raw_input("Please enter first stop line: "))
sl2 = float(raw_input("Please enter second stop line: "))
if sl2 <= sl1:
print "Ops, your stop line 2 should greater than stop line 1"
exit()
sl3 = float(raw_input("Please enter third stop line: "))
if sl3 <= sl2:
print "Ops, your stop line 3 should greater than stop line 2"
exit()
sl4 = float(raw_input("Please enter forth stop line: "))
if sl4 <= sl3:
print "Ops, your stop line 4 should greater than stop line 3"
exit()
sl5 = float(raw_input("Please enter fifth stop line: "))
if sl5 <= sl4:
print "Ops, your stop line 5 should greater than stop line 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]
#table_day = data_day.sheet_by_name(u'currency') # load sheet by name
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
# f2 = table_day.cell(row, 8).value
f3 = table_day.cell(row, 9).value
Bsetup = Low - f1 * (High - Close)
Ssetup = High + f1 * (Close - Low)
# Benter = (1 + f2) / 2 * (High + Low) - f2 * High
# Senter = (1 + f2) / 2 * (High + Low) - f2 * 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
# 改变了策略,当天进行多次买卖交易。并且停止线不再以Break和Sbeak为基准,
# 而是以当前买价或者卖价为基准。
while True:
if (curr_high - Bbreak >= 0) and bool_b[0]: #超过Bbreak,买。
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: #只要当前指数降到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 ">>>>>BUY POINT.\n"
for i in range(len(date_high)):
print "num:%d date: [%s]" %(i+1, date_high[i])
print " profit: [%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 = ('buy point profit'), 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 ">>>>>SELL POINT.\n"
for i in range(len(date_low)):
print "num:%d date: [%s]" %(i+1, date_low[i])
print " profit: [%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 = ('sell point profit'), 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("Please enter currency type: "))
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 "Ops, you enter a wrong type"
exit()
gap = float(raw_input("Please enter a gap: "))
sl1 = float(raw_input("Please enter first stop line: "))
sl2 = float(raw_input("Please enter second stop line: "))
if sl2 <= sl1:
print "Ops, your stop line 2 should greater than stop line 1"
exit()
sl3 = float(raw_input("Please enter third stop line: "))
if sl3 <= sl2:
print "Ops, your stop line 3 should greater than stop line 2"
exit()
sl4 = float(raw_input("Please enter forth stop line: "))
if sl4 <= sl3:
print "Ops, your stop line 4 should greater than stop line 3"
exit()
sl5 = float(raw_input("Please enter fifth stop line: "))
if sl5 <= sl4:
print "Ops, your stop line 5 should greater than stop line 4"
exit()
sl6 = float(raw_input("Please enter fifth stop line: "))
if sl6 <= sl5:
print "Ops, your stop line 5 should greater than stop line 5"
exit()
sl7 = float(raw_input("Please enter fifth stop line: "))
if sl7 <= sl6:
print "Ops, your stop line 5 should greater than stop line 6"
exit()
sl8 = float(raw_input("Please enter fifth stop line: "))
if sl8 <= sl7:
print "Ops, your stop line 5 should greater than stop line 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]
#table_day = data_day.sheet_by_name(u'currency') # load sheet by name
nrows_day = table_day.nrows
data_hour = xlrd.open_workbook('./data/jpy_min/jpy_2015_min.xlsx')
#data_hour = xlrd.open_workbook('./data/jpy_May_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
# f2 = table_day.cell(row, 8).value
f3 = table_day.cell(row, 9).value
Bsetup = Low - f1 * (High - Close)
Ssetup = High + f1 * (Close - Low)
# Benter = (1 + f2) / 2 * (High + Low) - f2 * High
# Senter = (1 + f2) / 2 * (High + Low) - f2 * 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 ">>>>>BUY POINT.\n"
for i in range(len(date_high)):
print "num:%d date: [%s]" %(i+1, date_high[i])
print " profit: [%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 = ('buy point profit'), width = 0.3, align = 'center')
plt.xticks(tuple(x_high), tuple(date_high))
plt.legend()
plt.grid()
plt.show()
plt.savefig('pic/buy_point_profit.jpg')
if len(profit_low) != 0:
print ">>>>>SELL POINT.\n"
for i in range(len(date_low)):
print "num:%d date: [%s]" %(i+1, date_low[i])
print " profit: [%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 = ('sell point profit'), width = 0.3, align = 'center')
plt.xticks(tuple(x_low), tuple(date_low))
plt.legend()
plt.grid()
plt.show()
plt.savefig('pic/sell_point_profit.jpg')
plt.figure()
x = [len(profit_high), len(profit_low)]
y = ['Long', 'Short']
plt.pie(x, labels = y, autopct = '%1.2f%%', colors = ('r', 'b'))
plt.show()
plt.savefig('pic/pie_chart.jpg')
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(['', 'Total', 'Buy', 'Sell'])
table.padding_width = 1
table.add_row(['Total Profit', total_high+total_low, total_high, total_low])
table.add_row(['Sample Time', total_day, total_day_high, total_day_low])
table.add_row(['Total Nums', total_num_high+total_num_low, total_num_high, total_num_low])
table.add_row(['Avg Day Nums', avg_day_num, '', ''])
print table