Overall Statistics
Total Orders
0
Average Win
0%
Average Loss
0%
Compounding Annual Return
0%
Drawdown
0%
Expectancy
0
Start Equity
100000
End Equity
100000
Net Profit
0%
Sharpe Ratio
0
Sortino Ratio
0
Probabilistic Sharpe Ratio
0%
Loss Rate
0%
Win Rate
0%
Profit-Loss Ratio
0
Alpha
0
Beta
0
Annual Standard Deviation
0
Annual Variance
0
Information Ratio
-0.446
Tracking Error
0.16
Treynor Ratio
0
Total Fees
$0.00
Estimated Strategy Capacity
$0
Lowest Capacity Asset
Portfolio Turnover
0%
#region imports
from AlgorithmImports import *
#endregion
SUFFIXES = ["*.html", "*.php"]
# enums or static class, only CAPITALIZE if in form of <enum>.<member>{space or comma character}
ENUMS = [
    'AlphaModelExtensions', 'PortfolioBias', 'with', 'StressSymbols', 'Symbols', 'Equity', 'NotifiedSecurityChanges', 'State', 'State', 
    'ReturnsSymbolDataExtensions', 'DebuggingMethod', 'DebuggerHelper', 'SeriesType', 'BinaryComparisonExtensions', 'ChartType', 'Currencies', 
    'Expiry', 'value', 'Extensions', 'BrokerageEnvironment', 'Language', 'ServerType', 'SecurityType', 'AccountType', 'MarketDataType', 
    'DataFeedEndpoint', 'StoragePermissions', 'TickType', 'DelistingType', 'SplitType', 'Resolution', 'PositionSide', 'OptionRight', 'OptionStyle', 
    'SettlementType', 'AlgorithmStatus', 'SubscriptionTransportMedium', 'WritePolicy', 'Period', 'DataNormalizationMode', 'DataMappingMode', 
    'CashBookUpdateType', 'DeploymentTarget', 'AlgorithmMode', 'DateFormat', 'Exchanges', 'ChannelStatus', 'Globals', 'IsolatorLimitResultProvider',
    'Market', 'OS', 'Parse', 'ScatterMarkerSymbol', 'StringExtensions', 'SymbolCache', 'SymbolRepresentation', 'Time', 'TimeZones', 'TradingDayType', 
    'InsightDirection', 'InsightScoreType', 'InsightType', 'CompileState', 'NodeType', 'OptimizationNodes', 'ProductType', 'BrokerageMessageType', 
    'BrokerageName', 'BrokerageModel', 'FileFormat', 'HistoryExtensions', 'RiskFreeInterestRateModelExtensions', 'SliceExtensions', 
    'SubscriptionDataConfigExtensions', 'FactorFileZipHelper', 'MapFileZipHelper', 'MappingExtensions', 'PriceScalingExtensions', 'Calendar', 
    'CalendarType', 'FilteredIdentityDataConsolidator', 'FxcmSymbolId', 'PlaceHolder', 'IntrinioEconomicDataSources', 'BofAMerrillLynch', 'CBOE', 
    'Commodities', 'ExchangeRates', 'Moodys', 'TradeWeightedUsDollarIndex', 'IntrinioConfig', 'IntrinioDataTransformation', 'Tiingo', 
    'TiingoSymbolMapper', 'StockType', 'StyleBox', 'MorningstarEconomySphereCode', 'MorningstarSectorCode', 'MorningstarIndustryGroupCode', 
    'MorningstarIndustryCode', 'FundamentalProperty', 'Period', 'PeriodAsByte', 'BarDirection', 'DataDictionaryExtensions', 'RenkoType', 
    'FundamentalService', 'UniverseExtensions', 'InsightManager', 'ReadOnlySecurityValuesCollection', 'Insight', 'InsightScore', 'PortfolioTarget', 
    'FuncBenchmark', 'DefaultBrokerageModel', 'AlphaStreamsBrokerageModel', 'AxosBrokerageModel', 'BinanceBrokerageModel', 'BinanceUSBrokerageModel', 
    'BrokerageMessageEvent', 'DefaultBrokerageMessageHandler', 'ExanteBrokerageModel', 'FTXBrokerageModel', 'FxcmBrokerageModel', 
    'CoinbaseBrokerageModel', 'InteractiveBrokersBrokerageModel', 'TradierBrokerageModel', 'TradingTechnologiesBrokerageModel', 
    'WolverineBrokerageModel', 'RBIBrokerageModel', 'BaseCommand', 'BaseCommandHandler', 'FileCommandHandler', 'OrderCommand', 
    'DllNotFoundPythonExceptionInterpreter', 'InvalidTokenPythonExceptionInterpreter', 'KeyErrorPythonExceptionInterpreter', 
    'NoMethodMatchPythonExceptionInterpreter', 'ScheduledEventExceptionInterpreter', 'StackExceptionInterpreter', 
    'UnsupportedOperandPythonExceptionInterpreter', 'IndicatorDataPoint', 'RollingWindow', 'NotificationEmail', 'NotificationJsonConverter', 
    'OptimizerObjectivesCommon', 'Constraint', 'ExtremumJsonConverter', 'Objective', 'Target', 'OptimizationParameterJsonConverter', 
    'OptimizationStepParameter', 'CancelOrderRequest', 'GroupOrderExtensions', 'LimitIfTouchedOrder', 'LimitOrder', 'Order', 'OrderEvent', 
    'OrderRequest', 'OrderResponse', 'OrderTicket', 'StopLimitOrder', 'StopMarketOrder', 'TrailingStopOrder', 'SubmitOrderRequest', 
    'UpdateOrderRequest', 'FeeModel', 'AlphaStreamsFeeModel', 'ExanteFeeModel', 'InteractiveBrokersFeeModel', 'TDAmeritradeFeeModel', 'FillModel', 
    'EquityFillModel', 'DefaultExerciseModel', 'VolumeShareSlippageModel', 'PythonCommon', 'MarginCallModelPythonWrapper', 'PandasConverter', 
    'PandasData', 'PythonInitializer', 'PythonWrapper', 'AlphaRuntimeStatistics', 'Chart', 'ChartPoint', 'Candlestick', 'Currencies', 
    'ExtendedDictionary', 'Extensions', 'Holding', 'AlgorithmControl', 'Isolator', 'Market', 'OS', 'Parse', 'SecurityIdentifier', 'StringExtensions', 
    'Symbol', 'SymbolCache', 'SymbolRepresentation', 'SymbolValueJsonConverter', 'Time', 'TradingCalendar', 'AccountEvent', 'BuyingPowerModel', 
    'PositionGroupBuyingPowerModel', 'Cash', 'CashBook', 'CashBuyingPowerModel', 'DefaultMarginCallModel', 'DynamicSecurityData', 
    'EquityPriceVariationModel', 'ErrorCurrencyConverter', 'FuncSecuritySeeder', 'IdentityCurrencyConverter', 'InitialMarginParameters', 
    'LocalMarketHours', 'MaintenanceMarginParameters', 'MarketHoursDatabase', 'MarketHoursSegment', 'RegisteredSecurityDataTypesProvider', 
    'Security', 'SecurityDatabaseKey', 'SecurityDefinitionSymbolResolver', 'SecurityExchangeHours', 'SecurityHolding', 'SecurityManager', 
    'SecurityPortfolioManager', 'SecurityService', 'SecurityTransactionManager', 'SymbolProperties', 'SymbolPropertiesDatabase', 'PositionGroup', 
    'OptimizationStatus', 'GroupOrderExtensions', 'IndiaProductType', 'OrderError', 'OrderExtensions', 'OrderField', 'OrderRequestStatus', 
    'OrderRequestType', 'OrderResponseErrorCode', 'OrderSizing', 'OrderType', 'OrderDirection', 'OrderPosition', 'OrderStatus', 'FeeModelExtensions', 
    'HistoryResultType', 'PacketType', 'PythonInitializer', 'PythonWrapper', 'BuyingPowerModelExtensions', 'MarginCallModel', 'MarginInterestRateModel',
    'OrderProviderExtensions', 'SecurityInitializer', 'SecurityProviderExtensions', 'SecuritySeeder', 'MarketHoursState', 'FutureExpirationCycles', 
    'FutureFilterUniverseEx', 'Futures', 'Grains', 'Currencies', 'Energies', 'Financials', 'Indices', 'Forestry', 'Meats', 'Metals', 'Softs', 'Dairy', 
    'FuturesExpiryUtilityFunctions', 'FuturesListings', 'FutureSymbol', 'FutureOptionSymbol', 'FuturesOptionsExpiryFunctions', 
    'FuturesOptionsSymbolMappings', 'FuturesOptionsUnderlyingMapper', 'IndexSymbol', 'IndexOptionSymbol', 'AdjustmentType', 'OptionFilterUniverseEx', 
    'OptionPriceModels', 'OptionStrategies', 'OptionSymbol', 'ConstantOptionStrategyLegReferenceValue', 'OptionStrategyDefinitions', 
    'PredicateTargetValue', 'PortfolioMarginChart', 'PositionExtensions', 'PositionGroupBuyingPowerModelExtensions', 'PositionGroupExtensions', 
    'VolatilityModel', 'VolatilityModelExtensions', 'PerformanceMetrics', 'StatisticsBuilder', 'TradeDirection', 'FillGroupingMethod', 
    'FillMatchingMethod', 'ComparisonOperator', 'ComparisonOperatorTypes', 'Match', 'CurrencyPairUtil', 'DisposableExtensions', 'EnumeratorExtensions',
    'ExpressionBuilder', 'LeanData', 'LinqExtensions', 'ObjectActivator', 'OptionPayoff', 'ReaderWriterLockSlimExtensions', 'Ref',
    'SecurityExtensions', 'StreamReaderExtensions', 'Validate', 'RegularExpression', 'XElementExtensions', 'TokenBucket', 'Compression', 
    'ApplicationParser', 'Config', 'LeanArgumentParser', 'OptimizerArgumentParser', 'ReportArgumentParser', 'ToolboxArgumentParser', 'Initializer', 
    'FillForwardResolutionOperation', 'SubscriptionDataSourceReader', 'SubscriptionUtils', 'CorporateEventEnumeratorFactory', 'ScheduledEventFactory',
    'BaseSetupHandler', 'CorrelationType', 'IndicatorExtensions', 'IndicatorStatus', 'MovingAverageType', 'MovingAverageTypeExtensions', 
    'OptionPricingModelType', 'PivotPointType', 'SwissArmyKnifeTool', 'CandleSettingType', 'CandleRangeType', 'CandleColor', 'CandleSettings', 
    'LogHandlerExtensions', 'Log', 'LogType', 'WhoCalledMe', 'CrisisEvent', 'DeedleUtil', 'Metrics', 'ReportKey', 'ResultsUtil', 'Rolling', 
    'AlgorithmRunner', 'SymbolsKey', 'Symbols', 'TestExtensions', 'TestGlobals', 'TestProcess', 'SecuritySeedData', 'Option', 
    'PerformanceBenchmarkAlgorithms', 'TestHelper', 'PythonTestingUtils', 'PythonWrapperTests', 'DataProcessor', 'StreamProvider', 
    'TemporaryPathProvider', 'AlgoSeekFuturesProgram', 'CryptoiqDownloaderProgram', 'DukascopyDownloaderProgram', 'IVolatilityEquityConverterProgram', 
    'KaikoDataConverterProgram', 'KrakenDownloaderProgram', 'NseMarketDataConverterProgram', 'DataDensity', 'RandomDataGeneratorProgram', 
    'YahooDownloaderProgram','TimeInForce','Universe','CryptoUniverse'
]
SWAPS = {
    "ETF": "Etf",
    "FIGI": "Figi",
    "ID": "Id",
    "BinanceUS": "BinanceUs",
    "PERatio": "PeRatio",
    "CrankNicolsonFD": "CrankNicolsonFd",
    "OECDRecessionIndicators": "OecdRecessionIndicators",
    "QuantConnectBrokerage": "QuantconnectBrokerage",
    "TDAmeritrade": "TdAmeritrade",
    "VWAP": "Vwap",
    "OneWeekBasedOnUSD": "OneWeekBasedOnUsd",
    "SP500EMini": "SP500E_MINI",
    "CrudeOilWTI": "CRUDE_OIL_WTI",
    "Gold": "GOLD",
}
EXCEPTIONS = [
    "ElementTree",
    "SMA",
    "EMA",
    "QuantConnect.Data",
    "QuantConnect.Data.UniverseSelection",
    "QuantConnect.DataSource",
    "QuantConnect.Python",
    "QuantConnect.symbol",
    "GaussianHMM",
    "ht_auth.SetToken",
    "nn.Module",
    "nn.Sequential",
    "nn.Linear",
    "nn.Flatten",
    "nn.ReLU",
    "nn.MSELoss",
    "gym.Env",
    "gym.spaces.Discrete",
    "gym.spaces.Box",
    "tf.Session",
    "tf.Variable",
    "tf.train.AdamOptimizer",
    "json_format.MessageToJson",
    "json_format.Parse",
    "tf.MetaGraphDef",
    "xgb.DMatrix",
    "Selection.OptionUniverseSelectionModel",
    "Selection.FutureUniverseSelectionModel",
    "pd.DataFrame",
    "pd.Series",
    "pd.MultiIndex",
    "opt.TargetWeights",
    "opt.MaxGrossExposure",
    "opt.DollarNeutral",
    "go.Candlestick",
    "go.Layout",
    "go.layout.Title",
    "go.Figure",
    "go.Scatter",
    "go.scatter.Marker",
    "Newtonsoft.Json",
    "System.Collections.Generic",
    "JsonConvert.SerializeObject",
    "Calculators.TaxesCalculator"
]
# region imports
from AlgorithmImports import *
# endregion

class CalculatingTanRat(QCAlgorithm):
    def Initialize(self):
        pass

import re
from exceptions import ENUMS, SWAPS, EXCEPTIONS

def get_code_snippet(pre_content):
    content = str(pre_content).split('>', maxsplit=1)[1].rsplit("</pre>", 1)[0]
    return content

def conversion(content):
    # do not check for the string in EXCEPTIONS list
    content_to_check = content
    for e in EXCEPTIONS:
        content_to_check = content_to_check.replace(e, "")

    methods = re.findall(r"def\s+(\w+)\s*\(", content_to_check)

    for method in sorted(set(methods), key=len, reverse=True):
        snake_case_method = _title_to_snake_case(method)
        content = content.replace(f"def {method}(", f"def {snake_case_method}(")
    
    methods = re.findall(r"\.(\w+)", content_to_check)

    for method in sorted(set(methods), key=len, reverse=True):
        snake_case_method = _title_to_snake_case(method)
        content = content.replace(f".{method}", f".{snake_case_method}")
    
    # Named arguments. E.g.: mappingResolveDate=datetime(2021, 12, 1)
    methods = [x for x in re.findall(r"\(([^)]+)\)", content_to_check) if '=' in x]
    for method in sorted(set(methods), key=len, reverse=True):
        for arg in re.findall(r"(\w+)=", method.replace(' ','')):
            snake_case_args = _title_to_snake_case(arg)
            content = content.replace(arg, snake_case_args)

    # Local variables: E.g.: sortedByDollarVolume
    methods = re.findall(r"\    (\w+) =", content_to_check)
    for method in sorted(set(methods), key=len, reverse=True):
        snake_case_method = _title_to_snake_case(method)
        content = content.replace(method, snake_case_method)
    
    methods = re.findall(r"<\?=\$research \? \"qb\.\" : \"self\.\"\?>(\w+)", content_to_check)

    for method in sorted(set(methods), key=len, reverse=True):
        snake_case_method = _title_to_snake_case(method)
        content = content.replace(f"<?=$research ? \"qb.\" : \"self.\"?>{method}", f"<?=$research ? \"qb.\" : \"self.\"?>{snake_case_method}")
    
    methods = re.findall(r"<\?=\$pythonPrefix\?>(\w+)", content_to_check)

    for method in sorted(set(methods), key=len, reverse=True):
        snake_case_method = _title_to_snake_case(method)
        content = content.replace(f"<?=$pythonPrefix?>{method}", f"<?=$pythonPrefix?>{snake_case_method}")
        
    for enum in sorted(ENUMS, key=len, reverse=True):
        wanted_pattern = fr"(?![\.]){enum}\.(\w+)(?!\(|\.)"
        unwanted_pattern = fr"(?![\.]){enum}\.(\w+)[\(\.]"
        wanted_methods = re.findall(wanted_pattern, f"{content}<")
        unwanted_methods = [x[:-1] for x in re.findall(unwanted_pattern, f"{content}<")]
        
        for method in sorted(set(wanted_methods).difference(set(unwanted_methods)), key=len, reverse=True):
            content = content.replace(f"{enum}.{method}", f"{enum}.{method.upper()}")
    
    return content

def _title_to_snake_case(title):
    for original, new in SWAPS.items():
        if original in title:
            title = title.replace(original, new)
        
    # for whole method is upper case: indicators, ID, ...
    if title.isupper():
        return title
    
    snake_case = re.sub(r'(?<!^)(?=[A-Z])', '_', title).lower()
    return snake_case