利用Warensoft Stock Service编写高频交易软件

利用Warensoft Stock Service编写高频交易软件,第1张

概述无论是哪种交易软件,对于程序员来讲,最麻烦的就是去实现各种算法。本文以SAR算法的实现过程为例,为大家说明如何使用WarensoftStockService来实现高频交易软件的快速开发。

无论是哪种交易软件,对于程序员来讲,最麻烦的就是去实现各种算法。本文以SAR算法的实现过程为例,为大家说明如何使用Warensoft Stock Service来实现高频交易软件的快速开发。

目前WarensoftStockService已经实现了C# 版本的客户端驱动,可以直接在Nuget上搜索Warensoft并安装。客户端驱动已经编译为跨平台.net standard1.6版本,可以在桌面应用(WPF,Winform)、Xamarin手机应用(WP,AndroID,IOS)、Web(asp.net,asp.net core)中应用, *** 作系统可以是Window,AndroID,IOS,IMAC,linux。

下面将以AndroID为例(注:本Demo可以直接平移到WPF中),说明SAR指标的实现过程,其他指标计算的综合应用,在其他文章中会专门讲解。

软件环境说明

IDE

VS2017 RC

客户端

AndroID4.4

服务器环境

Ubuntu16

客户端运行环境

Xamarin.Forms

客户端图形组件

Oxyplot

建立一个Xamarin.Forms手机App

这里选择基于XAML的App,注意共享库使用PCL。

工程目录下图所示:

添加Nuget引用包

首先,为Warensoft.StockApp共享库添加Oxyplot引用(此处可能需要科学上网),如下图所示:

然后再分别安装Warensoft.Entlib.Common,Warensoft.Entlib.StockServiceClIEnt,如下图所示:

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

然后在AndroID的MainActivity中加入平台注册代码:

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()); }

实现主窗口

主界面的实现采用MVVM模式来实现,关于MVVM的讲解,网上应该有很多了,后面的文章中,我会把我自己的理解写出来,让大家分享。本DEMO的MVVM框架已经集成在了Warensoft.Entlib.Common中,使用起来很简单。

第一步:

编写主界面(需要了解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"> <!-- 此处要注意在头中注册OxyPlot的命名空间 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并为视图(图面)添加其对应的模型,代码如下(注意要引入Warensoft.Entlib.Common):

public partial class MainPage : ContentPage { public MainPage() { InitializeComponent(); //此处注册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坐标轴,添加一个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,MinorGrIDlinestyle = linestyle.Dot,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线函数(以OKCoin为例),代码如下:

/// <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客户端驱动,此处使用的是测试用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();  //远程Warensoft Stock Service 分析SAR曲线  var sar = await this.driver.GetSAR(kline);  //绘图,注意办为需要更新UI,因此需要在主线程中执行更新代码  this.SafeInvoke(()=> {  //每次更新前,需要将旧数据清空  this.candle.Items.Clear();  this.line.Points.Clear();  foreach (var item in kline.OrderBy(k=>k.Time))  {  //注意将时间改为OxyPlot能识别的格式  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,"OK"); } public PlotModel Model { get { return this.GetProperty<PlotModel>("Model"); } set { this.SetProperty("Model",}); //添加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客户端驱动,"6C442B3AF58D4DDA81BB03B353C0D7D8"); await Task.Run(async()=> { while (true) { try { //读取K线 var kline =await this.LoadKline(); //远程Warensoft Stock Service 分析SAR曲线 var sar = await this.driver.GetSAR(kline); //绘图,注意办为需要更新UI,因此需要在主线程中执行更新代码 this.SafeInvoke(()=> { //每次更新前,需要将旧数据清空 this.candle.Items.Clear(); this.line.Points.Clear(); foreach (var item in kline.OrderBy(k=>k.Time)) { //注意将时间改为OxyPlot能识别的格式 var time = OxyPlot.Axes.DateTimeAxis.Todouble(item.Time); this.candle.Items.Add(new HighLowItem(time,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); } }); } }}

最后编译,并部署到手机上,最终运行效果如下:

最终编译完毕的APK文件(下载)。

以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,同时也希望多多支持编程小技巧!

总结

以上是内存溢出为你收集整理的利用Warensoft Stock Service编写高频交易软件全部内容,希望文章能够帮你解决利用Warensoft Stock Service编写高频交易软件所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1147250.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-31
下一篇 2022-05-31

发表评论

登录后才能评论

评论列表(0条)

保存