谢谢。
我将获得以下信息:
价格
btc中的价格不是在新的api版本中可以得到吗?
volume_24h
percent_change_1h
percent_change_24h
percent_change_7d
这是我的旧api版本的代码不适用于新的pro api怎么解决? https://api.coinmarketcap.com/v1/ticker/?limit=0
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlServerCe;
using Newtonsoft.Json.Linq;
using System.Net;
using System.IO;
namespace CoinMarketLive
{
public partial class CoinMarketLive : Form
{
public int counter = 31;
public int globalSyncDistance = 31;
public double sum1h = 0, sum24h = 0, sum7d = 0, balance = 0, value = 0, valueBTC = 0;
public CoinMarketLive()
{
InitializeComponent();
conn.connect();
this.loadSettings();
}
public void sync()
{
this.timer1.Stop();
this.lblLastSync.Text = "syncing...";
//string query = "select * from coins";
//SqlCeDataReader reader = conn.ExecuteReader(query);
this.dataGridView1.Rows.Clear();
try
{
string url = "https://api.coinmarketcap.com/v1/ticker/?limit=0";
string strResponse = "";
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
WebResponse response = request.GetResponse();
using (Stream responseStream = response.GetResponseStream())
{
StreamReader reader = new StreamReader(responseStream, Encoding.UTF8);
strResponse = reader.ReadToEnd();
}
List<Coin> coinsList = new List<Coin>();
string query = "select * from coins";
SqlCeDataReader reader2 = conn.ExecuteReader(query);
while (reader2.Read())
{
Coin c = new Coin();
c.id = Int32.Parse(reader2["id"].ToString());
c.name = reader2["coin"].ToString();
c.limitPercent = !reader2["limit_percent"].ToString().Equals(String.Empty) ? Double.Parse(reader2["limit_percent"].ToString()) : 0;
c.balance = !reader2["balance"].ToString().Equals(String.Empty) ? Double.Parse(reader2["balance"].ToString()) : 0;
coinsList.Add(c);
//coinsList.Add(reader2["coin"].ToString());
}
JArray jsonArray = JArray.Parse(strResponse);
this.sum1h = 0;
this.sum24h = 0;
this.sum7d = 0;
this.balance = 0;
this.value = 0;
this.valueBTC = 0;
foreach (JObject jsonObject in jsonArray)
{
try
{
if (containsCoin(coinsList, jsonObject.GetValue("id").ToString()))
{
Coin temp = getCoinFromList(coinsList, jsonObject.GetValue("id").ToString());
dataGridView1.Rows.Add(
jsonObject.GetValue("id").ToString(),
Double.Parse(jsonObject.GetValue("price_usd").ToString()) < 1 && Double.Parse(jsonObject.GetValue("price_usd").ToString()) > -1 ?
"$0" + String.Format("{0:#,#.##########################################}", Double.Parse(jsonObject.GetValue("price_usd").ToString())) :
"$" + String.Format("{0:#,#.##########################################}", Double.Parse(jsonObject.GetValue("price_usd").ToString())),
Double.Parse(jsonObject.GetValue("price_btc").ToString()) < 1 && Double.Parse(jsonObject.GetValue("price_btc").ToString()) > -1 ?
"0" + String.Format("{0:#,#.##########################################}", Double.Parse(jsonObject.GetValue("price_btc").ToString())) :
"" + String.Format("{0:#,#.##########################################}", Double.Parse(jsonObject.GetValue("price_btc").ToString())),
temp.balance,
temp.balance * Double.Parse(jsonObject.GetValue("price_usd").ToString()) < 1 && temp.balance * Double.Parse(jsonObject.GetValue("price_usd").ToString()) > -1 ?
"$0" + String.Format("{0:#,#.##########################################}", temp.balance * Double.Parse(jsonObject.GetValue("price_usd").ToString())) :
"$" + String.Format("{0:#,#.##########################################}", temp.balance * Double.Parse(jsonObject.GetValue("price_usd").ToString())),
temp.balance * Double.Parse(jsonObject.GetValue("price_btc").ToString()) < 1 && temp.balance * Double.Parse(jsonObject.GetValue("price_btc").ToString()) > -1 ?
"0" + String.Format("{0:#,#.##########################################}", temp.balance * Double.Parse(jsonObject.GetValue("price_btc").ToString())) :
"" + String.Format("{0:#,#.##########################################}", temp.balance * Double.Parse(jsonObject.GetValue("price_btc").ToString())),
jsonObject.GetValue("percent_change_1h").ToString(),
jsonObject.GetValue("percent_change_24h").ToString(),
jsonObject.GetValue("percent_change_7d").ToString(),
Double.Parse(jsonObject.GetValue("24h_volume_usd").ToString()) < 1 && Double.Parse(jsonObject.GetValue("24h_volume_usd").ToString()) > -1 ?
"$0" + String.Format("{0:#,#.##########################################}", Double.Parse(jsonObject.GetValue("24h_volume_usd").ToString())) :
"$" + String.Format("{0:#,#.##########################################}", Double.Parse(jsonObject.GetValue("24h_volume_usd").ToString()))
);
this.sum1h += Double.Parse(jsonObject.GetValue("percent_change_1h").ToString());
this.sum24h += Double.Parse(jsonObject.GetValue("percent_change_24h").ToString());
this.sum7d += Double.Parse(jsonObject.GetValue("percent_change_7d").ToString());
this.balance += temp.balance;
this.value += temp.balance * Double.Parse(jsonObject.GetValue("price_usd").ToString());
this.valueBTC += temp.balance * Double.Parse(jsonObject.GetValue("price_btc").ToString());
if (temp.limitPercent != 0 && Double.Parse(jsonObject.GetValue("percent_change_1h").ToString()) > temp.limitPercent)
{
playAlert();
}
}
}
catch (Exception ex)
{
continue;
}
}
try
{
this.dataGridView2.Rows.Clear();
dataGridView2.Rows.Add(
"TOTAL",
"",
"",
this.balance,
this.value < 1 && this.value > -1 ?
"$0" + String.Format("{0:#,#.##########################################}", this.value) :
"$" + String.Format("{0:#,#.##########################################}", this.value),
this.valueBTC < 1 && this.valueBTC > -1 ?
"0" + String.Format("{0:#,#.##########################################}", this.valueBTC) :
"" + String.Format("{0:#,#.##########################################}", this.valueBTC),
this.sum1h,
this.sum24h,
this.sum7d,
""
);
}
catch (Exception ex)
{
//
}
foreach (DataGridViewRow row in dataGridView1.Rows)
{
if (Convert.ToDouble(row.Cells["colPercent1h"].Value) > 0)
{
row.Cells["colPercent1h"].Style.BackColor = Color.LightGreen;
}
else if (Convert.ToDouble(row.Cells["colPercent1h"].Value) < 0)
{
row.Cells["colPercent1h"].Style.BackColor = Color.OrangeRed;
}
if (Convert.ToDouble(row.Cells["colPercent24h"].Value) > 0)
{
row.Cells["colPercent24h"].Style.BackColor = Color.LightGreen;
}
else if (Convert.ToDouble(row.Cells["colPercent24h"].Value) < 0)
{
row.Cells["colPercent24h"].Style.BackColor = Color.OrangeRed;
}
if (Convert.ToDouble(row.Cells["colPercent7d"].Value) > 0)
{
row.Cells["colPercent7d"].Style.BackColor = Color.LightGreen;
}
else if (Convert.ToDouble(row.Cells["colPercent7d"].Value) < 0)
{
row.Cells["colPercent7d"].Style.BackColor = Color.OrangeRed;
}
}
}
catch (Exception ex)
{
lblLog.Text = "[" + ex.Message + "]";
}
lblLastSync.Text = "synced";
this.counter = globalSyncDistance;
timer1.Start();
}
public void playAlert()
{
try
{
WMPLib.WindowsMediaPlayer wplayer = new WMPLib.WindowsMediaPlayer();
wplayer.URL = "alert.mp3";
wplayer.controls.play();
}
catch (Exception ex)
{
lblLog.Text = "[" + ex.Message + "]";
}
}
public void loadSettings()
{
try
{
string query = "select * from settings where setting_key = 'sync_distance'";
SqlCeDataReader reader = conn.ExecuteReader(query);
if (reader.Read())
{
int syncDistance = 0;
int.TryParse(reader["setting_value"].ToString(), out syncDistance);
if (syncDistance != 0)
{
this.globalSyncDistance = syncDistance + 1;
}
}
}
catch (Exception ex)
{
lblLog.Text = "[" + ex.Message + "]";
}
}
private bool containsCoin(List<Coin> coinsList, string coinName)
{
foreach (Coin c in coinsList)
{
if (c.name == coinName)
{
return true;
}
}
return false;
}
private Coin getCoinFromList(List<Coin> coinsList, string coinName)
{
foreach (Coin c in coinsList)
{
if (c.name == coinName)
{
return c;
}
}
return new Coin();
}
private void coinsToolStripMenuItem_Click(object sender, EventArgs e)
{
this.timer1.Stop();
using (Coins form = new Coins())
{
form.ShowDialog();
}
this.timer1.Start();
}
private void settingsToolStripMenuItem1_Click(object sender, EventArgs e)
{
this.timer1.Stop();
using (Settings form = new Settings())
{
form.ShowDialog();
}
this.loadSettings();
this.timer1.Start();
}
private void timer1_Tick(object sender, EventArgs e)
{
this.counter--;
this.lblLastSync.Text = this.counter.ToString();
if (this.counter <= 0)
{
this.sync();
}
}
private void exitToolStripMenuItem_Click(object sender, EventArgs e)
{
try
{
conn.disconnect();
Application.Exit();
}
catch (Exception ex)
{
lblLog.Text = "[" + ex.Message + "]";
}
}
private void btnPause_Click(object sender, EventArgs e)
{
if (this.timer1.Enabled)
{
this.timer1.Stop();
this.btnPause.Text = "Resume";
}
else
{
this.timer1.Start();
this.btnPause.Text = "Pause";
}
}
private void CoinMarketLive_Shown(object sender, EventArgs e)
{
this.sync();
}
private void dataGridView1_SortCompare(object sender, DataGridViewSortCompareEventArgs e)
{
try
{
double cellValue1 = Double.Parse(e.CellValue1.ToString().Replace(",", "").Replace("$", ""));
double cellValue2 = Double.Parse(e.CellValue2.ToString().Replace(",", "").Replace("$", ""));
if (e.Column.Name.Equals("colPercent1h") || e.Column.Name.Equals("colPercent24h") ||
e.Column.Name.Equals("colPercent7d") || e.Column.Name.Equals("colPrice") ||
e.Column.Name.Equals("colVolume24h") || e.Column.Name.Equals("colValue") ||
e.Column.Name.Equals("colValueBTC"))
{
//colValue colValueBTC
e.SortResult = cellValue1
.CompareTo(cellValue2);
e.Handled = true;//pass by the default sorting
}
}
catch (Exception ex)
{
lblLog.Text = "[" + ex.Message + "]";
}
}
}
}