2 Star 12 Fork 11

YBO / ShiPanETradingSDK

Create your Gitee Account
Explore and code with more than 12 million developers,Free private repositories !:)
Sign up
Clone or Download
contribute
Sync branch
Cancel
Notice: Creating folder will generate an empty file .keep, because not support in Git
Loading...
README
LGPL-3.0

#ShiPanETradingSDK

Introduction

Attention: This SDK not supports only ShiPanE 3.4.10 or higher due to the API change.

This is the SDK designed to wrap ShiPanE APIs in C#.

ShiPanE is a stock trading api that supporting merely all stock trading client made upon TDX published on "http://www.iguuu.com/e". You can go to this webpage to download and try it.Currently it is free to use as long as you regist at their website. I am not related to them and I am sololy a user.

ShiPane itself provides RESTFul api for you to use. This program is a wrapper that enables you to use it directly in C#.

This is a project written in Visual Studio 2015 Community. You can just open it and add it as a reference in your project.

This project depends on NLog, Newtonsoft.Json and Restclient. You don't need to worry about these since NuGet will help you with all the dependecies.

Comments & bug reports are welcome.

Note: I do speak Chinese as you can see in code comments, no worries, since I am a native born Chinese.

Usage

Usage is kept as simple as possible,easy as 1,2,3

YBO is my nick name, no real meaning in it.

1st Step: Create a ShiPanETrader Object

using YBOStockTrader;
ShiPanETrader mytrader = new ShiPanETrader();

2ed Step: Initialize it with host info by calling SetTdxwHost()

mytrader.SetTdwxHost("Address_To_ShiPanE_Host",[optional]Port Number);

Address_To_ShiPanE_Host:The Ip address of the machine that runs ShiPanE; Port Number: ShiPanE port number.This parameter is optional.If nothing is set,default value 8888 will be used.

3rd Step: Run your command. This SDK provides all the basic method you need to do your daily tradings.All the method are syncronized. Async support is not a main issue for me since the implementation will get much more complicated.

Here, we provide following method: 1. Query current orders:GetOrders()

mytrader.GetOrders();

This method will return all the orders placed in a list of ShiPanEOrderStatusRespond struct.

2. Query completed orders:GetCompletedOrders()

mytrader.GetCompletedOrders();

This method will return all completed(filled) orders placed in a list of ShiPanEOrderStatusRespond struct.

3. Query cancelable orders:GetCancelableOrders()

mytrader.GetCancelableOrders();

This method will return all cancelable(unfilled) orders placed in a list of ShiPanEOrderStatusRespond struct.

4. Query positions:GetPositions()

mytrader.GetPositions();

This method will return all current postion in a list of ShiPanEStockPosition.In the meantime, capital info described by object member mytrader.AccountAsset is updated.

5. Place a order:PlaceOrder(string label,string BuyOrSell,ShiPanEOrderPriceType OrderPriceType,float price,int amount)

mytrader.PlaceOrder("600256","BUY",12.56,1000);

The example will place a order that buy 1000 shares of 600256 at 12.56Yuan/share and return the order number of it if success. -1 is returned if error occures. label: 6 digits stock label in string format. BuyOrSell:Place "BUY" to buy and place "SELL" to sell.Upper and lower case both work. OrderPriceType: price type of the order. Currently 7 types are supported as listed in enum ShiPanEOrderPriceType price:self-explainary. amount:Stock share.1000 = 1000 shares,not 1000*100.

6. Cancel a order:CancelOrder(int Order_Number)

mytrader.CancelOrder(Order_Number)

Cancel the order with order number Order_Number.This number can be obtained by the return value of PlaceOrder() or from the OrderNum member of ShiPanEStockPosition.

7. Cancel all order:CancelOrders()

mytrader.CancelOrders()

Cancel all orders cancelable.

8. Obtain new stock list for a certain dateGetNewStockList(DateTime date)

mytrader.GetNewStockList(new DateTime(year,month,day))

Obtain the new stock list and return them in an array of string[].Only date part of date will be used. Time part will be ignored. Data is downloaded from SINA. Sometimes network does go nuts, you might want to try it multiple times to ensure consistent result. Don't go to hard, tho. You will get banned if you request the webpage too frequently.

9. Obtain new stock list for todayGetNewStockListToday()

mytrader.GetNewStockListToday()

Just a wrapper of GetNewStockList(DateTime.Now)

Full Demo

Since this is a class library, You will need to use it in a new project.Hereby I created a new C# Console project called "ShiPanETraderSDKDemo".

First,add my project to your solution.Second,add my project in your reference,then try the following code.

If you're running ShipanE and this project on different machines, you should replace 127.0.0.1 with the IP of the machine that ShiPanE runs on.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ShiPanEStockTrader;
namespace ShiPanETraderSDKDemo
{
    class Program
    {    
        static void Main(string[] args)
        {
            ShiPanETrader mytrader = new ShiPanETrader();

            //Use the IP address of the machine running ShiPanE here.localhost or 127.0.0.1 means the program runs on the same machine that runs ShiPanE
            int init=mytrader.SetTdxwHost("127.0.0.1");
            if (init != 0)
            {
                Console.WriteLine("Initialize ShiPanETrader failed.Exit.");
                return;
            }
            //Obtain Orders
            List<ShiPanEOrderStatusRespond> OrderResponses = mytrader.GetOrders();
            Console.WriteLine("Currently I have {0} orders.", OrderResponses.Count);

            //Place a order
            int Order_Num = mytrader.PlaceOrder("600256", "BUY", ShiPanEOrderPriceType.Limit, 12.56, 1000);
            if (Order_Num > 0)
                Console.WriteLine("Order placed. Order number is {0}", Order_Num);
            else
                Console.WriteLine("Placing order failed.Please check again.");

            //Obtain Positions
            List<ShiPanEStockPosition> AccountPositions = mytrader.GetPositions();
            Console.WriteLine("Currently I have {0} positions.", AccountPositions.Count);

            //Obtain Usable Capital
            Console.WriteLine("My usable capital is {0}.",mytrader.AccountAsset.CanUse);


            //Finish line
            Console.WriteLine("Program finished.");
            Console.ReadLine();
        }
    }
}

简介

注意:因为实盘易APi的变化,现不再提供对实盘易3.4.10.0以前的版本提供支持。

本程序实现的是实盘易在C#的调用API。

实盘易是一个在通达信软件终端模拟操作的软件,发布人为http://www.iguuu.com/e. 你可以前往他们的页面去下载。当前软件的使用的是免费的,唯一的需求是在网站上注册一个账号。我本人不是实盘易的开发人,只是一名用户。我本人与实盘易没有任何实质的联系。

实盘易提供了一系列RESTFul api供你使用.本程序能让你C#中直接调用所有的接口.

本程序用Visual Studio 2015社区版开发。你可以直接下载本程序然后在你自己的程序中添加引用。

本程序依赖于NLog, Newtonsoft.Json and Restclient.基本上你不需要操心这些依赖,NuGet会处理好这些问题。

欢迎提供评论和Bug反馈。

注意:中英文对于我来说区别不是很大,你可以用你觉得方便的语言和我交流。我本人是中国人,虽然这里的中文看起来比较别扭。

用法

该程序的设计的原则是尽量简单,正常的使用只需要三步。

YBO在这里是我的绰号,没有任何的实际意义。因为这个项目一开始是我本人使用,所以命名上没有特别注意。

第一步: 实例化ShiPanETrader对象

using YBOStockTrader;
ShiPanETrader mytrader = new ShiPanETrader();

第二步: 调用SetTdxwHost()对该对象进行初始化

mytrader.SetTdwxHost("Address_To_ShiPanE_Host",[optional]Port Number);

Address_To_ShiPanE_Host:运行实盘易的机器的IP; Port Number: 实盘易使用的端口号。这是一个可选选项,如果没有填写,则会使用默认的8888端口。

第三部: 运行命令。 本程序提供了所有的日常交易所需功能。所有的method都是同步调用的。异步调用涉及到界面的锁问题,过于复杂。

本程序提供了一下方法 1. 获取当前所有委托:GetOrders()

mytrader.GetOrders();

该方法会获取所有的委托,返回一个包含 ShiPanEOrderStatusRespond 结构的列表(List<>).

2. 获取当前所有成交委托:GetCompletedOrders()

mytrader.GetCompletedOrders();

该方法会获取所有成交的委托,返回一个包含 ShiPanEOrderStatusRespond 结构的列表(List<>).

3. 获取当前所有可撤销的委托:GetCancelableOrders()

mytrader.GetCancelableOrders();

该方法会获取所有可撤销的委托,返回一个包含 ShiPanEOrderStatusRespond 结构的列表(List<>).

4. 获取仓位:GetPositions()

mytrader.GetPositions();

该方法会获取当前账户的持仓,返回一个包含ShiPanEStockPosition结构的列表(List<>).同时更新包含账户资金信息的成员 mytrader.AccountAsset.

5. 下单:PlaceOrder(string label,string BuyOrSell,ShiPanEOrderPriceType OrderPriceType, float price,int amount)

mytrader.PlaceOrder("600256","BUY",12.56,1000);

这个实例会以12.56元的价格下单购买1000股600256,并返回订单号。如果下单过程出错,则会返回-1. label: 股票的六位数代码 BuyOrSell:"BUY" 代表买入, "SELL"代表卖出。大小写不敏感。 OrderPriceType:委托方式。现在一共支持7种委托方式,详情请见enum ShiPanEOrderPriceType price:委托价格 amount:委托数量。这是股票数量,不是手数。

6. 撤单:CancelOrder(int Order_Number)

mytrader.CancelOrder(Order_Number)

撤销订单号为Order_Num的订单。订单号可以从 PlaceOrder() 返回值或者ShiPanEStockPosition中的OrderNum成员处获得。

7. 撤销所有委托:CancelOrders()

mytrader.CancelOrders()

撤销所有可以撤销的委托。

8. 查询某日可申购的新股GetNewStockList(DateTime date)

mytrader.GetNewStockList(new DateTime(year,month,day))

查询date所代表的日期里可申购的新股,返回一个包含可申购的新股代码的string[]。date中只有日期会被调用,具体时间部分会被忽略。 数据是从新浪的新股列表页面获取的。这是一个非常基本的实现,没有加入可靠性的检查。如果你希望保证结果不会因为网络波动受到影响,你最好自己加入重试的机制。 重试的频率由你自己决定,要控制频率,高频的请求可能会导致你被新浪拉入黑名单。

9. 返回当日可申购新股GetNewStockListToday()

mytrader.GetNewStockListToday()

返回调用当日可申购新股的列表,实际为GetNewStockList(DateTime.Now)

完整实例

因为这是一个类库,所以你需要在一个新的项目中引用它。这里我们新创建一个C#控制台项目,叫 "ShiPanETraderSDKDemo"。

下面有两个准备工作要做,第一,将我的类库工程添加到"ShiPanETraderSDKDemo"解决方案,第二,在"ShiPanETraderSDKDemo"中添加对该工程的引用。

然后你就可以尝试运行以下代码。如果你的实盘易配置正常,你应该能够运行以下代码。

如果你的实盘易运行的机器和调试改程序的机器是分开的,那么请自行将SetTdxwHost的IP地址替换为运行实盘易的机器的IP地址。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ShiPanEStockTrader;
namespace ShiPanETraderSDKDemo
{
    class Program
    {    
        static void Main(string[] args)
        {
            ShiPanETrader mytrader = new ShiPanETrader();

            //如果你的实盘易运行的机器和调试改程序的机器是分开的,那么请自行将SetTdxwHost的IP地址替换为运行实盘易的机器的IP地址
            int init=mytrader.SetTdxwHost("127.0.0.1");
            if (init != 0)
            {
                Console.WriteLine("实盘易初始化失败,请检查设置。");
                return;
            }
            //获取所有当日委托
            List<ShiPanEOrderStatusRespond> OrderResponses = mytrader.GetOrders();
            Console.WriteLine("我有{0}笔委托", OrderResponses.Count);

            //下单(限价委托)
            int Order_Num = mytrader.PlaceOrder("600256", "BUY", ShiPanEOrderPriceType.Limit, 12.56, 1000);
            if (Order_Num > 0)
                Console.WriteLine("下单成功,委托代码为{0}", Order_Num);
            else
                Console.WriteLine("下单失败,请重试。");

            //获取持仓
            List<ShiPanEStockPosition> AccountPositions = mytrader.GetPositions();
            Console.WriteLine("当前我有{0}笔持仓.", AccountPositions.Count);

            //获取可用资金
            Console.WriteLine("我的可用资金为{0}元.",mytrader.AccountAsset.CanUse);


            //Finishe line
            Console.WriteLine("程序结束");
            Console.ReadLine();
        }
    }
}
GNU LESSER GENERAL PUBLIC LICENSE Version 3, 29 June 2007 Copyright (C) 2007 Free Software Foundation, Inc. [http://fsf.org/] Everyone is permitted to copy and distribute verbatim copies of this license document, but changing it is not allowed. This version of the GNU Lesser General Public License incorporates the terms and conditions of version 3 of the GNU General Public License, supplemented by the additional permissions listed below. 0. Additional Definitions. As used herein, "this License" refers to version 3 of the GNU Lesser General Public License, and the "GNU GPL" refers to version 3 of the GNU General Public License. "The Library" refers to a covered work governed by this License, other than an Application or a Combined Work as defined below. An "Application" is any work that makes use of an interface provided by the Library, but which is not otherwise based on the Library. Defining a subclass of a class defined by the Library is deemed a mode of using an interface provided by the Library. A "Combined Work" is a work produced by combining or linking an Application with the Library. The particular version of the Library with which the Combined Work was made is also called the "Linked Version". The "Minimal Corresponding Source" for a Combined Work means the Corresponding Source for the Combined Work, excluding any source code for portions of the Combined Work that, considered in isolation, are based on the Application, and not on the Linked Version. The "Corresponding Application Code" for a Combined Work means the object code and/or source code for the Application, including any data and utility programs needed for reproducing the Combined Work from the Application, but excluding the System Libraries of the Combined Work. 1. Exception to Section 3 of the GNU GPL. You may convey a covered work under sections 3 and 4 of this License without being bound by section 3 of the GNU GPL. 2. Conveying Modified Versions. If you modify a copy of the Library, and, in your modifications, a facility refers to a function or data to be supplied by an Application that uses the facility (other than as an argument passed when the facility is invoked), then you may convey a copy of the modified version: a) under this License, provided that you make a good faith effort to ensure that, in the event an Application does not supply the function or data, the facility still operates, and performs whatever part of its purpose remains meaningful, or b) under the GNU GPL, with none of the additional permissions of this License applicable to that copy. 3. Object Code Incorporating Material from Library Header Files. The object code form of an Application may incorporate material from a header file that is part of the Library. You may convey such object code under terms of your choice, provided that, if the incorporated material is not limited to numerical parameters, data structure layouts and accessors, or small macros, inline functions and templates (ten or fewer lines in length), you do both of the following: a) Give prominent notice with each copy of the object code that the Library is used in it and that the Library and its use are covered by this License. b) Accompany the object code with a copy of the GNU GPL and this license document. 4. Combined Works. You may convey a Combined Work under terms of your choice that, taken together, effectively do not restrict modification of the portions of the Library contained in the Combined Work and reverse engineering for debugging such modifications, if you also do each of the following: a) Give prominent notice with each copy of the Combined Work that the Library is used in it and that the Library and its use are covered by this License. b) Accompany the Combined Work with a copy of the GNU GPL and this license document. c) For a Combined Work that displays copyright notices during execution, include the copyright notice for the Library among these notices, as well as a reference directing the user to the copies of the GNU GPL and this license document. d) Do one of the following: 0) Convey the Minimal Corresponding Source under the terms of this License, and the Corresponding Application Code in a form suitable for, and under terms that permit, the user to recombine or relink the Application with a modified version of the Linked Version to produce a modified Combined Work, in the manner specified by section 6 of the GNU GPL for conveying Corresponding Source. 1) Use a suitable shared library mechanism for linking with the Library. A suitable mechanism is one that (a) uses at run time a copy of the Library already present on the user's computer system, and (b) will operate properly with a modified version of the Library that is interface-compatible with the Linked Version. e) Provide Installation Information, but only if you would otherwise be required to provide such information under section 6 of the GNU GPL, and only to the extent that such information is necessary to install and execute a modified version of the Combined Work produced by recombining or relinking the Application with a modified version of the Linked Version. (If you use option 4d0, the Installation Information must accompany the Minimal Corresponding Source and Corresponding Application Code. If you use option 4d1, you must provide the Installation Information in the manner specified by section 6 of the GNU GPL for conveying Corresponding Source.) 5. Combined Libraries. You may place library facilities that are a work based on the Library side by side in a single library together with other library facilities that are not Applications and are not covered by this License, and convey such a combined library under terms of your choice, if you do both of the following: a) Accompany the combined library with a copy of the same work based on the Library, uncombined with any other library facilities, conveyed under the terms of this License. b) Give prominent notice with the combined library that part of it is a work based on the Library, and explaining where to find the accompanying uncombined form of the same work. 6. Revised Versions of the GNU Lesser General Public License. The Free Software Foundation may publish revised and/or new versions of the GNU Lesser General Public License from time to time. Such new versions will be similar in spirit to the present version, but may differ in detail to address new problems or concerns. Each version is given a distinguishing version number. If the Library as you received it specifies that a certain numbered version of the GNU Lesser General Public License "or any later version" applies to it, you have the option of following the terms and conditions either of that published version or of any later version published by the Free Software Foundation. If the Library as you received it does not specify a version number of the GNU Lesser General Public License, you may choose any version of the GNU Lesser General Public License ever published by the Free Software Foundation. If the Library as you received it specifies that a proxy can decide whether future versions of the GNU Lesser General Public License shall apply, that proxy's public statement of acceptance of any version is permanent authorization for you to choose that version for the Library.

About

C# SDK For ShiPanE expand collapse
C#
LGPL-3.0
Cancel

Releases

No release

Contributors

All

Activities

Load More
can not load any more
C#
1
https://gitee.com/ybo1990/ShiPanETradingSDK.git
git@gitee.com:ybo1990/ShiPanETradingSDK.git
ybo1990
ShiPanETradingSDK
ShiPanETradingSDK
master

Search

53164aa7 5694891 3bd8fe86 5694891