使用mode众数函数 low值计算 结果出现-1值

关于序列值的计算结果异常
求众数
atr值如何计算
索引值的使用问题
【bug报告】tbquant计算商品之间的价差经常出现0(错误值)
起始值计算,到结束值循环计算
如何计算当日价格的最大值和最小值
A_BuyPosition函数返回值问题
if语句lowest计算值不对
为什么AverageFC函数计算出来的是无效值?

这个-1就是指没有找到数量最多的数,所有数字都只出现了一次

 

请贴完整代码方便复现

 

Params

    Numeric length(20);

    Numeric offset(0);

Vars

    //此处添加变量

    Series<Numeric> have(2);

    Series<Numeric> num(0);

    Numeric temp;

    Numeric avg;

    Global Integer timerId;

   

    Series<Numeric> uppe;

    Series<Numeric> lowe;

    Series<Numeric> midd;

    Series<Numeric> src;

    Series<Numeric> ma1;

   

    Bool longflag;

    bool longclose;

    Bool shortflag;

    Bool shortclose;


 

Defs

    //此处添加公式函数

    Numeric calcAvg(Numeric a,Numeric b)

    {

        return (a+b)/2;

    }

   


 

Events

    //此处实现事件函数

    OnInit()

    {

        //获得投资组合的初始资金

        //Print("Portfolio_InitCapital:" + Text(Portfolio_InitCapital()));

        //设置初始资金

        SetInitCapital(100000);

        //获得投资组合的初始资金

        //Print("Portfolio_InitCapital:" + Text(Portfolio_InitCapital()));

    }

 

        //在所有的数据源准备完成后调用,应用在数据源的设置等操作

    OnReady()

    {

        Integer i;

        For i = 0 To A_AccountCount - 1

        {

            String id = A_AccountID(i);

            Account acc;

            //Bool ret = A_GetAccount(acc, i);

            //Print("accountId:" + id + ",A_GetAccount:" + IIFString(ret,"True","False") + ",value:" + Text(acc));

           

           

            //Numeric value = A_FreeMargin(i);

            //Print("accountId:" + id + ",A_FreeMargin:" + Text(value));

        }

    }

 

    //在新bar的第一次执行之前调用一次,参数为新bar的图层数组

    OnBarOpen(ArrayRef<Integer> indexs)

    {

 

    }

 

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

    OnBar(ArrayRef<Integer> indexs)

    {

       

       

        src=(open+high+low+close)/4;

       

        uppe=Mode(High,length);

        lowe=Mode(Low,length);

        midd=(uppe+lowe)/2;

        ma1=Average(src,length);

       

       

        //PlotNumeric("src",src);

        PlotNumeric("uppe",uppe[offset]);

        PlotNumeric("lowe",lowe[offset]);

        //PlotNumeric("midd",midd[offset]);

       

 

        have=MarketPosition();

        //Print("have "+Text(have));

 

       

        //longflag= have !=1 and high>uppe[offset] and high[1]<=uppe[offset+1]  ;

        //longclose= have==1 and  low<lowe[offset] and low[1]>=lowe[offset+1] ;

        //shortflag=have!=-1 and  low<lowe[offset] and low[1]>=lowe[offset+1]  ;

        //shortclose=have==-1 and  high>uppe[offset] and high[1]<=uppe[offset+1] ;

       

        longflag= have !=1 and high>uppe[offset]  ;

 

        shortflag=have!=-1 and  low<lowe[offset]  ;

 

       

 

       

        //PlotBool("longflag",longflag);

        //PlotBool("shortflag",shortflag);

       

        // SellShort-产生一个空头建仓操作

        // BuyToCover-产生一个空头平仓操作

        // Buy-产生一个多头建仓操作

        // Sell-产生一个多头平仓操作

       

        //PlotNumeric("temp",temp);

       

       

        if (longflag) {

            //if(open>uppe[offset]){

                //Buy((1),open+3);

            //}else{

                //Buy((1),uppe[offset]+3);

            //}

            Buy((1),uppe[offset]+1);

            Print("long have "+Text(have));

            //have = 1;

        }

        //else if (longclose) {

            ////Sell((num),Close);

            ////have = 2;

//

        //}

        else if (shortflag){

            //if(open<lowe[offset]){

                //SellShort((1),open-3);

            //}else{

                //SellShort((1),lowe[offset]-3);

            //}

            SellShort((1),lowe[offset]-1);

            Print("short have "+Text(have));

            //have=-1;

        }

        //else if(shortclose){

            ////BuyToCover((num),Close);

            ////have=2;

        //}

       

        //PlotNumeric("have",A_FreeMargin(0));

       

    }


 

就没用其他东西 就是单纯的众数 注释的代码是复制过来没用的

麻烦以后记得把代码精简整理一下,不必要的内容去除掉,不要留这么多空行,给阅读带来很大的困难

光是给你调整格式就浪费了好一会儿时间