求一个超级趋势 (SuperTrend) 的 TB源

别的平台的源码

N:=10;    // ATR周期

M:=3;     // 倍数系数


// 1. 计算TR & ATR

TR:=MAX(MAX((HIGH-LOW),ABS(REF(CLOSE,1)-HIGH)),ABS(REF(CLOSE,1)-LOW));

ATR:=MA(TR,N);


// 2. 计算上下轨

HL2:=(H+L)/2;

UP:=HL2+M*ATR;

DN:=HL2-M*ATR;


// 3. 趋势状态与画线

SUPERTREND:IF(CLOSE>REF(UP,1),DN,IF(CLOSE<REF(DN,1),UP,REF(SUPERTREND,1))),COLORRED,LINETHICK2;


// 4. 颜色区分多空

IF(SUPERTREND>REF(SUPERTREND,1),SUPERTREND,NULL),COLORRED,LINETHICK2;

IF(SUPERTREND<REF(SUPERTREND,1),SUPERTREND,NULL),COLORGREEN,LINETHICK2;


// 5. 买卖信号标记

DRAWICON(CROSS(C,SUPERTREND),L*0.99,1);  // 红箭头 做多

DRAWICON(CROSS(SUPERTREND,C),H*1.01,2);  // 绿箭头 做空

SuperTrend指标编写为啥会出现与K线坐标不匹配,正负无穷大,求大佬帮分析下指标错误,谢谢
画线趋势线预警设置
求教老师,上升趋势,求每个新高后回调的收盘价大于前一个新高后的回调最低价的函数公式怎么写,谢谢!
if(ma1>ma2)定义为上升趋势,求最高点的bar,代码怎么写
跨周期SuperTrend编写问题
MaxBarsBack值超级大
求一个TB指标公式
获取趋势线的最新价格
如何调整工作区超级图标中的视觉?
超级炒手 SuperTrader 怎么使用?

小马过河的代码能编译,但是不能产生真实的交易信号。

Params
        Numeric N(10);
        Numeric M(3);
Vars
        Global Array<Array<Numeric>> g_Adjunct_Vars_AllSigs;
        Global Array<Array<Numeric>> g_Adjunct_Vars_curSigs;
        Global Array<Array<Numeric>> g_Adjunct_Vars_cacheSigs;
        Global Bool g_Adjunct_Vars_DayTradeClear(false);
        Global Integer  g_Adjunct_Vars_LayerSeq(0);
        Global Numeric  g_Adjunct_Vars_CLOSEKLINE_Type(InvalidNumeric);
        Global Numeric  g_Adjunct_Vars_CLOSEKLINE_N(InvalidNumeric);
        Global Numeric  g_Adjunct_Vars_MULTISIG_SEC1(InvalidNumeric);
        Global Numeric  g_Adjunct_Vars_MULTISIG_SEC2(InvalidNumeric);
        Global Numeric  g_Adjunct_Vars_MULTISIG_N(InvalidNumeric);
        Global Numeric  g_Adjunct_Vars_MULTISIG_INTERVAL(InvalidNumeric);
        Global Map<String,Array<Numeric>> g_Adjunct_Vars_CHECKSIG_Params;
        Global Bool g_Adjunct_Vars_DAYTRADE_Enable(false);
        Global Integer  g_Adjunct_Vars_FunIndex(0);
        Global Integer  g_Adjunct_Vars_lastFunType(0);
        Global Integer  g_Adjunct_Vars_lastSigType(0);
        Global Integer  g_Adjunct_Vars_BKVOL(0);
        Global Integer  g_Adjunct_Vars_SKVOL(0);
        Global Integer  g_Adjunct_Vars_MAXBKVOL(0);
        Global Integer  g_Adjunct_Vars_MAXSKVOL(0);
        Global Numeric  g_Adjunct_Vars_BKPrice(0);
        Global Numeric  g_Adjunct_Vars_SKPrice(0);
        Global Integer  g_Adjunct_Vars_BKindex(-1);
        Global Integer  g_Adjunct_Vars_SPindex(-1);
        Global Integer  g_Adjunct_Vars_SKindex(-1);
        Global Integer  g_Adjunct_Vars_BPindex(-1);
        Natural Numeric  g_Adjunct_Vars_BKHIGH(0);
        Natural Numeric  g_Adjunct_Vars_BKLOW(0);
        Natural Numeric  g_Adjunct_Vars_SKHIGH(0);
        Natural Numeric  g_Adjunct_Vars_SKLOW(0);
        Global Numeric  g_Adjunct_Vars_UNCheckBKHIGH(0);
        Global Numeric  g_Adjunct_Vars_UNCheckBKLOW(0);
        Global Numeric  g_Adjunct_Vars_UNCheckSKHIGH(0);
        Global Numeric  g_Adjunct_Vars_UNCheckSKLOW(0);
        Global Integer g_Adjunct_Vars_CheckProdId(-1);
        Global Map<Integer,Array<Array<Numeric>>> g_Adjunct_Vars_BKSigs(0);
        Global Map<Integer,Array<Array<Numeric>>> g_Adjunct_Vars_SKSigs(0);
        Global Map<Integer,Array<Array<Numeric>>> g_Adjunct_Vars_BPSigs(0);
        Global Map<Integer,Array<Array<Numeric>>> g_Adjunct_Vars_SPSigs(0);
        Global Map<Integer,Array<Array<Numeric>>> g_Adjunct_Vars_BPKSigs(0);
        Global Map<Integer,Array<Array<Numeric>>> g_Adjunct_Vars_SPKSigs(0);
        Global Map<Integer,Array<Array<Numeric>>> g_Adjunct_Vars_STOPSigs(0);
        Global Map<Integer,Array<Array<Numeric>>> g_Adjunct_Vars_CLOSEOUTSigs(0);
        Global bool g_Adjunct_Vars_NewTradePeriod(true);
        Global Array<Array<Numeric>> g_Adjunct_Vars_EntrySigs;
        Global Array<Array<Numeric>> g_Adjunct_Vars_ExitSigs;
        Global Bool g_Adjunct_Vars_SlippageGet(false);
        Global Integer  g_Adjunct_Vars_SlippageType(1);
        Global Numeric  g_Adjunct_Vars_Slippage(0);
        Global Array<Numeric> g_Adjunct_Vars_TradeRefs;
        Global Integer  g_Adjunct_Vars_TradeRefCoe(0);
        Global Array<Integer> g_Adjunct_Vars_SigVols;
        Global Numeric  g_Adjunct_Vars_EntryAvPrice(0);
        Global Array<Integer>  g_Adjunct_Vars_lastFun;
        Global String   g_Adjunct_Vars_Cur_group("");
        Global String   g_Adjunct_Vars_DrawIcon_Tips("");
        Global Map<String,Integer> g_Adjunct_Vars_Sound_play(-1);
        Global Map<String,Bool> g_Adjunct_Vars_PlaySound_play(True);
        Global Integer  g_Adjunct_Vars_OnBar_RunCount(0);
        Bool g_Adjunct_Vars_Condition_Symbol(true);
        Bool g_Adjunct_SendOrder_Symbol(false);
        Plot g_Adjunct_plot_transfer_main;
        Plot g_Adjunct_plot_transfer_child;
        Bool g_Adjunct_plot_transfer_flag(false);
        Global Integer g_Adjunct_Vars_BarUpper_Color(9);
        Global Integer g_Adjunct_Vars_BarLower_Color(10);
        Global bool g_Adjunct_Vars_USE_CLOSEOUT(false);
        Global Integer g_Adjunct_Vars_BarUpper_Style(0);
        Global Integer g_Adjunct_Vars_BarLower_Style(0);
        Global String g_Adjunct_Vars_Plot_Name_As("");
        Global Bool   g_Adjunct_Vars_Fixed_Primary_Axis(false);
        Global Plot draw_SetPlotOption_obj;
        Series<Array<Numeric>> f_islastbar_status(0,2);
        Global Array<Integer> g_Adjunct_Vars_runlayer;
        Series<Numeric> SUPERTREND(InvalidNumeric,2);
        Plot p_default_plot_line;
        Plot p_common_plot_line;
        Plot p_common_plot_bar;
        Plot p_common_plot;
        Series<Array<String>> common_Plot_line_break("",2);
        Global String plot_common_name_as("");
        Plot DRAWICON_p_draw_icon;
        Integer DRAWICON_index(0);
        Global Map<String,Integer> g_plot_instant_option;
        Global Map<String,Integer> g_plot_option;
        Global Integer g_plot_type(0);
Defs
        Numeric OnReady_Begin()
        {
            map<Integer,Integer> mapLayer;
                Array<Numeric> srcLayer;
                GetGlobalArray(FormulaName,srcLayer);
                if(GetArraySize(srcLayer) > 0)
                {
                    Integer i = 0;
                    for i = 0 to GetArraySize(srcLayer) - 1
                    {
                        mapLayer[IntPart(srcLayer[i] + 0.1)] = 0;
                    }
                    
                    Array<Integer> runLayer;
                    getMapKeys(mapLayer,runLayer);
                    for i = 0 to GetArraySize(runLayer) - 1
                    {
                        ArrayPushBack(g_Adjunct_Vars_runlayer, runLayer[i]);
                    }
                }
                else{
                    ArrayPushBack(g_Adjunct_Vars_runlayer, 0);
                }
                return 0;
        }
        Numeric OnReady_End()
        {
            Return 0;
        }
        Bool testBoolean(Numeric cond)
        {
            Return cond != 0 && cond != InvalidNumeric;
        }
        Bool testCondition(Numeric cond)
        {
            g_Adjunct_SendOrder_Symbol = true;
            Return cond != 0 && cond != InvalidNumeric;
        }
        Bool setPlotAllOption(StringRef name,MapRef<String,Integer> options,Array<String> FilterOpts)
        {
            Integer i = 0;
            Numeric clearX1 = -1;
            For i = 0 To GetMapSize(options) - 1
            {
                String optKey;
                GetMapKey(options,i,optKey);
                if(ArrayFind(FilterOpts,optKey))
                {
                    continue;
                }
                 Integer optValue;
                GetMapValue(options,i,optValue);
                if(optKey == "margin-top" || optKey == "margin-bottom")
                {
                    draw_SetPlotOption_obj.setOption(optKey, Text(optValue) + "%");
                    continue;
                }
                if(optKey == "clear-x1")
                {
                    clearX1 = optValue;
                    continue;
                }
                draw_SetPlotOption_obj.setOption(name,optKey,optValue);
            }
            if(clearX1>=0)
            {
                draw_SetPlotOption_obj.clear(name,Date[clearX1]+Time[clearX1]);
            }
            return true;
        }Numeric OnBar_Begin()
        {
            Return 0;
        }
        Numeric OnBar_End()
        {
            Return 0;
        }
        Numeric OnBarClose_Begin()
        {
            MapClear(g_Adjunct_Vars_Sound_play);
            MapClear(g_Adjunct_Vars_PlaySound_play);
            g_Adjunct_Vars_OnBar_RunCount = 0;
            return 0;
        }
        Numeric OnBarClose_End()
        {
            Return 0;
        }
        Numeric OnBarOpen_Begin()
        {
            return 0;
        }
        Numeric OnBarOpen_End()
        {
            Return 0;
        }
        Numeric ADJUNCT_SUPERTREND(Numeric nValue)
        {
            SUPERTREND = nValue;
            Return nValue;
        }
        Numeric Jan4Init_Default_Plot_Line(String sName="",Numeric nValue=0)
        {
            p_default_plot_line.figure();
            p_default_plot_line.hide();
            Return 0;
        }
        Numeric Default_Plot_Line(String sName="",Numeric nValue=0)
        {
            if(barstatus == 2)
            {
                p_default_plot_line.clear(sName);
            }
            if(nValue >= InvalidNumeric || nValue >= InvalidInteger)
            {
                return nValue;
            }
            p_default_plot_line.line(sName,-1,nValue);
            Return nValue;
        }
        Numeric Common_Plot_Line(String sName,Numeric nValue,String defName="")
        {
            if(sName == "")
            {
                sName = defName;
            }
            
            p_common_plot = p_common_plot_line;
            if(g_plot_type != 0)
            {
                p_common_plot = p_common_plot_bar;
            }
            if(g_Adjunct_plot_transfer_flag)
            {
                p_common_plot = g_Adjunct_plot_transfer_child;
                g_Adjunct_plot_transfer_flag = false;
            }
            
            if(g_Adjunct_Vars_Plot_Name_As != plot_common_name_as)
            {
                plot_common_name_as = g_Adjunct_Vars_Plot_Name_As;
                p_common_plot.setOption(sName,"plot_name_as",plot_common_name_as);
            }
            g_Adjunct_Vars_Plot_Name_As = "";
            
            draw_SetPlotOption_obj = p_common_plot;
            setPlotAllOption(sName,g_plot_instant_option,[""]);
        
            Array<Integer> indexs;
            ArrayFind(common_Plot_line_break[0],sName+"@True",indexs);
            Bool lineBreak = GetArraySize(indexs) != 0;
            
            p_common_plot.setOption(sName,"line-break", lineBreak);
            if(nValue >= InvalidNumeric || nValue >= InvalidInteger)
            {
                if(lineBreak)
                {
                    common_Plot_line_break[0][indexs[0]] = sName+"@True";
                }
                Else
                {
                    ArrayPushBack(common_Plot_line_break[0],sName+"@True");
                }
                p_common_plot.clear(sName,Date+Time);
                return nValue;
            }
            
            if(g_plot_type == -1)
            {
                p_common_plot.ployline(sName, nValue,0,true);
                Return InvalidNumeric;
            }
            if(1 == g_plot_type)
            {
                p_common_plot.setOption(sName,"style",IIF(nValue > 0,g_Adjunct_Vars_BarUpper_Style,g_Adjunct_Vars_BarLower_Style));
            }
            else if(2 == g_plot_type)
            {
                Bool isUp = Close >= Open;
                if(BarType == 2 && BarInterval == 1)
                {
                    isUp = Close >= Close[1];
                }
                p_common_plot.setOption(sName,"style",IIF(isUp,g_Adjunct_Vars_BarUpper_Style,g_Adjunct_Vars_BarLower_Style));
            }
            
            Numeric clearX1 = -1;
            Numeric clearX2 = -1;
            Integer i = 0;
            For i = 0 To GetMapSize(g_plot_option) - 1
            {
                String optKey;
                Integer optValue;
                GetMapKey(g_plot_option,i,optKey);
                GetMapValue(g_plot_option,i,optValue);
                if(optKey == "margin-top" || optKey == "margin-bottom")
                {
                    p_common_plot.setOption(sName,optKey,Text(optValue)+"%");
                    continue;
                }
                p_common_plot.setOption(sName,optKey,optValue);
                
                if(optKey == "clear-x1")
                {
                    clearX1 = optValue / 1000000;
                }
                if(optKey == "clear-x2")
                {
                    clearX2 = optValue / 1000000;
                }
                
            }
            
            if(1 == g_plot_type)
            {
                p_common_plot.setOption(sName,"color",IIF(nValue > 0,g_Adjunct_Vars_BarUpper_Color,g_Adjunct_Vars_BarLower_Color));
            }
            else if(2 == g_plot_type)
            {
                Bool isUp = Close >= Open;
                if(BarType == 2 && BarInterval == 1)
                {
                    isUp = Close >= Close[1];
                }
                p_common_plot.setOption(sName,"color",IIF(isUp,g_Adjunct_Vars_BarUpper_Color,g_Adjunct_Vars_BarLower_Color));
            }
            
            if(g_plot_type == 0)
            {
                p_common_plot.line(sName, nValue);
            }
            Else
            {
                p_common_plot.barv(sName, nValue);
            }
        
            if(clearX1 >=0 )
            {
                 p_common_plot.clear(sName,clearX1,clearX2);
            }
            if(lineBreak)
            {
                ArrayErase(common_Plot_line_break[0],indexs[0],1);
            }
            g_Adjunct_Vars_BarUpper_Color = Color9;
            g_Adjunct_Vars_BarLower_Color = Color10;
            g_Adjunct_Vars_BarUpper_Style = Enum_Line;
            g_Adjunct_Vars_BarLower_Style = Enum_Line;
            Return nValue;
        }
        Numeric JAN_REF(Numeric X,Numeric N)
        {
            Numeric ret = REF(X,N);
            if(CurrentBar < N || N == InvalidNumeric)
            {
                return InvalidNumeric;
            }    
            Return ret;
        }
        Numeric IFELSE(Numeric X,Numeric A,Numeric B)
        {
            If(X != 0 and X != InvalidNumeric)
            {
                Return A;
            }
            Return B;
        }
        Numeric COLORRED()
        {
            Return Red();
        }
        Numeric LINETHICK2()
        {
            Return Enum_2Pix;
        }
        Numeric NULLVALUE()
        {
            Return InvalidNumeric();
        }
        Numeric COLORGREEN()
        {
            Return Green();
        }
        Numeric Jan4Init_DRAWICON(Numeric Cond,Numeric Price,String Icon)
        {
            if(g_Adjunct_Vars_Fixed_Primary_Axis== True)
                {
                    DRAWICON_p_draw_icon.setOption("margin-top","100%");
            DRAWICON_p_draw_icon.setOption("margin-bottom","0%");
            };
            Return 0;
        }
        Numeric DRAWICON(Numeric Cond,Numeric Price,String Icon)
        {
            DRAWICON_index = DRAWICON_index+1;
            String name = "draw_icon_"+text(DRAWICON_index);
            DRAWICON_p_draw_icon.clear(name,Date+Time);
            DRAWICON_p_draw_icon.setOption(name,"no-text",true);
            
            draw_SetPlotOption_obj = DRAWICON_p_draw_icon;
            setPlotAllOption(name,g_plot_instant_option,[""]);
            
            If(InvalidNumeric == Cond || InvalidNumeric == Price)
            {
                Return InvalidNumeric;
            }
            if(Cond != 0 and cond != InvalidNumeric)
            {
                setPlotAllOption(name,g_plot_option,[""]);
                
                String iconName = Icon;
                if(0 != Value(Icon))
                {
                    iconName = "ICO"+iconName;
                }
                DRAWICON_p_draw_icon.icon(name,Price,iconName,g_Adjunct_Vars_DrawIcon_Tips);
                g_Adjunct_Vars_DrawIcon_Tips = "";
            }
            Return InvalidNumeric;
        }
        Numeric JAN_CROSS(Numeric value1,Numeric value2)
        {
            if(value1 == InvalidNumeric)
            {
                Return 0;
            }
                        
            if(value2 == InvalidNumeric)
            {
                Return 0;
            }
        
            If(Cross(value1,value2))
            {
                Return 1;
            }
            Return 0;
        }
Events

    OnInit()
    {
        Jan4Init_Default_Plot_Line();
        if(g_Adjunct_Vars_Fixed_Primary_Axis== True)
            {
                p_common_plot_line.setOption("margin-top","100%");
        p_common_plot_line.setOption("margin-bottom","0%");
        p_common_plot_bar.setOption("margin-top","100%");
        p_common_plot_bar.setOption("margin-bottom","0%");
        };
        
        SetbeginBarMaxCount(-1);
        SetBackBarMaxCount(InvalidInteger);
        Jan4Init_DRAWICON(0,0,"1");
        Jan4Init_DRAWICON(0,0,"2");
    }
    OnReady()
    {
            onready_begin();
            onready_end();
    }
    OnBarOpen(ArrayRef<Integer> indexs)
    {
        if(ArrayFind(indexs,0) == True)
        {
            onbaropen_begin();
            onbaropen_end();
        }
    }
    OnBar(ArrayRef<Integer> indexs)
    {
        onbar_begin();
        g_Adjunct_Vars_LayerSeq = 0;
        Range[g_Adjunct_Vars_LayerSeq in g_Adjunct_Vars_runlayer]
        {
            Numeric ATR = InvalidNumeric;
            Numeric TR1 = InvalidNumeric;
            Numeric HL2 = InvalidNumeric;
            Numeric UP = InvalidNumeric;
            Numeric DN = InvalidNumeric;

            TR1 = Default_Plot_Line("TR1",MAX(MAX(f_minus(HIGH,LOW),ABS(f_minus(JAN_REF(CLOSE,1),HIGH))),ABS(f_minus(JAN_REF(CLOSE,1),LOW))));
            MapClear(g_plot_instant_option);
            ATR = Default_Plot_Line("ATR",MA(TR1,N));
            MapClear(g_plot_instant_option);
            HL2 = Default_Plot_Line("HL2",f_div(f_plus(H,L),2));
            MapClear(g_plot_instant_option);
            UP = Default_Plot_Line("UP",f_plus(HL2,f_mul(M,ATR)));
            MapClear(g_plot_instant_option);
            DN = Default_Plot_Line("DN",f_minus(HL2,f_mul(M,ATR)));
            MapClear(g_plot_instant_option);
            g_plot_option["color"] = COLORRED;
            g_plot_option["width"] = LINETHICK2;
            SUPERTREND = Common_Plot_Line("SUPERTREND",ADJUNCT_SUPERTREND(IFELSE(f_gt(CLOSE,JAN_REF(UP,1)),DN,IFELSE(f_lt(CLOSE,JAN_REF(DN,1)),UP,JAN_REF(SUPERTREND,-1+1)))));
            MapClear(g_plot_instant_option);
            MapClear(g_plot_option);g_plot_type = 0;
            g_plot_option["color"] = COLORRED;
            g_plot_option["width"] = LINETHICK2;
            String line0_plot_name = "#line0"; Common_Plot_Line("", IFELSE(f_gt(SUPERTREND,JAN_REF(SUPERTREND,1)),SUPERTREND,NULLVALUE),line0_plot_name);
            MapClear(g_plot_instant_option);
            MapClear(g_plot_option);g_plot_type = 0;
            g_plot_option["color"] = COLORGREEN;
            g_plot_option["width"] = LINETHICK2;
            String line1_plot_name = "#line1"; Common_Plot_Line("", IFELSE(f_lt(SUPERTREND,JAN_REF(SUPERTREND,1)),SUPERTREND,NULLVALUE),line1_plot_name);
            MapClear(g_plot_instant_option);
            MapClear(g_plot_option);g_plot_type = 0;
            DRAWICON(JAN_CROSS(C,SUPERTREND),f_mul(L,0.99),"1");
            MapClear(g_plot_instant_option);
            DRAWICON(JAN_CROSS(SUPERTREND,C),f_mul(H,1.01),"2");
            MapClear(g_plot_instant_option);
        }
        onbar_end();
    }
    OnBarClose(ArrayRef<Integer> indexs)
    {
        if(ArrayFind(indexs,0) == True)
        {
            onbarclose_begin();
            onbarclose_end();
        }
    }

    OnEvent(StringRef evtName, MapRef<String, String> evtValue)
    {
    }


有错误老师


用在tbquant3

一句注释都没有啊😂