Guest User

Untitled

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