- Daily Fund API
- Adding Project Dependencies
- Requesting API Data
- Get all fund codes
- Get fund NAV information
- Functional Testing
- Reference Links
Daily Fund API
The common APIs are listed below:
- All Fund Codes:/js/fundcode_search.js
- Fund Details:/pingzhongdata/
- Real-time fund information:http://fundgz./js/
- All fund companies:/js/jjjz_gs.js
The APIs used in this article are mainly the first two, where the001186It's a query.Fund CodeThe data returned by the interface is a js file.
The conventional method of parsing the data inside the js file will be more cumbersome, so this article uses theJint library to parse the data, which should be considered the first method on the net.
Adding Project Dependencies
Use NuGet to install the RestSharp and Jint libraries for the following purposes
- RestSharp: used to send HTTP requests
- Jint: for parsing js files
Requesting API Data
Request the API using the RestSharp library as follows
using RestSharp;
public static string GetFundAPIData(string url)
{
var client = new RestClient();
var request = new RestRequest(url, );
var response = (request);
if ()
{
return ?? ;
}
else
{
throw new Exception($"Failed to fetch data from the API: {}");
}
}
Get all fund codes
The data returned by the interface is rather long and has the following general structure:
HUAXIACHENGZHANGHUNHE"]]].
Define a FundInfo class to store fund information:
public class FundInfo
{
public string Code { get; set; }
public string Name { get; set; }
public string Type { get; set; }
}
Call the Request API method above to get the interface data and call the following method to parse the data:
public static List<FundInfo> ParseFundList(string js)
{
var fundList =new List<FundInfo>();
// initialization Jint engine (loanword)
Engine engine = new Engine();
// fulfillment JavaScript coding
var result = (js).GetValue("r");
// Ensure that the results are JavaScript arrays
if (())
{
var jsArray = ();
// commander-in-chief (military) JavaScript arrays转换为 C# List<List<string>> typology
var clrList = new List<List<string>>();
foreach (var item in jsArray)
{
var sublist = new List<string>();
foreach (var subitem in ())
{
(());
}
(sublist);
}
fundList = (x => new FundInfo { Code = x[0], Name = x[2], Type = x[3] }).ToList();
return fundList;
}
else
{
throw new InvalidOperationException("Expected an array but got something else.");
}
}
Call the above method to get the result:
// Get all fund codes
var js=GetFundAPIData("/js/fundcode_search.js");
var list = ParseFundList(js);
Get fund NAV information
The data returned by the interface is long and can be analyzed using theJavascript Formatting Online ToolTo process it, the data we need to parse is as follows:
var Data_netWorthTrend = [{
"x": 1430841600000,
"y": 1.0,
"equityReturn": 0,
"unitMoney": ""
}, {
"x": 1431014400000,
"y": 1.004,
"equityReturn": 0,
"unitMoney": ""
}];
Define a NetWorthTrendItem class to hold fund net worth information:
public class NetWorthTrendItem
{
public DateTime Time { get; set; } // timestamp
public decimal Value { get; set; } // net value
public decimal EquityReturn { get; set; } // rise or fall in price
public string UnitMoney { get; set; } // per diem
}
Get JavaScript arrays directly from Jint and convert them to C# objects:
public static List<NetWorthTrendItem> GetFundValues(string js)
{
var engine = new Engine();
// define JavaScript variant
(js);
// gain JavaScript arrays
var jsArray = ("Data_netWorthTrend");
// commander-in-chief (military) JavaScript arrays转换为 C# listings
var netWorthTrendItems = ConvertJsArrayToCSharpList(jsArray);
return netWorthTrendItems;
}
private static List<NetWorthTrendItem> ConvertJsArrayToCSharpList(JsValue jsArray)
{
var array = ();
var list = new List<NetWorthTrendItem>();
for (int i = 0; i < ; i++)
{
var item = (i);
var newItem = new NetWorthTrendItem
{
Time = ((long)("x").AsNumber()).(),
Value = (decimal)("y").AsNumber(),
EquityReturn = (decimal)("equityReturn").AsNumber(),
UnitMoney = ("unitMoney").AsString()
};
(newItem);
}
return list;
}
Functional Testing
The test code is as follows:
static void Main()
{
var js1=GetFundAPIData("/js/fundcode_search.js");
var list1 = ParseFundList(js1);
(((), new JsonSerializerOptions()
{
Encoder = ()
}));
var js2=GetFundAPIData("/pingzhongdata/");
var list2 = GetFundValues(js2);
((()));
();
}
The test results are as follows:
{"Code": "970214", "Name": "CITIC Capital Joyous 6-Month Holding Bond C", "Type": "Bond - Mixed Tier 1"}
{"Time": "2024-10-15T00:00:00+08:00", "Value":2.269, "EquityReturn":-1.6, "UnitMoney":"}
The actual use of the fund NAV interface address inside the fund code can be extracted out separately, here just to demonstrate the method does not do too much encapsulation.
Reference Links
- Data Interface for TDF.com