Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package org.ape.main.robots;
- import java.util.ArrayList;
- import java.util.List;
- import jsonbeanV1.Order;
- import jsonbeanV1.Ticker;
- import mtgoxconnector.MTGoxConnector;
- import org.ape.analyzer.DepthAnalyzer;
- import org.ape.beanfast.BalanceFast;
- import org.ape.beanfast.DepthFast;
- import org.json.JSONException;
- public class RoboDepth {
- MTGoxConnector mtc = new MTGoxConnector();
- long pat = -1;
- long pbt = -1;
- boolean gainask;
- boolean gainbid;
- long orderamount = 1000000L;
- double usdold;
- double btcold;
- double usdgain=0;
- double btcgain=0;
- public static void main(String[] args) throws Exception {
- new RoboDepth();
- }
- public RoboDepth() throws Exception {
- while (true) {
- try {
- double gainpercent = 0.01;
- double shiftpercent = 0.001;
- DepthFast df = mtc.getDepthFast();
- DepthAnalyzer dpa = new DepthAnalyzer(df.asks, df.bids);
- List<Order> orders = mtc.getOrders();
- Ticker t = mtc.getTicker();
- long resistence = 0;
- long support = 0;
- int molt = 1;
- long priceAsk = 0;
- long priceBid = 0;
- while ((priceAsk < (priceBid * (1 + (gainpercent * 2))) || (support == 0))
- && molt < 100) {
- resistence = dpa.getResistence(molt * 1000);
- support = dpa.getSupport(molt * 1000);
- priceAsk = Math.round(resistence * (1 - shiftpercent));
- priceBid = Math.round(support * (1 + shiftpercent));
- molt++;
- }
- if (priceAsk < (priceBid * (1 + (gainpercent * 2)))) {
- System.out.println("ERROR");
- priceAsk = Math.round(priceBid * (1 + (gainpercent * 2)));
- }
- Order orderAsk = null;
- Order orderBid = null;
- boolean askFilled = false;
- boolean bidFilled = false;
- boolean changeAsk = false;
- boolean changeBid = false;
- if (orders.size() > 2) {
- System.err.println("ERROR ORDER SIZE >2 CLEANING ORDERS");
- for (Order o : orders) {
- cancelOrder(o);
- }
- orders = new ArrayList<Order>();
- }
- for (Order o : orders) {
- if ("ask".equals(o.getType())) {
- long price = o.getPrice().getValueInt();
- askFilled = true;
- orderAsk = o;
- if (price != priceAsk && !gainbid) {
- cancelOrder(o);
- changeAsk = true;
- }
- } else if ("bid".equals(o.getType())) {
- long price = o.getPrice().getValueInt();
- bidFilled = true;
- orderBid = o;
- if (price != priceBid && !gainask) {
- cancelOrder(o);
- changeBid = true;
- }
- }
- }
- if (changeAsk) {
- System.out.println("changeask");
- addOrder("ask", orderamount, priceAsk);
- }
- if (changeBid) {
- System.out.println("changebid");
- addOrder("bid", orderamount, priceBid);
- }
- if (!bidFilled) {
- if (pbt != -1 && !gainask && orderAsk != null) {
- System.out
- .println("support filled");
- priceAsk = Math.round(pbt * (1 + gainpercent));
- cancelOrder(orderAsk);
- gainbid = true;
- } else if (gainask) {
- gainask = false;
- }
- addOrder("ask", orderamount, priceAsk);
- if (gainbid)
- priceBid = Math.round(priceBid
- * (1 - (gainpercent * 5)));
- addOrder("bid", orderamount, priceBid);
- } else if (!askFilled) {
- if (pat != -1 && !gainbid && orderBid != null) {
- System.out
- .println("resistence");
- priceBid = Math.round(pat * (1 - gainpercent));
- cancelOrder(orderBid);
- gainask = true;
- } else if (gainbid) {
- gainbid = false;
- }
- if (gainask) {
- priceAsk = Math.round(priceAsk
- * (1 + (gainpercent * 5)));
- }
- addOrder("ask", orderamount, priceAsk);
- addOrder("bid", orderamount, priceBid);
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- Thread.sleep(5000);
- }
- }
- private String addOrder(String type, long amount, Long price)
- throws NumberFormatException, Exception {
- Thread.sleep(Math.round(Math.random()*10));
- System.out.println("Order " + type + " added " + price + "/"
- + amount);
- if ("ask".equals(type))
- pat = price;
- else if ("bid".equals(type))
- pbt = price;
- String out = "";
- try {
- out = mtc
- .addOrder(type, amount, Integer.parseInt(price.toString()));
- } catch (Exception e) {
- e.printStackTrace();
- out = mtc
- .addOrder(type, amount, Integer.parseInt(price.toString()));
- }
- return out;
- }
- private String cancelOrder(Order o) throws JSONException, Exception {
- Thread.sleep(Math.round(Math.random()*10));
- String out = "";
- try {
- out = mtc.cancelOrder(o);
- } catch (Exception e) {
- e.printStackTrace();
- out = mtc.cancelOrder(o);
- }
- System.out.println("Order Deleted: " + o.getType() + " price: "
- + o.getPrice().getValueInt() + " " + out);
- return out;
- }
- private void showBalance() throws Exception{
- BalanceFast bf = mtc.getBalanceFast();
- Ticker t = mtc.getTicker();
- double btcround = 100000000;
- double usdround = 100000;
- double totbc = bf.btc
- + (((double) bf.usd / t.getSell().getValueInt()) * btcround);
- double totusd = bf.usd
- + (((double) bf.btc * t.getBuy().getValueInt()) / btcround);
- if (totbc != btcold || totusd != usdold) {
- double diffDoll = totusd - usdold;
- double diffBtc = totbc - btcold;
- usdgain += diffDoll;
- btcgain += diffBtc;
- if(usdold==0)
- usdgain=0;
- if (btcold==0)
- btcgain=0;
- System.out.println("btc: " + (double) bf.btc / btcround
- + " usd: " + (double) bf.usd / usdround + " allbtc: "
- + (double) totbc / btcround + " allusd: "
- + (double) totusd / usdround + " USD prodotti:"
- + usdgain / usdround + " BTC prodotti:"
- + btcgain / btcround + " TickerValue: "
- + t.getLastLocal().getValue());
- }
- usdold = totusd;
- btcold = totbc;
- Thread.sleep(5000);
- }
- }
Add Comment
Please, Sign In to add comment