def show_trade(request, trade_id): ''' Show 2 states of a trade: day before entry and entry + exit (or open trade) ''' trade = Trade.objects.get(id=trade_id) #TODO: gcl = obsolete lookback = trade.method.rank.get_channel_lookback(trade=trade) if not lookback: lookback = Channel.YEAR if trade.rule_exit is not None: enddate = trade.date_exit else: enddate = datetime.date.today() trade.stock.date_range = (last_year(trade.date_entry), enddate) if trade.rule_exit is None: if request.method == 'POST': stoplossform = StoplossForm(request.POST) if stoplossform.is_valid(): trade.date_exit = stoplossform.cleaned_data['startdate'] angle = stoplossform.cleaned_data['angle'] if angle is not None: angle = int(angle) trade.price_exit = angle trade.save() stoplossform = StoplossForm(data={'angle': trade.price_exit, 'startdate': trade.date_exit }) stoplossdate = next_weekday(trade.stock.get_latest_date()) else: stoplossform = stoplossdate = stoploss = None entrychart = reverse('show_stock_chart_from_to_lookback', kwargs={ 'symbol': trade.stock.name, 'startdate_str': date2datestr(trade.date_entry - datetime.timedelta(days=365)), 'enddate_str': date2datestr(trade.date_entry - datetime.timedelta(days=1)), 'lookback': lookback}) exitchart = reverse('draw_trade_chart', kwargs={'trade_id': trade_id}) if not trade.rule_exit: if trade.price_exit and trade.date_exit: # This is an open trade (portfolio position) with a custom stoploss stoploss = trade.stoploss() else: # This is an open trade (portfolio position) with default stoploss stoploss = trade.stock.price.channel.stoploss(lookback=lookback, date=stoplossdate, date_entry=trade.date_entry) # previous, next trade links: trades = trade.system.trade_set.order_by('date_entry') trade_id = int(trade_id) prev_id = prev_trade = next_trade = None for tr in trades: if prev_id == trade_id: next_trade = reverse('show_trade', kwargs={'trade_id': tr.id}) if tr.id == trade_id and prev_id is not None: prev_trade = reverse('show_trade', kwargs={'trade_id': prev_id}) prev_id = tr.id return render(request, 'show_trade.html',{ 'trade': trade, 'entrychart': entrychart, 'exitchart': exitchart, 'entry_data': trade.stock.data_summary( trade.stock.price.close.get_date( trade.date_entry, -1), lookback), 'exit_data': trade.stock.data_summary(enddate, lookback), 'prev_trade': prev_trade, 'next_trade': next_trade, 'stoplossdate': stoplossdate, 'stoploss': stoploss, 'stoplossform': stoplossform})
def test_Pool(self): date1 = date(2000,1,1) date2 = date(2010,1,5) date3 = date(2010,1,20) date4 = date(2011,1,5) date5 = date(2011,1,18) date6 = date(2012,1,4) aapl = Stock.objects.get(name='AAPL') intc = Stock.objects.get(name='INTC') fdx = Stock.objects.get(name='FDX') snp = Stock.objects.create(name='^GSPC', description='S&P500', currency=Stock.US_DOLLAR, startdate=date1, enddate=date4) pool = Pool.objects.create(name='test', description='test', index=snp, startdate=date2, enddate=date5) StockPoolDates.objects.create(stock=aapl, pool=pool) StockPoolDates.objects.create(stock=intc, pool=pool) StockPoolDates.objects.create(stock=fdx, pool=pool) print 'SIZE', pool.size() # _get_raw_list temp = StockPoolDates.objects.create(stock=fdx, pool=pool) raw_list = pool._get_raw_list() self.assertEqual(len(raw_list), 4) # does not include index temp.delete() raw_list = pool._get_raw_list() self.assertEqual(len(raw_list), 3) # does not include index #TMP # pool2 = Pool.objects.create(name='test2', description='test2', # index=intc, startdate=date2, enddate=date5) # StockPoolDates.objects.create(stock=intc, pool=pool2) # print 'set prices' # print 'WARNINGS', pool2.set_prices(date2, date5) # print 'INDEX', pool2.index.price.close[date3] # print 'INTC', intc.index.price.close[date3] #/TMP # _get_list # # _get_offset_list dated_stock_list = pool._get_offset_list() self.assertEqual(len(dated_stock_list), 4) #must include index for stockdates in dated_stock_list: self.assertEqual(stockdates[2], date5) self.assertLessEqual(stockdates[1], last_year(date2)) # change the date ranges of the stocks in the pool for st,sd,ed in ((aapl,date3,date6), (intc,date3,date4), (fdx,date1,date4)): spd = StockPoolDates.objects.get(stock=st, pool=pool) spd.startdate = sd spd.enddate = ed spd.save() dated_stock_list = pool._get_offset_list() self.assertEqual(len(dated_stock_list), 4) #must include index for row in ((snp, last_year(date2), date5), (aapl, last_year(date3), date5), (intc, last_year(date3), date4), (fdx, last_year(date2), date4)): self.assertIn(row, dated_stock_list) pool.enddate = None today = date.today() dated_stock_list = pool._get_offset_list() self.assertEqual(len(dated_stock_list), 4) #must include index for row in ((snp, last_year(date2), None), (aapl, last_year(date3), date6), (intc, last_year(date3), date4), (fdx, last_year(date2), date4)): self.assertIn(row, dated_stock_list) # # validate_dates pool.enddate = date5 warnings = pool.check_date_ranges() self.assertEqual(len(warnings), 1) self.assertIn('enddate', warnings[0]) snp.set_dates(date3, date6) warnings = pool.check_date_ranges() self.assertEqual(len(warnings), 1) self.assertIn('startdate', warnings[0]) snp.set_dates(date1, date6) warnings = pool.check_date_ranges() self.assertFalse(warnings) # _set_cache self.assertFalse(hasattr(pool, '_cache')) pool._set_cache() self.assertTrue(hasattr(pool, '_cache')) self.assertEqual(len(pool._cache), 3) for row in ((aapl, date3, date5), (intc, date3, date4), (fdx, date2, date4)): self.assertIn(row, pool._cache) # get_cached_stocklist del pool._cache self.assertFalse(hasattr(pool, '_cache')) self.assertEqual(pool.get_cached_stocklist(last_year(date1)), []) self.assertEqual(pool.get_cached_stocklist(date1), []) self.assertFalse(hasattr(pool, '_cache')) self.assertEqual(pool.get_cached_stocklist(date2), [fdx,]) self.assertTrue(hasattr(pool, '_cache')) self.assertEqual(set(pool.get_cached_stocklist(date3)), set([aapl, intc, fdx])) self.assertEqual(set(pool.get_cached_stocklist(date4)), set([aapl, intc, fdx])) self.assertEqual(pool.get_cached_stocklist(date5), [aapl,]) self.assertEqual(pool.get_cached_stocklist(date6), []) # _get_stock_index_list self.assertEqual(pool._get_stock_index_list(last_year(date1)), []) self.assertEqual(pool._get_stock_index_list(date1), []) self.assertEqual(set(pool._get_stock_index_list(date2)), set([snp, fdx,])) self.assertEqual(set(pool._get_stock_index_list(date3)), set([snp, aapl, intc, fdx])) self.assertEqual(set(pool._get_stock_index_list(date4)), set([snp, aapl, intc, fdx])) self.assertEqual(set(pool._get_stock_index_list(date5)), set([snp, aapl,])) self.assertEqual(pool._get_stock_index_list(date6), []) # # activate self.assertIsNotNone(pool.enddate) pool.activate() self.assertIsNone(pool.enddate) # deactivate self.assertRaises(ValueError, pool.deactivate) # download_prices self.assertEqual(Price.objects.all().count(), 0) snp.set_dates(False, None) pool.download_prices() self.assertRaises(Price.DoesNotExist, Price.objects.get, stock=snp, date=previous_weekday(last_year(date3))) self.assertTrue(Price.objects.get(stock=snp, date=next_weekday(last_year(date3)))) # why use today??? # self.assertTrue(Price.objects.get(stock=snp, # date=previous_weekday(today))) self.assertRaises(Price.DoesNotExist, Price.objects.get, stock=aapl, date=previous_weekday(last_year(date3))) self.assertTrue(Price.objects.get(stock=aapl, date=next_weekday(last_year(date3)))) self.assertTrue(Price.objects.get(stock=aapl, date=previous_weekday(date6))) self.assertRaises(Price.DoesNotExist, Price.objects.get, stock=aapl, date=next_weekday(date6)) self.assertRaises(Price.DoesNotExist, Price.objects.get, stock=intc, date=previous_weekday(last_year(date3))) self.assertTrue(Price.objects.get(stock=intc, date=next_weekday(last_year(date3)))) self.assertTrue(Price.objects.get(stock=intc, date=previous_weekday(date4))) self.assertRaises(Price.DoesNotExist, Price.objects.get, stock=intc, date=next_weekday(date4)) self.assertRaises(Price.DoesNotExist, Price.objects.get, stock=fdx, date=previous_weekday(last_year(date2))) self.assertTrue(Price.objects.get(stock=fdx, date=next_weekday(last_year(date2)))) self.assertTrue(Price.objects.get(stock=fdx, date=previous_weekday(date4))) self.assertRaises(Price.DoesNotExist, Price.objects.get, stock=fdx, date=next_weekday(date4)) # deactivate pool.deactivate() self.assertEqual(pool.enddate, previous_weekday(today)) # check_prices self.assertEqual(pool.check_prices(), []) # intc,date3,date4), spd = StockPoolDates.objects.get(stock=intc, pool=pool) spd.startdate = date3 spd.enddate = date5 spd.save() self.assertEqual(len(pool.check_prices()), 1) spd = StockPoolDates.objects.get(stock=intc, pool=pool) spd.startdate = date2 spd.enddate = date4 spd.save() self.assertEqual(len(pool.check_prices()), 1) # get_todays_stocks_for_download spd = StockPoolDates.objects.get(stock=aapl, pool=pool) spd.enddate = None spd.save() del pool._cache self.assertEqual(Pool.get_todays_stocks_for_download( Stock.US_DOLLAR), []) pool.activate() self.assertEqual(Pool.get_todays_stocks_for_download(Stock.EURO), []) self.assertEqual(set(Pool.get_todays_stocks_for_download( Stock.US_DOLLAR)), set([aapl, snp])) self.assertEqual(Pool.get_todays_stocks_for_download( Stock.BRITISH_POUND), []) # download_prices_today # # import cPickle as pickle # stock_list = Pool.get_todays_stocks_for_download(Stock.US_DOLLAR) # print stock_list # pickle.dump(stock_list, open('save.p', 'wb')) # what if stock does not exist!?? # # export_all # self.assertEqual(Pool.objects.all().count(), 1) # self.assertEqual(Stock.objects.all().count(), 4) # self.assertEqual(StockPoolDates.objects.all().count(), 3) # out = open("test.json", "w") # Pool.export_all(out) # out.close() # Pool.objects.all().delete() # StockPoolDates.objects.all().delete() # self.assertEqual(Pool.objects.all().count(), 0) # self.assertEqual(Stock.objects.all().count(), 4) # self.assertEqual(StockPoolDates.objects.all().count(), 0) # Pool.import_all("test.json") # print Pool.objects.all() # print Stock.objects.all() # print StockPoolDates.objects.all() # self.assertEqual(Pool.objects.all().count(), 1) # self.assertEqual(Stock.objects.all().count(), 4) self.assertEqual(StockPoolDates.objects.all().count(), 3) # copy pool = Pool.objects.get(name='test') new = pool.copy() copy = Pool.objects.get(name=new.name) self.assertEqual(pool.index, copy.index) self.assertEqual(pool.startdate, copy.startdate) self.assertEqual(pool.enddate, copy.enddate) for s_pool,s_copy in zip(pool.members.all(), copy.members.all()): self.assertEqual(s_pool, s_copy) poolmember = s_pool.stockpooldates_set.get(pool=pool) copymember = s_copy.stockpooldates_set.get(pool=copy) self.assertEqual(poolmember.startdate, copymember.startdate) self.assertEqual(poolmember.enddate, copymember.enddate) self.assertEqual(poolmember.stock, copymember.stock)
def test_last_year(self): self.assertEqual(du.last_year(dt.date(2000,3,29)), dt.date(1999,3,29)) self.assertEqual(du.last_year(dt.date(2000,2,27)), dt.date(1999,2,27)) self.assertEqual(du.last_year(dt.date(2000,2,29)), dt.date(1999,2,28))