-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathUsageExample.java
109 lines (80 loc) · 4.57 KB
/
UsageExample.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package com.mtgox.examples;
import com.mtgox.api.ApiKeys;
import com.mtgox.api.MtGox;
import com.mtgox.api.MtGox.Currency;
import com.mtgox.examples.utils.Utils;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
/**
* https://github.com/adv0r/mtgox-apiv2-java
* @author adv0r <[email protected]>
* MIT License (see LICENSE.md)
* Run examples at your own risk. Only partially implemented and untested.
* Consider donations @ 1N7XxSvek1xVnWEBFGa5sHn1NhtDdMhkA7
*/
public class UsageExample {
public static void main(String args[])
{
initSSL(); //Setup the SSL certificate to interact with mtgox over secure http.
//Read api Keys---------------------------------------------------------------------------------
//from the JSON file located in res/api-keys.json
//ApiKeys keys = readApiKeys("res/api-keys.json");
//or simply create the keys passing them to the constructor
ApiKeys keys = new ApiKeys("your-secret-key",
"your-api-key");
//Library Usage Examples -----------------------------------------------------------------------
//Create the interface for trading on Mtgox, passing the apikeys object
MtGox trade = new MtGox(keys);
//trade.setPrintHTTPResponse(true); //Uncomment this line if you want to read the JSON HTTP response
//Get the current balance in USD,EUR, and BTC---------------------------------------------------
double[] balance = trade.getBalance(); //The answer consist of an array of three elements
double balanceBTC = balance[0]; //balance in BTC
double balanceUSD = balance[1]; //balance in USD
double balanceEUR = balance[2]; //balance in EUR
System.out.println("Current account balance : \n" +
" "+balanceBTC+" BTC\n"+
" "+balanceUSD+" $\n"+
" "+balanceEUR+" €");
//Get the current trading engine lag (as a String)---------------------------------------------
System.out.println("Current Lag : "+ trade.getLag());
//Get the current price of a bitcoin using the ticker fast-------------------------------------
double lastPriceUSD = trade.getLastPrice(Currency.USD);
double lastPriceEUR = trade.getLastPrice(Currency.EUR);
System.out.println("Current price of 1 BTC : \n" +
" "+lastPriceUSD+" $\n"+
" "+lastPriceEUR+" €");
//Buy 0.1 BTC at market price -----------------------------------------------------------------
//String buyResult = trade.buyBTC(0.1); //If you uncomment this, be sure of what you are doing
//System.out.println(buyResult);
//Sell 0.1 BTC at market price -----------------------------------------------------------------
//String sellResult = trade.sellBTC(0.1); //If you uncomment this, be sure of what you are doing
//System.out.println(sellResult);
//Finally, if you are using this library in some of your project consider-----------------------
//giving me a small donation by simpling uncommenting the line below :)
//withdraw 0.1 BTC from your mtgox to my wallet
//trade.withdrawBTC(0.1, "1N7XxSvek1xVnWEBFGa5sHn1NhtDdMhkA7"); //thanks! ;)
}
public static void initSSL()
{
// SSL Certificates trustStore ----------------------------------------
//Set the SSL certificate for mtgox - Read up on Java Trust store.
System.setProperty("javax.net.ssl.trustStore","res/ssl/mtgox.jks");
System.setProperty("javax.net.ssl.trustStorePassword","h4rdc0r_"); //I encripted the jks file using this pwd
//System.setProperty("javax.net.debug","ssl"); //Uncomment for debugging SSL errors
}
//readApiKeysFromFile
public static ApiKeys readApiKeys(String pathToJsonFile) {
//see https://code.google.com/p/json-simple/wiki/DecodingExamples
JSONParser parser=new JSONParser();
ApiKeys apiKeys = null;
String apiStr = Utils.readFromFile(pathToJsonFile);
try {
JSONObject obj2=(JSONObject)(parser.parse(apiStr));
apiKeys= new ApiKeys((String)obj2.get("mtgox_secret_key"), (String)obj2.get("mtgox_api_key"));
} catch (ParseException ex) {
System.err.println(ex);
}
return apiKeys;
}
}