Guest User

Untitled

a guest
May 26th, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.93 KB | None | 0 0
  1. package org.ape.main.robots;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.List;
  5.  
  6. import jsonbeanV1.Order;
  7. import jsonbeanV1.Ticker;
  8. import mtgoxconnector.MTGoxConnector;
  9.  
  10. import org.ape.analyzer.DepthAnalyzer;
  11. import org.ape.beanfast.BalanceFast;
  12. import org.ape.beanfast.DepthFast;
  13. import org.json.JSONException;
  14.  
  15. public class RoboDepth {
  16.  
  17.     MTGoxConnector mtc = new MTGoxConnector();
  18.     long pat = -1;
  19.     long pbt = -1;
  20.     boolean gainask;
  21.     boolean gainbid;
  22.     long orderamount = 1000000L;
  23.     double usdold;
  24.     double btcold;
  25.     double usdgain=0;
  26.     double btcgain=0;
  27.  
  28.     public static void main(String[] args) throws Exception {
  29.         new RoboDepth();
  30.     }
  31.  
  32.     public RoboDepth() throws Exception {
  33.         mtc.key = "2e64b901-a11e-40ca-827c-50a88c4c6b6e";
  34.         mtc.secret = "tVwqFrKJpUgxLKk1AXhdKFufBxI5Tg4wiT/fWHf9HRnKoLmg0+blbZAWVHdBakpB9e9GhgXs9Xg9N4jxeuploQ==";
  35.         while (true) {
  36.             try {
  37.                 double gainpercent = 0.01;
  38.                 double shiftpercent = 0.001;
  39.                 DepthFast df = mtc.getDepthFast();
  40.                 DepthAnalyzer dpa = new DepthAnalyzer(df.asks, df.bids);
  41.                 List<Order> orders = mtc.getOrders();
  42.                 Ticker t = mtc.getTicker();
  43.  
  44.                 long resistence = 0;
  45.                 long support = 0;
  46.                 int molt = 1;
  47.                 long priceAsk = 0;
  48.                 long priceBid = 0;
  49.                 while ((priceAsk < (priceBid * (1 + (gainpercent * 2))) || (support == 0))
  50.                         && molt < 100) {
  51.  
  52.                     resistence = dpa.getResistence(molt * 1000);
  53.                     support = dpa.getSupport(molt * 1000);
  54.                     priceAsk = Math.round(resistence * (1 - shiftpercent));
  55.                     priceBid = Math.round(support * (1 + shiftpercent));
  56.                     molt++;
  57.                 }
  58.                 if (priceAsk < (priceBid * (1 + (gainpercent * 2)))) {
  59.                     System.out.println("ERROR");
  60.                     priceAsk = Math.round(priceBid * (1 + (gainpercent * 2)));
  61.                 }
  62.                 Order orderAsk = null;
  63.                 Order orderBid = null;
  64.                 boolean askFilled = false;
  65.                 boolean bidFilled = false;
  66.                 boolean changeAsk = false;
  67.                 boolean changeBid = false;
  68.                 if (orders.size() > 2) {
  69.                     System.err.println("ERROR ORDER SIZE >2 CLEANING ORDERS");
  70.                     for (Order o : orders) {
  71.                         cancelOrder(o);
  72.  
  73.                     }
  74.                     orders = new ArrayList<Order>();
  75.  
  76.                 }
  77.                 for (Order o : orders) {
  78.  
  79.                     if ("ask".equals(o.getType())) {
  80.  
  81.                         long price = o.getPrice().getValueInt();
  82.  
  83.                         askFilled = true;
  84.                         orderAsk = o;
  85.                         if (price != priceAsk && !gainbid) {
  86.                             cancelOrder(o);
  87.                             changeAsk = true;
  88.  
  89.                         }
  90.  
  91.                     } else if ("bid".equals(o.getType())) {
  92.  
  93.                         long price = o.getPrice().getValueInt();
  94.  
  95.                         bidFilled = true;
  96.                         orderBid = o;
  97.                         if (price != priceBid && !gainask) {
  98.                             cancelOrder(o);
  99.                             changeBid = true;
  100.  
  101.                         }
  102.                     }
  103.                 }
  104.                 if (changeAsk) {
  105.                     System.out.println("changeask");
  106.                     addOrder("ask", orderamount, priceAsk);
  107.                 }
  108.                 if (changeBid) {
  109.                     System.out.println("changebid");
  110.  
  111.                     addOrder("bid", orderamount, priceBid);
  112.                 }
  113.                 if (!bidFilled) {
  114.  
  115.                     if (pbt != -1 && !gainask && orderAsk != null) {
  116.                         System.out
  117.                                 .println("support filled");
  118.                         priceAsk = Math.round(pbt * (1 + gainpercent));
  119.                         cancelOrder(orderAsk);
  120.                         gainbid = true;
  121.                     } else if (gainask) {
  122.                         gainask = false;
  123.                     }
  124.  
  125.                     addOrder("ask", orderamount, priceAsk);
  126.                     if (gainbid)
  127.                         priceBid = Math.round(priceBid
  128.                                 * (1 - (gainpercent * 5)));
  129.  
  130.                     addOrder("bid", orderamount, priceBid);
  131.  
  132.                 } else if (!askFilled) {
  133.                     if (pat != -1 && !gainbid && orderBid != null) {
  134.                         System.out
  135.                                 .println("resistence");
  136.                         priceBid = Math.round(pat * (1 - gainpercent));
  137.                         cancelOrder(orderBid);
  138.                         gainask = true;
  139.                     } else if (gainbid) {
  140.                         gainbid = false;
  141.                     }
  142.                     if (gainask) {
  143.                         priceAsk = Math.round(priceAsk
  144.                                 * (1 + (gainpercent * 5)));
  145.                     }
  146.                     addOrder("ask", orderamount, priceAsk);
  147.  
  148.                     addOrder("bid", orderamount, priceBid);
  149.  
  150.                 }
  151.             } catch (Exception e) {
  152.                 e.printStackTrace();
  153.             }
  154.             Thread.sleep(5000);
  155.  
  156.         }
  157.  
  158.     }
  159.  
  160.     private String addOrder(String type, long amount, Long price)
  161.             throws NumberFormatException, Exception {
  162.         Thread.sleep(Math.round(Math.random()*10));
  163.         System.out.println("Order " + type + " added " + price + "/"
  164.                 + amount);
  165.         if ("ask".equals(type))
  166.             pat = price;
  167.         else if ("bid".equals(type))
  168.             pbt = price;
  169.         String out = "";
  170.         try {
  171.             out = mtc
  172.                     .addOrder(type, amount, Integer.parseInt(price.toString()));
  173.         } catch (Exception e) {
  174.             e.printStackTrace();
  175.             out = mtc
  176.                     .addOrder(type, amount, Integer.parseInt(price.toString()));
  177.  
  178.         }
  179.         return out;
  180.  
  181.     }
  182.  
  183.     private String cancelOrder(Order o) throws JSONException, Exception {
  184.         Thread.sleep(Math.round(Math.random()*10));
  185.         String out = "";
  186.         try {
  187.             out = mtc.cancelOrder(o);
  188.         } catch (Exception e) {
  189.             e.printStackTrace();
  190.             out = mtc.cancelOrder(o);
  191.  
  192.         }
  193.         System.out.println("Order Deleted: " + o.getType() + " price: "
  194.                 + o.getPrice().getValueInt() + "    " + out);
  195.         return out;
  196.  
  197.     }
  198.     private void showBalance() throws Exception{
  199.         BalanceFast bf = mtc.getBalanceFast();
  200.         Ticker t = mtc.getTicker();
  201.         double btcround = 100000000;
  202.         double usdround = 100000;
  203.        
  204.        
  205.        
  206.         double totbc = bf.btc
  207.                 + (((double) bf.usd / t.getSell().getValueInt()) * btcround);
  208.        
  209.         double totusd = bf.usd
  210.                 + (((double) bf.btc * t.getBuy().getValueInt()) / btcround);
  211.        
  212.        
  213.         if (totbc != btcold || totusd != usdold) {
  214.             double diffDoll = totusd - usdold;
  215.             double diffBtc = totbc - btcold;
  216.             usdgain += diffDoll;
  217.             btcgain += diffBtc;
  218.             if(usdold==0)
  219.                 usdgain=0;
  220.             if (btcold==0)
  221.                 btcgain=0;
  222.             System.out.println("btc: " + (double) bf.btc / btcround
  223.                     + " usd: " + (double) bf.usd / usdround + " allbtc: "
  224.                     + (double) totbc / btcround + " allusd: "
  225.                     + (double) totusd / usdround + " USD prodotti:"
  226.                     + usdgain / usdround + " BTC prodotti:"
  227.                     + btcgain / btcround + " TickerValue: "
  228.                     + t.getLastLocal().getValue());
  229.         }
  230.         usdold = totusd;
  231.         btcold = totbc;
  232.  
  233.     }
  234. }
Add Comment
Please, Sign In to add comment