opt-chains-ex-tmpl.html 8.8 KB

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