def setUp(self):
     engine = HexunEngine()
     self.hexun_requester = Requester(engine)
     engine = SinaEngine()
     self.sina_requester = Requester(engine)
     engine = YahooEngine()
     self.yahoo_requester = Requester(engine)
class TestRequester(unittest.TestCase):

    def setUp(self):
        engine = HexunEngine()
        self.hexun_requester = Requester(engine)
        engine = SinaEngine()
        self.sina_requester = Requester(engine)
        engine = YahooEngine()
        self.yahoo_requester = Requester(engine)

    def test_hexun_request(self):
        stock = self.hexun_requester.request('000626')
        self.assertEqual(len(stock), 1)
        self.assertEqual(stock[0].__class__, Stock)

    def test_sina_request(self):
        stock = self.sina_requester.request('002475')
        self.assertEqual(len(stock), 1)
        self.assertEqual(stock[0].__class__, Stock)

    def test_yahoo_request(self):
        stock = self.yahoo_requester.request('002475',
                                             ('2015-03-04', '2015-03-05'))
        self.assertEqual(len(stock), 2)
        self.assertEqual(stock[0].__class__, Stock)
def SelectStockRule(stock):
    """Stock Selection Rule"""
    print stock
    # convert stock code and retrieve historical data
    ticker=cstock_to_yahoo(stock)    
    try:
       
       # From HexunEngine, get today's stock information
       engine = HexunEngine()
       requester=Requester(engine)
       stock_obj=requester.request(stock)
       SD=stock_obj[0].as_dict()
       print "Hexun's date=", SD['date']
       todayVolume=SD['volume']
       #todayPrice=SD['price']
       
       # History data from yahoo      
       df=data.DataReader(ticker, 'yahoo', YAHOO_BEGIN_DAY, YAHOO_END_DAY)
       #print df
       # clean the dataframe
       v=df['Volume']
       cdf=df[v>0]
       volume=cdf['Volume']
       L=len(cdf.index)-1
       #close=cdf['Close']
       print "Yahoo lastest volume date=", cdf.index[-1]
       print "Yahoo beginning volume date=", cdf.index[0]
    except:
        print "Can't get data"
        return False   
    # Analysis the stock data
    try:
       # Intraday data
       condition=(todayVolume>0 and volume[L]> 0) \
                  and (todayVolume / (volume[L]+1.0)<0.4
                  and todayVolume / (volume[L-1]+1.0)<0.99
                  and todayVolume / (volume[L-2]+1.0)<0.99
                  and todayVolume / (volume[L-3]+1.0)<0.99
                  and todayVolume / (volume[L-4]+1.0)<0.99
                  and todayVolume / (volume[L-5]+1.0)<0.99)
                  #and volume[L] /(volume[L-1])<0.1)
         
       # Lowest volume condition                    
       condition1=volume[L]>0 and (volume[L] / (volume[L-1]+1.0))<0.5 \
                   and (volume[L] / (volume[L-2]+1.0))<0.99 \
                   and (volume[L] / (volume[L-3]+1.0))<0.99 \
                   and (volume[L] / (volume[L-4]+1.0))<0.99 \
                   and (volume[L] / (volume[L-5]+1.0))<0.99 \
                   #and (volume[L-5] / (volume[L-6]+1.0))<0.99 
       if condition == True: 
           return stock 
       else:
           return False
    except IndexError:
        return False
示例#4
0
 def setUp(self):
     engine = HexunEngine()
     self.hexun_requester = Requester(engine)
     engine = SinaEngine()
     self.sina_requester = Requester(engine)
     engine = YahooEngine()
     self.yahoo_requester = Requester(engine)
示例#5
0
class TestRequester(unittest.TestCase):
    def setUp(self):
        engine = HexunEngine()
        self.hexun_requester = Requester(engine)
        engine = SinaEngine()
        self.sina_requester = Requester(engine)
        engine = YahooEngine()
        self.yahoo_requester = Requester(engine)

    def test_hexun_request(self):
        stock = self.hexun_requester.request('000626')
        self.assertEqual(len(stock), 1)
        self.assertEqual(stock[0].__class__, Stock)

    def test_sina_request(self):
        stock = self.sina_requester.request('002475')
        self.assertEqual(len(stock), 1)
        self.assertEqual(stock[0].__class__, Stock)

    def test_yahoo_request(self):
        stock = self.yahoo_requester.request('002475',
                                             ('2015-03-04', '2015-03-05'))
        self.assertEqual(len(stock), 2)
        self.assertEqual(stock[0].__class__, Stock)
示例#6
0
import vision

# project library
from cstock.yahoo_engine import YahooEngine
from cstock.request import Requester
from cstock.model import Stock

if __name__ == '__main__':

  try:
    input = open('002415.pkl', 'rb')
    stocks = pickle.load(input)
    input.close()
  except Exception:
    engine = YahooEngine()
    yahoo_requester = Requester(engine)
    stocks = yahoo_requester.request('002415', ('1900-01-01', '2016-08-29'))
    output = open('002415.pkl', 'wb')
    pickle.dump(stocks, output, pickle.HIGHEST_PROTOCOL)
    output.close()

for stock in stocks:
    print(stock)

    # def test_parse(self):
    #     data = ("Date,Open,High,Low,Close,Volume,Adj Close\n" 
    #             "2014-08-22,34.20,34.22,33.49,33.70,2222200,33.70\n"
    #             "2014-08-21,33.81,34.29,33.15,34.21,3544800,34.21")

    #     stocks = self.engine.parse(data, 'foo_id')
    #     self.assertEqual(len(stocks), 2)
示例#7
0
__author__ = 'roy'
from cstock.request import Requester
from cstock.sina_engine import SinaEngine
import json

engine = SinaEngine()
requester = Requester(engine)
stock = requester.request("300327")
data = stock[0].as_dict()
print data
print json.dumps(data, ensure_ascii=False)
print stock[0]
print data['sell1p']
print data['sell1v']
print data['buy1p']
print data['buy1v']