代码分享: 帐户透视中的 "当日盈亏C" 字段的计算

当日盈亏C,这个字段不是通过结算价算出来的,是通过前一日的收盘价计算出来,系统没有直接的函数可以调用,得自己计算。自己研究了下写了个计算公式,当日盈亏C打印到控制台里,分享给大家,如有发现哪些不对的,指出来,我改进下(因为我要调用这个来实现达到当日盈亏值后就全平清仓,也怕写得有问题,发出来让大家一起用,一起检验哈)


Params
  
Vars

    Global Integer timer_1;  //定时器
    Global Numeric Profit_todayC_N;
    
Defs
    //判断当前是在交易日K线中,还是未开始新的交易日,返回1交易中,0未开始
    Integer is_ri_ye(string Symbol_N)
    {
        Tick ti;
        GetTick(Symbol_N, ti);
    
        // 在每个新的 bar 生成时,更新昨天的收盘价
        Numeric tmp = ti.dateTime - IntPart(ti.dateTime);
    
        if(tmp >= 0.1430 and tmp <= 0.1530 and SystemDateTime - ti.dateTime > 0.01)
        {
            //Commentary("新交易日未开始");
            Return 0;
        }
        Else
        {
            //Commentary("在交易日K线中间");
            Return 1;
        }
    }
    
    //计算单品种的当日盈亏C
    Numeric today_YingKui(string Symbol_N)  //参数:具体合约
    {
        //浮动盈亏(总的)- 昨天的浮动盈亏(非结算价)
        //= 浮动盈亏(总的) - ContractUnit * BigPointValue * 净手数 * (昨收价(不复权) - 成本价)
        Numeric today_Profit;  //当日盈亏C
        Numeric today_Profit_b;
        Numeric today_Profit_s;
    
        Tick ti;
        GetTick(Symbol_N, ti);
    
        Position pos;
        A_GetPosition(Symbol_N, pos); //指定合约的持仓属性
    
        Array<CodeProperty> props;
        GetProperty(Symbol_N, props);
    
        Numeric buy_lots = pos.longCloseVolume + pos.longCanSellVolume;  //多手数
        Numeric sell_lots = pos.shortCloseVolume + pos.shortCanCoverVolume;  //空手数
    
        if(is_ri_ye(Symbol_N) == 1)
        {
            if(buy_lots > 0) //多
            {
                today_Profit_b = pos.longFloatProfitO - props[0].ContractUnit * props[0].bigPointValue * buy_lots * (ti.preClose - pos.longAvgPriceO);
            }
    
            if(sell_lots > 0) //空
            {
                today_Profit_s = pos.shortFloatProfitO - props[0].ContractUnit * props[0].bigPointValue * sell_lots * (pos.shortAvgPriceO - ti.preClose);
            }
        }
        else if(is_ri_ye(Symbol_N) == 0)
        {
            today_Profit_b = 0;
            today_Profit_s = 0;
        }
    
        today_Profit = today_Profit_b + today_Profit_s;
        //Commentary("此品种的当日盈亏C: " + text(today_Profit));
    
        Return today_Profit;
    
    }
    
    //帐号的当日盈亏C
    Numeric Account_today_YK()
    {
        Array<String> symbols;
        //获取账户持仓的合约代码
        Bool ret = A_GetPositionSymbols(symbols);
    
        Integer j;
        Numeric Account_Today_YingKui;
    
        For j = 0 To GetArraySize(symbols) - 1
        {
            //Commentary(symbols[j] + " 当日盈亏C: " + Text(today_YingKui(Symbols[j])));
            Account_Today_YingKui = Account_Today_YingKui + today_YingKui(symbols[j]);
        }
    
        Return Account_Today_YingKui;
    }
    
Events
    OnInit()
    {
        timer_1 = createTimer(5000); //5秒执行1次
    }
    
    OnTimer(Integer id, Integer millsecs)
    {
        if(id == timer_1)
        {
            Profit_todayC_N = Account_today_YK();
            print(text(round(SystemDateTime, 6)) + " 当日盈亏C: " + Text(Profit_todayC_N));
        }
    }


后来试了下,在盘中有开平操作的话,会变得非常复杂,推算不出来了,算了,放弃

👍

研究很久,还真算不出来,官方能不能升级这个函数调用呢?

大家有什么更好的实现方法,也欢迎指出,我会优化升级这个代码

欢迎分享

研究很久,还真算不出来,官方能不能升级这个函数调用呢?