opt-chains-tmpl-with-tabs.html 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. <html>
  2. <head>
  3. <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
  4. <title> Trading Options Monitor </title>
  5. <script type="text/javascript" src="https://www.google.com/jsapi"></script>
  6. <script type="text/javascript" src="http://cdnjs.cloudflare.com/ajax/libs/mathjs/2.1.1/math.min.js"></script>
  7. <link rel="stylesheet" href="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.4/themes/smoothness/jquery-ui.css">
  8. <script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.4/jquery-ui.min.js"></script>
  9. <script type="text/javascript">
  10. // google.load("visualization", "1", {packages:["corechart"]});
  11. // google.load('visualization', '1', {'packages':['table', 'corehart']});
  12. google.load("visualization", "1.1", {packages:["corechart", 'table']});
  13. google.setOnLoadCallback(drawChart);
  14. function drawChart() {
  15. // data contains implied vols for a series of this and next month options
  16. // the {{{stuff}}} is substituted by the backend python script with the actual data values
  17. var dataArray = [{{{data}}}];
  18. var data = google.visualization.arrayToDataTable(dataArray);
  19. // data2 contains premiums for a series of this and next month options
  20. var data2 = google.visualization.arrayToDataTable([
  21. {{{dataPremium}}}
  22. ]);
  23. var options = {
  24. title: "HSI PUT/CALL volatility",
  25. hAxis: {minValue: 18000, maxValue: 22500},
  26. vAxis: {minValue: 0.15, maxValue: 0.4},
  27. chartArea: {width:'80%'},
  28. pointShape: 'diamond',
  29. trendlines: {
  30. 0: {
  31. type: 'polynomial',
  32. showR2: true,
  33. visibleInLegend: true,
  34. },
  35. 1: {
  36. type: 'polynomial',
  37. showR2: true,
  38. visibleInLegend: true
  39. },
  40. 2: {
  41. type: 'polynomial',
  42. showR2: true,
  43. visibleInLegend: true
  44. },
  45. 3: {
  46. type: 'polynomial',
  47. showR2: true,
  48. visibleInLegend: true
  49. }
  50. }
  51. };
  52. // helper routine to search for the min and max values in the data array
  53. // the function skips row 1 which is a header row,
  54. // it skips the first column which is the strike price
  55. function minMax(myStringArray){
  56. var arrayLength = myStringArray.length;
  57. var s = '';
  58. var min, max;
  59. min = 99999;
  60. max = -1;
  61. var allNums = new Array();
  62. var k = 0;
  63. for (var i = 1; i < arrayLength; i++) {
  64. for (j = 1; j < myStringArray[i].length; j++){
  65. if (myStringArray[i][j] == null) continue;
  66. if (myStringArray[i][j] > max)
  67. max = myStringArray[i][j];
  68. if (myStringArray[i][j] < min)
  69. min = myStringArray[i][j];
  70. allNums[k++] = myStringArray[i][j];
  71. }
  72. //s = s + myStringArray[i][0];
  73. }
  74. return { "min" : min , "max": max, "median": math.median(allNums) };
  75. }
  76. var chartLinear = new google.visualization.ScatterChart(document.getElementById('chartLinear'));
  77. chartLinear.draw(data, options);
  78. options.trendlines[0].type = 'polynomial';
  79. options.trendlines[1].type = 'polynomial';
  80. // chart table display implied volatilities in a nicely formatted table
  81. var chartTbl = new google.visualization.Table(document.getElementById('chartTbl_div'));
  82. // determine the min max and median of the implied vols
  83. var data_range = minMax(dataArray);
  84. var formatter = new google.visualization.BarFormat({base: data_range.median,
  85. min: data_range.min,
  86. max: data_range.max, width: 120});
  87. document.getElementById('vol_divider').value = (data_range.median / data_range.max) * 100;
  88. document.getElementById('vol_divider_value').value = data_range.median;
  89. var formatter2 = new google.visualization.NumberFormat({pattern:'0.####'});
  90. for (i=1; i < 5; i++){
  91. formatter2.format(data, i);
  92. formatter.format(data, i); // Apply formatter to second column
  93. }
  94. var options2 = {
  95. displayAnnotations: true,
  96. showRowNumber: true, width: '100%', height: '100%', allowHtml: true,
  97. };
  98. chartTbl.draw(data, options2);
  99. var chartPremium = new google.visualization.ScatterChart(document.getElementById('chartPremium'));
  100. options.title = 'PUT/CALL Premium';
  101. chartPremium.draw(data2, options);
  102. document.getElementById('format-select').onchange = function() {
  103. options['pointShape'] = this.value;
  104. chartLinear.draw(data, options);
  105. };
  106. document.getElementById('vol_divider').onchange = function() {
  107. var x = this.value / 100 * (data_range.max - data_range.min) + data_range.min;
  108. document.getElementById('vol_divider_value').value = x;
  109. //formatter = new google.visualization.BarFormat({base: x,min:0, max:0.5, width: 120});
  110. formatter = new google.visualization.BarFormat({base: x,
  111. min: data_range.min,
  112. max: data_range.max, width: 120});
  113. for (i=1; i < 5; i++){
  114. formatter.format(data, i); // Apply formatter to second column
  115. }
  116. chartTbl.draw(data, options2);
  117. };
  118. document.getElementById('haxis_range').onchange = function() {
  119. document.getElementById('haxis_from_value').value = this.value;
  120. options.hAxis.minValue = this.value;
  121. chartPremium.draw(data2, options);
  122. chartLinear.draw(data, options);
  123. };
  124. // The select handler. Call the chart's getSelection() method
  125. function selectHandler() {
  126. var selectedItem = chartLinear.getSelection()[0];
  127. if (selectedItem) {
  128. var value = data.getValue(selectedItem.row, selectedItem.column);
  129. alert('The user selected ' + value);
  130. }
  131. }
  132. // Listen for the 'select' event, and call my function selectHandler() when
  133. // the user selects something on the chart.
  134. google.visualization.events.addListener(chartLinear, 'select', selectHandler);
  135. }
  136. </script>
  137. <script>
  138. $(document).ready(function () {
  139. $('input[id="b_refresh"]').click(function () {
  140. $.ajax({
  141. type: 'Post',
  142. url: '/ws_market_data?r_ckey=HSI-20151029-FUT-&fid=4',
  143. success: function (data) {
  144. document.getElementById('undly_last_px').value = data;
  145. }
  146. });
  147. $.ajax({
  148. type: 'Post',
  149. url: '/getSHquote?qs=0000001,1399001,1399300',
  150. success: function (data) {
  151. var json_data = JSON.parse(data);
  152. if (!String.prototype.format) {
  153. String.prototype.format = function() {
  154. var args = arguments;
  155. return this.replace(/{(\d+)}/g, function(match, number) {
  156. return typeof args[number] != 'undefined'
  157. ? args[number]
  158. : match
  159. ;
  160. });
  161. };
  162. }
  163. var s = "| {0}: {1} [{2}%] | {3}: {4} [{5}%] | {6}: {7} [{8}%]".format(
  164. json_data['0000001']['name'],
  165. json_data['0000001']['price'],
  166. (json_data['0000001']['percent']*100).toFixed(2) ,
  167. json_data['1399001']['name'],
  168. json_data['1399001']['price'],
  169. (json_data['1399001']['percent']*100).toFixed(2) ,
  170. json_data['1399300']['name'],
  171. json_data['1399300']['price'],
  172. (json_data['1399300']['percent']*100).toFixed(2) );
  173. document.getElementById('ssidx_px').value = s;
  174. }
  175. });
  176. })
  177. })
  178. </script>
  179. <script>
  180. $(document).ready(function() {
  181. $(function() {
  182. $( "#tabs" ).tabs();
  183. });
  184. });
  185. </script>
  186. </head>
  187. <body>
  188. <div id="tabs">
  189. <ul>
  190. <li><a href="#tabs-1">Volatility Curves</a></li>
  191. <li><a href="#tabs-2">Implied Volatility Table</a></li>
  192. <li><a href="#tabs-3">Risk Distribution</a></li>
  193. </ul>
  194. <div id="tabs-1">
  195. <div id="d_undly_last_px">
  196. <input type="button" id="b_refresh" value="Refresh Price" />
  197. <output id=undly_last_px></output>
  198. <output id=ssidx_px></output>
  199. </div>
  200. <div id="chartLinear" style="height: 350px; width: 850px"></div>
  201. <div id="chartPremium" style="height: 350px; width: 850px"></div>
  202. <li>
  203. <label for=haxis_from>H-Axis Range Starts At:</label>
  204. <input type=range id=haxis_range min=10000 value=20000 max=25000 step=500>
  205. <output id=haxis_from_value></output>
  206. </li>
  207. </div>
  208. <div id="tabs-2">
  209. <h2> Implied volatilities for this and next month HSI options</h2>
  210. <div id='chartTbl_div' style='width: 900px; height: 500px;'></div>
  211. <select id="format-select">
  212. <option value="">none</option>
  213. <option value="Diamond" selected>Diamond</option>
  214. <option value="triangle">triangle</option>
  215. <option value="square">square</option>
  216. <option value="diamond">diamond</option>
  217. <option value="star">star</option>
  218. <option value="polygon">polygon</option>
  219. </select>
  220. <div id="number_format_chart">
  221. <li>
  222. <label for=vol_divider>Volatility Divider</label>
  223. <input type=range id=vol_divider min=0 value=50 max=100 step=5>
  224. <output id=vol_divider_value></output>
  225. </li>
  226. </div>
  227. <div id="tabs-3">
  228. </div>
  229. </div>
  230. </body>
  231. </html>