def open_position(self, quote): shares = Position.get_shares(quote, self.get_risk()) next_quote = quote.next() print "opening on next day", next_quote self.positions.append(Position.open( quote.symbol, self.currency, self.currency_rate, next_quote.date, next_quote.open, self.enter_commission, shares))
print "Loss %s" % position.get_gain() total = total + position.get_value(currency) position = None elif quote.is_below_10_day_low(): print "10 day low MET %s" % position.get_trailing_stop() print "Selling %s" % position print "Gain %s" % position.get_gain() total = total + position.get_value(currency) position = None # total = total + (stocks * float(quote.close)) # position.value = 0 else: over = quote.is_above_20_day_high() if over: risk = Decimal(total/100) shares = Position.get_shares(quote, risk) #print "Stop %s, Risk %s, Portfolio max risk %s" % (stop, shares * indicator.atr_exp20 * 2, risk) position = Position.open(symbol, currency, 1, quote.date, quote.close, Decimal('9'), shares) position.current_quote = quote position.is_long = True print "Opened %s" % position #position = Position({'symbol': symbol, 'is_long': True, 'current_quote': quote}) #position.stop = indicator.calculate_stop(quote) print "Buy %s" % (position) #if indicator.atr_14: # print "%s quote over sma50 on %s" % (quote.symbol, quote.date) # stop = float(quote.close) - float(indicator.atr_14) * float(0.1); # stocks = float(total/2)/float(quote.close) # position.value = stocks * float(quote.close) total = total - position.get_value(currency)
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)
# 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)