我正在编写一个Python / Django应用程序来进行一些股票分析。
我有两个非常简单的模型,如下所示:
class Stock(models.Model):
symbol = models.CharField(db_index=True, max_length=5, null=False, editable=False, unique=True)
class StockHistory(models.Model):
stock = models.ForeignKey(Stock, related_name='StockHistory_stock', editable=False)
trading_date = models.DateField(db_index=True, null=False, editable=False)
close = models.DecimalField(max_digits=12, db_index=True, decimal_places=5, null=False, editable=False)
class Meta:
unique_together = ('stock', 'trading_date')
这是我填充的虚拟数据:
import datetime
a = Stock.objects.create(symbol='A')
b = Stock.objects.create(symbol='B')
c = Stock.objects.create(symbol='C')
d = Stock.objects.create(symbol='D')
StockHistory.objects.create(trading_date=datetime.date(2018,1,1), close=200, stock=a)
StockHistory.objects.create(trading_date=datetime.date(2018,1,2), close=150, stock=a)
StockHistory.objects.create(trading_date=datetime.date(2018,1,3), close=120, stock=a)
StockHistory.objects.create(trading_date=datetime.date(2018,4,28), close=105, stock=a)
StockHistory.objects.create(trading_date=datetime.date(2018,5,3), close=105, stock=a)
StockHistory.objects.create(trading_date=datetime.date(2017,5,2), close=400, stock=b)
StockHistory.objects.create(trading_date=datetime.date(2017,11,11), close=200, stock=b)
StockHistory.objects.create(trading_date=datetime.date(2017,11,12), close=300, stock=b)
StockHistory.objects.create(trading_date=datetime.date(2017,11,13), close=400, stock=b)
StockHistory.objects.create(trading_date=datetime.date(2017,11,14), close=500, stock=b)
StockHistory.objects.create(trading_date=datetime.date(2018,4,28), close=105, stock=c)
StockHistory.objects.create(trading_date=datetime.date(2018,4,29), close=106, stock=c)
StockHistory.objects.create(trading_date=datetime.date(2018,4,30), close=107, stock=c)
StockHistory.objects.create(trading_date=datetime.date(2018,5,1), close=108, stock=c)
StockHistory.objects.create(trading_date=datetime.date(2018,5,2), close=109, stock=c)
StockHistory.objects.create(trading_date=datetime.date(2018,5,3), close=110, stock=c)
StockHistory.objects.create(trading_date=datetime.date(2018,5,4), close=90, stock=c)
我想找到过去一周内年度低点的所有股票。
但是为了使这个问题更简单,假设我想找到'2017-05-04'
之后或'2018-04-30'
之后发生mysql> select
-> s.symbol,
-> sh.trading_date,
-> low_table.low
-> from
-> (
-> select
-> stock_id,
-> min(close) as low
-> from
-> stocks_stockhistory
-> where
-> trading_date >= '2017-05-04'
-> group by
-> stock_id
-> ) as low_table,
-> stocks_stockhistory as sh,
-> stocks_stock as s
-> where
-> sh.stock_id = low_table.stock_id
-> and sh.stock_id = s.id
-> and sh.close = low_table.low
-> and sh.trading_date >= '2018-04-30'
-> order by
-> s.symbol asc;
+--------+--------------+-----------+
| symbol | trading_date | low |
+--------+--------------+-----------+
| A | 2018-05-03 | 105.00000 |
| C | 2018-05-04 | 90.00000 |
+--------+--------------+-----------+
2 rows in set (0.02 sec)
以来最低点的所有股票。下面是我写的SQL来找到它。它有效。
但我需要帮助找出要编写的Django Query以获得与此SQL相同的结果。我该怎么办?
Invalid redeclaration of 'application(_:open:options:)'
答案 0 :(得分:7)
编辑:我设法使用Django子查询改造解决方案。
我们可以使用Django的aggregates with SubQuery expressions
将查询翻译成Django ORM:
创建子查询以检索每close
的最低symbol
:
from django.db.models import OuterRef, Subquery, Min
lows = StockHistory.objects.filter(
stock=OuterRef('stock'),
trading_date__gte='2017-05-04'
).values('stock__symbol')
.annotate(low=Min('close'))
.filter(trading_date__gte='2018-04-30')
<强> 故障: 强>
filter
查询集只能获得trading_date >= '2017-05-04'
。stock__symbol
(在Djnago中分组的示例:GROUP BY ... MIN/MAX
,GROUP BY ... COUNT/SUM
)。annotate
每个元素的最低价格(low
)。filter
查询集只能获取low
上发生trading_date >= '2018-04-30'
字段的对象。中级结果:
虽然我们无法在此阶段获得结果,但子查询将如下所示:
[
{'stock__symbol': 'A', 'low': Decimal('105.00000')},
{'stock__symbol': 'C', 'low': Decimal('90.00000')}
]
我们错过了trading_date
。
利用子查询检索特定的StockHistory
个对象:
StockHistory.objects.filter(
stock__symbol=Subquery(lows.values('stock__symbol')),
close=Subquery(lows.values('low')),
trading_date__gte='2018-04-30'
).values('stock__symbol', 'trading_date', 'close')
.order_by('stock__symbol')
<强>故障:强>
lows.values('stock__symbol')
和lows.values('low')从子查询中检索相应的值。filter
针对lows
子查询值的查询集。同时filter
针对指定日期,以消除在该日期之前发生的低close
价格。values
。stock__symbol
排序结果(默认为ascending
)。 <强>结果:强>
[
{
'close': Decimal('105.00000'),
'trading_date': datetime.date(2018, 5, 3),
'stock__symbol': 'A'
},
{
'close': Decimal('90.00000'),
'trading_date': datetime.date(2018, 5, 4),
'stock__symbol': 'C'
}
]
答案 1 :(得分:6)
对于较新版本的Django(1.11,2.0):
from django.db.models import Min
low_stocks_qs = StockHistory.objects.filter(trading_date__gt='2017-05-04').annotate(low=Min('close')).filter(trading_date__gte='2018-04-30').order_by('stock__symbol')
您可以遍历查询集以获取low和stock.symbol的单个值,可能是这样的:
low_stocks_dict = {}
for inst in low_stocks_qs:
low_stocks_dict[inst.stock.Symbol] = inst.low