Skip to content

Commit

Permalink
Add project files.
Browse files Browse the repository at this point in the history
  • Loading branch information
omidmohajers committed Jan 21, 2023
1 parent 77539bb commit 0cc276c
Show file tree
Hide file tree
Showing 17 changed files with 961 additions and 0 deletions.
28 changes: 28 additions & 0 deletions ATR.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using System;

namespace PA.Trading.UAPI
{
public class ATRCalculator
{
public static double[] Calculate(int period, UCandle[] timeSeries)
{
var temp = new double[timeSeries.Length];
temp[0] = timeSeries[0].HighPrice - timeSeries[0].LowPrice;

for (var i = 1; i < timeSeries.Length; i++)
{
var diff1 = Math.Abs(timeSeries[i].HighPrice - timeSeries[i - 1].ClosePrice);
var diff2 = Math.Abs(timeSeries[i].LowPrice - timeSeries[i - 1].ClosePrice);
var diff3 = timeSeries[i].HighPrice - timeSeries[i].LowPrice;

var max = Math.Max(diff1, diff3);
// var max = diff1 > diff2 ? diff1 : diff2;
temp[i] = Math.Max(max, diff2); ;
}

var atr = RMACalculator.Calculate(temp, period);

return atr;
}
}
}
18 changes: 18 additions & 0 deletions Bases/MarketApiBase.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using System.Collections.Generic;

namespace PA.Trading.UAPI
{
public class MarketApiBase<T>
{
public virtual List<T> Request(CandleRequestParams param)
{
List<T> list = new List<T>();
return list;
}
public virtual List<T> ConvertStringToSymbolPrice(string[] cArray)
{
List<T> list = new List<T>();
return list;
}
}
}
60 changes: 60 additions & 0 deletions Binance/fapi/BinanceFApi.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
using RestSharp;
using System;
using System.Collections.Generic;

namespace PA.Trading.UAPI.Binance.fapi
{
public class BinanceFApi : MarketApiBase<UCandle>
{
public override List<UCandle> Request(CandleRequestParams param)
{
List<UCandle> list = new List<UCandle>();

try
{
var client = new RestClient("https://binance.com/");
// "https://fapi.binance.com/fapi/v1/klines?symbol=BTCUSDT&interval=5m&limit=3";
string requestString = "fapi/";
if (param.CadleCount > 0)
{
requestString = string.Format("fapi/{0}/klines?symbol={1}&interval={2}&limit={3}", param.Version, param.Symbol, param.Interval.ToString().Substring(1), param.CadleCount);
}
else
{
requestString = string.Format("fapi/{0}/klines?symbol={1}&interval={2}&startTime={3}&endTime={4}", param.Version, param.Symbol, param.Interval.ToString(), Helper.DateTimeToUnixTimeStamp(param.StartDate), Helper.DateTimeToUnixTimeStamp(param.EndDate));
}
var request = new RestRequest(requestString);
var response = client.Post(request);
var content = response.Content;
string[] data = content.Split(new string[] { "],[" }, StringSplitOptions.RemoveEmptyEntries);
list = ConvertStringToSymbolPrice(data);
}
catch(Exception ex)
{

}

return list;
}

public override List<UCandle> ConvertStringToSymbolPrice(string[] cArray)
{
List<UCandle> list = new List<UCandle>();
foreach(string s in cArray)
{
string[] fields = s.Split(',');
UCandle uc = new UCandle();
uc.OpenTime = Helper.GetJsonTime(fields[0]);
uc.OpenPrice = Helper.ConvertStringToDecimal(fields[1]);
uc.HighPrice = Helper.ConvertStringToDecimal(fields[2]);
uc.LowPrice = Helper.ConvertStringToDecimal(fields[3]);
uc.ClosePrice = Helper.ConvertStringToDecimal(fields[4]);
uc.Volume = Helper.ConvertStringToDecimal(fields[5]);
uc.CloseTime = Helper.GetJsonTime(fields[6]);
uc.NumberOfTrades = Helper.ConvertStringToLong(fields[7]);
list.Add(uc);
}
return list;
}
}
}
127 changes: 127 additions & 0 deletions Binance/sapi/BinanceSApi.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
using RestSharp;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;

namespace PA.Trading.UAPI.Binance.sapi
{
public class BinanceSApi : MarketApiBase<USymbolPrice>
{
public override List<USymbolPrice> Request(CandleRequestParams param)
{
List<USymbolPrice> list = new List<USymbolPrice>();

try
{
var client = new RestClient("https://binance.com/");
// "https://fapi.binance.com/fapi/v1/klines?symbol=BTCUSDT&interval=5m&limit=3";
string requestString = "api/";
if (string.IsNullOrWhiteSpace(param.Symbol))
{
requestString = string.Format("api/v3/ticker/price", param.Version, param.Symbol, param.Interval.ToString().Substring(1), param.CadleCount);
}
else
{
requestString = string.Format("api/v3/ticker/price?symbol={0}", param.Symbol);
}
// client.Authenticator = new HttpBasicAuthenticator(username, password);
var request = new RestRequest(requestString);
var response = client.Post(request);
//while (!response.IsSuccessful)
//{
// response = client.Post(request);
//}
var content = response.Content; // Raw content as string
string[] data = content.Split(new string[] { "},{" }, StringSplitOptions.RemoveEmptyEntries);

list = ConvertStringToSymbolPrice(data);
// var response2 = client.Post<Person>(request);
// var name = response2.Data.Name;
}
catch (Exception ex)
{

}

return list;
}

public override List<USymbolPrice> ConvertStringToSymbolPrice(string[] cArray)
{
List<USymbolPrice> list = new List<USymbolPrice>();
List<string[]> records = new List<string[]>();
cArray[0] = cArray[0].Substring(2);
foreach (string s in cArray)
{
string[] fields = s.Split(',');
fields[0] = fields[0].Substring(10,fields[0].Length - 11);
// fields[1] = Helper.ConvertStringToDecimal(fields[1]).ToString();
records.Add(fields);
}

var usdts = records.Where(x => x[0].EndsWith("USDT")).ToArray();
string[] market = File.ReadAllLines("Spot.txt");
for (int i = 0; i < market.Length; i++)
market[i] = market[i].Trim();
foreach (string[] pair in usdts)
{
USymbolPrice us = new USymbolPrice();
us.Symbol = pair[0].Substring(0, pair[0].IndexOf("USDT"));
if (!market.Contains(us.Symbol))
continue;
us.USDTPrice = Helper.ConvertStringToDecimal(pair[1]);
if (us.Symbol == "BTC")
{
Helper.BtcUsdt = us.USDTPrice;
}
if (us.Symbol == "ETH")
{
Helper.EthUsdt = us.USDTPrice;
}
if (us.Symbol == "BNB")
{
Helper.BnbUsdt = us.USDTPrice;
}
if (us.Symbol == "BUSD")
{
Helper.BusdUsdt = us.USDTPrice;
}
list.Add(us);
}



var btcs = records.Where(x => x[0].EndsWith("BTC")).ToArray();
foreach (string[] pair in btcs)
{
string symbol = pair[0].Substring(0, pair[0].Length - 3);
if (symbol == "REN")
{

}
USymbolPrice us = list.FirstOrDefault(x => x.Symbol == symbol);
if (us != null)
us.BTCPrice = Helper.ConvertStringToDecimal(pair[1]);
}
// var bnbs = records.Where(x => x[0].EndsWith("BNB")).ToArray();
//foreach (string[] pair in bnbs)
//{
// string symbol = pair[0].Substring(0, pair[0].IndexOf("BNB"));
// USymbolPrice us = list.FirstOrDefault(x => x.Symbol == symbol);
// if (us != null)
// us.BNBPrice = Helper.ConvertStringToDecimal(pair[1]);
//}
//var eths = records.Where(x => x[0].EndsWith("ETH")).ToArray();
//foreach (string[] pair in eths)
//{
// string symbol = pair[0].Substring(0, pair[0].IndexOf("ETH"));
// USymbolPrice us = list.FirstOrDefault(x => x.Symbol == symbol);
// if (us != null)
// us.ETHPrice = Helper.ConvertStringToDecimal(pair[1]);
//}

return list;
}
}
}
38 changes: 38 additions & 0 deletions CandleRequest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using System.Collections.Generic;

namespace PA.Trading.UAPI
{
public class CandleRequest
{
public CandleRequest()
{

}
public List<UCandle> Fetch(CandleRequestParams param)
{
List<UCandle> list = new List<UCandle>();

switch (param.Api)
{
case "fapi":
Binance.fapi.BinanceFApi fapi = new Binance.fapi.BinanceFApi();
list = fapi.Request(param);
break;
}
return list;
}
public List<USymbolPrice> FetchPrice(CandleRequestParams param)
{
List<USymbolPrice> list = new List<USymbolPrice>();

switch (param.Api)
{
case "sapi":
Binance.sapi.BinanceSApi fapi = new Binance.sapi.BinanceSApi();
list = fapi.Request(param);
break;
}
return list;
}
}
}
15 changes: 15 additions & 0 deletions CandleRequestParams.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using System;

namespace PA.Trading.UAPI
{
public class CandleRequestParams
{
public string Version { get; set; }
public string Symbol { get; set; }
public string Api { get; set; }
public DateTime StartDate { get; set; }
public DateTime EndDate { get; set; }
public int CadleCount { get; set; }
public Resolution Interval { get; set; }
}
}
69 changes: 69 additions & 0 deletions EMACalculator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
namespace PA.Trading.UAPI
{
public static class EMACalculator
{


/// <summary>
/// Calculates indicator.
/// </summary>
/// <param name="price">Price series.</param>
/// <param name="period">Indicator period.</param>
/// <returns>Calculated indicator series.</returns>
public static double[] Calculate(double[] price, int period)
{
var ema = new double[price.Length];
double sum = SMACalculator.Calculate(price,period)[0];
ema[0] = sum;
double alpha = 2.0 / (1.0 + period);

for (int i = 1; i < price.Length; i++)
{
sum += alpha * (price[i] - sum);
ema[i] = sum;
}

return ema;
}
public static double[] Calculate(UCandle[] candlesticks, int period)
{
var ema = new double[candlesticks.Length];
double[] src = new double[candlesticks.Length];
for (int i = 0; i < candlesticks.Length; i++)
src[i] = candlesticks[i].ClosePrice;
return Calculate(src, period);
}
}

public static class RMACalculator
{
/// <summary>
/// Calculates indicator.
/// </summary>
/// <param name="price">Price series.</param>
/// <param name="period">Indicator period.</param>
/// <returns>Calculated indicator series.</returns>
public static double[] Calculate(double[] price, int period)
{
var ema = new double[price.Length];
double sum = price[0];
double alpha = 1.0 / period;

for (int i = 0; i < price.Length; i++)
{
sum += alpha * (price[i] - sum);
ema[i] = sum;
}

return ema;
}
public static double[] Calculate(UCandle[] candlesticks, int period)
{
var ema = new double[candlesticks.Length];
double[] src = new double[candlesticks.Length];
for (int i = 0; i < candlesticks.Length; i++)
src[i] = candlesticks[i].ClosePrice;
return Calculate(src, period);
}
}
}
Loading

0 comments on commit 0cc276c

Please sign in to comment.