コード例 #1
0
ファイル: sc.py プロジェクト: hsun/python-collection
 def load_signal(self, signal, signal_path, results):
     raw = self.site.fetch(signal_path)
     soup = BeautifulSoup(raw)
     logged = 0
     try:
         rows = soup.findAll("td", {"class" : "first chartoptionlinks"})
         
         print("Processing signal %s" % (signal))
         for row in rows:
             
             fields = row.parent.findAll("td")
             symbol = fields[1].b.string
             company = fields[2].string
             exchange = fields[3].string
             sector = fields[4].string
             price = float(fields[9].string)
             volume = float(fields[10].string)
             # skip symbols like GM.V
             if self.shall_process(symbol, exchange, sector, price, volume):
                 say("Found: %s - %s" % (symbol, company))
                 self.process_signal(symbol, signal, results)
                 logged = logged + 1
                 
         print "%d symbols processed with %d logged" % (len(rows), logged)
     except Exception, e:
         print "Failed processing signal %s at path %s: %s" % (signal, signal_path, e)
コード例 #2
0
ファイル: mainTreeAI.py プロジェクト: Lcbx/PythonOOSnake
	def think(self, board):
		
		self.paths.clear()
		
		for decision in direction.ALL:
			
			decisionValue, newBoard = board.potentialEndturn(decision)
			decisionValue *= WIDTH
			
			debug.say(str(decision) +" : " + str(decisionValue))
			
			#if decisionValue > CUTOFF:
			for _ in xrange(WIDTH):
				testBoard  = newBoard.clone()
				exp = 1
				for depth in range(1, DEPTH):
					exp *= len(direction.ALL)
					possibility = random.choice(direction.ALL)
					value = testBoard.endTurnDecision(possibility)
					addedValue = float(value) / float(exp)
					decisionValue += addedValue
					debug.say("it " + str(_) + ", depth " + str(depth) + " : val " + str(value) + ", addedVal " + str(addedValue) + ", decisionVal " + str(decisionValue))
					#debug.say("investigate " + str(possibility) + " : " + str(value) + " -> " + str(decisionValue))
					#if value <= CUTOFF: break
				#decisionValue= float(decisionValue) / float(WIDTH)
			
			self.paths[decision] = decisionValue #+  random.randint(0, 3) # randomize so at equal score different decision can be taken
コード例 #3
0
ファイル: sc.py プロジェクト: hsun/python-collection
 def load_signals(self, signals, results):
     for signal in signals:
         if self.signal_paths.has_key(signal):
             signal_path = self.signal_paths[signal]
             self.load_signal(signal, signal_path, results)
         else:
             say("Skip signal %s as we cannot find the URL path to the signal page." % (signal))
コード例 #4
0
ファイル: backtest.py プロジェクト: hsun/python-collection
 def is_a_trade(historic_quotes, position):
     start_day_close = _close(historic_quotes[0])
     next_day_open = _open(historic_quotes[1])
     
     if position == LONG:
         say("LONG: %.2f %.2f" % (start_day_close, next_day_open))
         return next_day_open >= start_day_close and is_in_trade_range(start_day_close, next_day_open)
     else:
         say("SHORT: %.2f %.2f" % (start_day_close, next_day_open))
         return next_day_open <= start_day_close and is_in_trade_range(start_day_close, next_day_open)
コード例 #5
0
ファイル: eval_mad.py プロジェクト: hsun/python-collection
 def evaluate(self, symbol, meta, potentials):
     try:
         if symbol in potentials:
             print "Mentioned by Mad: %s" % (symbol)
             meta[symbol]['eval'].append("Mentioned by mad")
             meta[symbol]['weight'] = meta[symbol]['weight'] + self.WEIGHT
         else:
             say("Skip symbol %s for missing from Mad." % (symbol))
     except Exception, e:
         print "Failed to evaluate Mad index for symbol %s: %s" % (symbol, e)
コード例 #6
0
ファイル: mainTreeAI.py プロジェクト: Lcbx/PythonOOSnake
	def getDecision(self):
		self.decision = None
		if self.paths:
			#for i in xrange(len(self.paths)):
			#	debug.say("end : " + str(self.paths.keys()[i]) + " -> " +str(self.paths.values()[i]))
			list = sorted(self.paths, key = self.paths.get)
			self.decision = list[-1]
			
		debug.say("chosen : " + str(self.decision))
		return self.decision
コード例 #7
0
ファイル: backtest.py プロジェクト: hsun/python-collection
    def load_csv_file(self, file_name):
        data = {}
        f = "%s/%s" % (self.data_dir, file_name)
        if not os.path.isfile(f):
            raise Exception("File %s does not exist." % file_name)

        say("Load data from file %s" % (f))
        reader = csv.reader(open(f, "r"))
        for row in reader:
            data[row[0]] = (row[1:])
        return data
コード例 #8
0
ファイル: mainMonteCarlo.py プロジェクト: Lcbx/PythonOOSnake
 def investigate(self):
     depth = 0
     node = self
     path = []
     while depth < DEPTH:
         depth += 1
         possibility = random.choice(direction.ALL)
         node = node._investigate(possibility)
         path.append(possibility)
     debug.say(" -> ".join(map(str, path)) + " : " + str(node.value))
     return node
コード例 #9
0
ファイル: siteutil.py プロジェクト: hsun/python-collection
    def post(self, path, data):

        self.__delay()
        headers = {"User-Agent" : Site.USER_AGENT, "Referer" : self.refer}
        self.conn.request("POST", path, data, headers)
        resp = self.conn.getresponse()
        if 200 == resp.status:
            data = None
            data = resp.read()
            say("Fetched content from path: %s --> %s (%s)" % (path, resp.status, resp.reason))
            return data
        else:
            say("Failed to fetch content from path: %s --> %s (%s)" % (path, resp.status, resp.reason))
            return None
コード例 #10
0
ファイル: mainMonteCarlo.py プロジェクト: Lcbx/PythonOOSnake
    def think(self, board):
        decision = None
        root = Node(0, board)

        debug.say("")

        for _ in xrange(WIDTH):
            root.investigate()

        if root.children:
            paths = {}
            for possibility, node in root.children.items():
                value = node.getValue()
                paths[possibility] = value
                debug.say(str(possibility) + " : " + str(value))
            list = sorted(paths, key=paths.get)
            decision = list[-1]

        debug.say("chosen : " + str(decision))
        self.decision = decision
コード例 #11
0
ファイル: backtest.py プロジェクト: hsun/python-collection
 def is_in_trade_range(close_price, open_price):
     delta = abs((close_price - open_price) / open_price)
     say("Delta is %.2f" % delta)
     return delta <= TRADE_RANGE
コード例 #12
0
ファイル: backtest.py プロジェクト: hsun/python-collection
    def measure_success(self, candidates, start_date, end_date, position):
        
        def register_profit(profit, signals):
            for signal in signals:
                total_profit = self.profits.setdefault(signal, 0.0)
                self.profits[signal] = (total_profit + profit)

                if profit > 0.0:
                    self.losers.setdefault(signal, 0)
                    count = self.winners.setdefault(signal, 0)
                    self.winners[signal] = (count + 1)
                else:
                    self.winners.setdefault(signal, 0)
                    count = self.losers.setdefault(signal, 0)
                    self.losers[signal] = (count + 1)

        def is_in_trade_range(close_price, open_price):
            delta = abs((close_price - open_price) / open_price)
            say("Delta is %.2f" % delta)
            return delta <= TRADE_RANGE

        def get_my_signals(signals, position):
            if position == LONG:
                my_signals = list(BULL_SIGNALS.intersection(signals))
                if len(my_signals) > 0:
                    return my_signals
            else:
                my_signals = list(BEAR_SIGNALS.intersection(signals))
                if len(my_signals) > 0:
                    return my_signals
            return None
        
        # historic quotes
        def is_a_trade(historic_quotes, position):
            start_day_close = _close(historic_quotes[0])
            next_day_open = _open(historic_quotes[1])
            
            if position == LONG:
                say("LONG: %.2f %.2f" % (start_day_close, next_day_open))
                return next_day_open >= start_day_close and is_in_trade_range(start_day_close, next_day_open)
            else:
                say("SHORT: %.2f %.2f" % (start_day_close, next_day_open))
                return next_day_open <= start_day_close and is_in_trade_range(start_day_close, next_day_open)
 
        def get_profit(historic_quotes, position):
            acquired_price = _open(historic_quotes[1])
            exit_price = acquired_price
            previous_day_low = low(historic_quotes[0])
            previous_day_high = high(historic_quotes[0])
            for quote in historic_quotes[1:]:
                if position == LONG:
                    today_low = low(quote)
                    if today_low < (previous_day_low - 0.01):
                        exit_price = previous_day_low
                        break
                    else:
                        previous_day_low = today_low
                        exit_price = _close(quote)
                else:
                    today_high = high(quote)
                    if today_high > (previous_day_high + 0.01):
                        exit_price = previous_day_high
                        break
                    else:
                        previous_day_high = today_high
                        exit_price = _close(quote)
            if position == LONG:
                return (exit_price - acquired_price) / acquired_price
            else:
                return (acquired_price - exit_price) / acquired_price
        
        for symbol, signals in candidates.items():
            
            my_signals = get_my_signals(signals, position)
            if my_signals is None:
                say("Skip symbol %s for non-interested signals %s " % (symbol, signals))
                continue
            
            try:
                historic_quotes = yahoo.get_historical_prices(symbol, start_date, end_date)[1:]
                if historic_quotes:
                    if is_a_trade(historic_quotes, position):
                        profit = get_profit(historic_quotes, position)
                        say("Register profit for %s" % symbol)
                        register_profit(profit, my_signals)
                    else:
                        say("Skip symbol %s as it is out trading range " % (symbol))
                else:
                    print "No historic data for %s" % symbol
            except Exception, e:
                print "Failed to evaluate %s due to %s" % (symbol, e)     
コード例 #13
0
ファイル: siteutil.py プロジェクト: hsun/python-collection
 def __init__(self, host, port, delay = 1.0):
     self.conn = httplib.HTTPConnection(host, port)
     say("Established connection to %s:%s" % (host, port))
     self.refer = "http://%s:%s/" % (host, port)
     self.delay = delay
コード例 #14
0
ファイル: mainMonteCarlo.py プロジェクト: Lcbx/PythonOOSnake
import gameloop
import pygame
import direction

import random
import debug

#seed = random.randint(0, 1<<32 -1)
seedInt = 12
debug.say("seed : " + str(seedInt))
random.seed(seedInt)

DEPTH = 6
WIDTH = 100


class Node:
    def __init__(self, value, board):
        self.value = float(value)
        self.board = board
        self.children = {}

    def _investigate(self, decision):
        if decision not in self.children:
            value, board = self.board.potentialEndturn(decision)
            child = Node(value, board)
            self.children[decision] = child
            return child  # a new Node was created
        return self.children[decision]  # found an existing Node

    def getValue(self):