自动处理中金所平今仓公式代码案例
本帖最后由 hyjok 于 2015-11-16 15:20 编辑自动处理中金所平今仓公式代码案例(实盘运行已经2个月的代码逻辑)
一、AorderDataNBuy、AorderDataNSellShort函数处理平今仓逻辑:
1、无今仓时,优先平昨仓代替开仓
2、有今仓时,优先开仓代替平仓
3、设置交易账户最大持仓手数,预先考虑资金不足时的情况,开仓达到最大持仓手数时按正常逻辑平今仓(其实TB中已经有自动处理平今仓的功能,无奈没有考虑账户资金不足的情况怎么处理,只好自己写代码实现,直接设定最大手数的方式虽然粗略,但基本够用了)
二、AutoChangeExitToday自动转换平今仓发单案例测试策略公式代码(以带止损的双均线策略为例)
TB设置忽略自动交易,由公式中的两个函数里面的A函数发单
三、Amaxlots尾盘自动锁仓下单公式
特别需要注意:
1、函数中用到"Data/.Close()"调用,而且是利用忽略自动交易后Buy类函数只显示信号但A函数仍然可以发单交易的特性,因此本案例公式代码只能用于V5.1.0.16版本到V5.2.2.5版本
(TB V5.1.0.16版本起支持"Data/.Close()"调用,而且保留忽略自动交易不包括A函数发单,从TB V5.2.2.5版本之后的版本起忽略自动交易包括了A函数发单,因此V5.2.2.5版本之后的版本不适用)
2、TB系统设置“中金所股指日内开仓不超过10手”,防止开仓手数超过10手限制
本帖最后由 hyjok 于 2015-11-16 11:39 编辑
买入发单函数AorderDataNBuy
//------------------------------------------------------------------------
// 简称: AorderDataNBuy
// 名称:
// 类别: 用户函数
// 类型: 用户函数
// 输出: 布尔型
//------------------------------------------------------------------------
Params
Numeric DataN(0); //发单合约序列,Data0为0、Data1为1,以此类推DataN
Numeric myLots(1);//下单手数
Numeric iLastPrice(1);//1为用最新价报单,其他为对手价报单
Numeric OffSet(10);//委托偏移跳数
Numeric MaxLots(4);//对锁持仓最大手数,根据交易账户资金大小自行预设最大持仓手数
Vars
Numeric myBuyPosition;
Numeric myTodayBuyPosition;
Numeric myPreDayBuyPosition;
Numeric mySellPosition;
Numeric myTodaySellPosition;
Numeric myPreDaySellPosition;
Numeric myAskPrice;
Numeric myBidPrice;
Numeric MinPoint;
Bool con;
Begin
MinPoint = Data.MinMove*Data.PriceScale;
myAskPrice = IIF(iLastPrice==1,Data.Q_Last,Data.Q_AskPrice) + MinPoint*OffSet;
myBidPrice = IIF(iLastPrice==1,Data.Q_Last,Data.Q_BidPrice) - MinPoint*OffSet;
If (A_AccountID<>"")
{
myBuyPosition=Data.A_BuyPosition;
myTodayBuyPosition=Data.A_TodayBuyPosition;
myPreDayBuyPosition=myBuyPosition-myTodayBuyPosition;
mySellPosition=Data.A_SellPosition;
myTodaySellPosition=Data.A_TodaySellPosition;
myPreDaySellPosition=mySellPosition-myTodaySellPosition;
}
if (myTodaySellPosition==0 && mySellPosition>=myLots)//无今仓时平昨仓,昨仓足够
{
con=Data.A_SendOrder(Enum_Buy,Enum_Exit,myLots,myAskPrice);
mySellPosition = mySellPosition - myLots;
}Else
if (myTodaySellPosition==0 && mySellPosition<myLots && mySellPosition>=0)//无今仓时平昨仓,昨仓不够时有多少平多少,余下的转为开仓
{
con=Data.A_SendOrder(Enum_Buy,Enum_Exit,mySellPosition,myAskPrice);
con=Data.A_SendOrder(Enum_Buy,Enum_Entry,myLots-mySellPosition,myAskPrice);
myTodayBuyPosition = myTodayBuyPosition + myLots-mySellPosition;
myBuyPosition = myBuyPosition + myLots-mySellPosition;
mySellPosition = 0;
}Else
if (myTodaySellPosition>0 && myBuyPosition + myLots<=MaxLots)//有今仓,开仓后持仓不超过MaxLots的情况,平仓转为开仓
{
con=Data.A_SendOrder(Enum_Buy,Enum_Entry,myLots,myAskPrice);
myTodayBuyPosition = myTodayBuyPosition + myLots;
myBuyPosition = myBuyPosition + myLots;
}Else
if (myTodaySellPosition>0 && myBuyPosition + myLots>MaxLots)//有今仓,开仓后持仓超过MaxLots的情况,能开多少开多少,余下的转为开仓
{
if (myBuyPosition<MaxLots)
{
con=Data.A_SendOrder(Enum_Buy,Enum_Entry,MaxLots-myBuyPosition,myAskPrice);
con=Data.A_SendOrder(Enum_Buy,Enum_ExitToday,myLots-(MaxLots-myBuyPosition),myAskPrice);
myTodaySellPosition = myTodaySellPosition - (myLots-(MaxLots-myBuyPosition));
mySellPosition = mySellPosition - (myLots-(MaxLots-myBuyPosition));
myTodayBuyPosition = myTodayBuyPosition + (MaxLots-myBuyPosition);
myBuyPosition = MaxLots;
}Else if (myBuyPosition>=MaxLots)
{
con=Data.A_SendOrder(Enum_Buy,Enum_ExitToday,myLots,myAskPrice);
myTodaySellPosition = myTodaySellPosition - myLots;
mySellPosition = mySellPosition - myLots;
}
}
Return con;
End
本帖最后由 hyjok 于 2015-11-16 11:43 编辑
卖出发单函数AorderDataNSellShort
//------------------------------------------------------------------------
// 简称: AorderDataNSellShort
// 名称:
// 类别: 用户函数
// 类型: 用户函数
// 输出: 布尔型
//------------------------------------------------------------------------
Params
Numeric DataN(0); //发单合约序列,Data0为0、Data1为1,以此类推DataN
Numeric myLots(1);//下单手数
Numeric iLastPrice(1);//1为用最新价报单,其他为对手价报单
Numeric OffSet(10);//委托偏移跳数
Numeric MaxLots(4);//对锁持仓最大手数,根据交易账户资金大小自行预设最大持仓手数
Vars
Numeric myBuyPosition;
Numeric myTodayBuyPosition;
Numeric myPreDayBuyPosition;
Numeric mySellPosition;
Numeric myTodaySellPosition;
Numeric myPreDaySellPosition;
Numeric myAskPrice;
Numeric myBidPrice;
Numeric MinPoint;
Bool con;
Begin
MinPoint = Data.MinMove*Data.PriceScale;
myAskPrice = IIF(iLastPrice==1,Data.Q_Last,Data.Q_AskPrice) + MinPoint*OffSet;
myBidPrice = IIF(iLastPrice==1,Data.Q_Last,Data.Q_BidPrice) - MinPoint*OffSet;
If (A_AccountID<>"")
{
myBuyPosition=Data.A_BuyPosition;
myTodayBuyPosition=Data.A_TodayBuyPosition;
myPreDayBuyPosition=myBuyPosition-myTodayBuyPosition;
mySellPosition=Data.A_SellPosition;
myTodaySellPosition=Data.A_TodaySellPosition;
myPreDaySellPosition=mySellPosition-myTodaySellPosition;
}
if (myTodayBuyPosition==0 && myBuyPosition>=myLots)
{
con=Data.A_SendOrder(Enum_Sell,Enum_Exit,myLots,myBidPrice);
myBuyPosition = myBuyPosition - myLots;
}Else
if (myTodayBuyPosition==0 && myBuyPosition<myLots && myBuyPosition>=0)
{
con=Data.A_SendOrder(Enum_Sell,Enum_Exit,myBuyPosition,myBidPrice);
con=Data.A_SendOrder(Enum_Sell,Enum_Entry,myLots-myBuyPosition,myBidPrice);
myTodaySellPosition = myTodaySellPosition + (myLots-myBuyPosition);
mySellPosition = mySellPosition + (myLots-myBuyPosition);
myBuyPosition = 0;
}Else
if (myTodayBuyPosition>0 && mySellPosition + myLots<=MaxLots)//有今仓,平仓转为开仓
{
con=Data.A_SendOrder(Enum_Sell,Enum_Entry,myLots,myBidPrice);
myTodaySellPosition = myTodaySellPosition - myLots;
mySellPosition = mySellPosition - myLots;
}Else
if (myTodayBuyPosition>0 && mySellPosition + myLots>MaxLots)
{
if (mySellPosition<MaxLots)
{
con=Data.A_SendOrder(Enum_Sell,Enum_Entry,MaxLots-mySellPosition,myBidPrice);
con=Data.A_SendOrder(Enum_Sell,Enum_ExitToday,myLots-(MaxLots-mySellPosition),myBidPrice);
myTodayBuyPosition = myTodayBuyPosition - (myLots-(MaxLots-mySellPosition));
myBuyPosition = myBuyPosition - (myLots-(MaxLots-mySellPosition));
myTodaySellPosition = myTodaySellPosition + (MaxLots-mySellPosition);
mySellPosition = MaxLots;
}Else if (mySellPosition>=MaxLots)
{
con=Data.A_SendOrder(Enum_Sell,Enum_ExitToday,myLots,myBidPrice);
myTodayBuyPosition = myTodayBuyPosition - myLots;
myBuyPosition = myBuyPosition - myLots;
}
}
Return con;
End
本帖最后由 hyjok 于 2015-11-16 11:44 编辑
双均线策略测试公式
//------------------------------------------------------------------------
// 简称: AutoChangeExitToday
// 名称: 自动转换平今仓发单案例
// 类别: 公式应用
// 类型: 用户应用
// 输出:
//------------------------------------------------------------------------
Params
Numeric FastLength(5);
Numeric SlowLength(20);
Numeric DataN(0); //发单合约序列,不叠加合约时Data0为0、叠加合约Data1为1,以此类推DataN
Numeric Lots(1);
Numeric AOrder(1);//A函数发单开关,1为A函数发单,其他为不允许A函数发单
Numeric iLastPrice(1);//1为用最新价报单,其他为对手价报单
Numeric OffSet(10);//委托偏移跳数
Numeric MaxLots(4);//对锁持仓最大手数,用户根据账户资金大小预设最大持仓手数,
//应用时设置忽略所有自动交易,用A函数发单,适用于TBV5.2.3.16之前的版本(V5.2.3.16版本开始忽略自动交易也会忽略A函数发单)
Vars
NumericSeries AvgValue1;
NumericSeries AvgValue2;
Numeric myBuyPosition;
Numeric myTodayBuyPosition;
Numeric mySellPosition;
Numeric myTodaySellPosition;
Numeric myLots;
Numeric MinPoint;
Begin
MinPoint = MinMove*PriceScale;
If (A_AccountID<>"" && BarStatus==2)
{
myBuyPosition=Data.A_BuyPosition;
myTodayBuyPosition=Data.A_TodayBuyPosition;
mySellPosition=Data.A_SellPosition;
myTodaySellPosition=Data.A_TodaySellPosition;
}
Commentary("myBuyPosition="+Text(myBuyPosition));
Commentary("myTodayBuyPosition="+Text(myTodayBuyPosition));
Commentary("mySellPosition="+Text(mySellPosition));
Commentary("myTodaySellPosition="+Text(myTodaySellPosition));
AvgValue1 = AverageFC(Close,FastLength);
AvgValue2 = AverageFC(Close,SlowLength);
If(MarketPosition <>1 && AvgValue1 > AvgValue2)
{
If (AOrder==1 && GetGlobalVar(0)<>1)//全局变量的作用是控制重复发单
{
If (AorderDataNBuy(DataN,IIF(MarketPosition==-1,2,1)*Lots,iLastPrice,OffSet,MaxLots)) SetGlobalVar(0,1);
}
Buy(Lots,Open);
}
If(MarketPosition <>-1 && AvgValue1 < AvgValue2)
{
If (AOrder==1 && GetGlobalVar(0)<>-1)
{
If (AOrderDataNSellShort(DataN,IIF(MarketPosition==1,2,1)*Lots,iLastPrice,OffSet,MaxLots)) SetGlobalVar(0,-1);
}
SellShort(Lots,Open);
}
If (MarketPosition==1 && C<=EntryPrice*0.99)
{
If (AOrder==1 && GetGlobalVar(0)<>2)
{
If (AOrderDataNSellShort(DataN,Abs(CurrentContracts),iLastPrice,OffSet,MaxLots)) SetGlobalVar(0,2);
}
Sell(0,Open);
}
If (MarketPosition==-1 && C>=EntryPrice*1.01)
{
If (AOrder==1 && GetGlobalVar(0)<>-2)
{
If (AorderDataNBuy(DataN,Abs(CurrentContracts),iLastPrice,OffSet,MaxLots)) SetGlobalVar(0,-2);
}
BuyToCover(0,Open);
}
PlotNumeric("MA1",AvgValue1);
PlotNumeric("MA2",AvgValue2);
End
本帖最后由 hyjok 于 2015-11-16 14:52 编辑
尾盘锁仓代码//------------------------------------------------------------------------
// 简称: Amaxlots
// 名称: A函数锁仓下单
// 类别: 公式应用
// 类型: 用户应用
// 输出:
//------------------------------------------------------------------------
//应用场景,尾盘锁仓下单,特别注意时间设置,确保其他策略在ActionTime之后不会再交易,Tick周期图表运行
Params
Numeric ActionTime(1514.02);
Numeric AOrder(1);
Numeric OffSet(2);//委托偏移跳数
Numeric MaxLots(2);//对锁持仓最大手数
Numeric ABspreadTick(5);//买卖价差跳数
Vars
Numeric myBuyPosition;
Numeric myTodayBuyPosition;
Numeric mySellPosition;
Numeric myTodaySellPosition;
Numeric myLots;
Numeric MinPoint;
Numeric wthd;//委托滑点
Numeric bcLots;//补充对锁手数
Numeric i;
Numeric todayEntryLots;//今开手数
Numeric nCount;
Begin
Commentary("10000*Time="+Text(10000*Time));
If (BarStatus<2) Return;
If (A_AccountID=="") Return;
MinPoint = MinMove*PriceScale;
wthd = MinPoint*OffSet;
If (A_AccountID<>"")
{
myBuyPosition=A_BuyPosition;
myTodayBuyPosition=A_TodayBuyPosition;
mySellPosition=A_SellPosition;
myTodaySellPosition=A_TodaySellPosition;
}
Commentary("myBuyPosition="+Text(myBuyPosition));
Commentary("myTodayBuyPosition="+Text(myTodayBuyPosition));
Commentary("mySellPosition="+Text(mySellPosition));
Commentary("myTodaySellPosition="+Text(myTodaySellPosition));
If (Date<>Date) SetGlobalVar(2,0);
If (AOrder==1 && 10000*Time>ActionTime)
{
If (GetGlobalVar(2)==InvalidNumeric || GetGlobalVar(2)==0)
{
todayEntryLots = 0;
nCount = A_GetOrderCount();
For i = 1 To nCount
{
If (A_OrderStatus(i)==Enum_Filled && A_OrderEntryOrExit(i)==Enum_Entry)
{
todayEntryLots = todayEntryLots + A_OrderLot(i);
}
}
bcLots = MaxLots-Max(myBuyPosition,mySellPosition);
If ((todayEntryLots+2*bcLots)>10) bcLots = IntPart((10-todayEntryLots)/2);
SetGlobalVar(2,bcLots);
Commentary("GetGlobalVar(2)="+Text(GetGlobalVar(2)));
Commentary("bcLots="+Text(bcLots));
}
If (GetGlobalVar(2)>0 && GetGlobalVar(0)<>1 && A_GetOpenOrderCount()==0 && Q_AskPrice-Q_BidPrice<=ABspreadTick*MinPoint)
{
If (A_SendOrder(Enum_Sell,Enum_Entry,GetGlobalVar(2),Q_BidPrice-wthd)) SetGlobalVar(0,1);
Return;
}Else
If (GetGlobalVar(2)>0 && GetGlobalVar(1)<>1 && A_GetOpenOrderCount()==0 && Q_AskPrice-Q_BidPrice<=ABspreadTick*MinPoint)
{
If (A_SendOrder(Enum_Buy,Enum_Entry,GetGlobalVar(2),Q_AskPrice+wthd)) SetGlobalVar(1,1);
Return;
}
}
Commentary("GetGlobalVar(0)="+Text(GetGlobalVar(0)));
Commentary("GetGlobalVar(1)="+Text(GetGlobalVar(1)));
Commentary("GetGlobalVar(2)="+Text(GetGlobalVar(2)));
End 赞,了不起
页:
[1]