Guest User

Untitled

a guest
Oct 18th, 2018
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.79 KB | None | 0 0
  1. declare
  2. symbolz character varying(10);
  3. i int;
  4. buy numeric;
  5. sell numeric;
  6. total_1min numeric;
  7. total_1h numeric;
  8. total_24h numeric;
  9. total_1m_vs_total_24h numeric;
  10. total_1h_vs_total_24h numeric;
  11. total_week numeric;
  12. total_percent_net_vol_1h_vs_24h numeric;
  13. total_percent_net_vol_1min_vs_vo_l24h numeric;
  14. total_percent_net_vol_1min_vs_1h numeric;
  15. total_full_1min numeric;
  16. total_full_24h numeric;
  17. total_full_1h numeric;
  18. tablenamefor_full text = 'public.ticker2' ;
  19. tablenamefor text = 'ticker2' ;
  20. tablenamefor_col text = 'symbol';
  21. tablename1 text;
  22. curs1 refcursor;
  23. len int;
  24. ttt text;'
  25. BEGIN
  26. EXECUTE 'SELECT reltuples::bigint AS EstimatedCount FROM pg_class
  27. WHERE oid = '||quote_literal(tablenamefor_full)||'::regclass' into len;
  28. FOR i IN 1..len LOOP OPEN curs1 FOR EXECUTE 'SELECT '||quote_ident(tablenamefor_col)||
  29. ' from '||quote_ident(tablenamefor)||
  30. ' order by '||quote_ident(tablenamefor_col)||' ASC';
  31. FETCH ABSOLUTE i FROM curs1 INTO symbolz;
  32. tablename1 = 'ctbh_trades_'||lower(symbolz);
  33. IF NOT EXISTS (SELECT * FROM pg_tables WHERE TABLENAME=tablename1) THEN
  34. NULL;
  35. ELSE
  36.  
  37. EXECUTE 'SELECT COALESCE(SUM(quantity),0) sum
  38. FROM '||quote_ident(tablename1)||'
  39. WHERE '||quote_ident(tablename1)||'.timestamp >=
  40. CURRENT_TIMESTAMP - interval ''1 minutes'' and '||quote_ident(tablename1)||'.side = ''buy''' into buy;
  41. EXECUTE 'SELECT COALESCE(SUM(quantity),0) sum
  42. FROM '||quote_ident(tablename1)||'
  43. WHERE '||quote_ident(tablename1)||'.timestamp >=
  44. CURRENT_TIMESTAMP - interval ''1 minutes'' and '||quote_ident(tablename1)||'.side = ''sell''' into sell;
  45. total_1min = buy - sell;
  46. total_full_1min = buy + sell;
  47.  
  48. EXECUTE 'SELECT COALESCE(SUM(quantity),0) sum
  49. FROM '||quote_ident(tablename1)||'
  50. WHERE '||quote_ident(tablename1)||'.timestamp >=
  51. CURRENT_TIMESTAMP - interval ''1 hour'' and '||quote_ident(tablename1)||'.side = ''buy''' into buy;
  52. EXECUTE 'SELECT COALESCE(SUM(quantity),0) sum
  53. FROM '||quote_ident(tablename1)||'
  54. WHERE '||quote_ident(tablename1)||'.timestamp >=
  55. CURRENT_TIMESTAMP - interval ''1 hour'' and '||quote_ident(tablename1)||'.side = ''sell''' into sell;
  56. total_1h = buy - sell;
  57. total_full_1h = buy + sell;
  58.  
  59. EXECUTE 'SELECT COALESCE(SUM(quantity),0) sum
  60. FROM '||quote_ident(tablename1)||'
  61. WHERE '||quote_ident(tablename1)||'.timestamp >=
  62. CURRENT_TIMESTAMP - interval ''24 hour'' and '||quote_ident(tablename1)||'.side = ''buy''' into buy;
  63. EXECUTE 'SELECT COALESCE(SUM(quantity),0) sum
  64. FROM '||quote_ident(tablename1)||'
  65. WHERE '||quote_ident(tablename1)||'.timestamp >=
  66. CURRENT_TIMESTAMP - interval ''24 hour'' and '||quote_ident(tablename1)||'.side = ''sell''' into sell;
  67. total_24h = buy - sell;
  68. total_full_24h = buy + sell;
  69.  
  70. total_1m_vs_total_24h = total_1min - total_24h;
  71.  
  72. total_1h_vs_total_24h = total_1h - total_24h;
  73.  
  74. EXECUTE 'SELECT COALESCE(SUM(quantity),0) sum
  75. FROM '||quote_ident(tablename1)||'
  76. WHERE '||quote_ident(tablename1)||'.timestamp >=
  77. CURRENT_TIMESTAMP - interval ''168 hour'' and '||quote_ident(tablename1)||'.side = ''buy''' into buy;
  78. EXECUTE 'SELECT COALESCE(SUM(quantity),0) sum
  79. FROM '||quote_ident(tablename1)||'
  80. WHERE '||quote_ident(tablename1)||'.timestamp >=
  81. CURRENT_TIMESTAMP - interval ''168 hour'' and '||quote_ident(tablename1)||'.side = ''sell''' into sell;
  82. total_week = buy - sell;
  83.  
  84.  
  85. total_percent_net_vol_1h_vs_24h = round(COALESCE((total_1h * 100) / NULLIF((total_1h - total_24h),0), 0), 2);
  86.  
  87.  
  88. total_percent_net_vol_1min_vs_vo_l24h = round(COALESCE((total_1min * 100) / NULLIF((total_1min - total_24h),0), 0) ,2);
  89.  
  90.  
  91. total_percent_net_vol_1min_vs_1h = round(COALESCE((total_1min * 100) / NULLIF((total_1min - total_1h),0), 0) , 2);
  92.  
  93. UPDATE ctbh_atotal SET net_vol_1min = total_1min, net_vol_1h = total_1h,
  94. timestamp = current_timestamp, net_vol_24h = total_24h, net_vol1min_vs_vol_24h = total_1m_vs_total_24h,
  95. net_vol1h_vs_vol_24h = total_1h_vs_total_24h, net_vol_week = total_week,
  96. percent_net_vol_1h_vs_vol_24h = total_percent_net_vol_1h_vs_24h,
  97. percent_net_vol_1min_vs_vo_l24h = total_percent_net_vol_1min_vs_vo_l24h,
  98. percent_net_vol_1min_vs_1h = total_percent_net_vol_1min_vs_1h,
  99. total_vol_1min = total_full_1min, total_vol_24h = total_full_24h WHERE symbol = symbolz;
  100. IF found THEN
  101. ELSE
  102. INSERT INTO
  103. ctbh_atotal(symbol,net_vol_1min,net_vol_1h,timestamp,net_vol_24h,
  104. net_vol1min_vs_vol_24h,net_vol1h_vs_vol_24h,
  105. net_vol_week,percent_net_vol_1h_vs_vol_24h,
  106. percent_net_vol_1min_vs_vo_l24h,percent_net_vol_1min_vs_1h,
  107. total_vol_1min,total_vol_24h)
  108. VALUES (symbolz, total_1min, total_1h, current_timestamp, total_24h, total_1m_vs_total_24h,
  109. total_1h_vs_total_24h, total_week, total_percent_net_vol_1h_vs_24h,
  110. total_percent_net_vol_1min_vs_vo_l24h, total_percent_net_vol_1min_vs_1h,
  111. total_full_1min, total_full_24h);
  112. END IF;
  113. END IF;
  114. CLOSE curs1;
  115. END LOOP;
  116. END;'
Add Comment
Please, Sign In to add comment