Need suggestion to improve my code for Real Time OHLCV

Hi, after so much efforts I have created a code which gives OHLCV results of any time frame and updates with every tick.


Problems I have Solved : (this I listed to help others)


1) I can not use timeSeriesEngine or dailyTimeSeriesEngine because it will not produce output with every tick and also when I use multiple windowSize due to same stepSize the output will not be same. (refer https://ask.dolphindb.net/question/1124 for more information). I have overcome this problem with my code. See all ts_m functions and all ohlcv functions in Full Code.

2) dailyTimeSeriesEngine can not align time according to sessionStart time. I have overcome this problem with my code. See ts_m10 and ts_m30 in full code for better explanation. (refer https://ask.dolphindb.net/question/1120 https://ask.dolphindb.net/question/1118 for more information)


My plan is to do live technical analysis on OHLCV data which updates with every tick. There are three problems remaining as shown under. Please provide me detailed solutions.


Problems remaining to solve:


1) Need suggestions to improve my code.


2) When I use functions directly on the streaming data, It won't give me correct results. Suppose If I have to use mavg(cm1,3) then the data will be Close (previous candle -2) + Close (previous candle -1) + current price / close. How to solve it ?

    e.g. For tick at 23:05:06 the mavg(close,3) Should be [close at 23:03:00 + close at 23:04:00 + Close at 23:05:06 ] / 3

    then For tick at 23:05:46 the mavg(close,3) Should be [close at 23:03:00 + close at 23:04:00 + Close at 23:05:46 ] / 3

    then For tick at 23:07:16 the mavg(close,3) Should be [close at 23:05:00 + close at 23:06:00 + Close at 23:07:16 ] / 3

    This cannot be achieved by using reactiveStateEngine with mavg, tmavg or any similar average functions. [ the mavg will calculate just 3 positions above the tick i.e. For tick at     23:05:46 mavg(close,3) will calculate avg of ticks at 23:05:44, 23:05:45 and 23:05:46 which will not give correct results ] [ Also tmavg for 3 minutes on 1 Min timeframe will     calculate all intermediate tick values so the final results will not be the same as the example shown above ] 

    (refer https://ask.dolphindb.net/question/1137 https://ask.dolphindb.net/question/1127 for more information). 


3) When I generate OHLCV data from reactiveStateEngine what should I do ?

    a) generate each timeframe individually then apply technical analysis then join all timeframe and get result ?

    [ tick ---> 1min OHLCV ---> Technical Analysis (reactiveStateEngine or CrossSectionalEngine) ---> Join all timeframe in realtime ---> Get result

      tick ---> 5min OHLCV ---> Technical Analysis (reactiveStateEngine or CrossSectionalEngine) ---> Join all timeframe in realtime ---> Get result ... etc for all timeframe]

    b) generate every timeframe in one table (as shown in Full Code) then apply technical analysis on all at once and then get result ? (refer https://ask.dolphindb.net/question/1139 https://ask.dolphindb.net/question/1138 for more information) 

    [ tick ---> all OHLCV (as shown in Full Code) ---> Technical Analysis (reactiveStateEngine or CrossSectionalEngine) on all at once and get result ] (here not joins needed and minimum steps among all methods)

    c) generate every timeframe in one table (as shown in Full Code) then split them and apply technical analysis and then join them and get result ?

    [ tick ---> all OHLCV (as shown in Full Code) ---> split each timeframe ---> perform Technical Analysis (reactiveStateEngine or CrossSectionalEngine) on each timeframe ---> join all timeframe in realtime to get result ]


Here is data for testing. sample.zip

Here is the full code for your reference. [ Also if you like it then please post it in tutorial and don't forget to mention my name (Vishvesh Upadhyay) in Tutorial ! It will help others. ]

temp2=table(1000:0,`symbol`ts`ts_m1`ts_m5`ts_m10`ts_m15`ts_m30`om1`hm1`lm1`cm1`vm1`om5`hm5`lm5`cm5`vm5`om10`hm10`lm10`cm10`vm10`om15`hm15`lm15`cm15`vm15`om30`hm30`lm30`cm30`vm30,[SYMBOL,TIMESTAMP,TIMESTAMP,TIMESTAMP,TIMESTAMP,TIMESTAMP,TIMESTAMP,DOUBLE,DOUBLE,DOUBLE,DOUBLE,DOUBLE,DOUBLE,DOUBLE,DOUBLE,DOUBLE,DOUBLE,DOUBLE,DOUBLE,DOUBLE,DOUBLE,DOUBLE,DOUBLE,DOUBLE,DOUBLE,DOUBLE,DOUBLE,DOUBLE,DOUBLE,DOUBLE,DOUBLE,DOUBLE])

ts_m1=<timestamp(temporalFormat(ts,"yyyy.MM.ddTHH:mm:00.000"))>
ts_m5=<timestamp(temporalFormat(ts-(mod(minute(ts),5))*60000,"yyyy.MM.ddTHH:mm:00.000"))>
ts_m10=<timestamp(temporalFormat((ts-60000*15)-(mod(minute(ts-60000*15),10))*60000,"yyyy.MM.ddTHH:mm:00.000"))+60000*15>
ts_m15=<timestamp(temporalFormat(ts-(mod(minute(ts),15))*60000,"yyyy.MM.ddTHH:mm:00.000"))>
ts_m30=<timestamp(temporalFormat((ts-60000*15)-(mod(minute(ts-60000*15),30))*60000,"yyyy.MM.ddTHH:mm:00.000"))+60000*15>

om1=<tmfirst(timestamp(temporalFormat(ts,"yyyy.MM.ddTHH:mm:00.000")),price,1m)>
hm1=<tmmax(timestamp(temporalFormat(ts,"yyyy.MM.ddTHH:mm:00.000")),price,1m)>
lm1=<tmmin(timestamp(temporalFormat(ts,"yyyy.MM.ddTHH:mm:00.000")),price,1m)>
vm1=<tmsum(timestamp(temporalFormat(ts,"yyyy.MM.ddTHH:mm:00.000")),ltq,1m)>

om5=<tmfirst(timestamp(temporalFormat(ts-(mod(minute(ts),5))*60000,"yyyy.MM.ddTHH:mm:00.000")),price,5m)>
hm5=<tmmax(timestamp(temporalFormat(ts-(mod(minute(ts),5))*60000,"yyyy.MM.ddTHH:mm:00.000")),price,5m)>
lm5=<tmmin(timestamp(temporalFormat(ts-(mod(minute(ts),5))*60000,"yyyy.MM.ddTHH:mm:00.000")),price,5m)>
vm5=<tmsum(timestamp(temporalFormat(ts-(mod(minute(ts),5))*60000,"yyyy.MM.ddTHH:mm:00.000")),ltq,5m)>

om10=<tmfirst(timestamp(temporalFormat((ts-60000*15)-(mod(minute(ts-60000*15),10))*60000,"yyyy.MM.ddTHH:mm:00.000"))+60000*15,price,10m)>
hm10=<tmmax(timestamp(temporalFormat((ts-60000*15)-(mod(minute(ts-60000*15),10))*60000,"yyyy.MM.ddTHH:mm:00.000"))+60000*15,price,10m)>
lm10=<tmmin(timestamp(temporalFormat((ts-60000*15)-(mod(minute(ts-60000*15),10))*60000,"yyyy.MM.ddTHH:mm:00.000"))+60000*15,price,10m)>
vm10=<tmsum(timestamp(temporalFormat((ts-60000*15)-(mod(minute(ts-60000*15),10))*60000,"yyyy.MM.ddTHH:mm:00.000"))+60000*15,ltq,10m)>

om15=<tmfirst(timestamp(temporalFormat(ts-(mod(minute(ts),15))*60000,"yyyy.MM.ddTHH:mm:00.000")),price,15m)>
hm15=<tmmax(timestamp(temporalFormat(ts-(mod(minute(ts),15))*60000,"yyyy.MM.ddTHH:mm:00.000")),price,15m)>
lm15=<tmmin(timestamp(temporalFormat(ts-(mod(minute(ts),15))*60000,"yyyy.MM.ddTHH:mm:00.000")),price,15m)>
vm15=<tmsum(timestamp(temporalFormat(ts-(mod(minute(ts),15))*60000,"yyyy.MM.ddTHH:mm:00.000")),ltq,15m)>

om30=<tmfirst(timestamp(temporalFormat((ts-60000*15)-(mod(minute(ts-60000*15),30))*60000,"yyyy.MM.ddTHH:mm:00.000"))+60000*15,price,30m)>
hm30=<tmmax(timestamp(temporalFormat((ts-60000*15)-(mod(minute(ts-60000*15),30))*60000,"yyyy.MM.ddTHH:mm:00.000"))+60000*15,price,30m)>
lm30=<tmmin(timestamp(temporalFormat((ts-60000*15)-(mod(minute(ts-60000*15),30))*60000,"yyyy.MM.ddTHH:mm:00.000"))+60000*15,price,30m)>
vm30=<tmsum(timestamp(temporalFormat((ts-60000*15)-(mod(minute(ts-60000*15),30))*60000,"yyyy.MM.ddTHH:mm:00.000"))+60000*15,ltq,30m)>

rse2=createReactiveStateEngine(name="rse2",metrics=[<ts>, ts_m1, ts_m5, ts_m10, ts_m15, ts_m30, om1, hm1, lm1, <price>, vm1, om5, hm5, lm5, <price>, vm5, om10, hm10, lm10, <price>, vm10, om15, hm15, lm15, <price>, vm15, om30, hm30, lm30, <price>, vm30],dummyTable=btc,outputTable=temp2,keyColumn=`symbol)

subscribeTable(tableName="btc", actionName="rse2", offset=0, handler=append!{rse2}, msgAsTable=true)


Thanks and Regards,

Vishvesh Upadhyay

请先 登录 后评论

1 个回答

Shena Mao

2) you could do "(msum(close,4)-close)\3" in metrics

3) why you want to join all the results with different time levels? It feels better to show the results separately.

And if you want to share your experience on using DolphinDB, we would like you write some blogs and share them in our community.

请先 登录 后评论
  • 1 关注
  • 0 收藏,1057 浏览
  • Vishvesh Upadhyay 提出于 2022-01-21 01:21

相似问题