全部 智大领峰 TBQuant功能 TBL语言 TB开户 问答专区 高手圈 其他
那位老师帮帮忙 合并修改
2022-11-19 16:28

杨刚:
第一个测试策略
Params
    Numeric Length1(60);
    Numeric ATR_times(3);
    Numeric ATR_period(14);
 
Vars
    Numeric ema;
    Series<Numeric> ATRup;
    Series<Numeric> ATRdown;
 
Events
    OnBar(ArrayRef<Integer> indexs)
    {
        Range[0:DataSourceSize() - 1]
        {
            ema = XAverage(Close, Length1);
            PlotNumeric("EMA1", ema);
             
            ATRup = ema + 3 * AvgTrueRange(ATR_period);
            ATRdown = ema - 3 * AvgTrueRange(ATR_period);
            PlotNumeric("ATRup",     ATRup);
            PlotNumeric("ATRdown",   ATRdown);
             
            if ( Close[1] > ATRup[1] && MarketPosition <>1 )
            {
                PlotBool("buy", true);
                Buy(0,Open);
            }
            if ( Close[1] < ATRdown[1] && MarketPosition <>-1 )
            {
                PlotBool("sell", False);
                SellShort(0,Open);
            }
 
        }
    }

 

 

第二个

延迟反手策略
Params
    Numeric FastLength(5);
    Numeric SlowLength(120);
    Numeric DelayTicks(5);
Vars    
    Series<Numeric> AvgValue1;
    Series<Numeric> AvgValue2;
    Numeric LastBarTime;
    Numeric TickCounter;
    Numeric dataIndex;
Events
OnBar(ArrayRef<Integer> indexs)
{    
    AvgValue1 = AverageFC(Close,FastLength);
    AvgValue2 = AverageFC(Close,SlowLength);
    LastBarTime = GetGlobalVar(0);    
    TickCounter = GetGlobalVar(1);

    // 最新Bar第一次生成时,Tick重新开始计数 
    If(BarStatus == 2 && gValue[0]!= Time)    
    {    
        LastBarTime = Time; 
        TickCounter = 0;
    }
    If(MarketPosition <> 1 && AvgValue1[1] > AvgValue2[1])
    {        
        If(MarketPosition == 0 || BarStatus != 2)    
        // 无持仓,直接买多仓
        // 持空仓且Bar不是实时行情,平空仓,买多仓
        {
            Buy(1,Open);
        }Else    // 持空仓,Bar实时行情,平空仓,通过TickCounter计数,延迟反手
        {    
            BuyToCover(1,Open);        
            If(TickCounter == 0)
            {    
               TickCounter = 1;
            }Else If(TickCounter < DelayTicks)
            {
               TickCounter = TickCounter + 1;
            }Else            
            {
                Buy(1,Open);        
            }
        }
    }
    
    If(MarketPosition <> -1 && AvgValue1[1] < AvgValue2[1])    
    {
        If(MarketPosition == 0 || BarStatus != 2)    
        {    
            SellShort(1,Open);
        }Else     // 持多仓且Bar为实时行情,平多,延迟反手
        {
            Sell(1,Open);
            If(TickCounter == 0)            
            {
                TickCounter = 1;
            }Else If(TickCounter < DelayTicks)
            {
                TickCounter = TickCounter + 1;
            }Else
            {
                SellShort(1,Open);
            }
        }
    }
    SetGlobalVar(0,LastBarTime); 
    SetGlobalVar(1,TickCounter);
}
把第二个延迟反手加到第一个里面只要延迟部分谢谢老师

Bryan2020

费这劲干嘛。

 

1、在本网站上方的“帮助中心”里面搜索“延迟”,会看到一个“信号延迟代理案例”,里面有完整的信号延迟代码,把代码复制到TB里面,一个字符都不用改,直接编译为一个公式。

2、把这个公式化和你的策略公式一起加载到策略单元上,按自己需求设置好参数。

 

即可完美实现延时反手。

2022-11-19 18:45
yang081112
@Bryan2020

老师我试了不行、、要么老师帮忙改改谢谢

 

2022-11-19 21:39
Bryan2020
@yang081112

我特别好奇,现在周末没有行情的情况下,你是怎么试出它不行的?

 

我可以非常明确的告诉你,那个延迟发单代理的代码,非常行,没有比它更行的了

 

当你的策略发出反手信号后,你让他延迟几秒开仓,它就延迟几秒开仓。

 

不需要任何修改,也不用和你的策略代码融合。直接和你的策略一起加载到图表或者策略单元上即可。

 

哪里还有比这更好的?

2022-11-19 22:24
yang081112
@Bryan2020

我弄的是个啥吗不忍直视

老师帮帮忙谢谢

Params
   Numeric Length1(60);
    Numeric ATR_times(3);
    Numeric ATR_period(14);
    Enum<String> type(["开仓","平仓","全部"]);//延迟委托类别
    Integer  delaySecs(10);//延迟秒数
Vars
    Numeric ema;
    Series<Numeric> ATRup;
    Series<Numeric> ATRdown;
    Global Array<Signal> delaySigs;//延迟信号缓存
    Global Integer i(0);
    Global Integer j(0);
    Global Integer k(0);
    Array<Integer> tmpIds;
    Array<String>  tmpMSyms; 
    Numeric ordPrice;
    Bool needDelay;
    CodeProperty tmpPty;
    Tick tmpTick;

Events
       OnBar(ArrayRef<Integer> indexs)
    {
        Range[0:DataSourceSize() - 1]
        {
            ema = XAverage(Close, Length1);
            PlotNumeric("EMA1", ema);
             
            ATRup = ema + 3 * AvgTrueRange(ATR_period);
            ATRdown = ema - 3 * AvgTrueRange(ATR_period);
            PlotNumeric("ATRup",     ATRup);
            PlotNumeric("ATRdown",   ATRdown);
             if ( Close[1] > ATRup[1] && MarketPosition <>1 )
            {
                PlotBool("buy", true);
                Buy(0,Open);
            }
            if ( Close[1] < ATRdown[1] && MarketPosition <>-1 )
            {
                PlotBool("sell", False);
                SellShort(0,Open);
            if(DateTimeDiff(delaySigs[i].datetime,SystemDateTime)>=delaySecs)
            {
                
                ArrayInsert(tmpIds,0,i);
            }
        }

        //删除已经委托的信号
        For i=0 To GetArraySize(tmpIds)-1
        {
            ArrayErase(delaySigs,tmpIds[i],1);
        }
    }

    //代理接收同策略单元的信号,包括历史、实时信号
   
        //1.有历史、忽略、换仓信号标识的不处理,只委托实时信号
        
            
         
               
            }

            //2.判断是否需要延迟
           
                        

            

            //3.延迟模式
         
        
              
            
          
            
           
               
        
     
 

2022-11-20 21:34
Bryan2020
@yang081112

不知道你在干什么。我说的那么多,你到底看懂没有?

我再给你说一遍,请你严格按照我说的去做,如果这样你还是做不成,那么非常抱歉,我绝对没办法帮助到你了。

1、在TBQ的公式管理器里面,新建一个公式,名字你随便取,比如取名叫"SignalDelay".把里面默认的所有内容全部删除。留下一个空白页面。

2、在这个网站上方的“帮助中心”里面,搜索“延迟”,看到“信号延迟代理案例”这个页面,在“3、实现”这个段落里面,有一个完整的代码,把里面所有的代码全部复制。

3、在TBQ的公式管理器里面刚才新建好的那个空白的公式里面,粘贴。

4、编译这个公式(一个字符都不要改,直接编译)。

 

就这样,完成了!!!!!!

接下来是怎么使用:

1、正常的把你的策略公式加载到图表上或者策略单元上。随便什么策略,随便什么公式。

2、把刚才新建并编译好的那个SignalDelay公式【也】加载到这个图表或者策略单元上。现在,你的图表上,有两个公式。

就这样,你的策略立刻拥有了延迟反手的功能。

你一句代码都不用写,就可以让你的任何一个公式拥有延迟开仓的功能。

不要总想着改编改编改编。。。。。。

2022-11-20 22:49
yang081112
@Bryan2020

非常感谢老师

2022-11-21 00:13
shenjinjin
@Bryan2020

现在怎么找不到“信号延迟代理案例”?

2024-03-29 16:18
yang081112

Params

   //此处添加参数

   Enum<String> type([\"开仓\",\"平仓\",\"全部\"]);//延迟委托类别

   Integer  delaySecs(10);//延迟秒数

Vars

   //此处添加变量

   Global Array<Signal> delaySigs;//延迟信号缓存

   Global Integer i(0);

   Global Integer j(0);

   Global Integer k(0);

   Array<Integer> tmpIds;

   Array<String>  tmpMSyms;

   Numeric ordPrice;

   Bool needDelay;

   CodeProperty tmpPty;

   Tick tmpTick;

Defs

   //此处添加公式函数

   Integer Print2out(StringRef msg)

   {

       FileAppend(\"./log/\"+FormulaName()+\"/\"+GetWorkspaceName()+\"_\"+GetUnitName()+\".tbf\",

               DateTimeToString(SystemDateTime)+\",\"+msg);

       Return 0;

   }

   Numeric getOrderPrice(String oSymbol,Numeric oPrice,Integer oSide,Integer oPrcOffset)

   {

       //没有映射偏移时,取信号价格

       If(oPrcOffset==InvalidInteger)

       {

           return oPrice;

       }

       If(GetProperty(oSymbol,tmpPty)==False||GetTick(oSymbol,tmpTick)==False)

       {

           AddStrategyFlag(Enum_Strategy_Error,\"获取合约属性或Tick失败\"+oSymbol);

           Stop();

           Return 0;

       }        


       //有映射偏移时,取合约对手价+偏移        

       If(oSide==Enum_Buy)

       {

           Return tmpTick.bidask1.askP+oPrcOffset*tmpPty.priceScale*tmpPty.minMove;

       }

       Return tmpTick.bidask1.bidP-oPrcOffset*tmpPty.priceScale*tmpPty.minMove;

   }


   //发送信号

   Bool SendSignal(SignalRef ss)

   {

       For j=0 To A_AccountCount-1

       {    

           ArrayClear(tmpIds);

           //判断是否有映射合约

           if(GetLayerOrderMapSymbols(tmpMSyms))

           {

               For k=0 To GetArraySize(tmpMSyms)-1

               {

                   ordPrice=getOrderPrice(tmpMSyms[k],ss.price,ss.side,GetOrderPriceOffset());  

                   A_SendOrderEx(tmpMSyms[k],ss.side,ss.combOffset,ss.volume,ordPrice,tmpIds,\"\",ss.createSource,j);                                        

                   Print2out(\"委托发送,\"+tmpMSyms[k]+\",\"+IIFString(ss.side==Enum_Buy,\"买\",\"卖\")+

                       IIFString(ss.combOffset==Enum_Entry,\"开\",\"平\")+\"\"+Text(ss.volume)+\"@\"+Text(ordPrice)+\",\"+ ss.createSource);  

               }

           }

           Else

           {

               ordPrice=getOrderPrice(Symbol,ss.price,ss.side,GetOrderPriceOffset());  

               A_SendOrderEx(ss.side,ss.combOffset,ss.volume,ordPrice,tmpIds,\"\",ss.createSource,j);

               Print2out(\"委托发送,\"+Symbol+\",\"+IIFString(ss.side==Enum_Buy,\"买\",\"卖\")+

                       IIFString(ss.combOffset==Enum_Entry,\"开\",\"平\")+\"\"+Text(ss.volume)+\"@\"+Text(ordPrice)+\",\"+ ss.createSource);  

           }

       }

       return true;

   }

Events

   //Bar更新事件函数,参数indexs表示变化的数据源图层ID数组

   OnBar(ArrayRef<Integer> indexs)

   {

       ArrayClear(tmpIds);

       For i=0 To GetArraySize(delaySigs)-1

       {

           //判断延长时间是否满足

           if(DateTimeDiff(delaySigs[i].datetime,SystemDateTime)>=delaySecs)

           {

               data[delaySigs[i].index].SendSignal(delaySigs[i]);

               ArrayInsert(tmpIds,0,i);

           }

       }


       //删除已经委托的信号

       For i=0 To GetArraySize(tmpIds)-1

       {

           ArrayErase(delaySigs,tmpIds[i],1);

       }

   }


   //代理接收同策略单元的信号,包括历史、实时信号

   OnSignal(ArrayRef<Signal> sigs)

   {

       //1.有历史、忽略、换仓信号标识的不处理,只委托实时信号

       For i=0 To GetArraySize(sigs)-1

       {

           Signal sig=sigs[i];

           If(BitHas(sig.flag,Enum_Signal_NotSend)  or BitHas(sig.flag,Enum_Signal_History) or BitHas(sig.flag,Enum_Signal_SwapPosition))

           {

               Continue;

           }


           //2.判断是否需要延迟

           needDelay = delaySecs>0 and (type==\"全部\" or

                       (type==\"开仓\" and sig.combOffset==Enum_Entry) or

                       (type==\"平仓\" and sig.combOffset<>Enum_Entry));


           Print2out(\"收到信号,\"+data[sig.index].Symbol+\",\"+IIFString(sig.side==Enum_Buy,\"买\",\"卖\")+

                   IIFString(sig.combOffset==Enum_Entry,\"开\",\"平\")+\"\"+Text(sig.volume)+\",\"+ sig.createSource+\",\" +

                   IIFString(needDelay,\"延迟\"+Text(delaySecs)+\"S\",\"立刻发出\"));


           //3.延迟模式

           if(needDelay)

           {

               sig.dateTime=SystemDateTime();//借用该字段存放收到时间(自身存的是bar时间,不是实时时间),方便计算延迟时间,

               delaySigs[GetArraySize(delaySigs)]=sig;

           }

           Else

           {

               data[sig.index].SendSignal(sig);

           }

       }

   }

2023-08-21 00:08
yang081112

Params

Numeric AvgLen(1);

Numeric Disp(268);

Numeric SDLen(268);

Numeric SDev(1);

Numeric AddSet(30); // 加仓设置

Numeric SubSet(30); // 减仓设置

Numeric StopLossSet(100);//止损

Numeric Lots(1); //手数  

Numeric zhiying(30);

Vars

Numeric MinPoint;//最小变动单位

Numeric MyEntryPrice; //我的最后1次建仓价格

Numeric MyExitPrice; //平仓价格

Series<Numeric> HighestAfterEntry; //最高点

Series<Numeric> LowestAfterEntry; //最低点

Numeric firstPrice ; // 第一次开仓价格

Series<Numeric> LastPrice; // 最后一次开仓价格

//Numeric Price;

Series<Numeric> AvgVal(0);

Series<Numeric> SDmult(0) ;

Series<Numeric> DispTop(0);

Series<Numeric> DispBottom(0);



Events


OnBarClose(ArrayRef<Integer> indexs)


{



MinPoint = MinMove*PriceScale;


firstPrice = Close;


AvgVal = Average(firstPrice ,AvgLen);


SDmult = StandardDev(firstPrice ,SDLen,2)*SDev;


DispTop = AvgVal[Disp] + SDmult;


DispBottom = AvgVal[Disp] - SDmult;


//PlotNumeric(\"DispTop\",DispTop);



//PlotNumeric(\"DispBottom\",DispBottom);



If(MarketPosition <>1 &&   High>=DispTop[1]+MinPoint )

{

MyExitPrice = MyEntryPrice -StopLossSet*Minpoint;


firstPrice = Max((DispTop[1]+MinPoint),open);// 第一次开仓价格firstPrice

Buy(Lots,Open);


}






If(MarketPosition <>-1 && Low<=DispBottom[1]-MinPoint )

{

firstPrice = Min((DispBottom[1]-MinPoint),open);

lastPrice = firstPrice;


             

SellShort(Lots,Open);

}


MinPoint = MinMove*PriceScale;

   

   MyEntryPrice = AvgEntryPrice;

   

   if( MarketPosition ==1 And Low <= MyEntryPrice - StopLossSet*MinPoint)//可以在这里写上初始的止损处理//

   

   {

   

   MyExitPrice = MyEntryPrice - StopLossSet*MinPoint;

  //Sell(Lots,Open);

   

   }

   If( MarketPosition == -1 And High >= MyEntryPrice + StopLossSet*MinPoint)//可以在这里写上初始的止损处理//

   

   {

   

   MyExitPrice = MyEntryPrice + StopLossSet*MinPoint;

   

   

   //BuyToCover(Lots,Open);

   

   }


If(h-AvgEntryPrice>=zhiying and MarketPosition==1 and abs(CurrentContracts)==2)

{

sell(1,max(o,AvgEntryPrice+zhiying));

Commentary(\"止盈\");

}

If(l<=AvgEntryPrice-zhiying and MarketPosition==-1 and abs(CurrentContracts)==2)

{

BuyToCover(1,Min(o,AvgEntryPrice-zhiying));

Commentary(\"止盈\");

}




}

2023-08-24 03:02
yang081112

Params

//此处添加参数

Numeric N(280);

Numeric M(5);

Numeric Fund(2000);

Numeric AddSet(30);         // 加仓设置

  Numeric SubSet(30);      // 减仓设置

  Numeric  zhiying(30);  //止盈

numeric Lots(2);//开仓数//此参数是交易数量,不用优化

Vars

numeric MinPoint;//最小变动单位  

 Numeric MyEntryPrice;  //我的最后1次建仓价格    

 Numeric MyExitPrice; //平仓价格

 Series<Numeric> HighestAfterEntry; //最高点      

 Series<Numeric> LowestAfterEntry; //最低点  

 Numeric firstPrice ;   // 第一次开仓价格

 Series<Numeric> lastPrice;    // 最后一次开仓价格

   Numeric lookBackDays(10);        

   Numeric todayVolatility(0);

   Numeric yesterDayVolatility(0);

   Numeric deltaVolatility(0);

   Series<Numeric> buyPoint(0);

   Series<Numeric> sellPoint(0);

   Series<Numeric> longLiqPoint(0);

   Series<Numeric> shortLiqPoint(0);

   Numeric upBand(0);

   Numeric dnBand(0);

   Numeric MidLine(0);

 



//此处添加变量

//Numeric Lots( 5 );

Series<Numeric> C_O( 0 );

Series<Numeric> Band( 0 );

Series<Numeric> Price_BPK( 0 );

Series<Numeric> Price_SPK( 0 );

Series<Numeric> Price_BP( 0 );

Series<Numeric> Price_SP( 0 );

Series<Bool> B( False );

Series<Bool> S( False );

Series<Bool> BuyPK(false);

Series<Bool> SellPK(false);

Series<Bool> BuyS(false);

Series<Bool> SellS(false);  

Series<Bool> BuyP(false);

Series<Bool> SellP(false);

Events

   onBar(ArrayRef<Integer> indexs)

   {    

    //此处添加代码正文

    //If(!CallAuctionFilter()) Return;

   

    //Lots=max(1,intpart(Fund/(O*ContractUnit*BigPointValue*0.1)));

    C_O=XAverage(C,N)-XAverage(O,N);

    B=CrossOver(C_O,0);

    S=CrossUnder(C_O,0);

    Band=AvgTrueRange(N)*0.4*M;

 

    If (B)

    {

    Price_BPK=H+Band;

    Price_SP=L-Band;

    }

    If (S)

    {

    Price_SPK=L-Band;

    Price_BP=H+Band;

    }

   

    BuyPK=C_O>0  AND C>=Price_BPK;

    SellPK=C_O<0  AND C<=Price_SPK;

   

    SellP=S;

    BuyP=B;

   

    SellS=C<=Price_SP+firstPrice;

    BuyS=C>=Price_BP+firstPrice;

   

    //If (MarketPosition<=0 and CurrentBar>N and BuyPK[1] )

    If (MarketPosition<=0 and CurrentBar>N and BuyPK[1] )

    {

   


   

   

    Buy(Lots, firstPrice);

    //Buy(0,O);

    Commentary(\"BPK\");

    }

    Commentary(\"多头触发价:\"+Text(buyPoint));

   

   

    If (MarketPosition>=0 and CurrentBar>N and SellPK[1])

    {

   

    SellShort(Lots,firstPrice);

    //SellShort(0,O);

    //Commentary(\"SPK\");

    }

   

   

   

   

   If(h-AvgEntryPrice>=zhiying and MarketPosition==1 and abs(CurrentContracts)==2)

{

   sell(1,max(o,AvgEntryPrice+zhiying));

Commentary(\"止盈\");

}




If(l<=AvgEntryPrice-zhiying and MarketPosition==-1 and abs(CurrentContracts)==2)

{

   BuyToCover(1,Min(o,AvgEntryPrice-zhiying));

Commentary(\"止盈\");

}


}


   


   





2023-08-28 22:11
yang081112

Params

//此处添加参数

Numeric Length1(20);

Numeric s1(2);

Numeric s2(2);

Numeric lots(1);

Numeric stoploss(5);

Numeric  zhiying(30);

Vars

//此处添加变量

Series<Numeric>ma1;

Bool Buyentry(false);

   Bool sellentry(false);

   Bool buyexit(false);

   Bool sellexit(false);

   Series<Numeric>Highe;

   Series<Numeric> HighAfterEntry;        // 开仓后出现的最高价

   Series<Numeric> LowAfterEntry;         // 开仓后出现的最低价


Events

//此处实现事件函数

//初始化事件函数,策略运行期间,首先运行且只有一次,应用在订阅数据等操作

onBar(ArrayRef<Integer> indexs)

{

MA1=AverageFC(Close,Length1);

PlotNumeric(\"ma1\",ma1);

BuyEntry =CountIf(Close[1]>ma1[1],s1)==s1;

sellentry =CountIf(Close[1]<ma1[1],s1)==s1;

sellexit =CountIf(Close[1]>ma1[1],s2)==s2;

buyexit=CountIf(Close[1]<ma1[1],s2)==s2;

If(MarketPosition!=1 && BuyEntry)

     {

       Buy(lots,Open);

       Commentary(\"开多\");  

       

      }  

      Else   If(MarketPosition!=-1 && sellentry)

      {

        SellShort(lots,Open);

       Commentary(\"开空\");  

     

       }

     Else   If(MarketPosition==-1 && sellexit)

      {


       BuyToCover(0,Open);

       Commentary(\"平空\");  

       //PlotNumeric(\"mark\",\"平空\",high,Yellow);

       }

      Else   If(MarketPosition==1 && buyexit)

      {


      sell(0,Open);

       Commentary(\"平多\");  

       //PlotNumeric(\"mark\",\"平多\",high,Yellow);

       }

       

   

    If(h-AvgEntryPrice>=zhiying and MarketPosition==1 and abs(CurrentContracts)==2)

{

   sell(1,max(o,AvgEntryPrice+zhiying));

Commentary(\"止盈\");

}

If(l<=AvgEntryPrice-zhiying and MarketPosition==-1 and abs(CurrentContracts)==2)

{

   BuyToCover(1,Min(o,AvgEntryPrice-zhiying));

Commentary(\"止盈\");

}

       }

2023-09-05 23:37
yang081112

Params

//此处添加参数

Numeric N(120);

Numeric M(15);

Numeric Fund(20000);

Numeric Lots(1);                        // 基本下单单位

   Numeric MaxLots(10);                // 最大下单单位

Vars

//此处添加变量

//Numeric Lots( 1 );

Series<Numeric> C_O( 0 );

Series<Numeric> Band( 0 );

Series<Numeric> Price_BPK( 0 );

Series<Numeric> Price_SPK( 0 );

Series<Numeric> Price_BP( 0 );

Series<Numeric> Price_SP( 0 );

Series<Bool> B( False );

Series<Bool> S( False );

Series<Bool> BuyPK(false);

Series<Bool> SellPK(false);

Series<Bool> BuyS(false);

Series<Bool> SellS(false);  

Series<Bool> BuyP(false);

Series<Bool> SellP(false);

Series<Numeric> myLots;                // 每次下单的手数

     

   Series<Numeric> myNetProfit;        // 累计的最大净利润

 

Events

   onBar(ArrayRef<Integer> indexs)

   {    

    //此处添加代码正文

    //If(!CallAuctionFilter()) Return;

   

    //Lots=max(1,intpart(Fund/(O*ContractUnit*BigPointValue*0.1)));

    C_O=XAverage(C,N)-XAverage(O,N);

    B=CrossOver(C_O,0);

    S=CrossUnder(C_O,0);

    Band=AvgTrueRange(N)*1.51*M;

   

    If (B)

    {

    Price_BPK=H+Band;

    Price_SP=L-Band;

    }

    If (S)

    {

    Price_SPK=L-Band;

    Price_BP=H+Band;

    }

   

    BuyPK=C_O>0  AND C>=Price_BPK;

    SellPK=C_O<0 AND C<=Price_SPK;

   

    SellP=S;

    BuyP=B;

   

    SellS=C<=Price_SP;

    BuyS=C>=Price_BP;

    If(TotalTrades == 0)

           {

                   myLots = Lots;

               

                   

                   myNetProfit = 0;

           }

    If (MarketPosition== 1 and BarsSinceEntry>1 and SellPK[1])

    {

    Sell(0,Open);

    //Commentary(\"SS\");

    If(NetProfit >= myNetProfit[1])

                   {

                           myLots = Lots;

                   }Else

                   {

                           myLots = Min((myLots[1] + Lots),MaxLots);

                   }

    }

    If  (MarketPosition== -1 and BarsSinceEntry>1 and BuyPK[1])

    {

    BuyToCover(0,Open);

    //Commentary(\"BS\");

    If(NetProfit >= myNetProfit[1])

                   {

                           myLots = Lots;

                   }Else

                   {

                           myLots = Min((myLots[1] + Lots),MaxLots);

                   }

   

    }

   

    If (MarketPosition!= 1 and CurrentBar>N and BuyPK[1])

    {

    Buy(myLots,Open);

    //Commentary(\"BPK\");

    }

    If (MarketPosition!= -1 and CurrentBar>N and SellPK[1])

    {

    SellShort(myLots,Open);

    //Commentary(\"SPK\");

    }

    myNetProfit = Max(myNetProfit[1],NetProfit);

   

    }

   

   

   

   

 

2023-11-04 23:19
yang081112

Params

Numeric FastLength(2);//声明数值参数FastLength,初值5.//


Numeric SlowLength(200);//声明数值参数SlowLength,初值20.//


Numeric DslowLength(300);//声明数值参数DslowLength,初值200.//


Vars


Series<Numeric> AvgValue1; //声明数值序列变量AvgValue1.//


Series<Numeric> AvgValue2;//声明数值序列变量AvgValue2.//


Series<Numeric> AvgValue3;//声明数值序列变量AvgValue3.//

Events

   onBar(ArrayRef<Integer> indexs)

   {    

   AvgValue1 = AverageFC(Close,FastLength);//求5日均线了。//

   

   AvgValue2 = AverageFC(Close,SlowLength);//求20日均线了。//

   

   AvgValue3 = AverageFC(Close,DslowLength);//求200日均线了。//

   

   PlotNumeric(\"MA1\",AvgValue1);//画5日均线。//

   

   PlotNumeric(\"MA2\",AvgValue2);//画20日均线。//

   

   PlotNumeric(\"MA3\",AvgValue3);//画200日均线。//

   

   //If(!CallAuctionFilter()) Return;// 集合竞价和小节休息过滤。//

   

   If(MarketPosition <>1 && AvgValue1[1] > AvgValue2[1] && High >= AvgValue3[1]) //假如当前没用持多单,且前一5日均线大于前一20日均线,且当前高价大于或等于200日均线。//

   

   {

   

   Buy(1,Max(Open , AvgValue3 ));//开仓买入,这里的细节处理就是把200日均线与当前k线开盘价比较,取较大值。//

   

   }

   

   If(MarketPosition ==1 && AvgValue1[1] < AvgValue2[1]) //假如当前持有多仓,且前一5日均线小于前一20日均线。//

   

   {

   

   Sell(1,Open);//平仓。//

   

   }

   

   If(MarketPosition <>-1 && AvgValue1[1] < AvgValue2[1] && Low <= AvgValue3[1]) //假如当前没有持空单,且前一5日均线小于前一20日均线,且当前低价小于前一200日均线。//

   

   {

   

   SellShort(1,Min(Open , AvgValue3 ));//开仓卖出,细节处理就是把当前k线的开盘价与200日均线值比较,取较小值即可。//

   

   }

   

   If(MarketPosition ==-1 && AvgValue1[1] > AvgValue2[1])//当前持有空单,且前一5日均线大于前一20日均线的。//

   

   {

   

   BuyToCover(1,open);//平仓。//

   

   }

   

   }


2023-11-04 23:53
yang081112

Params

//此处添加参数

Numeric N(920);

Numeric M(5);

Numeric Fund(20000);

Numeric Lots(1);                        // 基本下单单位

   Numeric MaxLots(10);                // 最大下单单位

Numeric  zhiying(30);  //止盈

Numeric  zhiying2(50);  //止盈

Numeric  zhiying3(50);  //止盈

Numeric  zhiying4(50);  //止盈

Numeric  zhiying5(50);  //止盈

Numeric  zhiying6(80);  //止盈

Numeric  zhiying7(120);  //止盈

Numeric  zhiying8(150);  //止盈

Vars

//此处添加变量

//Numeric Lots( 1 );

Series<Numeric> C_O( 0 );

Series<Numeric> Band( 0 );

Series<Numeric> Price_BPK( 0 );

Series<Numeric> Price_SPK( 0 );

Series<Numeric> Price_BP( 0 );

Series<Numeric> Price_SP( 0 );

Series<Bool> B( False );

Series<Bool> S( False );

Series<Bool> BuyPK(false);

Series<Bool> SellPK(false);

Series<Bool> BuyS(false);

Series<Bool> SellS(false);  

Series<Bool> BuyP(false);

Series<Bool> SellP(false);

Series<Numeric> myLots;                // 每次下单的手数

     

   Series<Numeric> myNetProfit;        // 累计的最大净利润

 

Events

   OnBarClose(ArrayRef<Integer> indexs)

   {    

    //此处添加代码正文

    //If(!CallAuctionFilter()) Return;

   

    //Lots=max(1,intpart(Fund/(O*ContractUnit*BigPointValue*0.1)));

    C_O=XAverage(C,N)-XAverage(O,N);

    B=CrossOver(C_O,0);

    S=CrossUnder(C_O,0);

    Band=AvgTrueRange(N)*1.51*M;

   

    If (B)

    {

    Price_BPK=H+Band;

    Price_SP=L-Band;

    }

    If (S)

    {

    Price_SPK=L-Band;

    Price_BP=H+Band;

    }

   

    BuyPK=C_O>0  AND C>=Price_BPK;

    SellPK=C_O<0 AND C<=Price_SPK;

   

    SellP=S;

    BuyP=B;

   

    SellS=C<=Price_SP;

    BuyS=C>=Price_BP;

    If(TotalTrades == 0)

           {

                   myLots = Lots;

               

                   

                   myNetProfit = 0;

           }

    If (MarketPosition== 1 and BarsSinceEntry>1 and SellPK[1])

    {

    Sell(0,Open);

    //Commentary(\"SS\");

    If(NetProfit >= myNetProfit[1])

                   {

                           myLots = Lots;

                   }Else

                   {

                           myLots = Min((myLots[1] + Lots),MaxLots);

                   }

    }

    If  (MarketPosition== -1 and BarsSinceEntry>1 and BuyPK[1])

    {

    BuyToCover(0,Open);

    //Commentary(\"BS\");

    If(NetProfit >= myNetProfit[1])

                   {

                           myLots = Lots;

                   }Else

                   {

                           myLots = Min((myLots[1] + Lots),MaxLots);

                   }

   

    }

   

    If (MarketPosition!= 1 and CurrentBar>N and BuyPK[1])

    {

    Buy(myLots,Open);

    //Commentary(\"BPK\");

    }

    If (MarketPosition!= -1 and CurrentBar>N and SellPK[1])

    {

    SellShort(myLots,Open);

    //Commentary(\"SPK\");

    }

    myNetProfit = Max(myNetProfit[1],NetProfit);

   

   

  If(h-AvgEntryPrice>=zhiying and MarketPosition==1 and abs(CurrentContracts)==2)

{

   sell(1,max(o,AvgEntryPrice+zhiying));

Commentary(\"止盈\");

}

  If(h-AvgEntryPrice>=zhiying2 and MarketPosition==1 and abs(CurrentContracts)==3)

{

   sell(1,max(o,AvgEntryPrice+zhiying2));

Commentary(\"止盈\");


}  

If(h-AvgEntryPrice>=zhiying3 and MarketPosition==1 and abs(CurrentContracts)==4)

{

   sell(1,max(o,AvgEntryPrice+zhiying3));

Commentary(\"止盈\");


}

If(h-AvgEntryPrice>=zhiying4 and MarketPosition==1 and abs(CurrentContracts)==5)

{

   sell(1,max(o,AvgEntryPrice+zhiying4));

Commentary(\"止盈\");


}

  If(h-AvgEntryPrice>=zhiying5 and MarketPosition==1 and abs(CurrentContracts)==6)

{

   sell(1,max(o,AvgEntryPrice+zhiying5));

Commentary(\"止盈\");


}  

If(h-AvgEntryPrice>=zhiying6 and MarketPosition==1 and abs(CurrentContracts)==7)

{

   sell(1,max(o,AvgEntryPrice+zhiying6));

Commentary(\"止盈\");


}

If(h-AvgEntryPrice>=zhiying7 and MarketPosition==1 and abs(CurrentContracts)==8)

{

   sell(1,max(o,AvgEntryPrice+zhiying7));

Commentary(\"止盈\");


}

If(l<=AvgEntryPrice-zhiying and MarketPosition==-1 and abs(CurrentContracts)==2 )

{

   BuyToCover(1,Min(o,AvgEntryPrice-zhiying));

Commentary(\"止盈\");

}


If(l<=AvgEntryPrice-zhiying2 and MarketPosition==-1 and abs(CurrentContracts)==3 )

{

   BuyToCover(1,Min(o,AvgEntryPrice-zhiying2));

Commentary(\"止盈\");

}


If(l<=AvgEntryPrice-zhiying3 and MarketPosition==-1 and abs(CurrentContracts)==4 )

{

   BuyToCover(1,Min(o,AvgEntryPrice-zhiying3));

Commentary(\"止盈\");

}


If(l<=AvgEntryPrice-zhiying4 and MarketPosition==-1 and abs(CurrentContracts)==5 )

{

   BuyToCover(1,Min(o,AvgEntryPrice-zhiying4));

Commentary(\"止盈\");


   }

If(l<=AvgEntryPrice-zhiying5 and MarketPosition==-1 and abs(CurrentContracts)==6 )

{

   BuyToCover(1,Min(o,AvgEntryPrice-zhiying5));

Commentary(\"止盈\");

}


If(l<=AvgEntryPrice-zhiying6 and MarketPosition==-1 and abs(CurrentContracts)==7 )

{

   BuyToCover(1,Min(o,AvgEntryPrice-zhiying6));

Commentary(\"止盈\");

}


If(l<=AvgEntryPrice-zhiying7 and MarketPosition==-1 and abs(CurrentContracts)==8)

{

   BuyToCover(1,Min(o,AvgEntryPrice-zhiying7));

Commentary(\"止盈\");

   }

    }

   

 


2023-11-05 20:24
yang081112

Params

Numeric notbef(9.00);

Numeric notaft(14.55);

Numeric f1(0.01);

Numeric f2(1);

Numeric f3(0.01);

Numeric reverse(1.00);

Numeric rangemin(0.1);

Numeric xdiv(3);

Numeric offset(5);




Vars

Series<Numeric> ssetup(0);

Series<Numeric> bsetup(0);

Series<Numeric> senter(0);

Series<Numeric> benter(0);

Series<Numeric> bbreak(0);

Series<Numeric> sbreak(0);

Series<Numeric> ltoday(0);

Series<Numeric> hitoday(9999);

Series<Numeric> startnow(0);

Series<Numeric> div(0);

Series<Bool> rfilter(false);

Numeric i_reverse;

Numeric i_rangemin;

Numeric i_vB;

Numeric i_vS;

Numeric i_offset;



Numeric offset2;

Events

   onBar(ArrayRef<Integer> indexs)

   {    

   If(BarStatus==2 && Time==0.090000 && high==low ) return; // 集合竟价过滤信号

   

   

   

   

   i_offset = offset*MinMove*PriceScale;

   i_reverse = reverse*(OpenD(0)/100);

   i_rangemin = rangemin*(OpenD(0)/100);

   if(BarStatus==0)

   {

   startnow=0;

   div=max(xdiv,1);

   }

   

   

   if(Date != Date[1])//如果当前公式应用商品在当前Bar的日期不等于前面公式应用商品在当前Bar的日期

   {

   SetGlobalVar(0,0);// 将第1个全局变量设置为0,将第2个全局变量设置为0

   SetGlobalVar(1,0);

   startnow=startnow+1;

   ssetup=hitoday[1]+f1*(Close[1]-ltoday[1]);

   senter=((1+f2)/2)*(hitoday[1]+Close[1])-(f2)*ltoday[1];

   benter=((1+f2)/2)*(ltoday[1]+Close[1])-(f2)*hitoday[1];

   bsetup=ltoday[1]-f1*(hitoday[1]-Close[1]);

   bbreak=ssetup+f3*(ssetup-bsetup);

   sbreak=bsetup-f3*(ssetup-bsetup);

   

   

   hitoday=High;

   ltoday=Low;

   

   

   rfilter=(hitoday[1]-ltoday[1])>=i_rangemin;

   }

   

   

   if(High>hitoday)

   {

   hitoday=High;

   }

   if(Low<ltoday)

   {

   ltoday=Low;

   }

   if(Time*100>notbef and Time*100<notaft and startnow>=2 and rfilter)//当前公式应用商品在当前Bar的时间乘以100》=9点或者小于14.55分

   {

   

   

   if(Time != GetGlobalVar(1) and GetGlobalVar(1) != 0)

   {

   SetGlobalVar(1,10000);

   }

   if(hitoday>=ssetup and marketposition>-1 and GetGlobalVar(1)<1)

   {

   If(Low<=(senter+(hitoday-ssetup)/div))

   {

   SellShort(1,senter+(hitoday-ssetup)/div+i_offset);

   SetGlobalVar(1,Time);

   Return;

   }

   }

   if(ltoday<=bsetup and marketposition<1 and GetGlobalVar(1)<1)

   {

   If(High>=(benter-(bsetup-ltoday)/div))

   {

   Buy(1,benter-(bsetup-ltoday)/div-i_offset);

   SetGlobalVar(1,Time);

   Return;

   }

   }

   

   

   if(marketposition==-1)//-1 当前位置为持空仓

   

   

   {

   SetGlobalVar(0,1);

   if(High-EntryPrice>=i_reverse)

   {

   BuyToCover(1,entryprice);

   Return;

   }

   }

   if(marketposition==1)//1 当前位置为持多仓

   {

   SetGlobalVar(0,1);

   if(EntryPrice-Low>=i_reverse)

   {

   Sell(1,entryprice);

   Return;

   }

   }

   

   

   if(marketposition==0)//1 当前位置为没有持仓

   {

   if(High>=bbreak and GetGlobalVar(0) == 0)

   {

   Buy(1,bbreak-i_offset);

   Return;

   }

   }

   if(marketposition==0)//1 当前位置为没有持仓

   {

   if(low<=sbreak and GetGlobalVar(0) == 0)

   {

   SellShort(1,sbreak+i_offset);

   Return;

   }

   }

   

   

   }

   

   

   if(Time*100>=notaft and Time<0.1600)

   {

   

   

   if(marketposition==-1)

   {

   BuyToCover(1,Open);

   }

   if(marketposition==1)

   {

   Sell(1,Open);

   }

   

   

   }

   

   

   

   

   }


2024-03-23 23:31
yang081112

Params

Numeric FastLength(5);// 短期指数平均线参数

Numeric SlowLength(20);// 长期指数平均线参数

//Numeric Lots(1);

Numeric A(189);

Numeric B(198);

Numeric CC(29);

Numeric s(3);

Numeric sK(0.5);

Vars

Numeric lot(0);

Numeric FastLengthA(A);

Numeric SlowLengthA(B);

Numeric MACDLength(CC);

Series<Numeric> MACDValue;

Series<Numeric> AvgMACD;

Series<Numeric> MACDDiff;

Series<Numeric> MA5;

Series<Numeric> MA20;

Bool BuyCon(False);

Bool SellCon(False);

Bool MacdJcCon(False);

Bool MacdScCon(False);

Numeric sd(0);

Events

   onBar(ArrayRef<Integer> indexs)

   {    

   MACDValue = XAverage(Close, FastLength) - XAverage(Close, SlowLength);

   AvgMACD = XAverage(MACDValue, MACDLength);

   MACDDiff = MACDValue - AvgMACD;

MA5 = AverageFC(Close,FastLength);

MA20 = AverageFC(Close,SlowLength);


   PlotNumeric(\"MA5\", Ma5);

   PlotNumeric(\"MA20\", Ma20);

       sd=(StandardDev((Close[1]-Close[2])/Close,100,2))*100;

   lot=IntPart(sK/sd);

   If(MarketPosition != 1 && MACDValue[1] > 0 && Ma5[1] > Ma20[1]) {

       Buy(lot, Open);

   } Else If(MarketPosition == -1 && (MACDValue[1] > 0 || Ma5[1] > Ma20[1])) {

       BuyToCover(Lot, Open);

   } Else If(MarketPosition != -1 && MACDValue[1] < 0 && Ma5[1] < Ma20[1]) {

       SellShort(Lot, Open);

   } Else If(MarketPosition == 1 && (MACDValue[1] < 0 || Ma5[1] < Ma20[1])) {

       Sell(Lot, Open);

   }

   }


2024-04-10 03:27
您未登录,请先 登录注册 后发表评论
顶部