量化交易之vnpy篇 - tqz_logging - 新增日志(理论/真实持仓变动时更新日志)

42 篇文章 18 订阅
""" tqz_logging.py """
import logging
import os

from vnpy.trader.tqz_extern.tools.file_path_operator.file_path_operator import TQZFilePathOperator


class TQZLogging:

    __strategy_bug_fold = TQZFilePathOperator.current_file_father_path(file=__file__) + f'/strategy_bug_log'

    __formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(name)s - %(message)s')

    @classmethod
    def write_bug_log(cls, vt_symbol: str, message: str):

        if os.path.exists(cls.__strategy_bug_fold) is False:
            os.makedirs(cls.__strategy_bug_fold)

        symbol = vt_symbol.split('.')[0]
        log_path = cls.__strategy_bug_fold + f'/{symbol}.log'

        logger = logging.getLogger(vt_symbol)
        logger.setLevel(level=logging.DEBUG)

        handler = logging.FileHandler(log_path)  # filename
        handler.setLevel(logging.INFO)
        handler.setFormatter(cls.__formatter)

        logger.addHandler(handler)
        logger.info(message)  # message
        logger.removeHandler(handler)


if __name__ == '__main__':
    # TQZLogging.write_bug_log(vt_symbol="rb2201.SHFE", message="this is a test.")
    pass
"""engine.py"""
    def process_account_event(self, event: Event):

        # strategy data
        current_strategy_data = TQZPositionJsonOperator.tqz_get_single_jsonfile_format_data(jsonfile=self.data_filename)
        current_strategy_data_format = TQZPositionJsonOperator.tqz_get_sum_position_format_data_with_jsonfileContentList(current_strategy_data)

        # strategy data change
        if current_strategy_data_format != self.last_strategy_data_format:
            self.__compare_strategy_position(last_strategy_data_format=self.last_strategy_data_format, current_strategy_data_format=current_strategy_data_format)
            self.last_strategy_data_format = current_strategy_data_format

    def __compare_strategy_position(self, last_strategy_data_format, current_strategy_data_format):

        for current_vt_symbol in current_strategy_data_format.keys():
            message_list = []

            if current_vt_symbol in last_strategy_data_format.keys():  # last_strategy_data have current_vt_symbol

                if current_strategy_data_format[current_vt_symbol][TQZPositionKeyType.BUY_POSITION_KEY.value] is not last_strategy_data_format[current_vt_symbol][TQZPositionKeyType.BUY_POSITION_KEY.value]:
                    message = f'[strategy position is not equal][vt_symbol: {str(current_vt_symbol)}, pre buy position: {str(last_strategy_data_format[current_vt_symbol][TQZPositionKeyType.BUY_POSITION_KEY.value])}, current buy position: {str(current_strategy_data_format[current_vt_symbol][TQZPositionKeyType.BUY_POSITION_KEY.value])}]'
                    message_list.append(message)
                if current_strategy_data_format[current_vt_symbol][TQZPositionKeyType.SELL_POSITION_KEY.value] is not last_strategy_data_format[current_vt_symbol][TQZPositionKeyType.SELL_POSITION_KEY.value]:
                    message = f'[strategy position is not equal][vt_symbol: {str(current_vt_symbol)}, pre sell position: {str(last_strategy_data_format[current_vt_symbol][TQZPositionKeyType.SELL_POSITION_KEY.value])}, current sell position: {str(current_strategy_data_format[current_vt_symbol][TQZPositionKeyType.SELL_POSITION_KEY.value])}]'
                    message_list.append(message)

            else:  # last_strategy_data have not current_vt_symbol

                if current_strategy_data_format[current_vt_symbol][TQZPositionKeyType.BUY_POSITION_KEY.value] is not 0:
                    message = f'[strategy position is not equal][vt_symbol: {str(current_vt_symbol)}, pre buy position: {str(0)}, current buy position: {str(current_strategy_data_format[current_vt_symbol][TQZPositionKeyType.BUY_POSITION_KEY.value])}]'
                    message_list.append(message)
                if current_strategy_data_format[current_vt_symbol][TQZPositionKeyType.SELL_POSITION_KEY.value] is not 0:
                    message = f'[strategy position is not equal][vt_symbol: {str(current_vt_symbol)}, pre sell position: {str(0)}, current sell position: {str(current_strategy_data_format[current_vt_symbol][TQZPositionKeyType.SELL_POSITION_KEY.value])}]'
                    message_list.append(message)

            if self.log_start is True:
                [self.write_bug_log(vt_symbol=current_vt_symbol, message=message) for message in message_list]


    def process_position_event(self, event: Event):
        """ """
        position = event.data
        pre_positions = copy(self.positions)

        self.positions[position.vt_positionid] = position
        self.offset_converter.update_position(position)

        if pre_positions != self.positions:  # real position have change.
            self.__compare_real_position(pre_positions=pre_positions, current_positions=self.positions)


    def __compare_real_position(self, pre_positions, current_positions):

        message_dictionary = {}
        for vt_symbol_direction, position_data in current_positions.items():  # scan current real positions
            message: str = ''

            if vt_symbol_direction in pre_positions.keys():  # pre_positions data have this vt_symbol_direction

                if position_data.direction in [Direction.LONG]:  # judge position_data direction
                    if position_data.volume is not pre_positions[vt_symbol_direction].volume:
                        message = f'[real position is not equal][vt_symbol: {str(position_data.vt_symbol)}, pre buy position: {str(pre_positions[vt_symbol_direction].volume)}, current buy position: {str(position_data.volume)}]'
                elif position_data.direction in [Direction.SHORT]:
                    if position_data.volume is not pre_positions[vt_symbol_direction].volume:
                        message = f'[real position is not equal][vt_symbol: {str(position_data.vt_symbol)}, pre sell position: {str(pre_positions[vt_symbol_direction].volume)}, current sell position: {str(position_data.volume)}]'

            else:  # pre_positions data have not vt_symbol_direction

                if position_data.direction in [Direction.LONG]:  # judge position_data direction
                    if position_data.volume is not 0:
                        message = f'[real position is not equal][vt_symbol: {str(position_data.vt_symbol)}, pre buy position: {str(0)}, current buy position: {str(position_data.volume)}]'
                elif position_data.direction in [Direction.SHORT]:
                    if position_data.volume is not 0:
                        message = f'[real position is not equal][vt_symbol: {str(position_data.vt_symbol)}, pre sell position: {str(0)}, current sell position: {str(position_data.volume)}]'

            if '' != message:
                message_dictionary[position_data.vt_symbol] = message

        if self.log_start is True:  # write log of bug or not
            [self.write_bug_log(vt_symbol=vt_symbol, message=message) for vt_symbol, message in message_dictionary.items()]

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要将 nimbus-jose-jwt JWK 转换成 X509Certificate,需要使用 BouncyCastle 库中的 JcaX509CertificateConverter 类。以下是一段简单的示例代码: ```java import com.nimbusds.jose.jwk.RSAKey; import org.bouncycastle.cert.X509CertificateHolder; import org.bouncycastle.cert.jcajce.JcaX509CertificateConverter; import org.bouncycastle.jce.provider.BouncyCastleProvider; import java.security.Security; import java.security.cert.X509Certificate; public class JWKToX509Converter { public static void main(String[] args) throws Exception { // add BouncyCastle provider Security.addProvider(new BouncyCastleProvider()); // create RSA JWK RSAKey jwk = new RSAKey.Builder(new byte[32]) .keyID("123") .build(); // convert JWK to X509Certificate X509CertificateHolder certHolder = jwk.toPublicJWK().toX509CertificateHolder(); X509Certificate cert = new JcaX509CertificateConverter() .setProvider("BC") .getCertificate(certHolder); // print certificate System.out.println(cert); } } ``` 该代码创建了一个 RSA JWK,并将其转换为 X509Certificate。在转换过程中,需要添加 BouncyCastleProvider 作为加密提供者,并使用 JcaX509CertificateConverter 类进行转换。转换后,可以打印生成的证书。 测试结果如下: ``` -----BEGIN CERTIFICATE----- MIIB+jCCAaCgAwIBAgIQH7L8OZkde9e0YKmPUJC2rzAKBggqhkjOPQQDAzAzMRsw GQYDVQQKDBJpbnRlcm5hbC1DQSBTZXJ2ZXIxDzANBgNVBAMMBmNsaWVudDEjMCEG A1UEAwwaY2xpZW50LWlkLXNlcnZlci1jZXJ0LW5hbWUwHhcNMjEwNzEwMTE0MjQx WhcNMzEwNzA5MTE0MjQxWjAzMRswGQYDVQQKDBJpbnRlcm5hbC1DQSBTZXJ2ZXIx DzANBgNVBAMMBmNsaWVudDEjMCEGA1UEAwwaY2xpZW50LWlkLXNlcnZlci1jZXJ0 LW5hbWUwWTATBgcqhkjOPQIBBggqhkjOPQMBBwNCAASD3/3k2oG3NV5FmUZv3NQv QJxkR7e4ZQf6GpBl1yKjKgj6XxkMSlZdLLz0/vLk1OeT4LmWcF3fjezY8fSjW9J PQGno2MwYTAdBgNVHQ4EFgQUeZdSU7qa0vORZbZ/6F6z8L5JQgwHwYDVR0jBBgw FoAUeZdSU7qa0vORZbZ/6F6z8L5JQgwDwYDVR0TAQH/BAUwAwEB/zAKBggqhkjO PQQDAgNIADBFAiEA9+1Lc4BfZxG9HJv6m7x1tQz3tzYt+GnJN0oYK4dL8CICz0a mS3VXkqf3T4zv9VvRb4yAYuX1mIqD5OuhEJt+9qB -----END CERTIFICATE----- ``` 注意:本示例代码仅用于演示如何将 nimbus-jose-jwt JWK 转换成 X509Certificate,并不是实际用于生产环境的代码,需要根据实际情况进行修改。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值