def find_events(symbol): events = [] quotes = Quote.get_quotes(symbol) if len(quotes) < 1: return events quote = quotes[0] stop = None entry_price = None hh50 = False hh20 = False while quote: prev = quote quote = quote.next() if not quote: # no more data if entry_price: #print "Still winning ", entry_price, hh50, hh20 events.append(Event(prev, 'eod', prev.close)) continue if not hh50 and quote.is_above_50_day_high(): #print "Found a hh50 event %s" % quote events.append(Event(quote, 'hh50')) hh50 = True if entry_price == None: (entry_price, stop) = get_entry_price_and_stop(quote) if not hh20 and quote.is_above_20_day_high(): #print "Found a hh20 event %s" % quote events.append(Event(quote, 'hh20')) hh20 = True if entry_price == None: (entry_price, stop) = get_entry_price_and_stop(quote) if(hh20 and quote.low < stop): # hit the stop #print "Found a stop event %s" % quote events.append(Event(quote, 'stop', stop)) stop = None entry_price = None hh50 = None hh20 = None continue if(hh20 and quote.close > entry_price and quote.close < quote.get_indicator().ll_10 ): # exit #print "Found a exit event %s" % quote events.append(Event(quote, 'exit', quote.get_indicator().ll_10)) stop = None entry_price = None hh50 = None hh20 = None continue #print "Found %s events for ticker %s" %(len(events), symbol) #print return events
from dao import Position from dao import Quote from connection import db #print("[%s] %s mavg20=%s mavg50=%s" % (symbol, date, days, sma_50)) total = 100000 currency = 'USD' #stocks = 0 #symbol = 'LUPE.ST' symbol = 'AAPL' symbol = 'RIO.L' symbol = 'SYSR.ST' position = None skip = 20 for quote in Quote.get_quotes(symbol): skip = skip -1 if skip > 0: continue print "\nSimulating %s" % quote print "%s" % quote.get_indicator() if position: position.current_quote = quote #print "I already have a position for %s" % symbol #if quote.is_below_10_day_low(position): if quote.has_met_stop(position): print "STOP MET %s" % position.get_stop() print "Selling %s" % position print "Loss %s" % position.get_gain() total = total + position.get_value(currency) position = None