自動處理中金所平今倉公式代碼案例
作者:開拓者 TB 來源:cxh99.com 發布時間:2016年02月16日
- 咨詢內容:
本帖最后由 hyjok 于 2015-11-16 15:20 編輯
自動處理中金所平今倉公式代碼案例(實盤運行已經2個月的代碼邏輯)
一、AorderDataNBuy、AorderDataNSellShort函數處理平今倉邏輯:
1、無今倉時,優先平昨倉代替開倉
2、有今倉時,優先開倉代替平倉
3、設置交易賬戶最大持倉手數,預先考慮資金不足時的情況,開倉達到最大持倉手數時按正常邏輯平今倉(其實TB中已經有自動處理平今倉的功能,無奈沒有考慮賬戶資金不足的情況怎么處理,只好自己寫代碼實現,直接設定最大手數的方式雖然粗略,但基本夠用了)
二、AutoChangeExitToday自動轉換平今倉發單案例測試策略公式代碼(以帶止損的雙均線策略為例)
TB設置忽略自動交易,由公式中的兩個函數里面的A函數發單
三、Amaxlots尾盤自動鎖倉下單公式
特別需要注意:
1、函數中用到"Data/[i/].Close()"調用,而且是利用忽略自動交易后Buy類函數只顯示信號但A函數仍然可以發單交易的特性,因此本案例公式代碼只能用于V5.1.0.16版本到V5.2.2.5版本
(TB V5.1.0.16版本起支持"Data/[i/].Close()"調用,而且保留忽略自動交易不包括A函數發單,從TB V5.2.2.5版本之后的版本起忽略自動交易包括了A函數發單,因此V5.2.2.5版本之后的版本不適用)
2、TB系統設置“中金所股指日內開倉不超過10手”,防止開倉手數超過10手限制
- TB技術人員:
本帖最后由 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[DataN].MinMove*Data[DataN].PriceScale;
- myAskPrice = IIF(iLastPrice==1,Data[DataN].Q_Last,Data[DataN].Q_AskPrice) + MinPoint*OffSet;
- myBidPrice = IIF(iLastPrice==1,Data[DataN].Q_Last,Data[DataN].Q_BidPrice) - MinPoint*OffSet;
- If (A_AccountID<>"")
- {
- myBuyPosition=Data[DataN].A_BuyPosition;
- myTodayBuyPosition=Data[DataN].A_TodayBuyPosition;
- myPreDayBuyPosition=myBuyPosition-myTodayBuyPosition;
- mySellPosition=Data[DataN].A_SellPosition;
- myTodaySellPosition=Data[DataN].A_TodaySellPosition;
- myPreDaySellPosition=mySellPosition-myTodaySellPosition;
- }
- if (myTodaySellPosition==0 && mySellPosition>=myLots)//無今倉時平昨倉,昨倉足夠
- {
- con=Data[DataN].A_SendOrder(Enum_Buy,Enum_Exit,myLots,myAskPrice);
- mySellPosition = mySellPosition - myLots;
- }Else
- if (myTodaySellPosition==0 && mySellPosition<myLots && mySellPosition>=0)//無今倉時平昨倉,昨倉不夠時有多少平多少,余下的轉為開倉
- {
- con=Data[DataN].A_SendOrder(Enum_Buy,Enum_Exit,mySellPosition,myAskPrice);
- con=Data[DataN].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[DataN].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[DataN].A_SendOrder(Enum_Buy,Enum_Entry,MaxLots-myBuyPosition,myAskPrice);
- con=Data[DataN].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[DataN].A_SendOrder(Enum_Buy,Enum_ExitToday,myLots,myAskPrice);
- myTodaySellPosition = myTodaySellPosition - myLots;
- mySellPosition = mySellPosition - myLots;
- }
- }
- Return con;
- End
復制代碼
- TB客服:
本帖最后由 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[DataN].MinMove*Data[DataN].PriceScale;
- myAskPrice = IIF(iLastPrice==1,Data[DataN].Q_Last,Data[DataN].Q_AskPrice) + MinPoint*OffSet;
- myBidPrice = IIF(iLastPrice==1,Data[DataN].Q_Last,Data[DataN].Q_BidPrice) - MinPoint*OffSet;
- If (A_AccountID<>"")
- {
- myBuyPosition=Data[DataN].A_BuyPosition;
- myTodayBuyPosition=Data[DataN].A_TodayBuyPosition;
- myPreDayBuyPosition=myBuyPosition-myTodayBuyPosition;
- mySellPosition=Data[DataN].A_SellPosition;
- myTodaySellPosition=Data[DataN].A_TodaySellPosition;
- myPreDaySellPosition=mySellPosition-myTodaySellPosition;
- }
- if (myTodayBuyPosition==0 && myBuyPosition>=myLots)
- {
- con=Data[DataN].A_SendOrder(Enum_Sell,Enum_Exit,myLots,myBidPrice);
- myBuyPosition = myBuyPosition - myLots;
- }Else
- if (myTodayBuyPosition==0 && myBuyPosition<myLots && myBuyPosition>=0)
- {
- con=Data[DataN].A_SendOrder(Enum_Sell,Enum_Exit,myBuyPosition,myBidPrice);
- con=Data[DataN].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[DataN].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[DataN].A_SendOrder(Enum_Sell,Enum_Entry,MaxLots-mySellPosition,myBidPrice);
- con=Data[DataN].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[DataN].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[DataN].A_BuyPosition;
- myTodayBuyPosition=Data[DataN].A_TodayBuyPosition;
- mySellPosition=Data[DataN].A_SellPosition;
- myTodaySellPosition=Data[DataN].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[1] > AvgValue2[1])
- {
- 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[1] < AvgValue2[1])
- {
- 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[1]<=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[1]>=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[1]) 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
復制代碼 |