|
鼎砥金融学院大三级同学
    
- 帖子
- 485
- 积分
- 8161
- 威望
- 4167
- 金钱
- 3927
- 活跃积分
- 0
- 魅力积分
- 0
- 阅读权限
- 150
- 在线时间
- 67 小时
- 注册时间
- 2007-3-31
|
1楼
大 中
小 发表于 2008-6-17 10:10 只看该作者
您是本帖第109个浏览者
[06-17] VTtrader API 1.0.4 beta3 常用方法说明文档
VTtrader API 1.0.4 beta3 常用方法说明文档
//初始化
VT_APIClass api= new VTAPI.VT_APIClass();
//得到服务器列表
string serverList= Api.GetServerList();
//登陆
int loginStatus=Api.Login(string name,string pwd,int serverID);
//退出
int isQuit=Api.Logout();
//卸载
Api.Finalize();
Api=null;
//得到服务器时间
DateTime dt=Api.GetServerTime();
//查看是否已登录
Bool isLogin=Api.LoggedIn;
//账号
Account a1=Api.GetAccountByIndex(0);
Account a2=Api.GetAccountByID(“3318189”);
//货币对
Instrument i1=Api.GetInstrumentByIndex(0);
Instrument i2=Api.GetInstruemntByCurrency(“EUR”,”USD”);
//已开仓信息
Position p1=Api.GetOpenPositionByIndex(0);
Position p2=Api.GetOpenPositionByID(“3318189”);
//已申请挂单
Order o1=Api.GetOrderByIndex(0);
Order o2=Api.GetOrderByID(“3318189”);
//VT系统常量
ConstRecord consts =API.constants;
//开仓(duration = consts.dur_GTC,如果hedge设置为False,当你为当前已有仓位开一反向单时,系统将自动平仓,而不是下反向单;如果hedge设置为True,你将可以给同一货币对开多个买卖仓位)
public static int Position_Create(Account account, Instrument instrument, short duration, short sellbuy, double amount, bool hedge)
{
int TraderRange=0,Rate=0;
bool Initial=Convert.ToBoolean(api.constants.ord_Initial);
return api.SendCreateOrder(account.ID, instrument.Index, Convert.ToBoolean(duration), TraderRange, Convert.ToBoolean(sellbuy), Rate, amount, hedge, Initial);
}
//添加挂单(offset一般为0,limit=limitPips,stop=stopPips)
public static int Order_Create(Account account, Instrument instrument, short duration, short sellbuy, double amount, bool hedge,int offset,int limit,int stop)
{
int TraderRange = 0,StopRange=0;
bool Initial = Convert.ToBoolean(api.constants.ord_Entry);
if (sellbuy == api.constants.bs_Sell)
{
offset = -offset;
limit = -limit;
stop = -limit;
}
Instrument tmp = (Instrument)instrument;
double closeRate, openRate, litmitRate, stopRate;
openRate = tmp.Ask + tmp.pointSize * offset;
closeRate = tmp.Bid + tmp.pointSize * offset;
litmitRate = closeRate + tmp.pointSize * limit;
stopRate = closeRate - tmp.pointSize * stop;
return api.SendCreateOrderWithSL(account, instrument, Convert.ToBoolean(duration), TraderRange, Convert.ToBoolean(sellbuy), openRate, amount, Convert.ToBoolean(hedge), Initial, litmitRate, stopRate, StopRange);
}
//修改限价
public static int Position_Limit_Change(Position position,double rate)
{
if (position.LimitOrder == null)
return api.SendSetLimitForOpenPosition(position.ID, rate);
else
return api.SendChangeLimitForOpenPosition(position.ID, rate);
}
//修改止损
public static int Position_Stop_Change(Position position, double rate)
{
if (position.StopOrder == null)
return api.SendSetStopForOpenPosition(position.ID, rate);
else
return api.SendChangeStopForOpenPosition(position.ID, rate);
}
//移除限价
public static int Position_Limit_Remove(Position position)
{
return api.SendRemoveLimitForOpenPosition(position.ID);
}
//移除止损
public static int Position_Stop_Remove(Position position)
{
return api.SendRemoveStopForOpenPosiotion(position.ID);
}
//VTtrader 事件类(只写出常用事件)
public class cl_event : IVT_APIEvents
{
public cl_event() { }
public void OnNewServerMessage(int msg_index)
{
//得到信息
ServerMessage sm = cl_api.API.ServerMessages.get_Items(msg_index);
string mlName = Enum.GetName(typeof(MessageLevel), sm.Level);
//输出信息内容
Console.WriteLine(string.Concat(sm.Time.ToString("yy-MM-dd hh:mm:ss"), mlName == null ? sm.Level.ToString() : mlName.ToLower(), sm.Text, sm.Sender));
//根据信息Level选择不同行为
MessageLevel ml = (MessageLevel)Enum.Parse(typeof(MessageLevel), mlName);
switch (ml)
{
case MessageLevel.ML_CONFIRM:
break;
case MessageLevel.ML_ERROR:
break;
}
}
public void OnPositionChange(string id, int action)
{
string mtName = Enum.GetName(typeof(MessageType), action);
if (mtName == null)
return;
//设置消息类型
MessageType mt = (MessageType)Enum.Parse(typeof(MessageType), mtName);
switch (mt)
{
case MessageType.MT_NEW_POSITION:
//开仓确认
break;
case MessageType.MT_CLOSE_POSITION:
//平仓确认
break;
}
}
public void OnConnectionLost()
{
//服务器断开连接
}
public void OnChangesComplete()
{
//更新完成
//当所有更新完成,从服务器上获取更新,赋值给本地变量
API.ServerMessages.GetRefreshRate();
}
}
抢钱,抢粮,抢地盘!
|