全部 智大领峰 TBQuant功能 TBL语言 TB开户 问答专区 高手圈 其他
策略交易工作区显示手数和K线里面手数不一致
2022-05-25 10:55

//------------------------------------------------------------------------
// 简称: strategy_l_5_1
// 名称: DMI+EMA
// 类别: 公式应用
// 类型: 用户应用
// 输出: Void
//------------------------------------------------------------------------
Params
    //此处添加参数
    Integer ADD_DEAL_MAX_NUM(20);  //最大加仓手数
    Numeric AvgLength(30);         //加权平均周期数
    Numeric DMI_LENGTH(14);      //DMI计算周期数
    Numeric BollLength(20);      //Boll线周期
    
    Integer FIRST_DEAL_NUM_L(1);     //当常量用的第一次开仓数量-做多
    Integer ADD_DEAL_NUM_L(2);       //当常量用的加仓数-做多
    Integer SUB_DEAL_NUM_L(1);       //当常量用的减仓数-做多
    Numeric DiffOpen_L(-25);          //A值开仓阈值-做多
    Numeric Clear_L(2.6);            //最大涨幅-做多
    Numeric RatioAdd_L(0.15);       //加仓比例-做多
    Numeric RatioSub_L(0.35);       //减仓比例-做多
    Numeric Stop_Surplus_L(0);     //止盈生效比例-做多

    Integer FIRST_DEAL_NUM_S(1);     //当常量用的第一次开仓数量-做空
    Integer ADD_DEAL_NUM_S(2);       //当常量用的加仓数-做空
    Integer SUB_DEAL_NUM_S(1);       //当常量用的减仓数-做空
    Numeric DiffOpen_S(25);              //A值开仓阈值-做空
    Numeric Clear_S(2.6);              //最大跌幅-做空
    Numeric RatioAdd_S(0.15);        //加仓比例-做空
    Numeric RatioSub_S(0.35);        //减仓比例-做空
    Numeric Stop_Surplus_S(0);     //止盈生效比例-做空

    //Integer CrossBoll(5);    //上下穿上下轨bar数量
    Integer UpMidLine_Zq(30);    //中轨上方bar数量计算周期
    Integer UpMidLine(20);    //中轨上方bar数量

    Numeric ATRLength(20);            //平均波动周期 ATR Length
    Numeric teLength(15);            //止盈周期
    Integer NB(3);                    // 止损N值倍数
    Integer Run_Time(0);             //0:全天、1:白盘、2:夜盘
    Numeric Charge(5);                 //每手交易手续费
    
    Numeric SurplusMoney(1000);    //止盈生效金额
    Numeric HcMoney(200);    //回撤金额
    Numeric LossMoney(500);  //亏损金额
Vars
    /* 配置参数 */
    Numeric millsecs(1000);
    Global Integer openPostionNumber_L;  //开仓数量
    Global Integer openPostionNumber_S;  //开仓数量

    /* 布林相关变量 */
    Global Numeric Up;                //上轨
    Global Numeric Down;            //下轨 

    Series<Numeric> UpLine;                //上轨
    Series<Numeric> DownLine;            //下轨 
    Series<Numeric> MidLine;    //中轨
        
    /* MA相关变量 */
    Global Numeric MA5;        //MA5
    Global Numeric MA10;    //MA10
    Global Numeric MA20;    //MA20
    Global Numeric MA30;    //MA30

    /* MDI相关变量 */
    Global Numeric pdi;
    Global Numeric mdi;
    Series<Numeric> adx;
    Global Numeric adxr;   

    /* 策略相关变量 */
    Global Integer gTimerId;
    
    //做多
    Global Numeric gBuyNumber_L;         //当前持仓手数-做多
    Global Numeric gPrice_L;               //买入价位
    Global Numeric gPriceMax_L;         //最高价位
    Global Numeric gPriceMin_L;         //最低价位
    Global Numeric gRatioMax_L;            //最大涨跌比例
    Global Numeric gLastAddPrice_L;     //上次加仓价位
    Global Numeric gLastSubPrice_L;     //上次减仓价位
    Global Integer gSubCount_L;         //减仓次数
    Global Integer gAddCount_L;         //加仓次数
    Global Numeric gAvgPrice_L;         //开仓均价
    Global Numeric gCostPrice_L;         //开仓均价

    //做空
    Global Numeric gBuyNumber_S;        //当前持仓手数-做空
    Global Numeric gPrice_S;            //买入价位
    Global Numeric gPriceMax_S;        //最高价位
    Global Numeric gPriceMin_S;        //最低价位
    Global Numeric gRatioMax_S;           //最大涨跌比例
    Global Numeric gLastAddPrice_S;    //上次加仓价位
    Global Numeric gLastSubPrice_S;    //上次减仓价位
    Global Integer gSubCount_S;        //减仓次数
    Global Integer gAddCount_S;        //加仓次数
    Global Numeric gAvgPrice_S;         //开仓均价
    Global Numeric gCostPrice_S;         //开仓均价

    Global Numeric gExitPL_L;        //做多-平仓盈亏
    Global Numeric gExitPL_S;        //做空-平仓盈亏
    
    Global Numeric gExitPL_Day_L;    //每日盈利 - 多头
    Global Numeric gExitPL_Day_S;   //每日盈利 - 空头

    Series<Numeric> gDiff;                 //PDI-MDI
    Series<Numeric> gDiffEMA;           //PDI-MDI
    Global Integer gOpType;           //当前运行类型 1:多 -1:空 0:无仓 2:锁仓
    
    Series<Numeric> AvgTR;            // ATR
    Numeric N;                        // N 值
    Numeric ExitHighestPrice;        // 离市时判断需要的N周期最高价
    Numeric ExitLowestPrice;        // 离市时判断需要的N周期最低价

    Global Numeric gDiffOpen_L;          //A值开仓阈值-做多
    Global Numeric gDiffOpen_S;          //A值开仓阈值-做多

    Global Integer gDirection;  //趋势方向 1:多头  -1:空头  0:盘整
    Global Numeric B;
    
    Series<Numeric> baramount;
    Series<Numeric> barvol;
    series<numeric> dayavg;
    
    Global Numeric ProfitMax_L;  //多头最大盈利
    Global Numeric ProfitMax_S;  //空头最大盈利
    
    Series<Numeric> qs;
Defs
    //实盘时校正合约仓位数
    Bool getPosition(){
        if(BarStatus == 2){
            Position pos;
            Bool ret = A_GetPosition(Symbol, pos, "", 0);
            if(ret){
                gBuyNumber_L = pos.longCurrentVolume;
                gBuyNumber_S = pos.shortCurrentVolume;
            }
            return true;
        }
        return false;
    }
    
    //清仓后初始化
    Bool init(){
        if(gBuyNumber_L == 0){
            gPrice_L = 0;
            gPriceMax_L = 0; 
            gPriceMin_L = 0; 
            gRatioMax_L = 0; 
            gLastAddPrice_L = 0; 
            gLastSubPrice_L = 0; 
            gAddCount_L = 0;
            gSubCount_L = 0;
            gCostPrice_L = 0;
            gExitPL_L = 0;
        }
        if(gBuyNumber_S == 0){
            gPrice_S = 0;
            gPriceMax_S = 0; 
            gPriceMin_S = 0;
            gRatioMax_S = 0; 
            gLastAddPrice_S = 0; 
            gLastSubPrice_S = 0; 
            gAddCount_S = 0;
            gSubCount_S = 0;
            gCostPrice_S = 0;
            gExitPL_S = 0;
        }
        return true;
    }
    
    //加仓
    Bool add(Integer number, Integer OpFlag){
        if((gBuyNumber_L + gBuyNumber_S + number) > ADD_DEAL_MAX_NUM){
            Commentary("已达到最大加仓手数="+Text(ADD_DEAL_MAX_NUM));
            return false;
        }
        if(OpFlag == 1){
            //做多
            gCostPrice_L = Round(((gBuyNumber_L * gCostPrice_L) + (Close * number)) / (gBuyNumber_L + number), 2);
            Buy(number, Close);
            gBuyNumber_L = gBuyNumber_L + number;
            PlotString("BS","B:"+Text(number,2),Low, Red);
        } else if(OpFlag == -1){
            //做空
            gCostPrice_S = Round(((gBuyNumber_S * gCostPrice_S) + (Close * number)) / (gBuyNumber_S + number), 2);
            SellShort(number, Close);
            gBuyNumber_S = gBuyNumber_S + number;
            PlotString("BS","SS:"+Text(number,2),Low, Green);
        }
        Return true;
    }
    
    //减仓
    Bool sub(Integer number, Integer OpFlag){
        if(number > 0){
            if(OpFlag == 1){
                //做多
                if((number - gBuyNumber_L) >= 0){
                    number = gBuyNumber_L;
                }
                gExitPL_L = gExitPL_L + (Close - gCostPrice_L) * number * ContractUnit();
                gExitPL_Day_L = gExitPL_Day_L + ((Close - gCostPrice_L) * number * ContractUnit());
                if(number < gBuyNumber_L){
                    //减仓
                    gBuyNumber_L = gBuyNumber_L - number;
                } else if(number - gBuyNumber_L == 0){
                    gBuyNumber_L = 0;
                    init();
                }
                Sell(number, Close);
                PlotString("BS","S:"+Text(number,2), High, White);
            } else if(OpFlag == -1){
                //做空
                if((number - gBuyNumber_S) >= 0){
                    number = gBuyNumber_S;
                }
                gExitPL_S = gExitPL_S + (gCostPrice_S - Close) * number * ContractUnit();
                gExitPL_Day_S = gExitPL_Day_S + (gCostPrice_S - Close) * number * ContractUnit();
                if(number < gBuyNumber_S){
                    //减仓
                    gBuyNumber_S = gBuyNumber_S - number;
                } else if(number - gBuyNumber_S == 0){
                    gBuyNumber_S = 0;
                    init();
                }
                BuyToCover(number, Close);
                PlotString("BS","BC:"+Text(number,2), High, Yellow);
            }
        }
        Return true;
    }
    
    //运行方向改变
    Bool opTypeChange(Integer opType){
        gOpType = opType;
        if(opType == 1){
            //做多
            gPrice_L = Close();
            gPriceMax_L = gPrice_L;
            gPriceMin_L = gPrice_L;
            gLastAddPrice_L = gPrice_L;
            gLastSubPrice_L = gPrice_L;
            gSubCount_L = 0;
            gAddCount_L = 0;
            gAvgPrice_L = gPrice_L;
            ProfitMax_L = 0;
        }
        if(opType == -1){
            //做空
            gPrice_S = Close();
            gPriceMax_S = gPrice_S;
            gPriceMin_S = gPrice_S;
            gLastAddPrice_S = gPrice_S;
            gLastSubPrice_S = gPrice_S;
            gSubCount_S = 0;
            gAddCount_S = 0;
            gAvgPrice_S = gPrice_S;
            ProfitMax_S = 0;
        }
        return true;
    }

    //指标-开仓
    Bool openPosition() {
        if((Hour == 21 || Hour == 9) && Minute <= 30){
            return false;
        }
        if((gExitPL_Day_L + gExitPL_Day_S) <= -2000){
            Commentary("当日亏损超过2000,不开仓");
            return false;
        }
        if(gDirection == 1 && gBuyNumber_L == 0 && gBuyNumber_S == 0){
            Commentary("收盘价>上轨,多头开仓");
            add(openPostionNumber_L, 1);
            opTypeChange(1);
            return true;
        }
        if(gDirection == -1 && gBuyNumber_L == 0 && gBuyNumber_S == 0){
            Commentary("收盘价<下轨,空头开仓");
            add(openPostionNumber_S, -1);
            opTypeChange(-1);
            return true;
        }
        return false;
    }
    
    Bool changeA(){
        Numeric i = 0;
        Numeric DownNumber = 0;
        
        Numeric high30 = Highest(Close, 30);
        Numeric high30Bar = HighestBar(Close, 30);
        Numeric low30 = Lowest(Close, 30);
        Numeric low30Bar = LowestBar(Close, 30);
                
        Commentary("最高价=" + Text(high30));
        Commentary("最高价位置=" + Text(high30Bar));
        Commentary("最低价=" + Text(low30));
        Commentary("最低价位置=" + Text(low30Bar));
        
        
        qs = 0;
        if(Hour == 9 && Minute <= 30){
            if(Close > MidLine){
                qs = 1;
            }
        } else {
            if(gDirection == 1){
                if(high30Bar == 0){
                    qs = 1;
                }
            } else if (gDirection == -1){
                if(low30Bar > 0){
                    qs = 1;
                }
            } else {
                if(Close > MidLine){
                    qs = 1;
                }
            }
            For i=0 To UpMidLine_Zq - 1 {
                if(qs[i] == 1){
                    DownNumber = DownNumber + 1;
                }
            }
        }
        Commentary("qs=" + Text(qs));
        if(UpMidLine <= UpMidLine_Zq && UpMidLine >= (UpMidLine_Zq/2 + 2)){
            if(DownNumber >= UpMidLine){
                Commentary("向上趋势");
                gDiffOpen_L = DiffOpen_L;
                gDiffOpen_S = 50;
                gDirection = 1;
            } else if(DownNumber <= (UpMidLine_Zq-UpMidLine)){
                Commentary("向下趋势");
                gDiffOpen_L = -50;
                gDiffOpen_S = DiffOpen_S;
                gDirection = -1;
            } else {
                Commentary("盘整趋势");
                gDirection = 0;
                gDiffOpen_L = DiffOpen_L;
                gDiffOpen_S = DiffOpen_S;
            }
        } else {
            Commentary("盘整趋势");
            gDirection = 0;
            gDiffOpen_L = DiffOpen_L;
            gDiffOpen_S = DiffOpen_S;
        }
        Commentary("UpNumber="+Text(DownNumber, 2));
        B = DownNumber - (UpMidLine_Zq-DownNumber);
        Commentary("B="+Text(B, 2));
        if(gBuyNumber_L > 0){
            if(B <= 0){
                B = 0;
            }
        }
        if(gBuyNumber_S > 0){
            if(B >= 0){
                B = 0;
            }
        }
        B = Abs(B);
        Commentary("B="+Text(B, 2));
        return true;
    }
    
    //清仓    
    Bool clearPosition(){
        AvgTR = XAverage(TrueRange,ATRLength);
        N = AvgTR[1];
        
        //Commentary("止盈周期="+Text(B+10));
        ExitLowestPrice = LowestFC(Low[1],B+10);
        ExitHighestPrice = HighestFC(High[1],B+10);
        
        /*Commentary("止损N="+Text(N,2)+" 倍数="+Text((B+10)/10 , 2));
        if(gBuyNumber_L > 0 && gDirection != 1){
            //止盈
            Numeric ra = (Close - gPrice_L) / gPrice_L * 100;
            Commentary("当前盈利比例="+Text(ra, 2)+" 最低值="+Text(ExitLowestPrice));
            if(Close > gPrice_L && Close < ExitLowestPrice && BarsSinceEntry() >= B+10 && ra >= Stop_Surplus_L){
                Commentary("跌破"+Text(B+10)+"周期最低值="+Text(ExitLowestPrice, 2)+"全部平仓");
                sub(gBuyNumber_L, 1);
            }
            //止损
            if(Close < gPrice_L && Close <= gPrice_L - ((B+10)/10 * N)){
                Commentary("N="+Text(N)+"止损");
                Commentary("止损");
                sub(gBuyNumber_L, 1);
            }
            //均价平仓
            if(Close < longAvgEntryPrice()){
                Commentary("跌破持仓均价="+Text(longAvgEntryPrice(), 2)+" 全部平仓");
                sub(gBuyNumber_L, 1);
            }
        }
        if(gBuyNumber_S > 0 && gDirection != -1){
            //止盈
            /*Numeric ra = (gPrice_S - Close) / Close * 100;
            Commentary("当前盈利比例="+Text(ra, 2)+" 最高值="+Text(ExitHighestPrice));
            if(Close < gPrice_S && Close > ExitHighestPrice && BarsSinceEntry() >= B+10 && ra >= Stop_Surplus_S){
                Commentary("涨破"+Text(B+10)+"周期最高价="+Text(ExitHighestPrice, 2)+"全部平仓");
                sub(gBuyNumber_S, -1);
            }
            //止损
            if(Close > gPrice_S && Close >= gPrice_S + ((B+10)/10 * N)){
                Commentary("N="+Text(N)+"止损");
                Commentary("止损");
                sub(gBuyNumber_S, -1);
            }
            //均价平仓
            if(Close > shortAvgEntryPrice()){
                Commentary("突破持仓均价="+Text(shortAvgEntryPrice(), 2)+" 全部平仓");
                sub(gBuyNumber_S, -1);
            }
        }*/
        return false;
    }
    
    Bool clearPositionMoney(){
        Numeric Profit_L = ((Close - gCostPrice_L) * gBuyNumber_L * ContractUnit()) + gExitPL_L;
        Numeric Profit_S = ((gCostPrice_S - Close) * gBuyNumber_S * ContractUnit()) + gExitPL_S;

        Commentary("多头-当前盈亏=" + Text(Profit_L, 2));    
        Commentary("空头-当前盈亏=" + Text(Profit_S, 2));

        if(gBuyNumber_L > 0){
            Commentary("多头最大盈亏="+ Text(ProfitMax_L, 2));
            if(Profit_L > ProfitMax_L){
                ProfitMax_L = Profit_L;
            }
            if(ProfitMax_L >= SurplusMoney && ProfitMax_L - Profit_L >= HcMoney){
                Commentary("盈利达到="+Text(SurplusMoney,2)+",回撤="+Text(HcMoney, 2)+"止盈");
                sub(gBuyNumber_L, 1);
            }
            if(Profit_L < 0 && abs(Profit_L) >= LossMoney){
                Commentary("亏损达到="+Text(LossMoney, 2)+"止损");
                sub(gBuyNumber_L, 1);
            }
        }
        if(gBuyNumber_S > 0){
            Commentary("空头最大盈亏="+ Text(ProfitMax_S, 2));
            if(Profit_S > ProfitMax_S){
                ProfitMax_S = Profit_S;
            }
            if(ProfitMax_S >= SurplusMoney && ProfitMax_S - Profit_S >= HcMoney){
                //止盈
                Commentary("盈利达到="+Text(SurplusMoney,2)+",回撤="+Text(HcMoney, 2)+"止盈");
                sub(gBuyNumber_S, -1);
            }
            if(Profit_S < 0 && abs(Profit_S) >= LossMoney){
                Commentary("亏损达到="+Text(LossMoney, 2)+"止损");
                sub(gBuyNumber_S, -1);
            }
        }
        return false;
    }
    
    //加减仓-做多
    Bool calePosition_L() {
        Numeric curPrice = Close;
        if(gBuyNumber_L > 0){
            //计算最大涨比例
            Numeric ratioCur = (curPrice - gPrice_L) / gPrice_L * 100;
            if(ratioCur > gRatioMax_L){
                gRatioMax_L = ratioCur;
            }
            Commentary("多头-买入价格="+Text(gPrice_L, 2)+" 当前价格="+Text(curPrice, 2)+" 最高价格="+Text(gPriceMax_L, 2)
                +" 最低价格="+Text(gPriceMin_L, 2)+" 当前涨比例="+Text(ratioCur,2)+" 最大涨比例="+Text(gRatioMax_L,2));
            
            if(gRatioMax_L >= Clear_L ){
                Commentary("最大涨比例达到"+Text(gRatioMax_L,2) + "全部平仓");
                sub(gBuyNumber_L, 1);
                Return true;
            }

            //加仓判断
            Numeric ra = (gLastAddPrice_L - curPrice) / curPrice * 100;
            Commentary("多头-上次加仓价格="+Text(gLastAddPrice_L, 2)+" 下跌比例="+Text(ra, 2));
            if(ra >= ratioAdd_L && gAddCount_L == 0 && gBuyNumber_L < (FIRST_DEAL_NUM_L + ADD_DEAL_NUM_L)){
                Integer addCount = ADD_DEAL_NUM_L;
                if(Close <= gPrice_L){
                    addCount = 1;
                }
                Commentary("下跌"+Text(ratioAdd_L,2)+"%,加仓"+Text(ADD_DEAL_NUM_L)+"手");
                add(addCount, 1);
                gLastAddPrice_L = curPrice;
                if(curPrice < gPrice_L){
                    gLastSubPrice_L = curPrice;
                }
                gAddCount_L = gAddCount_L + 1;
            }
                        
            //减仓判断
            Numeric rs = (curPrice - gLastSubPrice_L) / gLastSubPrice_L * 100;
            Commentary("多头-上次减仓价格="+Text(gLastSubPrice_L, 2)+" 上涨比例="+Text(rs, 2));
            if(rs >= ratioSub_L){
                if(gBuyNumber_S > 0){
                    Commentary("同时存在空仓,平全部空仓");
                    sub(gBuyNumber_S, -1);
                }
                Commentary("上涨"+Text(ratioSub_L,2)+"%,减仓"+Text(SUB_DEAL_NUM_L)+"手");
                sub(SUB_DEAL_NUM_L, 1);
                gLastSubPrice_L = curPrice;
                gSubCount_L = gSubCount_L + 1;
                gAddCount_L = 0;
            }
                        
            //更新买入后的最高价位
            if(curPrice > gPriceMax_L && gBuyNumber_L > 0){
                gPriceMax_L = curPrice;
                gLastAddPrice_L = curPrice;
            }
            //更新买入后的最低价位
            if(curPrice < gPriceMin_L && gBuyNumber_L > 0){
                gPriceMin_L = curPrice;
            }
        }
        return true;
    }
    //加减仓-做空
    Bool calePosition_S() {
        Numeric curPrice = Close;
        if(gBuyNumber_S > 0){
            //计算最大跌比例
            Numeric ratioCur = (gPrice_S - curPrice) / curPrice * 100;
            if(ratioCur > gRatioMax_S){
                gRatioMax_S = ratioCur;
            }
            Commentary("空头-买入价格="+Text(gPrice_S, 2)+" 当前价格="+Text(curPrice, 2)+" 最高价格="+Text(gPriceMax_S, 2)
                +" 最低价格="+Text(gPriceMin_S, 2)+" 当前跌比例="+Text(ratioCur,2)+" 最大跌比例="+Text(gRatioMax_S,2));

            if(gRatioMax_S >= Clear_S ){
                Commentary("最大涨比例达到"+Text(gRatioMax_S,2)+" 全部平仓");
                sub(gBuyNumber_S, -1);
                Return true;
            }

            //加仓判断
            Numeric ra = (curPrice - gLastAddPrice_S) / gLastAddPrice_S * 100;
            Commentary("空头-上次加仓价格="+Text(gLastAddPrice_L, 2)+" 上涨比例="+Text(ra, 2));
            if(ra >= ratioAdd_S && gAddCount_S == 0 && gBuyNumber_S < (FIRST_DEAL_NUM_L + ADD_DEAL_NUM_S)){
                Integer addCount = ADD_DEAL_NUM_S;
                if(Close >= gPrice_S){
                    addCount = 1;
                }
                Commentary("上涨"+Text(ratioAdd_S,2)+"%,加仓"+Text(ADD_DEAL_NUM_S)+"手");
                add(addCount, -1);
                gLastAddPrice_S = curPrice;
                gLastSubPrice_S = curPrice;
                if(curPrice > gPrice_S){
                    gLastSubPrice_S = curPrice;
                }
                gAddCount_S = gAddCount_S + 1;
            }
            
            //减仓判断
            Numeric rs = (gLastSubPrice_S - curPrice) / curPrice * 100;
            Commentary("空头-上次减仓价格="+Text(gLastSubPrice_S, 2)+" 下跌比例="+Text(rs, 2));
            if(rs >= ratioSub_S){
                Commentary("下跌"+Text(ratioSub_S,2)+"%,减仓"+Text(SUB_DEAL_NUM_S)+"手");
                sub(SUB_DEAL_NUM_S, -1);
                gLastSubPrice_S = curPrice;
                gSubCount_S = gSubCount_S + 1;
                gAddCount_S = 0;
            }
                        
            //更新买入后的最高价位
            if(curPrice > gPriceMax_S && gBuyNumber_S > 0){
                gPriceMax_S = curPrice;
            }
            //更新买入后的最低价位
            if(curPrice < gPriceMin_S && gBuyNumber_S > 0){
                gPriceMin_S = curPrice;
                gLastAddPrice_S = curPrice;
            }
        }
        return true;
    }
    
    //1450全部清仓
    Bool dayClear(){
        Bool isClear = false;
        if(Run_Time == 0){
            //全天
            if((Hour == 14 && Minute >= 50) || (Hour == 22 && Minute >= 50)){
                isClear = true;
            }
        } else if(Run_Time == 1){
            //白盘
            if(Hour == 14 && Minute >= 50){
                isClear = true;
            }
        } else if(Run_Time == 2){
            //夜盘
            if(Hour == 22 && Minute >= 50){
                isClear = true;
            }
        }
        if(isClear){
            sub(gBuyNumber_S, -1);
            sub(gBuyNumber_L, 1);
            gOpType = 0;
            gDirection = 0;
            gExitPL_Day_S = 0;
            gExitPL_Day_L = 0;
        }
        Return isClear;
    }
    
    Bool dayClear2(){
        Bool isClear = false;
        Commentary("Hour="+Text(Hour));
        Commentary("Minute="+Text(Minute));
        Commentary("Second="+Text(Second));
        if(Run_Time == 0){
            //全天
            if((Hour == 14 && Minute >= 50 && Second >= 30) || (Hour == 22 && Minute >= 50 && Second >= 30)){
                isClear = true;
            }
        } else if(Run_Time == 1){
            //白盘
            if(Hour == 14 && Minute >= 50 && Second >= 30){
                isClear = true;
            }
        } else if(Run_Time == 2){
            //夜盘
            if(Hour == 22 && Minute >= 50 && Second >= 30){
                isClear = true;
            }
        }
        if(isClear){
            Position pos;
            Bool ret = A_GetPosition(Symbol, pos, "", 0);
            if(ret){
                gBuyNumber_L = pos.longCurrentVolume;
                gBuyNumber_S = pos.shortCurrentVolume;
                sub(pos.shortCurrentVolume, -1);
                sub(pos.longCurrentVolume, 1);
            }
        }
        Return isClear;
    }
Events
    //此处实现事件函数
    
    //初始化事件函数,策略运行期间,首先运行且只有一次,应用在订阅数据等操作
    OnInit() {
        gTimerId=createTimer(millsecs);        
        openPostionNumber_L = FIRST_DEAL_NUM_L;
        openPostionNumber_S = FIRST_DEAL_NUM_S;
    }

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

    }

    //在新bar的第一次执行之前调用一次,参数为新bar的图层数组
    OnBarOpen(ArrayRef<Integer> indexs) {
        
    }

    //Bar更新事件函数,参数indexs表示变化的数据源图层ID数组
    OnBar(ArrayRef<Integer> indexs) {
        MidLine = getBoll(Up,Down,BollLength);
        UpLine = Up;
        DownLine = Down;
        adx = getDMI(pdi,mdi,adxr, DMI_LENGTH,6);
        
        if(truedate(0)!=truedate(1)) {
            baramount=close*Vol;
            barvol=vol;
        } else {
            baramount = baramount+close*vol;
            barvol = barvol+vol;
        }
        dayavg = Round(baramount / barvol,0);
        Plotnumeric("dayavg",dayavg, 0 , Yellow        );
        PlotNumeric("UpLine",UpLine,0,DarkGreen);
        PlotNumeric("DownLine",DownLine,0,DarkBrown);
        PlotNumeric("MidLine",MidLine);
        dayClear2();
    }

    //下一个Bar开始前,重新执行当前bar最后一次,参数为当前bar的图层数组
    OnBarClose(ArrayRef<Integer> indexs) {        
        gDiff = pdi - mdi;
        gDiffEMA = EMA(gDiff, AvgLength);
        Commentary("gOpType="+Text(gOpType) +" gDiff="+Text(gDiff,2)+" gDiffPMLast="+Text(gDiff[1],2));
        Commentary("gBuyNumber_L="+Text(gBuyNumber_L) +" 开仓均价="+Text(gCostPrice_L));
        Commentary("gBuyNumber_S="+Text(gBuyNumber_S) +" 开仓均价="+Text(gCostPrice_S));

        Commentary("gPriceMax_L="+Text(gPriceMax_L));
        Commentary("gPriceMin_S="+Text(gPriceMin_S));

        Commentary("--------------------------------");
        if(CurrentBar >= 30){
            Bool isRun = false;
            if(Run_Time == 0){
                //全天
                if(!(Hour == 14 && Minute >= 50) && !(Hour == 22 && Minute >= 50)){
                    isRun = true;
                }
            } else if(Run_Time == 1){
                //白盘
                if(!(Hour == 14 && Minute >= 50) && Hour >= 9 && Hour <= 15){
                    isRun = true;
                }
            } else if(Run_Time == 2){
                //夜盘
                if(!(Hour == 22 && Minute >= 50) && Hour >= 21 && Hour <= 23){
                    isRun = true;
                }
            }
            if(isRun){
                changeA();
                getPosition();
                if(!openPosition()){
                    clearPositionMoney();
                    clearPosition();
                    if(gOpType == 1){
                        //做多
                        calePosition_L();
                    } else if(gOpType == -1){
                        //做空
                        calePosition_S();
                    }
                }
                init();
            }
        }
        dayClear();
        if(Hour == 14 && Minute == 59){
            PlotString ("盈利","盈利="+Text(GrossProfit()+GrossLoss(), 0), close ,Yellow);
        }
        
        Commentary("gExitPL_Day_L="+Text(gExitPL_Day_L, 2));
        Commentary("gExitPL_Day_S="+Text(gExitPL_Day_S , 2));

    }

    //持仓更新事件函数,参数pos表示更新的持仓结构体
    OnPosition(PositionRef pos) {
        
    }

    //策略账户仓更新事件函数,参数pos表示更新的账户仓结构体
    OnStrategyPosition(PositionRef pos) {
        
    }

    //委托更新事件函数,参数ord表示更新的委托结构体
    OnOrder(OrderRef ord) {
        
    }

    //成交更新事件函数,参数ordFill表示更新的成交结构体
    OnFill(FillRef ordFill) {
        
    }

    //定时器更新事件函数,参数id表示定时器的编号,millsecs表示定时间的间隔毫秒值
    OnTimer(Integer id,Integer intervalMillsecs) {
        
    }

    //通用事件触发函数,参数evtName为事件名称,参数evtValue为事件内容
    OnEvent(StringRef evtName,MapRef<String,String> evtValue) {
        
    }

    //当前策略退出时触发
    OnExit() {

    }

kyover

你为什么会用global作为buy sell的判断条件?你确定清楚公式运行机制不会出问题?

 

你如果图表信号和具体持仓对不上,那就查信号的执行记录,消息中心,委托列表都可以查

2022-05-25 11:20
202****8113902175
@kyover

global 修饰的变量我用来维护当前持仓了。委托记录,消息中心都是显示挂单来源就是这个策略。

2022-05-25 11:25
您未登录,请先 登录注册 后发表评论
顶部