/
Control_interface.py
164 lines (139 loc) · 7.52 KB
/
Control_interface.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
from pyalgotrade.barfeed import yahoofeed
from pyalgotrade.technical import ma
from pyalgotrade.tools import yahoofinance
from pyalgotrade import plotter
import subprocess
from pyalgotrade.technical import bollinger
import sys, os, time, datetime, re
import operator
from old_strategy import MyStrategy
import old_strategy
import sendMail
import fund_stockquote
def run_strategy(smaPeriod, rsiPeriod, instrument, amount, plot):
# Load the yahoo feed from the CSV file
try:
feed = yahoofinance.build_feed([instrument], 2009, 2015, "/home/pwu/stock/database")
except:
print "can't download..."
pass
else:
# Evaluate the strategy with the feed's bars.
myStrategy = MyStrategy(feed, instrument, smaPeriod, rsiPeriod, amount)
if plot:
plt = plotter.StrategyPlotter(myStrategy, True, True, True)
plt.getInstrumentSubplot(instrument).addDataSeries("upper", myStrategy.getBollingerBands().getUpperBand())
plt.getInstrumentSubplot(instrument).addDataSeries("middle", myStrategy.getBollingerBands().getMiddleBand())
plt.getInstrumentSubplot(instrument).addDataSeries("lower", myStrategy.getBollingerBands().getLowerBand())
myStrategy.run()
if plot:
plt.plot()
# mainRoutine
investLen = 28
rsiLen = 15
#companyNameSign = "cyou"
amount = 5000
# whether the overall state has changed?
state = 0
#earning_stock = {}
#buying_stock = {}
list_name = 'nasdaq.list'
#list_name = 'core.list'
#line = "cyou"
g_changing_range = 0.05
prv_time = time.time()
initial_flag = False
email_file="/home/pwu/stock/trading_result.txt"
margin_seed = 1.3
if (sys.argv[1] != "pwu" and sys.argv[1] != "auto"):
companyNameSign = sys.argv[1]
print "RM the old data file first\n"
os.system("rm -rf %s-*"%(companyNameSign))
run_strategy(investLen, rsiLen, companyNameSign, amount, True)
elif (sys.argv[1] == "auto"):
while True:
cur_time = time.time()
cur_hour = datetime.datetime.today().hour
if(cur_hour != 20 and initial_flag == True):
#print cur_time, "\n"
#print prv_time, "\n"
print cur_hour, "\n"
#print cur_time - prv_time, "\n"
#time.sleep(1800)
else:
prv_time = cur_time
os.system("rm -rf database/*")
of = open(list_name, 'r')
lines = of.readlines()
for line in lines:
print '\n', line, '\n'
run_strategy(investLen, rsiLen, line.strip().lower(), amount, False)
#analyze the result
# envaluate yesterday's result
pf_result_file = 'pf_result.txt'
fpf = open(pf_result_file, 'w+')
fh = open('trading_result.txt', 'r+')
lines = fh.readlines()
for line in lines:
print line
if(re.match( r'^#', line, re.M|re.I) or re.match( r'^\s+', line, re.M|re.I)):
pass
else:
ticker, yes_price, yes_average = line.split()
print 'ticker = ', ticker, '\n'
print 'yes_price = ', yes_price, '\n'
print 'yes_average = ', yes_average, '\n'
if(float(fund_stockquote.get_price(ticker)) > float(yes_price)):
fpf.write('%s predict good :)\n'%(ticker))
else:
fpf.write('%s predict wrong :(\n'%(ticker))
fh.close()
fpf.close()
#--------------------------------------------
sorted_trading_value = sorted(old_strategy.earning_stock.iteritems(), key=operator.itemgetter(1))
sorted_buying_value = sorted(old_strategy.buying_stock.iteritems(), key=operator.itemgetter(1))
#print old_strategy.earning_stock
#print sorted_value
ts = datetime.date.today()
history_choice = 'trading_%s'%(ts)
os.system('cp %s history/%s'%(email_file, history_choice))
f1 = open('trading_result.txt', 'w+')
for entry in sorted_buying_value:
if(fund_stockquote.eps_threshold(entry[0]) > 0 and (g_changing_range < (float(fund_stockquote.get_todays_high(entry[0])) - float(fund_stockquote.get_todays_low(entry[0])))/float(fund_stockquote.get_price(entry[0]))) and (float(fund_stockquote.get_volume(entry[0])) > 1.5*float(fund_stockquote.get_average_volume(entry[0]))) and (fund_stockquote.get_volume(entry[0]) > 1000000)):
print entry[0]
# f1.write('%s %s %s'%(str(entry[0]), fund_stockquote.get_price(entry[0]), str(entry[-1])))
# f1.write('\n')
f1.write("# Above average curve and results | only work on bull market\n")
#f1.write('\n')
# condition: (0.03 >= (float(fund_stockquote.get_todays_high(entry[0])) - float(fund_stockquote.get_todays_low(entry[0])))/float(fund_stockquote.get_price(entry[0]))) and float(fund_stockquote.get_ebitd(entry[0])) > 0.12)
for entry in sorted_trading_value:
if(fund_stockquote.eps_threshold(entry[0]) > 0 and (((float(fund_stockquote.get_volume(entry[0])) > 1.5*float(fund_stockquote.get_average_volume(entry[0]))) and (g_changing_range < (float(fund_stockquote.get_todays_high(entry[0])) - float(fund_stockquote.get_price(entry[0])))/float(fund_stockquote.get_price(entry[0]))))) and (fund_stockquote.get_average_volume(entry[0]) > 1000000) and (float(fund_stockquote.get_ebitd(entry[0])) > 0.12)):
print entry[0]
f1.write('%s %s %s'%(str(entry[0]), fund_stockquote.get_price(entry[0]),str(entry[-1])))
f1.write('\n')
# f1.write("# Above average curve and slowly getting up | only work on bull market\n")
for entry in sorted_trading_value:
if(fund_stockquote.eps_threshold(entry[0]) > 0 and ((float(fund_stockquote.get_volume(entry[0])) > float(fund_stockquote.get_average_volume(entry[0]))) and (0.05 > (float(fund_stockquote.get_todays_high(entry[0])) - float(fund_stockquote.get_todays_low(entry[0])))/float(fund_stockquote.get_price(entry[0]))) and float(fund_stockquote.get_ebitd(entry[0])) > 0.17) and (fund_stockquote.get_volume(entry[0]) > 500000) and (float(fund_stockquote.get_ebitd(entry[0])) > 0.12)):
print entry[0]
# f1.write('%s %s %s'%(str(entry[0]), fund_stockquote.get_price(entry[0]),str(entry[-1])))
# f1.write('\n')
f1.close()
#print earning_stock
initial_flag = True
if(datetime.datetime.today().weekday() != 6):
sendMail.sendToGmail(email_file)
sendMail.sendToGmail(pf_result_file)
time.sleep(1800)
else:
os.system("rm -rf database/*")
of = open(list_name, 'r')
lines = of.readlines()
for line in lines:
print '\n', line, '\n'
run_strategy(investLen, rsiLen, line.strip().lower(), amount, False)
#analyze the result
sorted_value = sorted(earning_stock.iteritems(), key=operator.itemgetter(1))
f1 = open('trading_result.txt', 'w+')
for entry in sorted_value:
f1.write(str(entry))
f1.write('\n')