最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

利用Warensoft Stock Service編寫高頻交易軟件

 更新時(shí)間:2017年01月10日 08:36:17   作者:王宇 warensoft  
本文主要介紹了利用Warensoft Stock Service編寫高頻交易軟件的方法步驟,具有一定的參考價(jià)值,下面跟著小編一起來看下吧

無論是哪種交易軟件,對(duì)于程序員來講,最麻煩的就是去實(shí)現(xiàn)各種算法。本文以SAR算法的實(shí)現(xiàn)過程為例,為大家說明如何使用Warensoft Stock Service來實(shí)現(xiàn)高頻交易軟件的快速開發(fā)。

目前WarensoftStockService已經(jīng)實(shí)現(xiàn)了C# 版本的客戶端驅(qū)動(dòng),可以直接在Nuget上搜索Warensoft并安裝??蛻舳蓑?qū)動(dòng)已經(jīng)編譯為跨平臺(tái).net standard1.6版本,可以在桌面應(yīng)用(WPF,Winform)、Xamarin手機(jī)應(yīng)用(WP,Android,IOS)、Web(asp.net,asp.net core)中應(yīng)用,操作系統(tǒng)可以是Window,Android,IOS,IMAC,Linux。

下面將以Android為例(注:本Demo可以直接平移到WPF中),說明SAR指標(biāo)的實(shí)現(xiàn)過程,其他指標(biāo)計(jì)算的綜合應(yīng)用,在其他文章中會(huì)專門講解。

軟件環(huán)境說明

IDE

VS2017 RC

客戶端

Android4.4

服務(wù)器環(huán)境

Ubuntu16

客戶端運(yùn)行環(huán)境

Xamarin.Forms

客戶端圖形組件

Oxyplot

建立一個(gè)Xamarin.Forms手機(jī)App

這里選擇基于XAML的App,注意共享庫使用PCL。

工程目錄下圖所示:

添加Nuget引用包

首先,為Warensoft.StockApp共享庫添加Oxyplot引用(此處可能需要科學(xué)上網(wǎng)),如下圖所示:

然后再分別安裝Warensoft.EntLib.Common,Warensoft.EntLib.StockServiceClient,如下圖所示:

然后為Warensoft.StockApp.Droid添加OxyPlot的NuGet引用,如下所示:

然后在Android的MainActivity中加入平臺(tái)注冊(cè)代碼:

OxyPlot.Xamarin.Forms.Platform.Android.PlotViewRenderer.Init();

MainActivity.cs的代碼如下所示:

protected override void OnCreate(Bundle bundle)
 {
 TabLayoutResource = Resource.Layout.Tabbar;
ToolbarResource = Resource.Layout.Toolbar;
OxyPlot.Xamarin.Forms.Platform.Android.PlotViewRenderer.Init();
 base.OnCreate(bundle);
 global::Xamarin.Forms.Forms.Init(this, bundle);
 LoadApplication(new App());
 }

實(shí)現(xiàn)主窗口

主界面的實(shí)現(xiàn)采用MVVM模式來實(shí)現(xiàn),關(guān)于MVVM的講解,網(wǎng)上應(yīng)該有很多了,后面的文章中,我會(huì)把我自己的理解寫出來,讓大家分享。本DEMO的MVVM框架已經(jīng)集成在了Warensoft.EntLib.Common中,使用起來很簡(jiǎn)單。

第一步:

編寫主界面(需要了解XAML語法),并修改MainPage.xaml,如代碼如下:

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
 xmlns:local="clr-namespace:Warensoft.StockApp"
 xmlns:oxy="clr-namespace:OxyPlot.Xamarin.Forms;assembly=OxyPlot.Xamarin.Forms"
 x:Class="Warensoft.StockApp.MainPage">
 <!--
 此處要注意在頭中注冊(cè)O(shè)xyPlot的命名空間
 xmlns:oxy="clr-namespace:OxyPlot.Xamarin.Forms;assembly=OxyPlot.Xamarin.Forms"-->
 <Grid>
 <!--此處添加圖形組件-->
 <oxy:PlotView Model="{Binding Model}" VerticalOptions="Center" HorizontalOptions="Center" />
 </Grid>
</ContentPage>

第二步:

打開MainPage.xaml.cs并為視圖(圖面)添加其對(duì)應(yīng)的模型,代碼如下(注意要引入Warensoft.EntLib.Common):

public partial class MainPage : ContentPage
 {
 public MainPage()
 {
 InitializeComponent();
 //此處注冊(cè)ViewModel
 this.BindingContext = new MainPageViewModel();
 }
 }
 public class MainPageViewModel : ViewModelBase
 {
 public override Task ShowCancel(string title, string message)
 {
 throw new NotImplementedException();
 }
 public override Task<bool> ShowConfirm(string title, string message)
 {
 throw new NotImplementedException();
 }
 public override void ShowMessage(string message)
 {
 Application.Current.MainPage.DisplayAlert("提示",message,"OK");
 }
 protected override void InitBindingProperties()
 {
 }
}

第三步:

定義圖像組件的模型,并為圖像添加X、Y坐標(biāo)軸,添加一個(gè)K線和一條直線,代碼如下所示:

public PlotModel Model
 {
 get { return this.GetProperty<PlotModel>("Model"); }
 set { this.SetProperty("Model", value); }
 }
 protected override void InitBindingProperties()
 {
 this.Model = new PlotModel();
 //添加X、Y軸
 this.Model.Axes.Add(new OxyPlot.Axes.DateTimeAxis()
 {
 Position = AxisPosition.Bottom,
 StringFormat = "HH:mm",
 MajorGridlineStyle = LineStyle.Solid,
 IntervalType = DateTimeIntervalType.Minutes,
 IntervalLength = 30,
 MinorIntervalType = DateTimeIntervalType.Minutes,
 Key = "Time",
 });
 this.Model.Axes.Add(new OxyPlot.Axes.LinearAxis()
 {
 Position = AxisPosition.Right,
 MajorGridlineStyle = LineStyle.Solid,
 MinorGridlineStyle = LineStyle.Dot,
 IntervalLength = 30,
 IsPanEnabled = false,
 IsZoomEnabled = false,
 TickStyle = TickStyle.Inside,
 });
 //添加K線和直線
 this.candle = new OxyPlot.Series.CandleStickSeries();
 this.line = new OxyPlot.Series.LineSeries() { Color = OxyColors.Blue };
 this.Model.Series.Add(this.candle);
 this.Model.Series.Add(this.line);
 }

第四步:

添加獲取K線函數(shù)(以O(shè)KCoin為例),代碼如下:

/// <summary>
 /// 讀取OKCoin的15分鐘K線
 /// </summary>
 /// <returns></returns>
 public async Task<List<Kline>> LoadKline()
 {
 var url = $"https://www.okcoin.cn/api/v1/kline.do?symbol=btc_cny&type=15min&size=100";
 HttpClient client = new HttpClient();
 var result = await client.GetStringAsync(url);
 dynamic k = Newtonsoft.Json.JsonConvert.DeserializeObject(result);
 List<Kline> lines = new List<Kline>();
 int index = 0;
 foreach (var item in k)
 {
 List<double> d = new List<double>();
 foreach (var dd in item)
 {
  d.Add((double)((dynamic)dd).Value);
 }
 lines.Add(new Kline() { Data = d.ToArray()});
 index++;
 }
 return lines;
 }

第五步:

添加定時(shí)刷新并繪制圖像的函數(shù),代碼如下所示:

private StockServiceDriver driver;
 public async Task UpdateData()
 {
 //初始化WarensoftSocketService客戶端驅(qū)動(dòng),此處使用的是測(cè)試用AppKey和SecretKey
 this.driver = new StockServiceDriver("C6651783-A3B9-4B72-8B02-A2E67A59C5A6", "6C442B3AF58D4DDA81BB03B353C0D7D8");
 await Task.Run(async()=>
 {
 while (true)
 {
  try
  {
  //讀取K線
  var kline =await this.LoadKline();
  //遠(yuǎn)程Warensoft Stock Service 分析SAR曲線
  var sar = await this.driver.GetSAR(kline);
  //繪圖,注意辦為需要更新UI,因此需要在主線程中執(zhí)行更新代碼
  this.SafeInvoke(()=> {
  //每次更新前,需要將舊數(shù)據(jù)清空
  this.candle.Items.Clear();
  this.line.Points.Clear();
  foreach (var item in kline.OrderBy(k=>k.Time))
  {
  //注意將時(shí)間改為OxyPlot能識(shí)別的格式
  var time = OxyPlot.Axes.DateTimeAxis.ToDouble(item.Time);
  this.candle.Items.Add(new HighLowItem(time,item.High,item.Low,item.Open,item.Close));
  }
  if (sar.OperationDone)
  {
  foreach (var item in sar.AdditionalData.OrderBy(s=>s.DateTime))
  {
   var time= OxyPlot.Axes.DateTimeAxis.ToDouble(item.DateTime);
 this.line.Points.Add(new DataPoint(time, item.Value));
  }
  }
  //更新UI
  this.Model.InvalidatePlot(true);
  });
  }
  catch (Exception ex)
  {
  }
  await Task.Delay(5000);
 }
 });
 }

完整的ViewModel代碼如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;
using Warensoft.EntLib.Common;
using Warensoft.EntLib.StockServiceClient;
using OxyPlot;
using OxyPlot.Axes;
using OxyPlot.Series;
using Warensoft.EntLib.StockServiceClient.Models;
using System.Net.Http;
namespace Warensoft.StockApp
{
 public partial class MainPage : ContentPage
 {
 public MainPage()
 {
 InitializeComponent();
 this.BindingContext = new MainPageViewModel();
 }
 }
 public class MainPageViewModel : ViewModelBase
 {
 private CandleStickSeries candle;
 private LineSeries line;
 public override Task ShowCancel(string title, string message)
 {
 throw new NotImplementedException();
 }
 public override Task<bool> ShowConfirm(string title, string message)
 {
 throw new NotImplementedException();
 }
 public override void ShowMessage(string message)
 {
 Application.Current.MainPage.DisplayAlert("提示",message,"OK");
 }
 public PlotModel Model
 {
 get { return this.GetProperty<PlotModel>("Model"); }
 set { this.SetProperty("Model", value); }
 }
 protected override void InitBindingProperties()
 {
 this.Model = new PlotModel();
 //添加X、Y軸
 this.Model.Axes.Add(new OxyPlot.Axes.DateTimeAxis()
 {
 Position = AxisPosition.Bottom,
 StringFormat = "HH:mm",
 MajorGridlineStyle = LineStyle.Solid,
 IntervalType = DateTimeIntervalType.Minutes,
 IntervalLength = 30,
 MinorIntervalType = DateTimeIntervalType.Minutes,
 Key = "Time",
 });
 this.Model.Axes.Add(new OxyPlot.Axes.LinearAxis()
 {
 Position = AxisPosition.Right,
 MajorGridlineStyle = LineStyle.Solid,
 MinorGridlineStyle = LineStyle.Dot,
 IntervalLength = 30,
 IsPanEnabled = false,
 IsZoomEnabled = false,
 TickStyle = TickStyle.Inside,
 });
 //添加K線和直線
 this.candle = new OxyPlot.Series.CandleStickSeries();
 this.line = new OxyPlot.Series.LineSeries() { Color = OxyColors.Blue };
 this.Model.Series.Add(this.candle);
 this.Model.Series.Add(this.line);
 this.UpdateData();
 }
 /// <summary>
 /// 讀取OKCoin的15分鐘K線
 /// </summary>
 /// <returns></returns>
 public async Task<List<Kline>> LoadKline()
 {
 var url = $"https://www.okcoin.cn/api/v1/kline.do?symbol=btc_cny&type=15min&size=100";
 HttpClient client = new HttpClient();
 var result = await client.GetStringAsync(url);
 dynamic k = Newtonsoft.Json.JsonConvert.DeserializeObject(result);
 List<Kline> lines = new List<Kline>();
 int index = 0;
 foreach (var item in k)
 {
 List<double> d = new List<double>();
 foreach (var dd in item)
 {
 d.Add((double)((dynamic)dd).Value);
 }
 lines.Add(new Kline() { Data = d.ToArray()});
 index++;
 }
 return lines;
 }
 private StockServiceDriver driver;
 public async Task UpdateData()
 {
 //初始化WarensoftSocketService客戶端驅(qū)動(dòng),此處使用的是測(cè)試用AppKey和SecretKey
 this.driver = new StockServiceDriver("C6651783-A3B9-4B72-8B02-A2E67A59C5A6", "6C442B3AF58D4DDA81BB03B353C0D7D8");
 await Task.Run(async()=>
 {
 while (true)
 {
 try
 {
 //讀取K線
 var kline =await this.LoadKline();
 //遠(yuǎn)程Warensoft Stock Service 分析SAR曲線
 var sar = await this.driver.GetSAR(kline);
 //繪圖,注意辦為需要更新UI,因此需要在主線程中執(zhí)行更新代碼
 this.SafeInvoke(()=> {
 //每次更新前,需要將舊數(shù)據(jù)清空
 this.candle.Items.Clear();
 this.line.Points.Clear();
 foreach (var item in kline.OrderBy(k=>k.Time))
 {
 //注意將時(shí)間改為OxyPlot能識(shí)別的格式
 var time = OxyPlot.Axes.DateTimeAxis.ToDouble(item.Time);
 this.candle.Items.Add(new HighLowItem(time,item.High,item.Low,item.Open,item.Close));

 }
 if (sar.OperationDone)
 {
 foreach (var item in sar.AdditionalData.OrderBy(s=>s.DateTime))
 {
 var time= OxyPlot.Axes.DateTimeAxis.ToDouble(item.DateTime);
this.line.Points.Add(new DataPoint(time, item.Value));
 }

 }
 //更新UI
 this.Model.InvalidatePlot(true);
 });
 }
 catch (Exception ex)
 {
 }
 await Task.Delay(5000);
 }
 });
 }
 }
}

最后編譯,并部署到手機(jī)上,最終運(yùn)行效果如下:

最終編譯完畢的APK文件(下載)。

以上就是本文的全部?jī)?nèi)容,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作能帶來一定的幫助,同時(shí)也希望多多支持腳本之家!

相關(guān)文章

  • Android中Service與Activity之間通信的幾種方式

    Android中Service與Activity之間通信的幾種方式

    本篇文章主要介紹了Android中Service與Activity之間通信的幾種方式,Activity主要負(fù)責(zé)前臺(tái)頁面的展示,Service主要負(fù)責(zé)需要長(zhǎng)期運(yùn)行的任務(wù),具有一定的參考價(jià)值,有興趣的可以了解一下。
    2017-02-02
  • Android編程實(shí)現(xiàn)自定義手勢(shì)的方法詳解

    Android編程實(shí)現(xiàn)自定義手勢(shì)的方法詳解

    這篇文章主要介紹了Android編程實(shí)現(xiàn)自定義手勢(shì)的方法,結(jié)合實(shí)例形式分析了Android自定義手勢(shì)的功能、相關(guān)函數(shù)與具體實(shí)現(xiàn)步驟,需要的朋友可以參考下
    2016-10-10
  • Android瀑布流照片墻實(shí)現(xiàn) 體驗(yàn)不規(guī)則排列的美感

    Android瀑布流照片墻實(shí)現(xiàn) 體驗(yàn)不規(guī)則排列的美感

    這篇文章主要為大家詳細(xì)介紹了Android瀑布流照片墻實(shí)現(xiàn),體驗(yàn)不規(guī)則排列的美感,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2016-10-10
  • 詳解Android中BroadCastReceiver組件

    詳解Android中BroadCastReceiver組件

    這篇文章主要為大家詳細(xì)介紹了Android中BroadCastReceiver組件,Broadcast Receiver是Android的五大組件之一,使用頻率也很高,用于異步接收廣播Intent,感興趣的小伙伴們可以參考一下
    2016-02-02
  • Android Retrofit框架的使用

    Android Retrofit框架的使用

    這篇文章主要介紹了Android Retrofit框架的使用,幫助大家更好的理解和學(xué)習(xí)使用Android,感興趣的朋友可以了解下
    2021-03-03
  • Android仿微信QQ聊天頂起輸入法不頂起標(biāo)題欄的問題

    Android仿微信QQ聊天頂起輸入法不頂起標(biāo)題欄的問題

    這篇文章主要介紹了Android之仿微信QQ聊天頂起輸入法不頂起標(biāo)題欄問題,本文實(shí)例圖文相結(jié)合給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2019-11-11
  • Android 軟鍵盤自動(dòng)彈出與關(guān)閉實(shí)例詳解

    Android 軟鍵盤自動(dòng)彈出與關(guān)閉實(shí)例詳解

    這篇文章主要介紹了Android 軟鍵盤自動(dòng)彈出與關(guān)閉實(shí)例詳解的相關(guān)資料,為了用戶體驗(yàn)應(yīng)該自動(dòng)彈出軟鍵盤而不是讓用戶主動(dòng)點(diǎn)擊輸入框才彈出,這里舉例說明該如何實(shí)現(xiàn),需要的朋友可以參考下
    2016-12-12
  • Android之AnimationDrawable簡(jiǎn)單模擬動(dòng)態(tài)圖

    Android之AnimationDrawable簡(jiǎn)單模擬動(dòng)態(tài)圖

    這篇文章主要為大家詳細(xì)介紹了Android之AnimationDrawable簡(jiǎn)單模擬動(dòng)態(tài)圖的實(shí)現(xiàn)代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2016-09-09
  • 獲取Android系統(tǒng)唯一識(shí)別碼的方法

    獲取Android系統(tǒng)唯一識(shí)別碼的方法

    這篇文章主要介紹了獲取Android系統(tǒng)唯一識(shí)別碼的方法,涉及通過編程獲取Android系統(tǒng)硬件設(shè)備標(biāo)識(shí)的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下
    2015-10-10
  • Android監(jiān)聽ScrollView滑動(dòng)距離的簡(jiǎn)單處理

    Android監(jiān)聽ScrollView滑動(dòng)距離的簡(jiǎn)單處理

    這篇文章主要為大家詳細(xì)介紹了Android監(jiān)聽ScrollView滑動(dòng)距離的簡(jiǎn)單處理,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-02-02

最新評(píng)論

昆明市| 田东县| 临西县| 泉州市| 卓资县| 应城市| 同仁县| 北安市| 宜州市| 汉寿县| 合作市| 上高县| 曲麻莱县| 金沙县| 龙井市| 凌云县| 青海省| 通城县| 黄龙县| 屏边| 敦化市| 科尔| 延寿县| 东兰县| 北安市| 荔波县| 灵山县| 龙门县| 虞城县| 铜梁县| 资溪县| 谢通门县| 嘉祥县| 乌苏市| 闵行区| 二连浩特市| 鸡西市| 琼中| 万源市| 修水县| 偏关县|