У меня есть таблица с этими полями:
RefDate
Symbol
Timestamp
Sequence
Quantity
Price
SaleCondition
Pid
SubMkt
В ней есть список сделок с акциями с датой (RefDate), символом (AAPL, MSFT, DAVE, AMZN), количеством, ценой и т. Д.
SaleCondition
содержит список кодов, которые мне нужно использовать для поиска различных вещей, например, "O"
означает «открытие сделки», "6"
означает «закрытие сделки» и т. Д.
Мне нужно рассчитать цену открытия, цену закрытия, цену min / max / avg, VWAP (средневзвешенную цену по объему), количество сделок, количество торгуемых акций и волатильность (рассчитывается как максимальная цена).-Min Price / Last price).
Мне пришлось исключить много кодов в SaleCondition
, чтобы получить min / max / avg / VWAP, что я и сделал правильно.
Но я не делаюзнать, как включить цену запроса и цену закрытия в запрос.Мне в основном нужно дать расчетные условия поля (Select PRICE where SaleCondition="O" and PID="Q"
).Очевидно, я не могу этого сделать, потому что мне нужно предложение WHERE
, чтобы исключить многие другие коды.
Вот что я придумал.Первая выдает правильные значения min / max / medium / vwap, но цены открытия / закрытия являются местозаполнителями, а Акции / Сделки неверны.Вторые два запроса - это правильные цены открытия и закрытия.
dbGetQuery(nqdb, statement =
"select
RefDate,
Symbol,
Price as OpeningPrice,
Price as ClosingPrice,
Min(Price) as MinPrice,
Max(Price) as MaxPrice,
AVG(Price) as AvgPrice,
Sum(Quantity*Price)/Sum(Quantity) as VWAP,
Count(Quantity) as Trades,
Sum(Quantity) as Shares,
(Max(Price)-Min(Price))/(Price) as PctRange
from trds
where
SaleCondition not like '%C%' and
SaleCondition not like '%G%' and
SaleCondition not like '%I%' and SaleCondition not like '%H%' and
SaleCondition not like '%M%' and SaleCondition not like '%N%' and
SaleCondition not like '%P%' and SaleCondition not like '%Q%' and
SaleCondition not like '%R%' and SaleCondition not like '%T%' and
SaleCondition not like '%U%' and SaleCondition not like '%V%' and
SaleCondition not like '%W%' and SaleCondition not like '%Z%' and
SaleCondition not like '%4%' and SaleCondition not like '%7%' and
SaleCondition not like '%9%'
group by Symbol order by PctRange DESC")
dbGetQuery(nqdb, statement =
"select
RefDate,
Symbol,
Price as OpeningPrice
from trds
where SaleCondition like '%O%' and Pid='Q'")
dbGetQuery(nqdb, statement =
"select
RefDate,
Symbol,
Price as ClosingPrice
from trds
where SaleCondition like '%6%' and Pid='Q'")