using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
namespace demo
{
class Program
{
static void Main(string[] args)
{
//DLL是32位的,因此必须把C#工程生成的目标平台从Any CPU改为X86,才能调用DLL;
//必须把Trade.dll等4个DLL复制到Debug或Release工程目录下;
StringBuilder ErrInfo=new StringBuilder(256);
StringBuilder Result = new StringBuilder(1024*1024);
GetEdition(Result);//获取交易DLL版本
Console.WriteLine(Result);
OpenTdx();//打开通达信
int ClientID = Logon("119.145.12.70", 443, "2.20", "1111", "1111", string.Empty, ErrInfo);//登录
if (ClientID==-1)
{
Console.WriteLine(ErrInfo);
return;
}
SendOrder(ClientID, 0, 0, "A000001", "601988", 2.5, 100, Result, ErrInfo);//下单
Console.WriteLine("下单结果: {0}", Result);
GetQuote(ClientID, "601988", Result, ErrInfo);//查询五档报价
if (ErrInfo.ToString() != string.Empty)
{
Console.WriteLine(ErrInfo.ToString());
return;
}
Console.WriteLine("行情结果: {0}", Result);
QueryData(ClientID, 0, Result, ErrInfo);//查询资金
if (ErrInfo.ToString()!=string.Empty)
{
Console.WriteLine(ErrInfo.ToString());
return;
}
Console.WriteLine("查询结果: {0}", Result);
//批量查询多个证券的五档报价
string[] Zqdm=new string[]{"600030","600031"};
string[] Results = new string[Zqdm.Length];
string[] ErrInfos = new string[Zqdm.Length];
IntPtr[] ResultPtr = new IntPtr[Zqdm.Length];
IntPtr[] ErrInfoPtr = new IntPtr[Zqdm.Length];
for (int i = 0; i < Zqdm.Length; i++)
{
ResultPtr[i] = Marshal.AllocHGlobal(1024 * 1024);
ErrInfoPtr[i] = Marshal.AllocHGlobal(256);
}
GetQuotes(ClientID, Zqdm, Zqdm.Length, ResultPtr, ErrInfoPtr);
for (int i = 0; i < Zqdm.Length; i++)
{
Results[i] =Marshal.PtrToStringAnsi(ResultPtr[i]);
ErrInfos[i] = Marshal.PtrToStringAnsi(ErrInfoPtr[i]);
Marshal.FreeHGlobal(ResultPtr[i]);
Marshal.FreeHGlobal(ErrInfoPtr[i]);
}
Logoff(ClientID);//注销
CloseTdx();//关闭通达信
Console.ReadLine();
}
[DllImport("trade.dll", CharSet = CharSet.Ansi)]
public static extern void OpenTdx();
[DllImport("trade.dll", CharSet = CharSet.Ansi)]
public static extern void CloseTdx();
[DllImport("trade.dll", CharSet = CharSet.Ansi)]
public static extern int Logon(string IP, short Port, string Version, string AccountNo, string JyPassword, string TxPassword, StringBuilder ErrInfo);
[DllImport("trade.dll", CharSet = CharSet.Ansi)]
public static extern void Logoff(int ClientID);
[DllImport("trade.dll", CharSet = CharSet.Ansi)]
public static extern void QueryData(int ClientID, int Category, StringBuilder Result, StringBuilder ErrInfo);
[DllImport("trade.dll", CharSet = CharSet.Ansi)]
public static extern void SendOrder(int ClientID, int Category, int PriceType, string Gddm, string Zqdm, float Price, int Quantity, StringBuilder Result, StringBuilder ErrInfo);
[DllImport("trade.dll", CharSet = CharSet.Ansi)]
public static extern void CancelOrder(int ClientID, string hth, StringBuilder Result, StringBuilder ErrInfo);
[DllImport("trade.dll", CharSet = CharSet.Ansi)]
public static extern void GetQuote(int ClientID, string Zqdm, StringBuilder Result, StringBuilder ErrInfo);
[DllImport("trade.dll", CharSet = CharSet.Ansi)]
public static extern void GetEdition(StringBuilder Result);
[DllImport("trade.dll", CharSet = CharSet.Ansi)]
public static extern void QueryHistoryData(int ClientID, int Category, string StartDate, string EndDate, StringBuilder Result, StringBuilder ErrInfo);
[DllImport("trade.dll", CharSet = CharSet.Ansi)]
public static extern void QueryDatas(int ClientID, int[] Category, int Count, IntPtr[] Result, IntPtr[] ErrInfo);
[DllImport("trade.dll", CharSet = CharSet.Ansi)]
public static extern void SendOrders(int ClientID, int[] Category, int[] PriceType, string[] Gddm, string[] Zqdm, float[] Price, int[] Quantity, int Count, IntPtr[] Result, IntPtr[] ErrInfo);
[DllImport("trade.dll", CharSet = CharSet.Ansi)]
public static extern void CancelOrders(int ClientID, string[] hth, int Count, IntPtr[] Result, IntPtr[] ErrInfo);
[DllImport("trade.dll", CharSet = CharSet.Ansi)]
public static extern void GetQuotes(int ClientID, string[] Zqdm, int Count, IntPtr[] Result, IntPtr[] ErrInfo);
[DllImport("trade.dll", CharSet = CharSet.Ansi)]
public static extern void Repay(int ClientID, string Amount, StringBuilder Result, StringBuilder ErrInfo);
}
}