: | : | :期货程序化 | :期货程序化研究 | :期货量化学习 | :期货量化 |
返回列表 发帖

[TB源码] 较完善的日内交易系统框架

[TB源码] 较完善的日内交易系统框架

本帖隐藏的内容需要回复才可以浏览
  1. //------------------------------------------------------------------------
  2. // 简称: s007
  3. // 名称:
  4. // 类别: 交易指令
  5. // 类型: 其他
  6. // 输出:
  7. //------------------------------------------------------------------------
  8. Params
  9.        Numeric maxLots(1);//单次开仓手数
  10.        Numeric maxTrad(3);//最大交易次数
  11.        Numeric splitRate(3); //交易滑点和佣金      
  12.       
  13.        Numeric ma1(5);
  14.        Numeric ma2(18);
  15.       
  16.        Numeric tradBegin(909); //开仓时间      
  17.        Numeric tradEnd(1440); //开仓时间      
  18.        Numeric closeTime(1456); //bar的时间超过此值后平仓
  19.       
  20.        Numeric stopLoss(40); //亏损大于于此值时止损,为0不判断
  21.        Numeric stopProfis(200); //盈利高于此值时止赢,为0不判断
  22.       
  23.        Numeric tracProfis(0); //盈利高于此值后执行追踪止赢,为0不判断
  24.        Numeric tracLoss(0); //追踪止盈的回撤值,为0不判断,
  25.       
  26.        Numeric returnProfis(70); //盈利高于此值后执行回撤止赢,为0不判断      
  27.             
  28.        Numeric minProfis(10); //持仓bar数超过maxHoles后盈利小于此值平仓,为0不判断
  29.        Numeric maxHolds(7);//为0不判断
  30. Vars
  31.        String fileName;      
  32.        String fileName2;
  33.        String toDay;
  34.        Numeric splitDot;      //交易滑点
  35.       
  36.        Bool b1(False);//开多条件
  37.        Bool b2(False);//开多条件      
  38.       
  39.        Bool s1(False);//开空条件
  40.        Bool s2(False);//开空条件      
  41.       
  42.        Bool bc(False);//开多条件
  43.        Bool sc(False);//开多条件
  44.       
  45.        Numeric tradePrice(0);//发单的价格      
  46.       
  47.       StringSeries  tradMem("");//交易描述      
  48.        Numeric tradProve(0);//可否开仓:0/禁止,1/允许
  49.        Numeric tradNum(0);//交易次数
  50.        Numeric tradState(0);//持仓状态:0/无,1/多,-1/空      
  51.        Numeric tradCost(0);//持仓成本
  52.        Numeric tradIdx(0);//持仓Bar的index
  53.        Numeric tradCyc(0);//持仓周期
  54.        String pKey;//
  55.        String pKeyTradProve("TRADPROVE");//
  56.        String pKeyTradState("TRADSTATE");//
  57.        String pKeyTradCost("TRADCOST");//
  58.        String pKeyTradNum("TRADNUM");//
  59.        String pKeyTradIdx("TRADIDX");//
  60.       
  61.        Numeric curProfit(0);//持仓当前浮动盈亏
  62.        NumericSeries maxProfit(0);//持仓最大浮盈
  63.        NumericSeries maxLoss(0);//持仓最大浮亏
  64.       
  65.       String  dopos("");//持仓处理代码
  66.        NumericSeries m1(0);
  67.        NumericSeries m2(0);
  68.       
  69. Begin
  70.        splitDot=splitRate*MinMove();
  71.       
  72.       pKey=FormulaName()+SymbolName()+Text(BarType())+Text(BarInterval());
  73.        pKeyTradProve="TRADPROVE";//
  74.        pKeyTradState="TRADSTATE";//
  75.        pKeyTradCost="TRADCOST";//
  76.        pKeyTradNum="TRADNUM";//
  77.        pKeyTradIdx="TRADIDX";//      
  78.        //初始化
  79.        If(BarStatus==0)
  80.        {            
  81.              SetTBProfileString(pKey,pKeyTradProve,Text(1));
  82.              SetTBProfileString(pKey,pKeyTradState,Text(0));
  83.              SetTBProfileString(pKey,pKeyTradCost,Text(0));
  84.              SetTBProfileString(pKey,pKeyTradNum,Text(0));
  85.              SetTBProfileString(pKey,pKeyTradIdx,Text(0));

  86.               tradMem="";
  87.               maxProfit=0;
  88.               maxLoss=0;      
  89.               Return;
  90.        }

  91.        if(Day !=Day[1])
  92.        {
  93.              SetTBProfileString(pKey,pKeyTradProve,Text(1));
  94.              SetTBProfileString(pKey,pKeyTradState,Text(0));
  95.              SetTBProfileString(pKey,pKeyTradCost,Text(0));
  96.              SetTBProfileString(pKey,pKeyTradNum,Text(0));
  97.              SetTBProfileString(pKey,pKeyTradIdx,Text(0));
  98.             
  99.               tradMem=" ";            
  100.               maxProfit=0;
  101.               maxLoss=0;            
  102.        }      
  103.        Else
  104.        {
  105.               //获取交易状态
  106.              tradProve=Value(GetTBProfileString(pKey,pKeyTradProve));
  107.              tradState=Value(GetTBProfileString(pKey,pKeyTradState));
  108.              tradCost=Value(GetTBProfileString(pKey,pKeyTradCost));
  109.              tradNum=Value(GetTBProfileString(pKey,pKeyTradNum));
  110.              tradIdx=Value(GetTBProfileString(pKey,pKeyTradIdx));
  111.             
  112.               tradMem=" ";            
  113.               maxProfit=maxProfit[1];
  114.               maxLoss=maxLoss[1];

  115.              m1=iTodayEMA(Open,ma1);//指标
  116.              m2=iTodayEMA(Open,ma2);//指标

  117.               //开仓条件
  118.               //跌停板附近不开多仓,涨停附近不开空仓
  119.               if(BarStatus==2)
  120.               {
  121.                     b1=Close>(Q_LowerLimit()+15*MinMove());
  122.                     s1=Close<(Q_UpperLimit()-15*MinMove()) ;
  123.               }
  124.               Else
  125.               {      
  126.                     b1=Not(High==Low And High==Close Or High[1]==Low[1] );
  127.                     s1=Not(High==Low And High==Close Or High[1]==Low[1] );
  128.               }            
  129.                     
  130.              If(tradNum<=maxTrad And Time>=0.0001*tradBegin And Time<=0.0001*tradEnd)
  131.               {
  132.                     //开仓条件
  133.                     bc=CrossOver(m1,m2) And b1 ;//
  134.                     sc=CrossUnder(m1,m2) And b1 ;//                                       
  135.               }            
  136.       
  137.               // 当前无仓-----------------------------------------------------------------Begin
  138.               if(tradState==0 )
  139.               {            
  140.                      // 当前无仓,开始建立多头
  141.                     if(bc)
  142.                      {
  143.                            if(BarStatus==2)      tradePrice= Q_AskPrice +splitDot; Else tradePrice=Open+splitDot;                          
  144.                            If(Buy(maxLots,tradePrice))
  145.                            {
  146.                                  SetTBProfileString(pKey,pKeyTradProve,Text(0));
  147.                                  SetTBProfileString(pKey,pKeyTradState,Text(1));
  148.                                  SetTBProfileString(pKey,pKeyTradCost,Text(tradePrice));
  149.                                  SetTBProfileString(pKey,pKeyTradNum,Text(1+tradNum));
  150.                                  SetTBProfileString(pKey,pKeyTradIdx,Text(CurrentBar()));            
  151.                                  tradMem="开多-"+Text(tradePrice);
  152.                                  Commentary(tradMem);                                                                  
  153.                            }
  154.                      }
  155.                      Else
  156.                      // 当前无仓,开始建立空头
  157.                     If(sc)
  158.                      {
  159.                            if(BarStatus==2)tradePrice= Q_BidPrice -splitDot; Else tradePrice=Open-splitDot;                          
  160.                            If(SellShort(maxLots,tradePrice))
  161.                            {
  162.                                  SetTBProfileString(pKey,pKeyTradProve,Text(0));
  163.                                  SetTBProfileString(pKey,pKeyTradState,Text(-1));
  164.                                  SetTBProfileString(pKey,pKeyTradCost,Text(tradePrice));
  165.                                  SetTBProfileString(pKey,pKeyTradNum,Text(1+tradNum));
  166.                                  SetTBProfileString(pKey,pKeyTradIdx,Text(CurrentBar()));                                                   
  167.                                  tradMem="开空-"+Text(tradePrice);
  168.                                  Commentary(tradMem);                                 
  169.                            }
  170.                      }
  171.               }
  172.               // 当前无仓-----------------------------------------------------------------end
  173.               // 当前有仓-----------------------------------------------------------------begin
  174.               Else
  175.               {
  176.                     //计算当前盈亏和最大浮动盈亏
  177.                     curProfit=tradState*(Close-tradCost);                  
  178.                     If(BarStatus==2)
  179.                     {                          
  180.                           If(curProfit>maxProfit)      maxProfit=curProfit;
  181.                           If(curProfit<maxLoss)             maxLoss=curProfit;
  182.                      }
  183.                      Else
  184.                      {
  185.                            If(tradState==1)
  186.                            {
  187.                                  If((High-tradCost)>maxProfit) maxProfit=(High-tradCost);
  188.                                  If((Low-tradCost)<maxLoss)             maxLoss=(Low-tradCost);
  189.                            }
  190.                            If(tradState==-1)
  191.                            {
  192.                                  If((tradCost-Low)>maxProfit)      maxProfit=tradCost-Low;
  193.                                  If((tradCost-High)<maxLoss)      maxLoss=tradCost-High;      
  194.                            }
  195.                     }                    
  196.                     
  197.                     //平多反空
  198.                     If(tradState==1 And sc And tradNum<maxTrad And Time>=0.0001*tradBegin And Time<=0.0001*tradEnd)
  199.                      {
  200.                            if(BarStatus==2)tradePrice= Q_BidPrice -splitDot; Else tradePrice=Open-splitDot;      
  201.                            If(SellShort(maxLots,tradePrice))
  202.                            {
  203.                                  tradMem="平多反空-"+Text(tradePrice);Commentary(tradMem);
  204.                                  SetTBProfileString(pKey,pKeyTradProve,Text(1));
  205.                                  SetTBProfileString(pKey,pKeyTradState,Text(-1));
  206.                                  SetTBProfileString(pKey,pKeyTradCost,Text(tradePrice));
  207.                                  SetTBProfileString(pKey,pKeyTradNum,Text(1+tradNum));
  208.                                  SetTBProfileString(pKey,pKeyTradIdx,Text(CurrentBar()));
  209.                                   maxProfit=0;
  210.                                   maxLoss=0;      
  211.                                   curProfit=0;
  212.                            }
  213.                      }
  214.                     //平空反多
  215.                     If(tradState==-1 And bc And tradNum<maxTrad And Time>=0.0001*tradBegin And Time<=0.0001*tradEnd)
  216.                      {
  217.                            if(BarStatus==2)      tradePrice= Q_AskPrice +splitDot; Else tradePrice=Open+splitDot;      
  218.                            If(Buy(maxLots,tradePrice))
  219.                            {
  220.                                  tradMem="平空反多-"+Text(tradePrice);Commentary(tradMem);
  221.                                  SetTBProfileString(pKey,pKeyTradProve,Text(1));
  222.                                  SetTBProfileString(pKey,pKeyTradState,Text(1));
  223.                                  SetTBProfileString(pKey,pKeyTradCost,Text(tradePrice));
  224.                                  SetTBProfileString(pKey,pKeyTradNum,Text(1+tradNum));
  225.                                  SetTBProfileString(pKey,pKeyTradIdx,Text(CurrentBar()));
  226.                                   maxProfit=0;
  227.                                   maxLoss=0;
  228.                                  curProfit=0;                                 
  229.                            }
  230.                     }      
  231.                     
  232.                     tradCyc=(CurrentBar()-tradIdx);
  233.                     tradMem="浮盈:"+Text(curProfit)+",最大浮盈:"+Text(maxProfit)+",仓期:"+Text(tradCyc);Commentary(tradMem);
  234.                     dopos="";
  235.                     //开仓BAR的处理
  236.                     if(tradCyc==0)
  237.                      {
  238.                     
  239.                     }                    
  240.                     //持仓BAR的处理
  241.                      Else
  242.                     if(tradCyc>0)
  243.                      {
  244.                            //开仓后第一根BAR的处理-应对bar走完后的信号消失问题**********************************************                          
  245.                            if(tradCyc==1)
  246.                            {
  247.                                 。。。。。
  248.                            }            
  249.                            Else                          
  250.                           dopos=DoPosition(tradState,tradCyc,curProfit,maxProfit,stopLoss,stopProfis,tracProfis,tracLoss,returnProfis,minProfis,maxHolds,closeTime);
  251.                      }
  252.                     
  253.                     dopos=DoPosition(tradState,tradCyc,curProfit,maxProfit,stopLoss,stopProfis,tracProfis,tracLoss,returnProfis,minProfis,maxHolds,closeTime);
  254.                     //统一的平仓处理-------------------------------------------------------------------------------------------
  255.                     if(Len(dopos)>2)
  256.                      {
  257.                            //处理交易价格,叫卖叫买价加上滑点,便于成交
  258.                            if(BarStatus==2)
  259.                            {
  260.                                   If(tradState==1)        tradePrice= Q_BidPrice -splitDot;
  261.                                   If(tradState==-1)        tradePrice= Q_AskPrice +splitDot;
  262.                            }Else tradePrice=Close-tradState*splitDot;
  263.                           
  264.                            //平多
  265.                            If(tradState==1)
  266.                            {
  267.                                  If(Sell(maxLots,tradePrice))
  268.                                   {
  269.                                         tradMem=dopos+":平多-"+Text(tradePrice);
  270.                                         SetTBProfileString(pKey,pKeyTradProve,Text(1));
  271.                                         SetTBProfileString(pKey,pKeyTradState,Text(0));                                       
  272.                                         maxProfit=0;
  273.                                         maxLoss=0;
  274.                                   }
  275.                            }
  276.                            //平空
  277.                            If(tradState==-1)
  278.                            {
  279.                                  If(BuyToCover(maxLots,tradePrice))
  280.                                   {
  281.                                         tradMem=dopos+":平空-"+Text(tradePrice);
  282.                                         SetTBProfileString(pKey,pKeyTradProve,Text(1));
  283.                                         SetTBProfileString(pKey,pKeyTradState,Text(0));                                       
  284.                                         maxProfit=0;
  285.                                         maxLoss=0;                                       
  286.                                   }
  287.                            }            
  288.                            Commentary(tradMem);                  
  289.                     }                    
  290.               }                                
  291.        }
  292. End
复制代码

论坛官方微信、群(期货热点、量化探讨、开户与绑定实盘)
 
期货论坛 - 版权/免责声明   1.本站发布源码(包括函数、指标、策略等)均属开放源码,用意在于让使用者学习程序化语法撰写,使用者可以任意修改语法內容并调整参数。仅限用于个人学习使用,请勿转载、滥用,严禁私自连接实盘账户交易
  2.本站发布资讯(包括文章、视频、历史记录、教材、评论、资讯、交易方案等)均系转载自网络主流媒体,内容仅为作者当日个人观点,本网转载此文出于传递更多信息之目的,并不意味着赞同其观点或证实其描述。本网不对该类信息或数据做任何保证。不对您构成任何投资建议,不能依靠信息而取代自身独立判断,不对因使用本篇文章所诉信息或观点等导致的损失承担任何责任。
  3.本站发布资源(包括书籍、杂志、文档、软件等)均从互联网搜索而来,仅供个人免费交流学习,不可用作商业用途,本站不对显示的内容承担任何责任。请在下载后24小时内删除。如果喜欢,请购买正版,谢谢合作!
  4.龙听期货论坛原创文章属本网版权作品,转载须注明来源“龙听期货论坛”,违者本网将保留追究其相关法律责任的权力。本论坛除发布原创文章外,亦致力于优秀财经文章的交流分享,部分文章推送时若未能及时与原作者取得联系并涉及版权问题时,请及时联系删除。联系方式:http://www.qhlt.cn/thread-262-1-1.html
如何访问权限为100/255贴子:/thread-37840-1-1.html;注册后仍无法回复:/thread-23-1-1.html;微信/QQ群:/thread-262-1-1.html;网盘链接失效解决办法:/thread-93307-1-1.html

谢谢
[img][/img]

TOP

学习了

TOP

隐藏了啥内容?

TOP

这么长的代码

TOP

不错,研究,学习,思考

TOP

为啥不能用啊

TOP

学习
量化小菜

TOP

返回列表