def test_find_breakout_hh20_preceeded_by_exit(self): events = [ Event(Quote({'date': '2010-08-01'}), 'exit'), Event(Quote({'date': '2010-09-01'}), 'hh20'), Event(Quote({'date': '2010-11-01'}), 'eod'), ] self.assertFalse(find_recent_breakout(events, '2010-09-15'))
def test_find_breakout_a_stop(self): events = [ Event(Quote({'date': '2010-09-01'}), 'hh20'), Event(Quote({'date': '2010-09-20'}), 'hh50'), Event(Quote({'date': '2010-11-01'}), 'stop'), ] self.assertFalse(find_recent_breakout(events, '2010-09-15'))
def test_find_breakout_hh50_too_old(self): events = [ Event(Quote({'date': '2010-09-01'}), 'hh20'), Event(Quote({'date': '2010-09-20'}), 'hh50'), Event(Quote({'date': '2010-11-01'}), 'eod'), ] self.assertFalse(find_recent_breakout(events, '2010-09-25'))
def test_find_breakout_hh20_preceeded_by_stop(self): events = [ Event(Quote({'date': '2010-08-01'}), 'stop'), Event(Quote({'date': '2010-09-20'}), 'hh20'), Event(Quote({'date': '2010-11-01'}), 'eod'), ] self.assertEquals(events[1], find_recent_breakout(events, '2010-09-15'))
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
last_event = events[-1] if(last_event.type != 'eod'): return False # Good, we are still in a breakout. # When did it start ? breakout_start = events[-2] if breakout_start.quote.date < not_older_than: return False if breakout_start.type == 'hh50' or events[-3].type == 'stop': return breakout_start account_value = Decimal('550000.0') / Currency.get_rate('SEKSEK') risk_factor = 100 print "-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------" print "Symbol\tDate\tBreakout\nClose\tStop\tTarget\tShares\tPosition" print "-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------" if __name__ == '__main__': for quote in Quote.get_latest_quotes('%'): breakout = find_recent_breakout(find_events(quote.symbol), date.today() - timedelta(days=3)) if breakout: #print 'Found breakout for %s %s' % (quote.symbol, breakout) stop = quote.get_indicator().calculate_stop(quote.close) #TODO add transaction cost risk = quote.close - stop target = risk * 6 + quote.close shares = Position.get_shares(quote, account_value/risk_factor) position = long(shares * quote.close) print "%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s" % (quote.symbol, quote.date, breakout, quote.close, stop, target, shares, position)
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
# When did it start ? breakout_start = events[-2] if breakout_start.quote.date < not_older_than: return False if breakout_start.type == 'hh50' or events[-3].type == 'stop': return breakout_start account_value = Decimal('550000.0') / Currency.get_rate('SEKSEK') risk_factor = 100 print "-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------" print "Symbol\tDate\tBreakout\nClose\tStop\tTarget\tShares\tPosition" print "-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------" if __name__ == '__main__': for quote in Quote.get_latest_quotes('%'): breakout = find_recent_breakout(find_events(quote.symbol), date.today() - timedelta(days=3)) if breakout: #print 'Found breakout for %s %s' % (quote.symbol, breakout) stop = quote.get_indicator().calculate_stop( quote.close) #TODO add transaction cost risk = quote.close - stop target = risk * 6 + quote.close shares = Position.get_shares(quote, account_value / risk_factor) position = long(shares * quote.close) print "%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s" % ( quote.symbol, quote.date, breakout, quote.close, stop, target, shares, position)
def test_find_breakout_hh20_with_no_past(self): events = [ Event(Quote({'date': '2010-09-20'}), 'hh20'), Event(Quote({'date': '2010-11-01'}), 'eod'), ] self.assertFalse(find_recent_breakout(events, '2010-09-15'))