[原文网址](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