Skip to content

Commit b35028a

Browse files
committed
order settings
1 parent 0a9b73f commit b35028a

File tree

5 files changed

+297
-72
lines changed

5 files changed

+297
-72
lines changed

NHB3/Api.cs

+36-18
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
1-
using System;
1+
using Newtonsoft.Json;
2+
using Newtonsoft.Json.Linq;
3+
using System;
24
using System.Collections.Generic;
35
using System.Linq;
6+
using System.Net;
47
using System.Text;
58
using System.Threading.Tasks;
69

@@ -81,6 +84,7 @@ private static string JoinSegments(List<string> segments)
8184
sb.Append(segment);
8285
}
8386
}
87+
Console.ForegroundColor = ConsoleColor.DarkGray;
8488
Console.Out.WriteLine("req: [" + sb.ToString() + "]");
8589
return sb.ToString();
8690
}
@@ -101,9 +105,14 @@ private static string CalcHMACSHA256Hash(string plaintext, string salt)
101105
private string srvrTime()
102106
{
103107
string timeResponse = this.get("/api/v2/time", false);
104-
ServerTime serverTimeObject = Newtonsoft.Json.JsonConvert.DeserializeObject<ServerTime>(timeResponse);
105-
time = serverTimeObject.serverTime;
106-
return time;
108+
JObject timeObject = JsonConvert.DeserializeObject<JObject>(timeResponse);
109+
110+
if (timeObject["error_id"] == null)
111+
{
112+
this.time = "" + timeObject["serverTime"];
113+
return this.time;
114+
}
115+
return "0";
107116
}
108117

109118
public string get(string url, bool auth)
@@ -124,9 +133,13 @@ public string get(string url, bool auth)
124133
}
125134

126135
var response = client.Execute(request, RestSharp.Method.GET);
127-
//Console.Out.WriteLine("res: [" + response.Content + "]");
128-
var content = response.Content;
129-
return content;
136+
Console.Out.WriteLine("res: [" + response.Content + "]");
137+
138+
if (response.StatusCode != HttpStatusCode.OK)
139+
{
140+
return "{error_id: -1}";
141+
}
142+
return response.Content;
130143
}
131144

132145
public string post(string url, string payload, bool requestId)
@@ -156,9 +169,14 @@ public string post(string url, string payload, bool requestId)
156169
}
157170

158171
var response = client.Execute(request, RestSharp.Method.POST);
159-
//Console.Out.WriteLine("res: [" + response.Content + "]");
160-
var content = response.Content;
161-
return content;
172+
Console.ForegroundColor = ConsoleColor.DarkGray;
173+
Console.Out.WriteLine("res: [" + response.Content + "]");
174+
175+
if (response.StatusCode != HttpStatusCode.OK)
176+
{
177+
return "{error_id: -1}";
178+
}
179+
return response.Content;
162180
}
163181

164182
public string delete(string url, bool requestId)
@@ -181,14 +199,14 @@ public string delete(string url, bool requestId)
181199
}
182200

183201
var response = client.Execute(request, RestSharp.Method.DELETE);
184-
//Console.Out.WriteLine("res: [" + response.Content + "]");
185-
var content = response.Content;
186-
return content;
187-
}
188-
}
202+
Console.ForegroundColor = ConsoleColor.DarkGray;
203+
Console.Out.WriteLine("res: [" + response.Content + "]");
189204

190-
public class ServerTime
191-
{
192-
public string serverTime { get; set; }
205+
if (response.StatusCode != HttpStatusCode.OK)
206+
{
207+
return "{error_id: -1}";
208+
}
209+
return response.Content;
210+
}
193211
}
194212
}

NHB3/ApiConnect.cs

+35-1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ public void setup(ApiSettings settings) {
2424

2525
//read algo settings
2626
string algosResponse = api.get("/main/api/v2/mining/algorithms", false);
27+
2728
JObject aslgoObject = JsonConvert.DeserializeObject<JObject>(algosResponse);
2829
if (aslgoObject["error_id"] == null)
2930
{
@@ -33,6 +34,10 @@ public void setup(ApiSettings settings) {
3334

3435
public JArray getMarket() {
3536
string marketResponse = api.get("/main/api/v2/public/orders/active2", false);
37+
if (String.IsNullOrEmpty(marketResponse)) {
38+
return new JArray();
39+
}
40+
3641
JObject marektObject = JsonConvert.DeserializeObject<JObject>(marketResponse);
3742
if (marektObject["error_id"] == null)
3843
{
@@ -43,8 +48,8 @@ public JArray getMarket() {
4348

4449
public bool settingsOk() {
4550
string accountsResponse = api.get("/main/api/v2/accounting/accounts2", true);
46-
JObject accountsObject = JsonConvert.DeserializeObject<JObject>(accountsResponse);
4751

52+
JObject accountsObject = JsonConvert.DeserializeObject<JObject>(accountsResponse);
4853
if (accountsObject["error_id"] == null)
4954
{
5055
return true;
@@ -54,13 +59,16 @@ public bool settingsOk() {
5459

5560
public JObject getBalance(string currency) {
5661
string accountsResponse = api.get("/main/api/v2/accounting/accounts2", true);
62+
5763
JObject accountsObject = JsonConvert.DeserializeObject<JObject>(accountsResponse);
5864

5965
if (accountsObject["error_id"] != null)
6066
{
6167
//api call failed
6268
Console.WriteLine("Error reading API ... {0}", accountsObject);
69+
return null;
6370
}
71+
6472
JArray currencies = accountsObject["currencies"] as JArray;
6573
foreach (JObject obj in currencies)
6674
{
@@ -228,6 +236,17 @@ public ApiSettings readSettings() {
228236
return new ApiSettings();
229237
}
230238

239+
public OrdersSettings readOrdersSettings()
240+
{
241+
String fileName = Path.Combine(Directory.GetCurrentDirectory(), "orders.json");
242+
if (File.Exists(fileName))
243+
{
244+
OrdersSettings saved = JsonConvert.DeserializeObject<OrdersSettings>(File.ReadAllText(@fileName));
245+
return saved;
246+
}
247+
return new OrdersSettings();
248+
}
249+
231250
public JObject getAlgo(string algo) {
232251
foreach (JObject obj in algorithms)
233252
{
@@ -245,6 +264,9 @@ private string getApiUrl(int Enviorment)
245264
{
246265
return "https://api2.nicehash.com";
247266
}
267+
else if (Enviorment == 99) {
268+
return "https://api-test-dev.nicehash.com";
269+
}
248270
return "https://api-test.nicehash.com";
249271
}
250272

@@ -259,5 +281,17 @@ public class ApiSettings
259281
public int Enviorment { get; set; }
260282
}
261283

284+
public class OrdersSettings
285+
{
286+
public List<OrderSettings> OrderList { get; set; }
287+
}
288+
289+
public class OrderSettings
290+
{
291+
292+
public string Id { get; set; }
293+
294+
public string MaxPrice { get; set; }
295+
}
262296
}
263297
}

NHB3/Home.cs

+54-6
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ public Home()
3131
ac = new ApiConnect();
3232

3333
ApiSettings saved = ac.readSettings();
34+
OrdersSettings orders = ac.readOrdersSettings();
35+
3436
if (saved.OrganizationID != null) {
3537
ac.setup(saved);
3638

@@ -100,6 +102,9 @@ private void refreshBalance() {
100102

101103
private void refreshOrders(bool fromThread)
102104
{
105+
//read custom order settings
106+
ApiConnect.OrdersSettings cos = ac.readOrdersSettings();
107+
103108
if (ac.connected)
104109
{
105110
orders = ac.getOrders();
@@ -125,6 +130,10 @@ private void refreshOrders(bool fromThread)
125130
cleanOrder.Add("spentPercent", "" + spent_factor.ToString("0.00")+ "%");
126131
cleanOrder.Add("limit", "" + order["limit"]);
127132
cleanOrder.Add("price", "" + order["price"]);
133+
134+
//max price
135+
cleanOrder.Add("maxPrice", getMaxPrice("" + order["id"], cos));
136+
128137
cleanOrder.Add("rigsCount", "" + order["rigsCount"]);
129138
cleanOrder.Add("acceptedCurrentSpeed", "" + order["acceptedCurrentSpeed"]);
130139
cleanOrders.Add(cleanOrder);
@@ -148,6 +157,17 @@ private void refreshOrders(bool fromThread)
148157
}
149158
}
150159

160+
private string getMaxPrice(string id, ApiConnect.OrdersSettings cos) {
161+
foreach (var order in cos.OrderList)
162+
{
163+
if (id.Equals(order.Id))
164+
{
165+
return order.MaxPrice;
166+
}
167+
}
168+
return "";
169+
}
170+
151171
private void refreshMarket() {
152172
if (ac.connected)
153173
{
@@ -197,7 +217,12 @@ private void runBot() {
197217
}
198218

199219
toolStripStatusLabel1.Text = "Working";
220+
221+
//read order individual settings
222+
ApiConnect.OrdersSettings cos = ac.readOrdersSettings();
223+
200224
BotSettings saved = JsonConvert.DeserializeObject<BotSettings>(File.ReadAllText(@fileName));
225+
Console.ForegroundColor = ConsoleColor.Green;
201226
Console.WriteLine("bot iteration tasks {0} {1} {2}", saved.reffilOrder, saved.lowerPrice, saved.increasePrice);
202227

203228
Control.CheckForIllegalCrossThreadCalls = false;
@@ -207,20 +232,23 @@ private void runBot() {
207232
refreshMarket();
208233
}
209234

210-
//do refill??
235+
Console.ForegroundColor = ConsoleColor.White;
211236
Console.WriteLine("orders to process: {0}", orders.Count);
212237

238+
//do refill??
213239
if (saved.reffilOrder) {
214240
foreach (JObject order in orders)
215241
{
216242
float payed = float.Parse("" + order["payedAmount"], CultureInfo.InvariantCulture);
217243
float available = float.Parse("" + order["availableAmount"], CultureInfo.InvariantCulture);
218244
float spent_factor = payed/available*100;
245+
Console.ForegroundColor = ConsoleColor.Cyan;
219246
Console.WriteLine("?refill?; order {0}, payed {1}, available {2}, percent {3}", order["id"], payed, available, spent_factor.ToString("0.00"));
220247

221248
if (spent_factor > 90)
222249
{
223250
JObject algo = ac.getAlgo(""+order["algorithm"]["algorithm"]);
251+
Console.ForegroundColor = ConsoleColor.Cyan;
224252
Console.WriteLine("===> refill order for {0}", algo["minimalOrderAmount"]);
225253
ac.refillOrder(""+order["id"], ""+algo["minimalOrderAmount"]);
226254
}
@@ -233,17 +261,36 @@ private void runBot() {
233261
string order_type = "" + order["type"]["code"];
234262
if (order_type.Equals("STANDARD"))
235263
{
264+
//get order custom settings
265+
String omp = getMaxPrice("" + order["id"], cos);
266+
float maxOrderPriceLimit = 0F;
267+
if (!String.IsNullOrEmpty(omp)) {
268+
maxOrderPriceLimit = float.Parse("" + omp, CultureInfo.InvariantCulture);
269+
}
270+
236271
JObject algo = ac.getAlgo("" + order["algorithm"]["algorithm"]);
237272
float order_speed = float.Parse("" + order["acceptedCurrentSpeed"], CultureInfo.InvariantCulture);
273+
float rigs_count = float.Parse("" + order["rigsCount"], CultureInfo.InvariantCulture);
238274
float order_price = float.Parse("" + order["price"], CultureInfo.InvariantCulture);
239275
float price_step_down = float.Parse("" + algo["priceDownStep"], CultureInfo.InvariantCulture);
240-
Console.WriteLine("?adjust price?; order {0}, speed {1}, price {2}, step_down {3}", order["id"], order_speed, order_price, price_step_down);
241276

242-
if (saved.increasePrice && order_speed == 0) {
277+
Console.ForegroundColor = ConsoleColor.Green;
278+
Console.WriteLine("?adjust price?; order {0}, speed {1}, rigs {2}, price {3}, step_down {4}, max order limit {5}", order["id"], order_speed, rigs_count, order_price, price_step_down, maxOrderPriceLimit);
279+
280+
if (saved.increasePrice && (order_speed == 0 || rigs_count == 0)) {
243281
float new_price = (float)Math.Round(order_price + (price_step_down * -1), 4);
244-
Console.WriteLine("===> price up order to {0}", new_price);
245-
ac.updateOrder("" + order["algorithm"]["algorithm"], "" + order["id"], new_price.ToString(new CultureInfo("en-US")), "" + order["limit"]);
246-
} else if (saved.lowerPrice && order_speed > 0) {
282+
283+
if (maxOrderPriceLimit > 0 && new_price > maxOrderPriceLimit) {
284+
Console.ForegroundColor = ConsoleColor.Red;
285+
Console.WriteLine("===> price up denied - max limit enforced {0} {1}", new_price, maxOrderPriceLimit);
286+
}
287+
else
288+
{
289+
Console.ForegroundColor = ConsoleColor.Yellow;
290+
Console.WriteLine("===> price up order to {0}", new_price);
291+
ac.updateOrder("" + order["algorithm"]["algorithm"], "" + order["id"], new_price.ToString(new CultureInfo("en-US")), "" + order["limit"]);
292+
}
293+
} else if (saved.lowerPrice && (order_speed > 0 || rigs_count > 0)) {
247294
Dictionary<string, float> market = getOrderPriceRangesForAlgoAndMarket("" + order["algorithm"]["algorithm"], "" + order["market"]);
248295
var list = market.Keys.ToList();
249296
list.Sort();
@@ -260,6 +307,7 @@ private void runBot() {
260307

261308
if (idx > 1) {
262309
float new_price = (float)Math.Round(order_price + price_step_down, 4);
310+
Console.ForegroundColor = ConsoleColor.Yellow;
263311
Console.WriteLine("===> price down order to {0}", new_price);
264312
ac.updateOrder("" + order["algorithm"]["algorithm"], "" + order["id"], new_price.ToString(new CultureInfo("en-US")), "" + order["limit"]);
265313
}

0 commit comments

Comments
 (0)