/MongoWebService/src/main/java/mongo/PriceTickResource.java

https://github.com/bsiver/PT-App · Java · 311 lines · 196 code · 66 blank · 49 comment · 24 complexity · 3b0fbaa4b73962ace5035f670eb3fa73 MD5 · raw file

  1. package mongo;
  2. import java.net.UnknownHostException;
  3. import java.util.regex.Pattern;
  4. import javax.ws.rs.GET;
  5. import javax.ws.rs.Path;
  6. import javax.ws.rs.Produces;
  7. import org.bson.BSONObject;
  8. import org.bson.BasicBSONObject;
  9. import com.mongodb.BasicDBObject;
  10. import com.mongodb.DB;
  11. import com.mongodb.DBCursor;
  12. import com.mongodb.Mongo;
  13. @Path("/")
  14. public class PriceTickResource {
  15. private static final String hostName = "192.168.0.210";
  16. private static final String dbName = "test";
  17. private static final String collName = "banana";
  18. // Threshold for strikes above/below current futures price
  19. private static final double EUD_STRIKE_RANGE = 55;
  20. private static final double CORN_STRIKE_RANGE = 55;
  21. private static final double OIL_STRIKE_RANGE = 550;
  22. private static final double TY_STRIKE_RANGE = 55;
  23. Mongo m;
  24. @GET
  25. @Produces("text/html")
  26. public String showWelcome() {
  27. return "<h1>Welcome to the MongoDB Web Service</h1>";
  28. }
  29. @GET @Path("/pt/opt/eud")
  30. @Produces("text/html")
  31. public String getEUD() throws UnknownHostException {
  32. // Create MongoDB connection
  33. m = new Mongo(hostName);
  34. // Select DB and authenticate
  35. DB db = m.getDB(dbName);
  36. char[] passwd = "admin".toCharArray();
  37. db.authenticate("admin", passwd);
  38. // Find all commodity codes using regex
  39. Pattern p = Pattern.compile("ES..", Pattern.CASE_INSENSITIVE);
  40. // Formulate query
  41. BasicDBObject query = new BasicDBObject("instrument.commodity", p);
  42. // Get the results and iterate through them
  43. DBCursor cur = db.getCollection(collName).find(query);
  44. String result="";
  45. double futurePrice = -1;
  46. while (cur.hasNext()) {
  47. BSONObject current = cur.next();
  48. BasicBSONObject instrument = (BasicBSONObject) current.get("instrument");
  49. BasicBSONObject latestprices = (BasicBSONObject) current.get("latestprices");
  50. // If we are processing the future, set the future price
  51. if (instrument.get("putcall").equals("future") && !latestprices.get("premium").equals(Double.valueOf(0))) {
  52. futurePrice = Double.parseDouble(latestprices.get("premium").toString());
  53. result+=current.toString()+"\n";
  54. }
  55. // Otherwise, include only options within 100 ticks of the future price
  56. else {
  57. Double thisStrike = Double.parseDouble(instrument.get("strike").toString());
  58. if (thisStrike < futurePrice + EUD_STRIKE_RANGE && thisStrike > futurePrice - EUD_STRIKE_RANGE)
  59. result+=current.toString()+"\n";
  60. }
  61. }
  62. m.close();
  63. return result;
  64. }
  65. @GET @Path("/pt/opt/corn")
  66. @Produces("text/html")
  67. public String getCorn() throws UnknownHostException {
  68. // Create MongoDB connection
  69. m = new Mongo(hostName);
  70. // Select DB and authenticate
  71. DB db = m.getDB(dbName);
  72. char[] passwd = "admin".toCharArray();
  73. db.authenticate("admin", passwd);
  74. // Find all commodity codes using regex
  75. Pattern p = Pattern.compile("OZC..|ZC..", Pattern.CASE_INSENSITIVE);
  76. // Formulate query
  77. BasicDBObject query = new BasicDBObject("instrument.commodity", p);
  78. // Get the results and iterate through them
  79. DBCursor cur = db.getCollection(collName).find(query);
  80. String result="";
  81. double futurePrice = -1;
  82. while (cur.hasNext()) {
  83. BSONObject current = cur.next();
  84. BasicBSONObject instrument = (BasicBSONObject) current.get("instrument");
  85. BasicBSONObject latestprices = (BasicBSONObject) current.get("latestprices");
  86. // If we are processing the future, set the future price
  87. if (instrument.get("putcall").equals("future") && !latestprices.get("premium").equals(Double.valueOf(0))) {
  88. futurePrice = Double.parseDouble(latestprices.get("premium").toString());
  89. result+=current.toString()+"\n";
  90. }
  91. // Otherwise, include only options within 100 ticks of the future price
  92. else {
  93. Double thisStrike = Double.parseDouble(instrument.get("strike").toString());
  94. if (thisStrike < futurePrice + CORN_STRIKE_RANGE && thisStrike > futurePrice - CORN_STRIKE_RANGE)
  95. result+=current.toString()+"\n";
  96. }
  97. }
  98. m.close();
  99. return result;
  100. }
  101. @GET @Path("/pt/opt/tenyear")
  102. @Produces("text/html")
  103. public String getTenYearNotes() throws UnknownHostException {
  104. // Create MongoDB connection
  105. m = new Mongo(hostName);
  106. // Select DB and authenticate
  107. DB db = m.getDB(dbName);
  108. char[] passwd = "admin".toCharArray();
  109. db.authenticate("admin", passwd);
  110. // Find all commodity codes using regex
  111. Pattern p = Pattern.compile("OZN..|ZN..", Pattern.CASE_INSENSITIVE);
  112. // Formulate query
  113. BasicDBObject query = new BasicDBObject("instrument.commodity", p);
  114. // Get the results and iterate through them
  115. DBCursor cur = db.getCollection(collName).find(query);
  116. String result="";
  117. double futurePrice = -1;
  118. while (cur.hasNext()) {
  119. BSONObject current = cur.next();
  120. BasicBSONObject instrument = (BasicBSONObject) current.get("instrument");
  121. BasicBSONObject latestprices = (BasicBSONObject) current.get("latestprices");
  122. // If we are processing the future, set the future price
  123. if (instrument.get("putcall").equals("future") && !latestprices.get("premium").equals(Double.valueOf(0))) {
  124. futurePrice = Double.parseDouble(latestprices.get("premium").toString());
  125. result+=current.toString()+"\n";
  126. }
  127. // Otherwise, include only options within 100 ticks of the future price
  128. else {
  129. Double thisStrike = Double.parseDouble(instrument.get("strike").toString());
  130. if (thisStrike < futurePrice + TY_STRIKE_RANGE && thisStrike > futurePrice - TY_STRIKE_RANGE)
  131. result+=current.toString()+"\n";
  132. }
  133. }
  134. m.close();
  135. return result;
  136. }
  137. @GET @Path("/pt/opt/oil")
  138. @Produces("text/html")
  139. public String getOil() throws UnknownHostException {
  140. // Create MongoDB connection
  141. m = new Mongo(hostName);
  142. // Select DB and authenticate
  143. DB db = m.getDB(dbName);
  144. char[] passwd = "admin".toCharArray();
  145. db.authenticate("admin", passwd);
  146. // Find all commodity codes using regex
  147. Pattern p = Pattern.compile("LO..|CL..", Pattern.CASE_INSENSITIVE);
  148. // Formulate query
  149. BasicDBObject query = new BasicDBObject("instrument.commodity", p);
  150. // Get the results and iterate through them
  151. DBCursor cur = db.getCollection(collName).find(query);
  152. String result="";
  153. double futurePrice = -1;
  154. while (cur.hasNext()) {
  155. BSONObject current = cur.next();
  156. BasicBSONObject instrument = (BasicBSONObject) current.get("instrument");
  157. BasicBSONObject latestprices = (BasicBSONObject) current.get("latestprices");
  158. // If we are processing the future, set the future price
  159. if (instrument.get("putcall").equals("future") && !latestprices.get("premium").equals(Double.valueOf(0))) {
  160. futurePrice = Double.parseDouble(latestprices.get("premium").toString());
  161. result+=current.toString()+"\n";
  162. }
  163. // Otherwise, include only options within 100 ticks of the future price
  164. else {
  165. Double thisStrike = Double.parseDouble(instrument.get("strike").toString());
  166. if (thisStrike < futurePrice + OIL_STRIKE_RANGE && thisStrike > futurePrice - OIL_STRIKE_RANGE)
  167. result+=current.toString()+"\n";
  168. }
  169. }
  170. m.close();
  171. return result;
  172. }
  173. public double findEUDPremium() throws UnknownHostException {
  174. // Create MongoDB connection
  175. m = new Mongo(hostName);
  176. // Select DB and authenticate
  177. DB db = m.getDB(dbName);
  178. char[] passwd = "admin".toCharArray();
  179. db.authenticate("admin", passwd);
  180. // Find all commodity codes using regex
  181. Pattern p = Pattern.compile("ES..", Pattern.CASE_INSENSITIVE);
  182. // Formulate query
  183. BasicDBObject query = new BasicDBObject("instrument.commodity", p);
  184. query.append("instrument.putcall", "future");
  185. // Get the results and iterate through them
  186. DBCursor cur = db.getCollection(collName).find(query);
  187. BSONObject obj = (BSONObject) cur.next().get("latestprices");
  188. m.close();
  189. return Double.parseDouble(obj.get("premium").toString());
  190. }
  191. public double findCornPremium() throws UnknownHostException {
  192. // Create MongoDB connection
  193. m = new Mongo(hostName);
  194. // Select DB and authenticate
  195. DB db = m.getDB(dbName);
  196. char[] passwd = "admin".toCharArray();
  197. db.authenticate("admin", passwd);
  198. // Find all commodity codes using regex
  199. Pattern p = Pattern.compile("ZC..", Pattern.CASE_INSENSITIVE);
  200. // Formulate query
  201. BasicDBObject query = new BasicDBObject("instrument.commodity", p);
  202. query.append("instrument.putcall", "future");
  203. // Get the results and iterate through them
  204. DBCursor cur = db.getCollection(collName).find(query);
  205. BSONObject obj = (BSONObject) cur.next().get("latestprices");
  206. m.close();
  207. return Double.parseDouble(obj.get("premium").toString());
  208. }
  209. public double findOilPremium() throws UnknownHostException {
  210. // Create MongoDB connection
  211. m = new Mongo(hostName);
  212. // Select DB and authenticate
  213. DB db = m.getDB(dbName);
  214. char[] passwd = "admin".toCharArray();
  215. db.authenticate("admin", passwd);
  216. // Find all commodity codes using regex
  217. Pattern p = Pattern.compile("CL..", Pattern.CASE_INSENSITIVE);
  218. // Formulate query
  219. BasicDBObject query = new BasicDBObject("instrument.commodity", p);
  220. query.append("instrument.putcall", "future");
  221. // Get the results and iterate through them
  222. DBCursor cur = db.getCollection(collName).find(query);
  223. BSONObject obj = (BSONObject) cur.next().get("latestprices");
  224. m.close();
  225. return Double.parseDouble(obj.get("premium").toString());
  226. }
  227. public double findTenYearPremium() throws UnknownHostException {
  228. // Create MongoDB connection
  229. m = new Mongo(hostName);
  230. // Select DB and authenticate
  231. DB db = m.getDB(dbName);
  232. char[] passwd = "admin".toCharArray();
  233. db.authenticate("admin", passwd);
  234. // Find all commodity codes using regex
  235. Pattern p = Pattern.compile("ZN..", Pattern.CASE_INSENSITIVE);
  236. // Formulate query
  237. BasicDBObject query = new BasicDBObject("instrument.commodity", p);
  238. query.append("instrument.putcall", "future");
  239. // Get the results and iterate through them
  240. DBCursor cur = db.getCollection(collName).find(query);
  241. BSONObject obj = (BSONObject) cur.next().get("latestprices");
  242. m.close();
  243. return Double.parseDouble(obj.get("premium").toString());
  244. }
  245. }