編輯:關於 Android 開發
無論哪一種交易軟件,對程式設計師而言最麻煩的往往是如何實作各種演算法。本文以 SAR 演算法的實作過程為例,說明如何用 Warensoft Stock Service 快速開發高頻交易軟件。
目前 Warensoft Stock Service 已提供 C# 版客戶端驅動,可直接在 Nuget 搜尋 Warensoft 並安裝。客戶端驅動已編譯為跨平台的 .NET Standard 1.6 版本,可應用於桌面應用(WPF、Winform)、Xamarin 手機應用(WP、Android、iOS)與 Web(ASP.NET、ASP.NET Core),作業系統則支援 Windows、Android、iOS、macOS 與 Linux。
下面以 Android 為例(本 Demo 可直接平移到 WPF),說明 SAR 指標的實作過程,其他指標的綜合應用會在別篇文章專門講解。
環境:IDE 為 VS2017 RC;客戶端為 Android 4.4;伺服器為 Ubuntu 16;客戶端執行環境為 Xamarin.Forms;圖形元件為 Oxyplot。


這裡選擇基於 XAML 的 App,注意共享庫使用 PCL。
首先為 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 模式實作。本 Demo 的 MVVM 框架已整合在 Warensoft.EntLib.Common 中,使用起來很簡單。
第一步:編寫主界面並修改 MainPage.xaml(需了解 XAML 語法):
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="https://xamarin.com/schemas/2014/forms"
xmlns:x="https://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 命名空間 -->
<Grid>
<!-- 添加圖形組件 -->
<oxy:PlotView Model="{Binding Model}"
VerticalOptions="Center" HorizontalOptions="Center" />
</Grid>
</ContentPage>
第二步:為視圖添加對應的模型(需引入 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,
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 線的函數(以 OKCoin 為例):
/// <summary>
/// 讀取 OKCoin 的 15 分鐘 K 線
/// </summary>
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>();
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() });
}
return lines;
}
第五步:添加定時刷新並繪圖的函數:
private StockServiceDriver driver;
public async Task UpdateData() {
// 初始化 Warensoft Stock Service 客戶端驅動(此處用測試用 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();
// 遠端分析 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)) {
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));
}
}
this.Model.InvalidatePlot(true);
});
} catch (Exception ex) { }
await Task.Delay(5000);
}
});
}
最後編譯並部署到手機,運行效果如下:

作者:科學家 Email:warensoft@163.com 微信:43175692
仿 QQ 空間的 PopupWindow:依據點擊位置自動朝上或朝下彈出
要做出的效果 打開 QQ 空間時有個箭頭按鈕,點下去會彈出一個 PopupWindow,而且它會依按鈕在畫面上的位置,自動決定顯示在箭頭上方還是下方。這比一律彈在螢幕正中
Android 輸入法按鍵監聽:用 dispatchKeyEvent 攔截方向鍵
Android 輸入法按鍵監聽:用 dispatchKeyEvent 攔截方向鍵 在 Android 開發中,若頁面包含多個輸入框,又希望在使用軟鍵盤時避免方向鍵造成焦點
網上商城實戰(四):首頁二級分類的查詢
網上商城實戰(四):首頁二級分類的查詢 1. 創建表 CREATE TABLE `categorysecond` ( `csid` int(11) NOT NULL A
行動端 HTTP 框架選型指南:Android 開發者的實務思考
眾所周知,Android 應用主要透過客戶端—伺服器模式進行資料交互。一般開發時,往往是一個介面對應一個顯示模組,這樣的設計在客戶端更新時會非常棘手。為了解決這個問題,