Sample windows form which queries ticker once every ten seconds and display the results on the form...

 

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.Threading;
using Taype.Trading.BTC.MtGoxAPIClient;
using Taype.Trading.BTC.MtGoxAPIClient.JSONObjects;

    public partial class Form1 : Form
    {

        System.Threading.Timer tickerUIUpdateTimer,
        v0 _client;
        MtGoxTickerItem _currentTick;

        public Form1()
        {
            _client = new v0();
            _client2 = new v0();
            _client3 = new v0();
            // launch the thread that polls the ticker every 10 seconds
            tickerUIUpdateTimer = new System.Threading.Timer(new TimerCallback(UpdateTickerUI), null, 1000, 10000);
        }

/// <summary>
        /// This background worker is for the thread that polls MtGox ticker via HTTP API
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ticker_backgroundWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            // get the currenct ticker
            _currentTick = _client1.ticker();
        }

        private void UpdateTickerUI(object state)
        {
            ticker_backgroundWorker.RunWorkerAsync();
            this.BeginInvoke(new MethodInvoker(UpdateTickerUI));
        }

        private void UpdateTickerUI()
        {
            try
            {
                // update the ticker info
                //string outStr = string.Format("LAST: {3}   HIGH: {2}   AVG: {0}   BUY: {1}   LAST_ALL: {4}   LAST_LOCAL: {5}   LOW: {6}   SELL: {7}   VOL: {8}   VWAP: {9}", new object[] { _currentTick.avg, _currentTick.buy, _currentTick.high, _currentTick.last, _currentTick.last_all, _currentTick.last_local, _currentTick.low, _currentTick.sell, _currentTick.vol, _currentTick.vwap });
                tickerAvg_textBox.Text = _currentTick.avg.ToString();
                tickerBuy_textBox.Text = _currentTick.buy.ToString();
                tickerHigh_textBox.Text = _currentTick.high.ToString();
                tickerLast_textBox.Text = _currentTick.last.ToString();
                tickerLastAll_textBox.Text = _currentTick.last_all.ToString();
                tickerLastLocal_textBox.Text = _currentTick.last_local.ToString();
                tickerLow_textBox.Text = _currentTick.low.ToString();
                tickerSell_textBox.Text = _currentTick.sell.ToString();
                tickerVol_textBox.Text = _currentTick.vol.ToString();
                tickerVwap_textBox.Text = _currentTick.vwap.ToString();
                tickerLastUpdate_textBox.Text = DateTime.UtcNow.ToLongTimeString();
                // update the chart
                // chart1.Series[0].Points.AddXY(DateTime.UtcNow.Second, _currentTick.last);
                // chart1.Update();
            }
            catch (Exception ex)
            { }
        }
}

Last edited Jan 31, 2012 at 6:37 AM by taype, version 4

Comments

No comments yet.