[原文網址](https://connect.spotware.com/docs/open_api_2/example_projects/dot_net_v2)
### .NET
以下示例旨在演示如何使用一些基本的protobuf消息來授權您的應用程序,獲取帳戶信息和市場數據,以及與您的應用程序進行交易。 您可以找到示例的完整源代碼 [here](https://github.com/spotware/Open-API-2.0-.Net-Sample).
以下步驟旨在添加并激活您的應用程序并獲取客戶端ID,客戶端密鑰和訪問令牌,如本文檔的“入門”部分所述。
**Authorization**
要開始使用您的應用程序,您應該將您的憑據 - 客戶端ID,客戶端密鑰和訪問令牌添加到您的應用程序。 查看上面的開放認證部分以獲取更多詳細信息
*Example:*
~~~
clientId = "353_hX10YJ24gg7Gv9dfkA6n6INayHrC6vBlfKoNxjkaP4lvAgiwg4";
clientSecret = "cEQQgmFGLZ5YlXEwpSuKErvd4CFvJr2sWyo8ifgj9iemvxvQA0";
token = "uSjOgU_Q2jpY3LJmqFeVJGdPkzpjFbssqqm51GEiPqo";
~~~
然后您可以使用以下方法授權您的應用程序:
~~~
CreateAppAuthorizationRequest(_clientId, \_clientSecret)
~~~
分別使用您的客戶端ID和客戶端密鑰。
要授權所需的帳戶使用:
~~~
CreateAccAuthorizationRequest(_token, \_accountID)
~~~
分別使用您的訪問令牌和所需的帳戶ID。
要獲取所需的帳戶ID,請使用:
~~~
CreateAccountListRequest(_token)
~~~
with your access token.
因此,您將獲得交易賬戶ID列表,如下所示:
*Send: ProtoMessage{GetAccountsByAccessTokenReq}*
*Received: ProtoMessage{GetAccountsByAccessTokenRes{ID: 104989*
*ID: 104990*
*ID: 104462*
*}}*
**Getting Market Data**
要獲取symbol列表,請使用:
~~~
CreateSymbolsListRequest(_accountID)
~~~
要獲取所有帳戶交易ID列表,請使用:
~~~
CreateDealsListRequest(_accountID, startDate.ToUnixTimeMilliseconds(),
now.ToUnixTimeMilliseconds())
~~~
要獲取所有帳戶訂單的列表,請使用:
~~~
CreateReconcileRequest(_accountID)
~~~
要獲取帳戶交易記錄,請使用:
~~~
CreateCashflowHistoryRequest(_accountID, 1,
((DateTimeOffset)DateTime.Now).ToUnixTimeMilliseconds());
~~~
要獲取所有趨勢欄的列表,請使用:
To get the list of all the trendbars use:
~~~
CreateTrendbarsRequest(_accountID,1,
((DateTimeOffset)DateTime.Now.AddDays(-5)).ToUnixTimeMilliseconds(),
((DateTimeOffset)DateTime.Now).ToUnixTimeMilliseconds(),ProtoOATrendbarPeriod.M1);
~~~
預計結果將以Open,High,Low和Close位置x軸值的形式返回:
~~~
Received: ProtoMessage{Trendbars{Open: 6
High: 21
Low: 115677
Close: 21
Open: 1
High: 15
Low: 115698
Close: 15...
~~~
To get the tick data run:
~~~
CreateTickDataRequest(_accountID, 1,
((DateTimeOffset)DateTime.Now.AddDays(-5)).ToUnixTimeMilliseconds(),
((DateTimeOffset)DateTime.Now).ToUnixTimeMilliseconds(), ProtoOAQuoteType.BID);
~~~
To subscribe for the spots use:
~~~
CreateSubscribeForSpotsRequest(_accountID, 1);
~~~
To unsubscribe from the spots use:
~~~
CreateUnsubscribeFromSpotsRequest(_accountID,1);
~~~
**Trading operations**
To create a Market Order use:
~~~
CreateMarketOrderRequest(Convert.ToInt32(_accountID), \_token, 1,
ProtoOATradeSide.BUY, Convert.ToInt64(100000))
~~~
響應示例:
~~~
*Received:
ProtoMessage{
OrderAccepted{
Order{
orderId:649095,
orderType:MARKET,
tradeSide:BUY,
symbolName:1,
requestedVolume:100000,
executedVolume:0,
closingOrder:FALSE
},
Position{
positionId:396536,
positionStatus:unknown,
tradeSide:BUY,
symbolId:1,
volume:0,
Price:0,
swap:0,
commission:0,
openTimestamp:0
}
}
}*
~~~
~~~
*Received:
ProtoMessage{
OrderFilled{
Order{
orderId:649095,
orderType:MARKET,
tradeSide:BUY,
symbolName:1,
requestedVolume:100000,
executedVolume:100000,
closingOrder:FALSE,
executionPrice:1.15725
},
Position{
positionId:396536,
positionStatus:OPENED,
tradeSide:BUY,
symbolId:1,
volume:100000,
Price:1.15725,
swap:0,
commission:-3,
openTimestamp:1536579322377
}
}
}*
~~~
要創建止損訂單,請使用:
~~~
CreateStopOrderRequest(Convert.ToInt32(_accountID), \_token, 1,
ProtoOATradeSide.BUY, Convert.ToInt64(100000), 1.2);
~~~
要發送限價訂單,請使用:
~~~
CreateLimitOrderRequest(Convert.ToInt32(_accountID), \_token, 1,
ProtoOATradeSide.BUY, Convert.ToInt64(100000), 1.1);
~~~
To create a Stop Limit Order use:
~~~
CreateStopLimitOrderRequest(Convert.ToInt32(_accountID), \_token, 1,
ProtoOATradeSide.BUY, Convert.ToInt64(100000), 1.2,5);
~~~
關閉頭寸使用:
~~~
CreateClosePositionRequest(Convert.ToInt32(_accountID), \_token,
Convert.ToInt64(txtPositionID.Text), Convert.ToInt64(txtVolume.Text))
~~~
修改止損/止盈運行:
~~~
CreateAmendPositionStopLossTakeProfitRequest(Convert.ToInt32(_accountID),
\_token, Convert.ToInt64(txtPositionIDTPSL.Text),
Convert.ToDouble(txtStopLoss.Text), Convert.ToDouble(txtTakeProfit.Text));
~~~
- 空白目錄
- API Reference 2.0
- Getting Started
- Open Authentication
- Protocol Buffers
- Protobuf Messages Reference
- Open API Messages
- Open API Model Messages
- Open API Error Codes
- Example Projects .NET
- Spotware Proxy Cloud
- Getting Started in C#
- Test Environment
- Playground
- Frequently Asked Questions
- Lesson 1. Your First App
- Lesson 2. Get Trading Data
- Lesson 3. Get Market Data
- Lesson 4. Using Trading API