opt-chains-tmpl.html 8.4 KB

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