大伊人青草狠狠久久-大伊香蕉精品视频在线-大伊香蕉精品一区视频在线-大伊香蕉在线精品不卡视频-大伊香蕉在线精品视频75-大伊香蕉在线精品视频人碰人

您現在的位置:程序化交易>> 期貨公式>> 交易開拓者(TB)>> 開拓者知識>>正文內容

自動處理中金所平今倉公式代碼案例 [開拓者 TB]

  • 咨詢內容: 本帖最后由 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
    1. //------------------------------------------------------------------------
    2. // 簡稱: AorderDataNBuy
    3. // 名稱:
    4. // 類別: 用戶函數
    5. // 類型: 用戶函數
    6. // 輸出: 布爾型
    7. //------------------------------------------------------------------------

    8. Params
    9.     Numeric DataN(0); //發單合約序列,Data0為0、Data1為1,以此類推DataN
    10.     Numeric myLots(1);//下單手數
    11.         Numeric iLastPrice(1);//1為用最新價報單,其他為對手價報單
    12.         Numeric OffSet(10);//委托偏移跳數
    13.         Numeric MaxLots(4);//對鎖持倉最大手數,根據交易賬戶資金大小自行預設最大持倉手數
    14. Vars
    15.         Numeric myBuyPosition;
    16.         Numeric myTodayBuyPosition;
    17.         Numeric myPreDayBuyPosition;
    18.         Numeric mySellPosition;
    19.         Numeric myTodaySellPosition;
    20.         Numeric myPreDaySellPosition;
    21.         Numeric myAskPrice;
    22.         Numeric myBidPrice;
    23.         Numeric MinPoint;
    24.         Bool con;
    25. Begin
    26.     MinPoint = Data[DataN].MinMove*Data[DataN].PriceScale;
    27.         myAskPrice = IIF(iLastPrice==1,Data[DataN].Q_Last,Data[DataN].Q_AskPrice) + MinPoint*OffSet;
    28.         myBidPrice = IIF(iLastPrice==1,Data[DataN].Q_Last,Data[DataN].Q_BidPrice) - MinPoint*OffSet;
    29.     If (A_AccountID<>"")
    30.         {
    31.             myBuyPosition=Data[DataN].A_BuyPosition;
    32.                 myTodayBuyPosition=Data[DataN].A_TodayBuyPosition;
    33.                 myPreDayBuyPosition=myBuyPosition-myTodayBuyPosition;
    34.                 mySellPosition=Data[DataN].A_SellPosition;
    35.                 myTodaySellPosition=Data[DataN].A_TodaySellPosition;
    36.                 myPreDaySellPosition=mySellPosition-myTodaySellPosition;
    37.         }
    38.         if (myTodaySellPosition==0 && mySellPosition>=myLots)//無今倉時平昨倉,昨倉足夠
    39.         {
    40.             con=Data[DataN].A_SendOrder(Enum_Buy,Enum_Exit,myLots,myAskPrice);
    41.                 mySellPosition = mySellPosition - myLots;
    42.         }Else
    43.         if (myTodaySellPosition==0 && mySellPosition<myLots && mySellPosition>=0)//無今倉時平昨倉,昨倉不夠時有多少平多少,余下的轉為開倉
    44.         {
    45.             con=Data[DataN].A_SendOrder(Enum_Buy,Enum_Exit,mySellPosition,myAskPrice);
    46.             con=Data[DataN].A_SendOrder(Enum_Buy,Enum_Entry,myLots-mySellPosition,myAskPrice);
    47.                 myTodayBuyPosition = myTodayBuyPosition + myLots-mySellPosition;
    48.                 myBuyPosition = myBuyPosition + myLots-mySellPosition;
    49.                 mySellPosition = 0;
    50.         }Else
    51.         if (myTodaySellPosition>0 && myBuyPosition + myLots<=MaxLots)//有今倉,開倉后持倉不超過MaxLots的情況,平倉轉為開倉
    52.         {
    53.                 con=Data[DataN].A_SendOrder(Enum_Buy,Enum_Entry,myLots,myAskPrice);
    54.                 myTodayBuyPosition = myTodayBuyPosition + myLots;
    55.                 myBuyPosition = myBuyPosition + myLots;
    56.         }Else
    57.         if (myTodaySellPosition>0 && myBuyPosition + myLots>MaxLots)//有今倉,開倉后持倉超過MaxLots的情況,能開多少開多少,余下的轉為開倉
    58.         {
    59.                 if (myBuyPosition<MaxLots)
    60.                 {
    61.                     con=Data[DataN].A_SendOrder(Enum_Buy,Enum_Entry,MaxLots-myBuyPosition,myAskPrice);
    62.                         con=Data[DataN].A_SendOrder(Enum_Buy,Enum_ExitToday,myLots-(MaxLots-myBuyPosition),myAskPrice);
    63.                         myTodaySellPosition = myTodaySellPosition - (myLots-(MaxLots-myBuyPosition));
    64.                         mySellPosition = mySellPosition - (myLots-(MaxLots-myBuyPosition));
    65.                         myTodayBuyPosition = myTodayBuyPosition + (MaxLots-myBuyPosition);
    66.                     myBuyPosition = MaxLots;
    67.                 }Else if (myBuyPosition>=MaxLots)
    68.                 {
    69.                     con=Data[DataN].A_SendOrder(Enum_Buy,Enum_ExitToday,myLots,myAskPrice);
    70.                         myTodaySellPosition = myTodaySellPosition - myLots;
    71.                         mySellPosition = mySellPosition - myLots;
    72.                 }
    73.         }
    74.     Return con;
    75. End
    復制代碼

     

  • TB客服: 本帖最后由 hyjok 于 2015-11-16 11:43 編輯

    賣出發單函數AorderDataNSellShort
    1. //------------------------------------------------------------------------
    2. // 簡稱: AorderDataNSellShort
    3. // 名稱:
    4. // 類別: 用戶函數
    5. // 類型: 用戶函數
    6. // 輸出: 布爾型
    7. //------------------------------------------------------------------------

    8. Params
    9.     Numeric DataN(0); //發單合約序列,Data0為0、Data1為1,以此類推DataN
    10.     Numeric myLots(1);//下單手數
    11.         Numeric iLastPrice(1);//1為用最新價報單,其他為對手價報單
    12.         Numeric OffSet(10);//委托偏移跳數
    13.         Numeric MaxLots(4);//對鎖持倉最大手數,根據交易賬戶資金大小自行預設最大持倉手數
    14. Vars
    15.         Numeric myBuyPosition;
    16.         Numeric myTodayBuyPosition;
    17.         Numeric myPreDayBuyPosition;
    18.         Numeric mySellPosition;
    19.         Numeric myTodaySellPosition;
    20.         Numeric myPreDaySellPosition;
    21.         Numeric myAskPrice;
    22.         Numeric myBidPrice;
    23.         Numeric MinPoint;
    24.         Bool con;
    25. Begin
    26.     MinPoint = Data[DataN].MinMove*Data[DataN].PriceScale;
    27.         myAskPrice = IIF(iLastPrice==1,Data[DataN].Q_Last,Data[DataN].Q_AskPrice) + MinPoint*OffSet;
    28.         myBidPrice = IIF(iLastPrice==1,Data[DataN].Q_Last,Data[DataN].Q_BidPrice) - MinPoint*OffSet;
    29.     If (A_AccountID<>"")
    30.         {
    31.             myBuyPosition=Data[DataN].A_BuyPosition;
    32.                 myTodayBuyPosition=Data[DataN].A_TodayBuyPosition;
    33.                 myPreDayBuyPosition=myBuyPosition-myTodayBuyPosition;
    34.                 mySellPosition=Data[DataN].A_SellPosition;
    35.                 myTodaySellPosition=Data[DataN].A_TodaySellPosition;
    36.                 myPreDaySellPosition=mySellPosition-myTodaySellPosition;
    37.         }
    38.         if (myTodayBuyPosition==0 && myBuyPosition>=myLots)
    39.         {
    40.                 con=Data[DataN].A_SendOrder(Enum_Sell,Enum_Exit,myLots,myBidPrice);
    41.                 myBuyPosition = myBuyPosition - myLots;
    42.         }Else
    43.         if (myTodayBuyPosition==0 && myBuyPosition<myLots && myBuyPosition>=0)
    44.         {
    45.                 con=Data[DataN].A_SendOrder(Enum_Sell,Enum_Exit,myBuyPosition,myBidPrice);
    46.                 con=Data[DataN].A_SendOrder(Enum_Sell,Enum_Entry,myLots-myBuyPosition,myBidPrice);
    47.                 myTodaySellPosition = myTodaySellPosition + (myLots-myBuyPosition);
    48.                 mySellPosition = mySellPosition + (myLots-myBuyPosition);
    49.                 myBuyPosition = 0;
    50.         }Else
    51.         if (myTodayBuyPosition>0 && mySellPosition + myLots<=MaxLots)//有今倉,平倉轉為開倉
    52.         {
    53.                 con=Data[DataN].A_SendOrder(Enum_Sell,Enum_Entry,myLots,myBidPrice);
    54.                 myTodaySellPosition = myTodaySellPosition - myLots;
    55.                 mySellPosition = mySellPosition - myLots;
    56.         }Else
    57.         if (myTodayBuyPosition>0 && mySellPosition + myLots>MaxLots)
    58.         {
    59.                 if (mySellPosition<MaxLots)
    60.                 {
    61.                     con=Data[DataN].A_SendOrder(Enum_Sell,Enum_Entry,MaxLots-mySellPosition,myBidPrice);
    62.                         con=Data[DataN].A_SendOrder(Enum_Sell,Enum_ExitToday,myLots-(MaxLots-mySellPosition),myBidPrice);
    63.                         myTodayBuyPosition = myTodayBuyPosition - (myLots-(MaxLots-mySellPosition));
    64.                         myBuyPosition = myBuyPosition - (myLots-(MaxLots-mySellPosition));
    65.                         myTodaySellPosition = myTodaySellPosition + (MaxLots-mySellPosition);
    66.                     mySellPosition = MaxLots;
    67.                 }Else if (mySellPosition>=MaxLots)
    68.                 {
    69.                     con=Data[DataN].A_SendOrder(Enum_Sell,Enum_ExitToday,myLots,myBidPrice);
    70.                         myTodayBuyPosition = myTodayBuyPosition - myLots;
    71.                         myBuyPosition = myBuyPosition - myLots;
    72.                 }
    73.         }
    74.     Return con;
    75. End
    復制代碼

     

  • 網友回復: 本帖最后由 hyjok 于 2015-11-16 11:44 編輯

    雙均線策略測試公式
    1. //------------------------------------------------------------------------
    2. // 簡稱: AutoChangeExitToday
    3. // 名稱: 自動轉換平今倉發單案例
    4. // 類別: 公式應用
    5. // 類型: 用戶應用
    6. // 輸出:
    7. //------------------------------------------------------------------------

    8. Params
    9.         Numeric FastLength(5);
    10.         Numeric SlowLength(20);
    11.        
    12.         Numeric DataN(0); //發單合約序列,不疊加合約時Data0為0、疊加合約Data1為1,以此類推DataN
    13.         Numeric Lots(1);
    14.         Numeric AOrder(1);//A函數發單開關,1為A函數發單,其他為不允許A函數發單
    15.         Numeric iLastPrice(1);//1為用最新價報單,其他為對手價報單
    16.         Numeric OffSet(10);//委托偏移跳數
    17.         Numeric MaxLots(4);//對鎖持倉最大手數,用戶根據賬戶資金大小預設最大持倉手數,
    18.         //應用時設置忽略所有自動交易,用A函數發單,適用于TBV5.2.3.16之前的版本(V5.2.3.16版本開始忽略自動交易也會忽略A函數發單)
    19. Vars
    20.         NumericSeries AvgValue1;
    21.         NumericSeries AvgValue2;
    22.        
    23.         Numeric myBuyPosition;
    24.         Numeric myTodayBuyPosition;
    25.         Numeric mySellPosition;
    26.         Numeric myTodaySellPosition;
    27.         Numeric myLots;
    28.         Numeric MinPoint;
    29. Begin
    30.      MinPoint = MinMove*PriceScale;
    31.      If (A_AccountID<>"" && BarStatus==2)
    32.         {
    33.             myBuyPosition=Data[DataN].A_BuyPosition;
    34.                 myTodayBuyPosition=Data[DataN].A_TodayBuyPosition;
    35.                 mySellPosition=Data[DataN].A_SellPosition;
    36.                 myTodaySellPosition=Data[DataN].A_TodaySellPosition;
    37.         }
    38.         Commentary("myBuyPosition="+Text(myBuyPosition));
    39.         Commentary("myTodayBuyPosition="+Text(myTodayBuyPosition));
    40.         Commentary("mySellPosition="+Text(mySellPosition));
    41.         Commentary("myTodaySellPosition="+Text(myTodaySellPosition));
    42.        
    43.         AvgValue1 = AverageFC(Close,FastLength);
    44.         AvgValue2 = AverageFC(Close,SlowLength);

    45.         If(MarketPosition <>1 && AvgValue1[1] > AvgValue2[1])
    46.         {
    47.                 If (AOrder==1 && GetGlobalVar(0)<>1)//全局變量的作用是控制重復發單
    48.                 {
    49.                         If (AorderDataNBuy(DataN,IIF(MarketPosition==-1,2,1)*Lots,iLastPrice,OffSet,MaxLots)) SetGlobalVar(0,1);
    50.                 }
    51.                 Buy(Lots,Open);
    52.         }
    53.        
    54.         If(MarketPosition <>-1 && AvgValue1[1] < AvgValue2[1])
    55.         {
    56.                 If (AOrder==1 && GetGlobalVar(0)<>-1)
    57.                 {
    58.                         If (AOrderDataNSellShort(DataN,IIF(MarketPosition==1,2,1)*Lots,iLastPrice,OffSet,MaxLots)) SetGlobalVar(0,-1);
    59.                 }
    60.                 SellShort(Lots,Open);
    61.         }
    62.        
    63.         If (MarketPosition==1 && C[1]<=EntryPrice*0.99)
    64.         {
    65.             If (AOrder==1 && GetGlobalVar(0)<>2)
    66.             {
    67.               If (AOrderDataNSellShort(DataN,Abs(CurrentContracts),iLastPrice,OffSet,MaxLots)) SetGlobalVar(0,2);
    68.             }
    69.             Sell(0,Open);
    70.         }
    71.         If (MarketPosition==-1 && C[1]>=EntryPrice*1.01)
    72.         {
    73.             If (AOrder==1 && GetGlobalVar(0)<>-2)
    74.             {
    75.                 If (AorderDataNBuy(DataN,Abs(CurrentContracts),iLastPrice,OffSet,MaxLots)) SetGlobalVar(0,-2);
    76.             }
    77.             BuyToCover(0,Open);
    78.         }
    79.        
    80.         PlotNumeric("MA1",AvgValue1);
    81.         PlotNumeric("MA2",AvgValue2);               
    82. End
    復制代碼

     

  • 網友回復: 本帖最后由 hyjok 于 2015-11-16 14:52 編輯

    尾盤鎖倉代碼
    1. //------------------------------------------------------------------------
    2. // 簡稱: Amaxlots
    3. // 名稱: A函數鎖倉下單
    4. // 類別: 公式應用
    5. // 類型: 用戶應用
    6. // 輸出:
    7. //------------------------------------------------------------------------
    8. //應用場景,尾盤鎖倉下單,特別注意時間設置,確保其他策略在ActionTime之后不會再交易,Tick周期圖表運行
    9. Params
    10.     Numeric ActionTime(1514.02);
    11.         Numeric AOrder(1);
    12.         Numeric OffSet(2);//委托偏移跳數
    13.         Numeric MaxLots(2);//對鎖持倉最大手數
    14.         Numeric ABspreadTick(5);//買賣價差跳數
    15. Vars
    16.         Numeric myBuyPosition;
    17.         Numeric myTodayBuyPosition;
    18.         Numeric mySellPosition;
    19.         Numeric myTodaySellPosition;
    20.         Numeric myLots;
    21.         Numeric MinPoint;
    22.         Numeric wthd;//委托滑點
    23.         Numeric bcLots;//補充對鎖手數
    24.        
    25.         Numeric i;
    26.         Numeric todayEntryLots;//今開手數
    27.     Numeric nCount;
    28. Begin
    29.     Commentary("10000*Time="+Text(10000*Time));
    30.     If (BarStatus<2) Return;
    31.         If (A_AccountID=="") Return;
    32.     MinPoint = MinMove*PriceScale;
    33.         wthd = MinPoint*OffSet;
    34.        
    35.     If (A_AccountID<>"")
    36.         {
    37.             myBuyPosition=A_BuyPosition;
    38.                 myTodayBuyPosition=A_TodayBuyPosition;
    39.                 mySellPosition=A_SellPosition;
    40.                 myTodaySellPosition=A_TodaySellPosition;
    41.         }
    42.         Commentary("myBuyPosition="+Text(myBuyPosition));
    43.         Commentary("myTodayBuyPosition="+Text(myTodayBuyPosition));
    44.         Commentary("mySellPosition="+Text(mySellPosition));
    45.         Commentary("myTodaySellPosition="+Text(myTodaySellPosition));
    46.         If (Date<>Date[1]) SetGlobalVar(2,0);
    47.         If (AOrder==1 && 10000*Time>ActionTime)
    48.         {
    49.             If (GetGlobalVar(2)==InvalidNumeric || GetGlobalVar(2)==0)
    50.                 {
    51.                     todayEntryLots = 0;
    52.                     nCount = A_GetOrderCount();
    53.             For i = 1 To nCount
    54.             {
    55.                                 If (A_OrderStatus(i)==Enum_Filled && A_OrderEntryOrExit(i)==Enum_Entry)
    56.                                 {
    57.                                     todayEntryLots = todayEntryLots + A_OrderLot(i);
    58.                                 }
    59.             }
    60.                     bcLots = MaxLots-Max(myBuyPosition,mySellPosition);
    61.                         If ((todayEntryLots+2*bcLots)>10) bcLots = IntPart((10-todayEntryLots)/2);
    62.                         SetGlobalVar(2,bcLots);
    63.                         Commentary("GetGlobalVar(2)="+Text(GetGlobalVar(2)));
    64.                         Commentary("bcLots="+Text(bcLots));
    65.                 }
    66.                
    67.             If (GetGlobalVar(2)>0 && GetGlobalVar(0)<>1 && A_GetOpenOrderCount()==0 && Q_AskPrice-Q_BidPrice<=ABspreadTick*MinPoint)
    68.                 {
    69.                     If (A_SendOrder(Enum_Sell,Enum_Entry,GetGlobalVar(2),Q_BidPrice-wthd)) SetGlobalVar(0,1);
    70.                         Return;
    71.                 }Else
    72.                 If (GetGlobalVar(2)>0 && GetGlobalVar(1)<>1 && A_GetOpenOrderCount()==0 && Q_AskPrice-Q_BidPrice<=ABspreadTick*MinPoint)
    73.                 {
    74.                     If (A_SendOrder(Enum_Buy,Enum_Entry,GetGlobalVar(2),Q_AskPrice+wthd)) SetGlobalVar(1,1);
    75.                         Return;
    76.                 }
    77.         }
    78.         Commentary("GetGlobalVar(0)="+Text(GetGlobalVar(0)));
    79.         Commentary("GetGlobalVar(1)="+Text(GetGlobalVar(1)));
    80.         Commentary("GetGlobalVar(2)="+Text(GetGlobalVar(2)));
    81. End
    復制代碼

 

有思路,想編寫各種指標公式,程序化交易模型,選股公式,預警公式的朋友

可聯系技術人員 QQ: 511411198  點擊這里給我發消息進行 有償 編寫!不貴!點擊查看價格!


【字體: 】【打印文章】【查看評論

相關文章

    沒有相關內容
主站蜘蛛池模板: 久久一区二区三区免费播放 | 午夜亚洲国产理论秋霞 | 神马手机不卡影院 | 99久久精品国产一区二区成人 | 精品国产免费第一区二区三区日韩 | 偷拍肉窝窝视频在线播放 | 97精品久久天干天天蜜 | 欧美大成色www永久网站 | 毛片.com| 亚洲精品国产成人专区 | 欧美国一级毛片片aa | 九九热欧美 | 亚洲免费黄色网 | 欧美激情日本一道免费视频 | 牛牛a级毛片在线播放 | 国产区视频在线 | 欧美又乱又伦观看 | 99r精品在线| 久久久久久久国产免费看 | 中文字幕久久精品波多野结 | 久久99热久久精品99 | 欧美乱妇高清无乱码视频在线 | 久久久人体 | 这里只有精品视频 | 自拍 欧美 在线 综合 另类 | 久久国产精品久久精品国产 | 久草美女 | 熟妇毛茸茸xxxoo | 久久精品视频1 | 深夜精品影院18以下勿进 | 日本亚洲一区二区 | 日韩在线视频在线 | 日日操夜夜操狠狠操 | 亚洲美女性视频 | 中文字幕在线观看不卡 | 国产伦精品一区二区三区四区 | 久久久久国产成人精品 | 久久e| 色综合天天综合网国产成人 | 97色综合| 久久久在线 |