Please understand my problem and help me solve it 请理解我的问题并帮我解决

I want to calculate mavg for 3 places with realtime streaming data. Here is the sample file x.zip

Suppose the 5min OHLC data begins from 15:00:00 and we want to calculate mavg(close,3)

1) close1 = when first candle (15:00:00) completes we get our first close, as we want to calculate mavg(close,3) we wont have enough data. So result is NULL.

2) close2 = when second candle (15:05:00) completes we get our seond close, as we want to calculate mavg(close,3) we wont have enough data. So result is NULL.

3) close3 = when third candle (15:10:00) completes we get our third close, we will get our mavg(close,3) = (close1+close2+close3)/3

4) close4 = when fourth candle (15:15:00) completes we get our fourth close, we will get our mavg(close,3) = (close2+close3+close4)/3

5) now suppose we get a tick at 15:20:01 so at that moment the value of close = value of price of tick (we will call it  close_tick_1). So mavg(close,3) = (close3+close4+close_tick_1) /3

6) close5 = when fifth candle ( 15:20:00 ) completes we get our third close, we will get our mavg(close,3) = (close3+close4+close5)/3

7) now suppose we get a tick at 15:26:41 so at that moment the value of close = value of price of tick (we will call it close_tick_2). So mavg(close,3) = (close4+close5+close_tick_2) / 3


As you can see in the image posted as under, I have highlighted what data should be included for calculating mavg(cm5,3). As you can see the first two data 43196.52 and 

43167.69 will not change. But the tick data 43152.00 and 43172.07 will change respectively.


attachments-2022-01-46rtFn5j61e57c70d7cee.png


What I tried:


1) I sent the data (included in zip) to keyedStreamTable with `symbol`ts_m5 as keys, but it will show incorrect data. Because it will not update the data after the first value (we need the last data).

2) I sent the data (included in zip) to keyedTable with `symbol`ts_m5 as keys, but it will not publish data to reactiveStateEngine

3) I sent the data (included in zip) to keyedTable with `symbol`ts_m5 as keys, and queried it with sql statement select *,mavg(cm5,3) from x context by symbol. Here "context by symbol" is used for future work with mutiple symbols. It will correct data. But I don't know how to implement it with realtime streaming data.

4) I sent the data (included in zip) to reactiveStateEngine and then used function like mavg(cm5,3), tmavg(ts_m5,cm5,15m) etc but as it process all intermediate tick data the result will be incorrect.


I hope you understood my problem. Please solve it.

Please reply as soon as possible.


Thank you.


Translated from google translate:


我想用实时流数据计算 3 个地方的 mavg。 这是示例文件 

假设 5min OHLC 数据从 15:00:00 开始,我们要计算 mavg(close,3)


1) close1 = 当第一根蜡烛 (15:00:00) 完成时,我们得到第一次收盘,因为我们要计算 mavg(close,3) 我们没有足够的数据。所以结果是NULL。

2) close2 = 当第二根蜡烛 (15:05:00) 完成时,我们得到第二个关闭,因为我们要计算 mavg(close,3) 我们没有足够的数据。所以结果是NULL。

3) close3 = 当第三根蜡烛 (15:10:00) 完成时,我们将获得第三个收盘价,我们将获得我们的 mavg(close,3) = (close1+close2+close3)/3

4) close4 = 当第四根蜡烛 (15:15:00) 完成时,我们得到第四个收盘价,我们将得到我们的 mavg(close,3) = (close2+close3+close4)/3

5) 现在假设我们在 15:20:01 得到一个分时,所以在那一刻收盘价 = 分时价格的值(我们称之为 close_tick_1)。所以 mavg(close,3) = (close3+close4+close_tick_1) /3

6) close5 = 当第五根蜡烛 ( 15:20:00 ) 完成时,我们将获得第三个收盘价,我们将获得我们的 mavg(close,3) = (close3+close4+close5)/3

7) 现在假设我们在 15:26:41 得到一个分时,所以在那一刻收盘的价值 = 分时的价格值(我们称之为 close_tick_2)。所以 mavg(close,3) = (close4+close5+close_tick_2) / 3


正如您在下面发布的图片中看到的那样,我已经强调了计算mavg(cm5,3)应该包含哪些数据。如您所见,前两个数据 43196.52 和


43167.69 不会改变。但分时数据 43152.00 和 43172.07 会分别发生变化。


1) 我将数据(包含在 zip 中)发送到 keyedStreamTable,使用 `symbol`ts_m5 作为键,但它会显示不正确的数据。 因为它不会更新第一个值之后的数据(我们需要最后一个数据)。

2)我将数据(包含在 zip 中)以 `symbol`ts_m5 作为键发送到 keyedTable,但它不会将数据发布到 reactiveStateEngine

3) 我将数据(包含在 zip 中)发送到 keyedTable,以 `symbol`ts_m5 作为键,并使用 sql 语句 select *,mavg(cm5,3) from x context by symbol 进行查询。 这里“符号上下文”用于将来使用多个符号的工作。 它将更正数据。 但我不知道如何用实时流数据来实现它。

4) 我将数据(包含在 zip 中)发送到 reactiveStateEngine,然后使用了 mavg(cm5,3)、tmavg(ts_m5,cm5,15m) 等函数,但是当它处理所有中间刻度数据时,结果将不正确。


我希望你能理解我的问题。 请解决它。


请尽可能尽快回复。


Update 1:

as suggested by Shena Mao I tried the solution but it did not give me true result.

Result: (as you can see the values are incorrect)

attachments-2022-01-fERJCabt61e7ad261cea4.png

please refer https://ask.dolphindb.net/question/1140 for updates and / or solutions

请先 登录 后评论

1 个回答

Shena Mao

here is a sample of how to get mavg using updated close price. Firstly using CrossSectionalEnginge to get the last records and then using RSE to calculate mavg.

x is the data you shared.

share streamTable(10:0,`symbol`ts`ts_m5`om5`hm5`lm5`cm5`vm5,[SYMBOL,TIMESTAMP,TIMESTAMP,DOUBLE,DOUBLE,DOUBLE,DOUBLE,DOUBLE]) as trades1
tempTable = table(1:0, `ts_m5`symbol`ts`om5`hm5`lm5`cm5`vm5, [TIMESTAMP,SYMBOL,TIMESTAMP,DOUBLE,DOUBLE,DOUBLE,DOUBLE,DOUBLE])
outputTable = table(1:0, `symbol`ts_m5`ts`om5`hm5`lm5`cm5`vm5, [SYMBOL,TIMESTAMP,TIMESTAMP,DOUBLE,DOUBLE,DOUBLE,DOUBLE,DOUBLE])
rsMavg = createReactiveStateEngine(name="calc_mavg", metrics=<[ts_m5, ts,mavg(om5,3), mavg(hm5,3),mavg(lm5,3),mavg(cm5,3),mavg(vm5,3)]>, dummyTable=tempTable, outputTable=outputTable, keyColumn=`symbol)
csEngine1=createCrossSectionalEngine(name="csEngineDemo1", metrics=<[last(ts), last(om5), last(hm5), last(lm5), last(cm5), last(vm5)]>, dummyTable=trades1, outputTable=rsMavg, keyColumn=`symbol, triggeringPattern="perBatch", useSystemTime=false, timeColumn=`ts_m5,contextByColumn=`symbol)
subscribeTable(tableName="trades1", actionName="tradesStats", handler=append!{csEngine1}, msgAsTable=true)
replay(inputTables=x,outputTables=trades1,dateColumn=`ts_m5,timeColumn=`ts_m5)
select * from outputTable;

here is the result:

symbol  ts_m5                   ts                      om5                   hm5                   ...
------- ----------------------- ----------------------- --------------------- --------------------- ---
BTCUSDT 2022.01.15T15:15:00.000 2022.01.15T15:18:05.828                                             ...
BTCUSDT 2022.01.15T15:25:00.000 2022.01.15T15:26:59.002                                             ...
BTCUSDT 2022.01.15T15:20:00.000 2022.01.15T15:23:05.070 42940.986666666671226 42973.790000000000873 ...
BTCUSDT 2022.01.15T15:30:00.000 2022.01.15T15:30:18.516 42982.233333333329937 43015.040000000000873 ...
BTCUSDT 2022.01.15T15:30:00.000 2022.01.15T15:32:45.936 43014.689999999995052 43024.466666666667151 ...
BTCUSDT 2022.01.15T15:35:00.000 2022.01.15T15:35:33.746 43049.553333333336922 43059.089999999996507 ...
BTCUSDT 2022.01.15T15:35:00.000 2022.01.15T15:38:00.013 43037.40666666666948  43068.849999999998544 ...
请先 登录 后评论
  • 1 关注
  • 0 收藏,964 浏览
  • Vishvesh Upadhyay 提出于 2022-01-17 22:33

相似问题