Skip to content

Commit

Permalink
order settings
Browse files Browse the repository at this point in the history
  • Loading branch information
bl4z committed Nov 4, 2020
1 parent 0a9b73f commit b35028a
Show file tree
Hide file tree
Showing 5 changed files with 297 additions and 72 deletions.
54 changes: 36 additions & 18 deletions NHB3/Api.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
using System;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;

Expand Down Expand Up @@ -81,6 +84,7 @@ private static string JoinSegments(List<string> segments)
sb.Append(segment);
}
}
Console.ForegroundColor = ConsoleColor.DarkGray;
Console.Out.WriteLine("req: [" + sb.ToString() + "]");
return sb.ToString();
}
Expand All @@ -101,9 +105,14 @@ private static string CalcHMACSHA256Hash(string plaintext, string salt)
private string srvrTime()
{
string timeResponse = this.get("/api/v2/time", false);
ServerTime serverTimeObject = Newtonsoft.Json.JsonConvert.DeserializeObject<ServerTime>(timeResponse);
time = serverTimeObject.serverTime;
return time;
JObject timeObject = JsonConvert.DeserializeObject<JObject>(timeResponse);

if (timeObject["error_id"] == null)
{
this.time = "" + timeObject["serverTime"];
return this.time;
}
return "0";
}

public string get(string url, bool auth)
Expand All @@ -124,9 +133,13 @@ public string get(string url, bool auth)
}

var response = client.Execute(request, RestSharp.Method.GET);
//Console.Out.WriteLine("res: [" + response.Content + "]");
var content = response.Content;
return content;
Console.Out.WriteLine("res: [" + response.Content + "]");

if (response.StatusCode != HttpStatusCode.OK)
{
return "{error_id: -1}";
}
return response.Content;
}

public string post(string url, string payload, bool requestId)
Expand Down Expand Up @@ -156,9 +169,14 @@ public string post(string url, string payload, bool requestId)
}

var response = client.Execute(request, RestSharp.Method.POST);
//Console.Out.WriteLine("res: [" + response.Content + "]");
var content = response.Content;
return content;
Console.ForegroundColor = ConsoleColor.DarkGray;
Console.Out.WriteLine("res: [" + response.Content + "]");

if (response.StatusCode != HttpStatusCode.OK)
{
return "{error_id: -1}";
}
return response.Content;
}

public string delete(string url, bool requestId)
Expand All @@ -181,14 +199,14 @@ public string delete(string url, bool requestId)
}

var response = client.Execute(request, RestSharp.Method.DELETE);
//Console.Out.WriteLine("res: [" + response.Content + "]");
var content = response.Content;
return content;
}
}
Console.ForegroundColor = ConsoleColor.DarkGray;
Console.Out.WriteLine("res: [" + response.Content + "]");

public class ServerTime
{
public string serverTime { get; set; }
if (response.StatusCode != HttpStatusCode.OK)
{
return "{error_id: -1}";
}
return response.Content;
}
}
}
36 changes: 35 additions & 1 deletion NHB3/ApiConnect.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public void setup(ApiSettings settings) {

//read algo settings
string algosResponse = api.get("/main/api/v2/mining/algorithms", false);

JObject aslgoObject = JsonConvert.DeserializeObject<JObject>(algosResponse);
if (aslgoObject["error_id"] == null)
{
Expand All @@ -33,6 +34,10 @@ public void setup(ApiSettings settings) {

public JArray getMarket() {
string marketResponse = api.get("/main/api/v2/public/orders/active2", false);
if (String.IsNullOrEmpty(marketResponse)) {
return new JArray();
}

JObject marektObject = JsonConvert.DeserializeObject<JObject>(marketResponse);
if (marektObject["error_id"] == null)
{
Expand All @@ -43,8 +48,8 @@ public JArray getMarket() {

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

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

public JObject getBalance(string currency) {
string accountsResponse = api.get("/main/api/v2/accounting/accounts2", true);

JObject accountsObject = JsonConvert.DeserializeObject<JObject>(accountsResponse);

if (accountsObject["error_id"] != null)
{
//api call failed
Console.WriteLine("Error reading API ... {0}", accountsObject);
return null;
}

JArray currencies = accountsObject["currencies"] as JArray;
foreach (JObject obj in currencies)
{
Expand Down Expand Up @@ -228,6 +236,17 @@ public ApiSettings readSettings() {
return new ApiSettings();
}

public OrdersSettings readOrdersSettings()
{
String fileName = Path.Combine(Directory.GetCurrentDirectory(), "orders.json");
if (File.Exists(fileName))
{
OrdersSettings saved = JsonConvert.DeserializeObject<OrdersSettings>(File.ReadAllText(@fileName));
return saved;
}
return new OrdersSettings();
}

public JObject getAlgo(string algo) {
foreach (JObject obj in algorithms)
{
Expand All @@ -245,6 +264,9 @@ private string getApiUrl(int Enviorment)
{
return "https://api2.nicehash.com";
}
else if (Enviorment == 99) {
return "https://api-test-dev.nicehash.com";
}
return "https://api-test.nicehash.com";
}

Expand All @@ -259,5 +281,17 @@ public class ApiSettings
public int Enviorment { get; set; }
}

public class OrdersSettings
{
public List<OrderSettings> OrderList { get; set; }
}

public class OrderSettings
{

public string Id { get; set; }

public string MaxPrice { get; set; }
}
}
}
60 changes: 54 additions & 6 deletions NHB3/Home.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ public Home()
ac = new ApiConnect();

ApiSettings saved = ac.readSettings();
OrdersSettings orders = ac.readOrdersSettings();

if (saved.OrganizationID != null) {
ac.setup(saved);

Expand Down Expand Up @@ -100,6 +102,9 @@ private void refreshBalance() {

private void refreshOrders(bool fromThread)
{
//read custom order settings
ApiConnect.OrdersSettings cos = ac.readOrdersSettings();

if (ac.connected)
{
orders = ac.getOrders();
Expand All @@ -125,6 +130,10 @@ private void refreshOrders(bool fromThread)
cleanOrder.Add("spentPercent", "" + spent_factor.ToString("0.00")+ "%");
cleanOrder.Add("limit", "" + order["limit"]);
cleanOrder.Add("price", "" + order["price"]);

//max price
cleanOrder.Add("maxPrice", getMaxPrice("" + order["id"], cos));

cleanOrder.Add("rigsCount", "" + order["rigsCount"]);
cleanOrder.Add("acceptedCurrentSpeed", "" + order["acceptedCurrentSpeed"]);
cleanOrders.Add(cleanOrder);
Expand All @@ -148,6 +157,17 @@ private void refreshOrders(bool fromThread)
}
}

private string getMaxPrice(string id, ApiConnect.OrdersSettings cos) {
foreach (var order in cos.OrderList)
{
if (id.Equals(order.Id))
{
return order.MaxPrice;
}
}
return "";
}

private void refreshMarket() {
if (ac.connected)
{
Expand Down Expand Up @@ -197,7 +217,12 @@ private void runBot() {
}

toolStripStatusLabel1.Text = "Working";

//read order individual settings
ApiConnect.OrdersSettings cos = ac.readOrdersSettings();

BotSettings saved = JsonConvert.DeserializeObject<BotSettings>(File.ReadAllText(@fileName));
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("bot iteration tasks {0} {1} {2}", saved.reffilOrder, saved.lowerPrice, saved.increasePrice);

Control.CheckForIllegalCrossThreadCalls = false;
Expand All @@ -207,20 +232,23 @@ private void runBot() {
refreshMarket();
}

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

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

if (spent_factor > 90)
{
JObject algo = ac.getAlgo(""+order["algorithm"]["algorithm"]);
Console.ForegroundColor = ConsoleColor.Cyan;
Console.WriteLine("===> refill order for {0}", algo["minimalOrderAmount"]);
ac.refillOrder(""+order["id"], ""+algo["minimalOrderAmount"]);
}
Expand All @@ -233,17 +261,36 @@ private void runBot() {
string order_type = "" + order["type"]["code"];
if (order_type.Equals("STANDARD"))
{
//get order custom settings
String omp = getMaxPrice("" + order["id"], cos);
float maxOrderPriceLimit = 0F;
if (!String.IsNullOrEmpty(omp)) {
maxOrderPriceLimit = float.Parse("" + omp, CultureInfo.InvariantCulture);
}

JObject algo = ac.getAlgo("" + order["algorithm"]["algorithm"]);
float order_speed = float.Parse("" + order["acceptedCurrentSpeed"], CultureInfo.InvariantCulture);
float rigs_count = float.Parse("" + order["rigsCount"], CultureInfo.InvariantCulture);
float order_price = float.Parse("" + order["price"], CultureInfo.InvariantCulture);
float price_step_down = float.Parse("" + algo["priceDownStep"], CultureInfo.InvariantCulture);
Console.WriteLine("?adjust price?; order {0}, speed {1}, price {2}, step_down {3}", order["id"], order_speed, order_price, price_step_down);

if (saved.increasePrice && order_speed == 0) {
Console.ForegroundColor = ConsoleColor.Green;
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);

if (saved.increasePrice && (order_speed == 0 || rigs_count == 0)) {
float new_price = (float)Math.Round(order_price + (price_step_down * -1), 4);
Console.WriteLine("===> price up order to {0}", new_price);
ac.updateOrder("" + order["algorithm"]["algorithm"], "" + order["id"], new_price.ToString(new CultureInfo("en-US")), "" + order["limit"]);
} else if (saved.lowerPrice && order_speed > 0) {

if (maxOrderPriceLimit > 0 && new_price > maxOrderPriceLimit) {
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("===> price up denied - max limit enforced {0} {1}", new_price, maxOrderPriceLimit);
}
else
{
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine("===> price up order to {0}", new_price);
ac.updateOrder("" + order["algorithm"]["algorithm"], "" + order["id"], new_price.ToString(new CultureInfo("en-US")), "" + order["limit"]);
}
} else if (saved.lowerPrice && (order_speed > 0 || rigs_count > 0)) {
Dictionary<string, float> market = getOrderPriceRangesForAlgoAndMarket("" + order["algorithm"]["algorithm"], "" + order["market"]);
var list = market.Keys.ToList();
list.Sort();
Expand All @@ -260,6 +307,7 @@ private void runBot() {

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

0 comments on commit b35028a

Please sign in to comment.