| Overall Statistics |
|
Total Orders 9462 Average Win 0.10% Average Loss -0.05% Compounding Annual Return 12.431% Drawdown 17.000% Expectancy 0.040 Start Equity 1000000.00 End Equity 1092644.94 Net Profit 9.264% Sharpe Ratio 0.148 Sortino Ratio 0.207 Probabilistic Sharpe Ratio 38.127% Loss Rate 65% Win Rate 35% Profit-Loss Ratio 1.99 Alpha 0.015 Beta -0.014 Annual Standard Deviation 0.092 Annual Variance 0.009 Information Ratio -0.24 Tracking Error 0.205 Treynor Ratio -0.998 Total Fees $0.00 Estimated Strategy Capacity $680000.00 Lowest Capacity Asset BTCUSDT 18N Portfolio Turnover 3371.18% Drawdown Recovery 44 |
#region imports
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.Globalization;
using System.Linq;
using System.Text;
using Newtonsoft.Json;
using QuantConnect;
using QuantConnect.Algorithm;
using QuantConnect.Algorithm.Framework;
using QuantConnect.Algorithm.Framework.Alphas;
using QuantConnect.Algorithm.Framework.Execution;
using QuantConnect.Algorithm.Framework.Portfolio;
using QuantConnect.Algorithm.Framework.Portfolio.SignalExports;
using QuantConnect.Algorithm.Framework.Risk;
using QuantConnect.Algorithm.Framework.Selection;
using QuantConnect.Algorithm.Selection;
using QuantConnect.Api;
using QuantConnect.Benchmarks;
using QuantConnect.Brokerages;
using QuantConnect.Commands;
using QuantConnect.Configuration;
using QuantConnect.Data;
using QuantConnect.Data.Auxiliary;
using QuantConnect.Data.Consolidators;
using QuantConnect.Data.Custom;
using QuantConnect.Data.Custom.IconicTypes;
using QuantConnect.Data.Fundamental;
using QuantConnect.Data.Market;
using QuantConnect.Data.Shortable;
using QuantConnect.Data.UniverseSelection;
using QuantConnect.DataSource;
using QuantConnect.Indicators;
using QuantConnect.Interfaces;
using QuantConnect.Notifications;
using QuantConnect.Orders;
using QuantConnect.Orders.Fees;
using QuantConnect.Orders.Fills;
using QuantConnect.Orders.OptionExercise;
using QuantConnect.Orders.Slippage;
using QuantConnect.Orders.TimeInForces;
using QuantConnect.Parameters;
using QuantConnect.Python;
using QuantConnect.Scheduling;
using QuantConnect.Securities;
using QuantConnect.Securities.Crypto;
using QuantConnect.Securities.CryptoFuture;
using QuantConnect.Securities.Equity;
using QuantConnect.Securities.Forex;
using QuantConnect.Securities.Future;
using QuantConnect.Securities.IndexOption;
using QuantConnect.Securities.Interfaces;
using QuantConnect.Securities.Option;
using QuantConnect.Securities.Positions;
using QuantConnect.Securities.Volatility;
using QuantConnect.Statistics;
using QuantConnect.Storage;
using QuantConnect.Util;
using Calendar = QuantConnect.Data.Consolidators.Calendar;
using QCAlgorithmFramework = QuantConnect.Algorithm.QCAlgorithm;
using QCAlgorithmFrameworkBridge = QuantConnect.Algorithm.QCAlgorithm;
#endregion
namespace QuantConnect.Algorithm.CSharp
{
public class CryptoExampleStrategyPublic : QCAlgorithm
{
public static string ModelParamsFileName = "baseline_model_params.json";
public static string ThresholdArrayFileName = "baseline_threshold_array.json";
public class ModelParams
{
[JsonProperty("feature_cols")]
public string[] FeatureCols { get; set; }
[JsonProperty("coefficients")]
public decimal[] Coefficients { get; set; }
[JsonProperty("intercept")]
public decimal Intercept { get; set; }
[JsonProperty("center")]
public decimal[] Center { get; set; }
[JsonProperty("scale")]
public decimal[] Scale { get; set; }
}
// Ring buffer for prediction history
private class RingBuffer<T>
{
private T[] _buffer;
private DateTime[] _timestamps;
private int _size;
private int _currentIndex;
private int _count;
public RingBuffer(int size)
{
_size = size;
_buffer = new T[size];
_timestamps = new DateTime[size];
_currentIndex = 0;
_count = 0;
}
public void Add(DateTime timestamp, T item)
{
_timestamps[_currentIndex] = timestamp;
_buffer[_currentIndex] = item;
_currentIndex = (_currentIndex + 1) % _size;
if (_count < _size)
_count++;
}
public int Count => _count;
public T GetByIndex(int index)
{
if (index < 0 || index >= _count)
throw new IndexOutOfRangeException();
int actualIndex = (_currentIndex - _count + index + _size) % _size;
return _buffer[actualIndex];
}
public DateTime GetTimestampByIndex(int index)
{
if (index < 0 || index >= _count)
throw new IndexOutOfRangeException();
int actualIndex = (_currentIndex - _count + index + _size) % _size;
return _timestamps[actualIndex];
}
public T GetLatest()
{
if (_count == 0)
throw new InvalidOperationException("Buffer is empty");
int index = (_currentIndex - 1 + _size) % _size;
return _buffer[index];
}
public DateTime GetLatestTimestamp()
{
if (_count == 0)
throw new InvalidOperationException("Buffer is empty");
int index = (_currentIndex - 1 + _size) % _size;
return _timestamps[index];
}
public List<T> GetItems()
{
List<T> result = new List<T>(_count);
for (int i = 0; i < _count; i++)
{
int index = (_currentIndex - _count + i + _size) % _size;
result.Add(_buffer[index]);
}
return result;
}
public List<KeyValuePair<DateTime, T>> GetAllWithTimestamps()
{
List<KeyValuePair<DateTime, T>> result = new List<KeyValuePair<DateTime, T>>(
_count
);
for (int i = 0; i < _count; i++)
{
int index = (_currentIndex - _count + i + _size) % _size;
result.Add(new KeyValuePair<DateTime, T>(_timestamps[index], _buffer[index]));
}
return result;
}
public List<T> GetItemsInTimeRange(DateTime startTime, DateTime endTime)
{
List<T> result = new List<T>();
for (int i = 0; i < _count; i++)
{
int index = (_currentIndex - _count + i + _size) % _size;
if (_timestamps[index] >= startTime && _timestamps[index] <= endTime)
{
result.Add(_buffer[index]);
}
}
return result;
}
}
// Prediction record to track accuracy
private class PredictionRecord
{
public decimal Probability { get; set; }
public decimal EntryPrice { get; set; }
public bool? IsCorrect { get; set; } // null means not yet determined
}
private enum ModelState
{
Normal,
Reversed,
NotReliable,
}
private Symbol _btcusdt;
private ModelParams _modelParams;
private decimal[] _thresholdArr;
private bool _modelLoaded = false;
private decimal _positionSize = 0.98m;
private decimal _leverage = 1.0m;
private decimal _enterPositionThreshold = 0.02m;
private decimal _exitPositionThreshold = 0.73m;
private decimal _takeProfitTarget = 0.005m;
private decimal _stopLossLevel = 5m;
// Min accuracy threshold for normal operation
private decimal _normalThreshold = 0.46m; // TODO
// Max accuracy threshold for reversed operation
private decimal _reversedThreshold = 0.41m; // TODO
// Min number of predictions needed to evaluate accuracy
private int _minPredictionsForAccuracy = 30; // half in 30, half in 60, // TODO
private ModelState _currentModelState = ModelState.Normal;
private DateTime _positionEntryTime;
private bool _inLongPosition = false;
private bool _inShortPosition = false;
private decimal _entryPrice = 0m;
private int _positionHoldingWindow = 10;
private int _earlyProfitMinHoldingTime = 1;
private RingBuffer<decimal> _predictionHistory;
private RingBuffer<decimal> _priceHistory;
private RingBuffer<PredictionRecord> _predictionRecords; // Records for accuracy tracking
private int _maxPredictionHistory => 60 + _positionHoldingWindow + _earlyProfitMinHoldingTime; // TODO
private List<TradeRecord> _tradeRecords = new List<TradeRecord>();
private class TradeRecord
{
public DateTime EntryTime { get; set; }
public DateTime ExitTime { get; set; }
public decimal EntryPrice { get; set; }
public decimal ExitPrice { get; set; }
public string Direction { get; set; }
public decimal PnL { get; set; }
public string ExitReason { get; set; }
public ModelState ModelStateAtEntry { get; set; }
public decimal OriginalPrediction { get; set; }
public decimal AdjustedPrediction { get; set; }
}
public override void Initialize()
{
// SetStartDate(2023, 1, 1);
SetStartDate(2023, 7, 1);
// SetStartDate(2023, 10, 14);
// SetStartDate(2024, 8, 1);
// SetEndDate(2024, 6, 1);
// SetEndDate(2024, 9, 1);
// SetEndDate(2023, 10, 30);
SetEndDate(DateTime.Now);
// SetAccountCurrency("USDT", 1_000_000);
SetAccountCurrency("USD", 1_000_000);
// SetCash("USDT", 0);
SetBrokerageModel(new DefaultBrokerageModel());
// SetBrokerageModel(new BinanceBrokerageModel(AccountType.Margin));
SetTimeZone(TimeZones.Utc);
// We use 2x leverage for quantconnect live paper trading for the high sharpe ratio
if (LiveMode)
{
_positionSize = 0.98m;
_leverage = 2.0m;
}
var security = AddCrypto(
"BTCUSDT",
Resolution.Minute,
LiveMode ? null: Market.Binance,
fillForward: true,
leverage: _leverage
);
// security.SetFeeModel(new ConstantFeeModel(0.0m));
_btcusdt = security.Symbol;
_predictionHistory = new RingBuffer<decimal>(_maxPredictionHistory);
_priceHistory = new RingBuffer<decimal>(_maxPredictionHistory + _positionHoldingWindow + _earlyProfitMinHoldingTime);
_predictionRecords = new RingBuffer<PredictionRecord>(_maxPredictionHistory);
// Reload model every 00:00 UTC
// Schedule.On(
// DateRules.EveryDay("BTCUSDT"),
// TimeRules.At(new TimeSpan(00, 00, 00)),
// LoadModelParameters
// );
// Reset state machine at start of each day
// Schedule.On(
// DateRules.EveryDay("BTCUSDT"),
// TimeRules.At(00, 00, 01), // Just after midnight
// ResetStateMachine
// );
// Liquidate at the start of each day
// Schedule.On(
// DateRules.EveryDay("BTCUSDT"),
// TimeRules.At(00, 00, 05), // Just after midnight
// CheckAndLiquidateForNonTestDays
// );
// Schedule evaluation of past predictions
Schedule.On(
DateRules.EveryDay("BTCUSDT"),
TimeRules.Every(TimeSpan.FromMinutes(1)),
EvaluatePastPredictions
);
ResetStateMachine();
LoadModelParameters();
LoadThresholdArray();
}
private void ResetStateMachine()
{
if (_currentModelState != ModelState.Normal)
{
Log(
$"Resetting state machine. Previous state: {_currentModelState}"
);
}
_currentModelState = ModelState.Normal;
Log(
$"State machine reset for {Time.Date:yyyy-MM-dd}. Now in {_currentModelState} state."
);
}
private void EvaluatePastPredictions()
{
var predictions = _predictionRecords.GetAllWithTimestamps();
if (predictions.Count == 0) return;
foreach (var pair in predictions)
{
DateTime predictionTime = pair.Key;
PredictionRecord record = pair.Value;
if (record.IsCorrect.HasValue) continue;
DateTime evalStartTime = predictionTime.AddMinutes(_earlyProfitMinHoldingTime);
DateTime evalEndTime = predictionTime.AddMinutes(_earlyProfitMinHoldingTime + _positionHoldingWindow);
if (Time >= evalEndTime)
{
var pricesInWindow = _priceHistory.GetItemsInTimeRange(evalStartTime, evalEndTime);
if (pricesInWindow.Count > 0)
{
decimal avgPrice = pricesInWindow.Average();
bool priceWentUp = avgPrice > record.EntryPrice;
bool predictedUp = record.Probability > 0.5m;
record.IsCorrect = predictedUp == priceWentUp;
Log($"Evaluated prediction from {predictionTime}: predicted {(predictedUp ? "UP" : "DOWN")}, " +
$"actual {(priceWentUp ? "UP" : "DOWN")}, correct: {record.IsCorrect}");
}
else
{
Log($"Warning: No price data found for window {evalStartTime} to {evalEndTime}. Cannot evaluate prediction from {predictionTime}.");
}
}
}
UpdateModelState();
}
private void UpdateModelState()
{
var predictions = _predictionRecords.GetItems();
var evaluatedPredictions = predictions.Where(p => p.IsCorrect.HasValue).ToList();
if (evaluatedPredictions.Count < _minPredictionsForAccuracy)
{
Log($"Not enough evaluated predictions ({evaluatedPredictions.Count}/{_minPredictionsForAccuracy}) to determine accuracy");
return;
}
int correctCount = evaluatedPredictions.Count(p => p.IsCorrect.Value);
decimal accuracy = (decimal)correctCount / evaluatedPredictions.Count;
ModelState previousState = _currentModelState;
if (accuracy >= _normalThreshold)
{
_currentModelState = ModelState.Normal;
}
else if (accuracy <= _reversedThreshold)
{
_currentModelState = ModelState.Reversed;
}
else
{
_currentModelState = ModelState.NotReliable;
}
if (previousState != _currentModelState)
{
Log($"State transition: {previousState} -> {_currentModelState} based on prediction accuracy of {accuracy:P2} " +
$"(correct: {correctCount}/{evaluatedPredictions.Count})");
}
}
public override void OnData(Slice slice)
{
Log($"[OnData] - {Time} - Before Check {_btcusdt}, _modelLoaded {_modelLoaded}");
if (!slice.Bars.ContainsKey(_btcusdt) || !_modelLoaded)
return;
Log($"[OnData] - {Time} - After Check: {slice.Bars[_btcusdt]}");
var bar = slice.Bars[_btcusdt];
_priceHistory.Add(Time, bar.Close);
decimal[] features = CalculateFeatures(bar);
decimal originalPredictProb = PredictProbability(features);
decimal adjustedPredictProb = AdjustPredictionByState(originalPredictProb);
decimal percentile = GetProbabilityPercentile(adjustedPredictProb);
_predictionHistory.Add(Time, originalPredictProb);
_predictionRecords.Add(Time, new PredictionRecord
{
Probability = originalPredictProb,
EntryPrice = bar.Close,
IsCorrect = null // Will be evaluated later
});
string accuracyStr = "N/A";
var evaluatedPredictions = _predictionRecords.GetItems().Where(p => p.IsCorrect.HasValue).ToList();
if (evaluatedPredictions.Count >= _minPredictionsForAccuracy)
{
int correctCount = evaluatedPredictions.Count(p => p.IsCorrect.Value);
decimal accuracy = (decimal)correctCount / evaluatedPredictions.Count;
accuracyStr = $"{accuracy:P2} ({correctCount}/{evaluatedPredictions.Count})";
}
Log(
$"[OnData] - Time: {Time}, Price: {bar.Close}, Original Prediction: {originalPredictProb:F4}, "
+ $"Adjusted Prediction: {adjustedPredictProb:F4}, Percentile: {percentile:P2}, State: {_currentModelState}, "
+ $"Accuracy: {accuracyStr}"
);
bool shouldBeLong = percentile >= (1m - _enterPositionThreshold / 2m);
bool shouldBeShort = percentile <= (_enterPositionThreshold / 2m);
bool shouldExitLong = percentile <= (_exitPositionThreshold / 2m);
bool shouldExitShort = percentile >= (1m - _exitPositionThreshold / 2m);
// Don't take positions if model is NotReliable
if (_currentModelState == ModelState.NotReliable)
{
shouldBeLong = false;
shouldBeShort = false;
}
bool holdingTimeElapsed = false;
bool earlyProfitTimeElapsed = false;
decimal currentPnlPercent = 0m;
if (_inLongPosition || _inShortPosition)
{
TimeSpan holdingTime = Time - _positionEntryTime;
holdingTimeElapsed = holdingTime.TotalMinutes >= _positionHoldingWindow;
earlyProfitTimeElapsed = holdingTime.TotalMinutes >= _earlyProfitMinHoldingTime;
if (_inLongPosition)
{
currentPnlPercent = (bar.Close - _entryPrice) / _entryPrice * 100m;
}
else if (_inShortPosition)
{
currentPnlPercent = (_entryPrice - bar.Close) / _entryPrice * 100m;
}
if (holdingTimeElapsed)
{
Log($"Position holding window of {_positionHoldingWindow} minutes elapsed");
}
}
bool takeProfitTriggered =
earlyProfitTimeElapsed && currentPnlPercent >= _takeProfitTarget;
bool stopLossTriggered = currentPnlPercent <= -_stopLossLevel;
if (_inLongPosition)
{
// Exit if:
// 1. opposite signal
// 2. holding time elapsed
// 3. exit threshold reached
// 4. take profit target hit
// 5. stop loss triggered
if (
shouldBeShort
|| holdingTimeElapsed
|| shouldExitLong
|| takeProfitTriggered
|| stopLossTriggered
)
{
string reason =
shouldBeShort ? "Opposite signal"
: holdingTimeElapsed ? "Holding time elapsed"
: takeProfitTriggered ? $"Take profit target hit: {currentPnlPercent:F4}%"
: stopLossTriggered ? $"Stop loss triggered: {currentPnlPercent:F4}%"
: "Exit threshold reached";
ClosePosition(
"LONG",
bar.Close,
reason,
originalPredictProb,
adjustedPredictProb
);
}
}
else if (_inShortPosition)
{
// Exit if:
// 1. opposite signal
// 2. holding time elapsed
// 3. exit threshold reached
// 4. take profit target hit
// 5. stop loss triggered
if (
shouldBeLong
|| holdingTimeElapsed
|| shouldExitShort
|| takeProfitTriggered
|| stopLossTriggered
)
{
string reason =
shouldBeLong ? "Opposite signal"
: holdingTimeElapsed ? "Holding time elapsed"
: takeProfitTriggered ? $"Take profit target hit: {currentPnlPercent:F4}%"
: stopLossTriggered ? $"Stop loss triggered: {currentPnlPercent:F4}%"
: "Exit threshold reached";
ClosePosition(
"SHORT",
bar.Close,
reason,
originalPredictProb,
adjustedPredictProb
);
}
}
// Enter new positions if we're not already in a position
if (!_inLongPosition && !_inShortPosition)
{
if (shouldBeLong)
{
EnterLong(bar.Close, originalPredictProb, adjustedPredictProb);
}
else if (shouldBeShort)
{
EnterShort(bar.Close, originalPredictProb, adjustedPredictProb);
}
}
}
private decimal AdjustPredictionByState(decimal originalPrediction)
{
switch (_currentModelState)
{
case ModelState.Normal:
// No adjustment needed
return originalPrediction;
case ModelState.Reversed:
// Invert the prediction (1-p)
return 1m - originalPrediction;
case ModelState.NotReliable:
// Just return 0.5 (no clear signal)
return 0.5m;
default:
return originalPrediction;
}
}
private void EnterLong(
decimal price,
decimal originalPrediction,
decimal adjustedPrediction
)
{
SetHoldings(_btcusdt, _positionSize * _leverage);
_inLongPosition = true;
_inShortPosition = false;
_positionEntryTime = Time;
_entryPrice = price;
Log(
$"ENTERED LONG at {Time}, Price: {price}, Position Size: {_positionSize * _leverage}, Model State: {_currentModelState}"
);
var trade = new TradeRecord
{
EntryTime = Time,
EntryPrice = price,
Direction = "LONG",
ModelStateAtEntry = _currentModelState,
OriginalPrediction = originalPrediction,
AdjustedPrediction = adjustedPrediction,
};
_tradeRecords.Add(trade);
}
private void EnterShort(
decimal price,
decimal originalPrediction,
decimal adjustedPrediction
)
{
SetHoldings(_btcusdt, -_positionSize * _leverage);
_inShortPosition = true;
_inLongPosition = false;
_positionEntryTime = Time;
_entryPrice = price;
Log(
$"ENTERED SHORT at {Time}, Price: {price}, Position Size: {_positionSize * _leverage}, Model State: {_currentModelState}"
);
var trade = new TradeRecord
{
EntryTime = Time,
EntryPrice = price,
Direction = "SHORT",
ModelStateAtEntry = _currentModelState,
OriginalPrediction = originalPrediction,
AdjustedPrediction = adjustedPrediction,
};
_tradeRecords.Add(trade);
}
private void ClosePosition(
string positionType,
decimal price,
string reason,
decimal originalPrediction,
decimal adjustedPrediction
)
{
Liquidate(_btcusdt);
decimal pnl = 0;
if (positionType == "LONG")
{
pnl = (price - _entryPrice) / _entryPrice * 100;
_inLongPosition = false;
}
else
{
pnl = (_entryPrice - price) / _entryPrice * 100;
_inShortPosition = false;
}
Log(
$"EXITED {positionType} at {Time}, Price: {price}, PnL: {pnl:F4}%, Reason: {reason}, Model State: {_currentModelState}"
);
if (_tradeRecords.Count > 0)
{
var lastTrade = _tradeRecords[_tradeRecords.Count - 1];
lastTrade.ExitTime = Time;
lastTrade.ExitPrice = price;
lastTrade.PnL = pnl;
lastTrade.ExitReason = reason;
}
}
private decimal[] CalculateFeatures(TradeBar bar)
{
decimal[] features = new decimal[_modelParams.FeatureCols.Length];
int hour = Time.Hour;
int minute = Time.Minute;
decimal dayPct = (hour * 60 + minute) / (24m * 60m);
for (int i = 0; i < _modelParams.FeatureCols.Length; i++)
{
switch (_modelParams.FeatureCols[i])
{
case "close_open_ratio":
features[i] = bar.Close / bar.Open;
break;
case "high_low_ratio":
features[i] = bar.High / bar.Low;
break;
case "day_pct":
features[i] = dayPct;
break;
default:
Log($"Unknown feature: {_modelParams.FeatureCols[i]}");
features[i] = 0;
break;
}
}
return features;
}
/// <summary>
/// This method is written just for fun! Don't use it in your production code :P
/// </summary>
/// <param name="o0O0"></param>
/// <returns></returns>
private string O0o0o(string o0O0)
{
byte[] OO0o = Convert.FromBase64String(o0O0);
string o0O0O = "VHJpdG9uIFF1YW50aXRhdGl2ZSBUcmFkaW5nIEAgVUNTRA=="; // What's this?
byte[] O0o0 = Encoding.UTF8.GetBytes(o0O0O);
byte[] o00O = new byte[OO0o.Length];
for (int o = 0; o < OO0o.Length; o++)
{
byte O0 = OO0o[o];
byte o0 = O0o0[o % O0o0.Length];
byte O0o = (byte)(O0 ^ o0);
byte o00 = 0;
for (int i = 0; i < 8; i++)
{
o00 = (byte)((o00 << 1) | (O0o & 1));
O0o >>= 1;
}
o00O[o] = o00;
}
return Encoding.UTF8.GetString(o00O);
}
private void LoadModelParameters()
{
Log($"Model parameters file {ModelParamsFileName} not found.");
// NOTE: base64 is used for encoding string easier in C# code, I can simply use the direct base64 transformation, but the additional manipulation is for fun :)
string defaultModelJsonStr = O0o0o("8NYYxj6tW3lvXBQHQxtHXidK4P6W0S50yOUwmyWhNF17G6mAE6/tAZDjLFjUqyGrZIrI5uKlDwPPJNr/H5sTVqfuMGTirXCkaJHAwzmPJMd7m3c4b4cTQWSXzMLIDaELDP4Qfv4LD0MPMAxvywHD1ovGXoZWEe4+KAXYSxEzkHUTfa/wO2fDOaR/5Fhgg7/TZB7ILhYRo1k7EICvKwmvfNNywEQWEfb+KJGss1FHxMXTdZeAu2fzyyQD1FjgY6cxJGKMLtZl3+tTjKjXy+EDnke6OPb+CwbcfJHQOfGvSoVTAbfw+2eH+aR/3LrgH5dxJGrA/NZtszk70KjXy+FHBicuGF6i0f5eyDGMS1E7xKXzdZfA+2fDieQDzAoga48xJGKcfNYRo1k7hKjX65UZ3tN6+OQWbca+KHno2RFH7OcTfZ/iO28bQWSXzMKUDU8rEKrg5pbRezP7hGrf63W3LhN6hKQWZe7c6AXA2RFPgGcTAZfCO4evgeSXYrrgY7/x5B7g7hYRqyu7bJA96wmfbhMGtKb2Ze6c6HGUu5FHgPXTAafw+xPT+eQD3MrgF08bMML2zA==");
try
{
Log($"defaultModelJsonStr: {defaultModelJsonStr}");
string jsonModelStr = Encoding.UTF8.GetString(Convert.FromBase64String(defaultModelJsonStr));
_modelParams = JsonConvert.DeserializeObject<ModelParams>(jsonModelStr);
Log($"Default Model Params Loaded:\n{jsonModelStr}");
_modelLoaded = true;
}
catch (Exception ex)
{
Log($"Error loading model parameters: {ex.Message}");
Log($"Exception type: {ex.GetType().Name}");
if (ex.InnerException != null)
{
Log($"Inner exception: {ex.InnerException.Message}");
}
_modelLoaded = false;
}
return;
}
private void LoadThresholdArray()
{
Log($"Threshold array file {ThresholdArrayFileName} not found.");
string defaultThresholdArrayStr = O0o0o("vBbI3tZli2v7ZNSvK3WfHNMGlOSWbboeKA2UmVGvxIVTAYfwOxOXiSR/qJggH6+x5B74nhZl77vbZMSf6wmvPBMOtKTWEbqeKAXwWdEzgHUTwduI+4dtOaR35MrgF9sxpGrofBZlk3k7bMwda5W33lMO4MSWbc6+KHHIS5FPxIXTda+i+xunK2SXzLpgF7fTpGr4fNZt32v7GPTfKwGvrtMGtKb2Ze6cKHHg2RFH5PXTdZ+iO2fL+SQDzHggi9Pb5IpmfNYRu9k7ZMyv63XzLtN6wDaWbeYsaJHAu1E7zPUTfb/iu2fTy+QDmMqgY4+jJGLAvvZlu9s7ZNSdK3Xz/NMOyESWZe6+qHHQyxE77KXzdbfAO2fzOeQDqMqga7+x5BaMnhYRo+s7jKjX65UZPNMGyOQWbf5s6HHYy5FH5MfTdZ+AO9OvgeSXYljgH+vxJGLY3NZl75v7GNQv63WvrtPGvI7WhUA+KHHQmdFPzPUTAa9wOxOHieQDiBhgg7/TZB7AfBZtsys7bKAdK3XTvBNy+ETWZYpsaJHAu1E7zGeTdbfiO2/Ly+R/1BjgH7cRJGKcPFaFu5t7EMy963W/PBN6yOQWEeb+6A3ASxFHoKXzdbfAO2fr+SQDmFjgH6+xpGr4PNZlu+t7hMTfawG/vBN6lMQWZe6s6A2EmRFH5EcTA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QLmNjgF6fxJN6kltaFFdm7bJD9K32/rhN60OSWZbqe6Hngy3nn+tc=");
try
{
Log($"defaultThresholdArrayStr: {defaultThresholdArrayStr}");
string jsonThresholdArrayStr = Encoding.UTF8.GetString(Convert.FromBase64String(defaultThresholdArrayStr));
Log($"Default Threshold Array Loaded:\n{jsonThresholdArrayStr}");
_thresholdArr = JsonConvert.DeserializeObject<decimal[]>(jsonThresholdArrayStr);
}
catch (Exception ex)
{
Log($"Error loading ThresholdArrayStr: {ex.Message}");
Log($"Exception type: {ex.GetType().Name}");
if (ex.InnerException != null)
{
Log($"Inner exception: {ex.InnerException.Message}");
}
}
return;
// string jsonStr = ObjectStore.Read(ThresholdArrayFileName);
// try
// {
// _thresholdArr = JsonConvert.DeserializeObject<decimal[]>(jsonStr);
// var formattedJson = JsonConvert.SerializeObject(_thresholdArr, Formatting.None);
// Log($"Threshold array loaded with {_thresholdArr.Length} values. Array: {formattedJson}\nBase64: {Convert.ToBase64String(Encoding.UTF8.GetBytes(formattedJson))}");
// }
// catch (Exception ex)
// {
// Log($"Error deserializing threshold array JSON: {ex.Message}");
// InitializeDefaultThresholdArray();
// }
}
private void InitializeDefaultThresholdArray()
{
// Create a default threshold array with 200 points (0.5% resolution)
// Values will be distributed according to a Gaussian (Normal) distribution
int arraySize = 200;
_thresholdArr = new decimal[arraySize];
double mean = 0.5;
double stdDev = 0.15;
for (int i = 0; i < arraySize; i++)
{
double x = (double)i / (arraySize - 1);
// Apply sigmoid function to approximate Gaussian CDF
// This gives a reasonable S-shaped curve similar to the normal distribution CDF
double z = (x - mean) / stdDev;
double probability = 1.0 / (1.0 + Math.Exp(-z * 1.702));
_thresholdArr[i] = (decimal)probability;
}
Array.Sort(_thresholdArr);
Log(
$"Initialized default threshold array with {arraySize} Gaussian-distributed values."
);
}
private decimal PredictProbability(decimal[] features)
{
// sklearn RobustScaler equivalent
decimal[] scaledFeatures = new decimal[features.Length];
for (int i = 0; i < features.Length; i++)
{
scaledFeatures[i] = (features[i] - _modelParams.Center[i]) / _modelParams.Scale[i];
}
decimal logit = _modelParams.Intercept;
for (int i = 0; i < scaledFeatures.Length; i++)
{
logit += scaledFeatures[i] * _modelParams.Coefficients[i];
}
decimal prob = 1m / (1m + (decimal)Math.Exp(-(double)logit));
return prob;
}
private decimal GetProbabilityPercentile(decimal probability)
{
// If threshold array is not loaded, initialize it with default values
if (_thresholdArr == null || _thresholdArr.Length == 0)
{
InitializeDefaultThresholdArray();
}
int index = Array.BinarySearch(_thresholdArr, probability);
if (index >= 0)
{
return (decimal)index / (_thresholdArr.Length - 1);
}
else
{
// No direct match - get the insertion point
int insertPoint = ~index;
if (insertPoint == 0)
{
return 0m; // Probability is lower than all values in the array
}
else if (insertPoint >= _thresholdArr.Length)
{
return 1m; // Probability is higher than all values in the array
}
else
{
// Interpolate between the two closest points
decimal lowerProb = _thresholdArr[insertPoint - 1];
decimal upperProb = _thresholdArr[insertPoint];
decimal lowerPct = (decimal)(insertPoint - 1) / (_thresholdArr.Length - 1);
decimal upperPct = (decimal)insertPoint / (_thresholdArr.Length - 1);
// Linear interpolation
decimal ratio = (probability - lowerProb) / (upperProb - lowerProb);
return lowerPct + ratio * (upperPct - lowerPct);
}
}
}
}
}