PTMagic/Core/ProfitTrailer/StrategyHelper.cs

457 lines
14 KiB
C#
Raw Normal View History

2018-05-22 10:11:50 +02:00
using System;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using System.Net;
using System.Net.Security;
using System.Security.Cryptography.X509Certificates;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using Core.Main;
using Core.Helper;
using Core.Main.DataObjects.PTMagicData;
using Newtonsoft.Json;
namespace Core.ProfitTrailer
{
public static class StrategyHelper
{
public static string GetStrategyShortcut(string strategyName, bool onlyValidStrategies)
{
2018-05-22 10:11:50 +02:00
string result = strategyName;
switch (strategyName.ToLower())
{
2018-05-22 10:11:50 +02:00
case "lowbb":
result = "LBB";
break;
case "highbb":
result = "HBB";
break;
case "gain":
result = "G";
break;
case "loss":
result = "L";
break;
case "smagain":
result = "SG";
break;
case "emagain":
result = "EG";
2019-01-24 13:03:19 +01:00
case "hmagain":
result = "EG";
case "dmagain":
result = "EG";
2018-05-22 10:11:50 +02:00
break;
case "smaspread":
result = "SS";
break;
case "emaspread":
result = "ES";
break;
2019-01-24 13:03:19 +01:00
case "hmaspread":
result = "ES";
case "dmaspread":
result = "ES";
2018-05-22 10:11:50 +02:00
case "smacross":
result = "SC";
break;
case "emacross":
result = "EC";
break;
2019-01-24 13:03:19 +01:00
case "hmacross":
result = "SC";
break;
case "dmacross":
result = "EC";
break;
2018-05-22 10:11:50 +02:00
case "rsi":
result = "RSI";
break;
case "stoch":
result = "STOCH";
break;
case "stochrsi":
result = "SRSI";
break;
2018-12-01 17:55:12 +01:00
case "stochrsik":
result = "SRSIK";
break;
case "stochrsid":
result = "SRSID";
break;
case "stochrsicross":
result = "SRSIC";
break;
2018-05-22 10:11:50 +02:00
case "macd":
result = "MACD";
break;
case "obv":
result = "OBV";
break;
case "bbwidth":
result = "BBW";
2019-01-24 13:03:19 +01:00
break
case "pdhigh":
result = "PDH";
2018-05-22 10:11:50 +02:00
break;
case "anderson":
result = "AND";
break;
case "som enabled":
result = "SOM";
break;
case "max buy times":
2018-12-19 15:40:44 +01:00
result = "DCAMAX";
2018-05-22 10:11:50 +02:00
break;
case "max pairs":
result = "PAIRS";
break;
case "max spread":
result = "SPREAD";
break;
case "price increase":
result = "PIN";
break;
case "min buy volume":
result = "VOL";
break;
case "min buy balance":
result = "MIN";
break;
case "coin age":
result = "AGE";
break;
case "too new":
result = "NEW";
break;
case "blacklisted":
result = "BLACK";
break;
case "insufficient balance":
result = "BAL";
break;
case "max cost reached":
result = "COST";
break;
default:
break;
}
if (onlyValidStrategies)
{
if (strategyName.IndexOf("SOM") > -1 || strategyName.IndexOf("MAX") > -1 || strategyName.IndexOf("MIN") > -1 || strategyName.IndexOf("PRICE") > -1 || strategyName.IndexOf("BLACK") > -1 || strategyName.IndexOf("INSUFFICIENT") > -1 || strategyName.IndexOf("COST") > -1)
{
2018-05-22 10:11:50 +02:00
result = "";
}
}
return result;
}
public static bool IsValidStrategy(string strategyName)
{
2018-05-22 10:11:50 +02:00
return StrategyHelper.IsValidStrategy(strategyName, false);
}
public static bool IsValidStrategy(string strategyName, bool checkForAnyInvalid)
{
2018-05-22 10:11:50 +02:00
bool result = false;
if (!checkForAnyInvalid)
{
switch (strategyName.ToLower())
{
2018-05-22 10:11:50 +02:00
case "lowbb":
case "highbb":
case "gain":
case "loss":
case "smagain":
case "emagain":
2019-01-24 13:03:19 +01:00
case "hmagain":
case "dmagain":
2018-05-22 10:11:50 +02:00
case "smaspread":
case "emaspread":
2019-01-24 13:03:19 +01:00
case "hmaspread":
case "dmaspread":
2018-05-22 10:11:50 +02:00
case "smacross":
case "emacross":
2019-01-24 13:03:19 +01:00
case "hmacross":
case "dmacross":
2018-05-22 10:11:50 +02:00
case "rsi":
case "stoch":
case "stochrsi":
2018-12-01 18:06:14 +01:00
case "stochrsik":
case "stochrsid":
case "stochrsicross":
2018-05-22 10:11:50 +02:00
case "macd":
case "obv":
case "bbwidth":
case "anderson":
2019-01-24 13:03:19 +01:00
case "dema":
case "hma":
case "pdhigh":
2018-05-22 10:11:50 +02:00
result = true;
break;
default:
break;
}
}
else
{
2018-05-22 10:11:50 +02:00
if (strategyName.IndexOf("max", StringComparison.InvariantCultureIgnoreCase) == -1
&& strategyName.IndexOf("min", StringComparison.InvariantCultureIgnoreCase) == -1
&& strategyName.IndexOf("som", StringComparison.InvariantCultureIgnoreCase) == -1
&& strategyName.IndexOf("price", StringComparison.InvariantCultureIgnoreCase) == -1
&& strategyName.IndexOf("black", StringComparison.InvariantCultureIgnoreCase) == -1
&& strategyName.IndexOf("new", StringComparison.InvariantCultureIgnoreCase) == -1
&& strategyName.IndexOf("insufficient", StringComparison.InvariantCultureIgnoreCase) == -1
&& strategyName.IndexOf("timeout", StringComparison.InvariantCultureIgnoreCase) == -1
&& strategyName.IndexOf("spread", StringComparison.InvariantCultureIgnoreCase) == -1
&& strategyName.IndexOf("pairs", StringComparison.InvariantCultureIgnoreCase) == -1)
{
2018-05-22 10:11:50 +02:00
result = true;
}
}
return result;
}
public static int GetStrategyValueDecimals(string strategyName)
{
2018-05-22 10:11:50 +02:00
int result = 0;
switch (strategyName.ToLower())
{
2018-05-22 10:11:50 +02:00
case "lowbb":
case "highbb":
result = 8;
break;
case "gain":
case "loss":
case "smagain":
case "emagain":
2019-01-24 13:03:19 +01:00
case "hmagain":
case "dmagain":
2018-05-22 10:11:50 +02:00
case "smaspread":
case "emaspread":
2019-01-24 13:03:19 +01:00
case "hmaspread":
case "dmaspread":
2018-05-22 10:11:50 +02:00
case "smacross":
case "emacross":
2019-01-24 13:03:19 +01:00
case "hmacross":
case "dmacross":
case "anderson":
case "pdhigh":
2018-05-22 10:11:50 +02:00
result = 2;
break;
case "rsi":
case "stochrsi":
2018-12-01 18:06:14 +01:00
case "stochrsik":
case "stochrsid":
case "stochrsicross":
2018-05-22 10:11:50 +02:00
case "stoch":
case "macd":
case "obv":
case "bbwidth":
result = 0;
break;
default:
break;
}
return result;
}
public static string GetStrategyText(Summary summary, List<Strategy> strategies, string strategyText, bool isTrue, bool isTrailingBuyActive)
{
if (strategies.Count > 0)
{
foreach (Strategy strategy in strategies)
{
string textClass = (strategy.IsTrue) ? "label-success" : "label-danger";
if (!StrategyHelper.IsValidStrategy(strategy.Name))
{
2018-05-22 10:11:50 +02:00
strategyText += "<span class=\"label label-warning\" data-toggle=\"tooltip\" data-placement=\"top\" title=\"" + strategy.Name + "\">" + StrategyHelper.GetStrategyShortcut(strategy.Name, false) + "</span> ";
}
else
{
2018-05-22 10:11:50 +02:00
strategyText += "<span class=\"label " + textClass + "\" data-toggle=\"tooltip\" data-placement=\"top\" title=\"" + strategy.Name + "\">" + StrategyHelper.GetStrategyShortcut(strategy.Name, false) + "</span> ";
}
}
if (isTrailingBuyActive)
{
2018-05-22 10:11:50 +02:00
strategyText += " <i class=\"fa fa-flag text-success\" data-toggle=\"tooltip\" data-placement=\"top\" title=\"Trailing active!\"></i>";
}
}
else
{
if (isTrue)
{
2018-05-22 10:11:50 +02:00
strategyText = "<span class=\"label label-success\" data-toggle=\"tooltip\" data-placement=\"top\" title=\"" + strategyText + "\">" + StrategyHelper.GetStrategyShortcut(strategyText, true) + "</span>";
if (isTrailingBuyActive)
{
strategyText += " <i class=\"fa fa-flag text-success\" data-toggle=\"tooltip\" data-placement=\"top\" title=\"Trailing active!\"></i>";
}
}
else
{
if (StrategyHelper.IsValidStrategy(strategyText))
{
2018-05-22 10:11:50 +02:00
strategyText = "<span class=\"label label-danger\" data-toggle=\"tooltip\" data-placement=\"top\" title=\"" + strategyText + "\">" + StrategyHelper.GetStrategyShortcut(strategyText, true) + "</span>";
}
else if (strategyText.Equals(""))
{
2018-05-22 10:11:50 +02:00
strategyText = summary.DCABuyStrategy;
strategyText = "<span class=\"label label-danger\" data-toggle=\"tooltip\" data-placement=\"top\" title=\"" + strategyText + "\">" + StrategyHelper.GetStrategyShortcut(strategyText, true) + "</span>";
}
else
{
2018-05-22 10:11:50 +02:00
strategyText = "<span class=\"label label-warning\" data-toggle=\"tooltip\" data-placement=\"top\" title=\"" + strategyText + "\">" + StrategyHelper.GetStrategyShortcut(strategyText, false) + "</span> ";
}
}
}
return strategyText;
}
public static string GetCurrentValueText(List<Strategy> strategies, string strategyText, double bbValue, double simpleValue, bool includeShortcut)
{
2018-05-22 10:11:50 +02:00
string result = "";
if (strategies.Count > 0)
{
foreach (Strategy strategy in strategies)
{
if (StrategyHelper.IsValidStrategy(strategy.Name))
{
2018-05-22 10:11:50 +02:00
if (!result.Equals("")) result += "<br />";
string decimalFormat = "";
int decimals = StrategyHelper.GetStrategyValueDecimals(strategy.Name);
for (int d = 1; d <= decimals; d++)
{
2018-05-22 10:11:50 +02:00
decimalFormat += "0";
}
if (includeShortcut)
{
2018-05-22 10:11:50 +02:00
result += "<span class=\"text-muted\">" + StrategyHelper.GetStrategyShortcut(strategy.Name, true) + "</span> ";
}
if (StrategyHelper.GetStrategyShortcut(strategy.Name, true).IndexOf("and", StringComparison.InvariantCultureIgnoreCase) > -1)
{
2018-05-22 10:11:50 +02:00
result += simpleValue.ToString("#,#0.00", new System.Globalization.CultureInfo("en-US"));
}
else
{
if (decimals == 0)
{
if (!SystemHelper.IsInteger(strategy.CurrentValue))
{
2018-05-22 10:11:50 +02:00
result += strategy.CurrentValue.ToString("#,#", new System.Globalization.CultureInfo("en-US"));
}
else
{
2018-05-22 10:11:50 +02:00
result += strategy.CurrentValue.ToString("#,#0", new System.Globalization.CultureInfo("en-US"));
}
}
else
{
2018-05-22 10:11:50 +02:00
result += strategy.CurrentValue.ToString("#,#0." + decimalFormat, new System.Globalization.CultureInfo("en-US"));
}
}
}
}
}
else
{
if (StrategyHelper.GetStrategyShortcut(strategyText, true).IndexOf("bb", StringComparison.InvariantCultureIgnoreCase) > -1)
{
2018-05-22 10:11:50 +02:00
result = bbValue.ToString("#,#0.00000000", new System.Globalization.CultureInfo("en-US"));
}
else
{
2018-05-22 10:11:50 +02:00
result = simpleValue.ToString("#,#0.00", new System.Globalization.CultureInfo("en-US")) + "%";
}
}
return result;
}
public static string GetTriggerValueText(Summary summary, List<Strategy> strategies, string strategyText, double bbValue, double simpleValue, int buyLevel, bool includeShortcut)
{
2018-05-22 10:11:50 +02:00
string result = "";
if (strategies.Count > 0)
{
foreach (Strategy strategy in strategies)
{
if (StrategyHelper.IsValidStrategy(strategy.Name))
{
2018-05-22 10:11:50 +02:00
if (!result.Equals("")) result += "<br />";
string decimalFormat = "";
int decimals = StrategyHelper.GetStrategyValueDecimals(strategy.Name);
for (int d = 1; d <= decimals; d++)
{
2018-05-22 10:11:50 +02:00
decimalFormat += "0";
}
if (includeShortcut)
{
2018-05-22 10:11:50 +02:00
result += "<span class=\"text-muted\">" + StrategyHelper.GetStrategyShortcut(strategy.Name, true) + "</span> ";
}
if (StrategyHelper.GetStrategyShortcut(strategy.Name, true).IndexOf("and", StringComparison.InvariantCultureIgnoreCase) > -1)
{
2018-05-22 10:11:50 +02:00
result += strategy.TriggerValue.ToString("#,#0.00", new System.Globalization.CultureInfo("en-US"));
}
else
{
if (decimals == 0)
{
if (!SystemHelper.IsInteger(strategy.EntryValue))
{
2018-05-22 10:11:50 +02:00
result += strategy.EntryValue.ToString(new System.Globalization.CultureInfo("en-US"));
}
else
{
2018-05-22 10:11:50 +02:00
result += strategy.EntryValue.ToString("#,#0", new System.Globalization.CultureInfo("en-US"));
}
}
else
{
2018-05-22 10:11:50 +02:00
result += strategy.EntryValue.ToString("#,#0." + decimalFormat, new System.Globalization.CultureInfo("en-US"));
}
}
}
}
}
else
{
if (StrategyHelper.GetStrategyShortcut(strategyText, true).IndexOf("bb", StringComparison.InvariantCultureIgnoreCase) > -1)
{
2018-05-22 10:11:50 +02:00
result = bbValue.ToString("#,#0.00000000", new System.Globalization.CultureInfo("en-US"));
}
else
{
if (simpleValue == Constants.MinTrendChange)
{
if (summary.DCATriggers.ContainsKey(buyLevel + 1))
{
2018-05-22 10:11:50 +02:00
simpleValue = summary.DCATriggers[buyLevel + 1];
}
}
result = simpleValue.ToString("#,#0.00", new System.Globalization.CultureInfo("en-US")) + "%";
}
}
return result;
}
}
}