Trading bot grain (#33)
* Trading bot Grain * Fix a bit more of the trading bot * Advance on the tradingbot grain * Fix build * Fix db script * Fix user login * Fix a bit backtest * Fix cooldown and backtest * start fixing bot start * Fix startup * Setup local db * Fix build and update candles and scenario * Add bot registry * Add reminder * Updateing the grains * fix bootstraping * Save stats on tick * Save bot data every tick * Fix serialization * fix save bot stats * Fix get candles * use dict instead of list for position * Switch hashset to dict * Fix a bit * Fix bot launch and bot view * add migrations * Remove the tolist * Add agent grain * Save agent summary * clean * Add save bot * Update get bots * Add get bots * Fix stop/restart * fix Update config * Update scanner table on new backtest saved * Fix backtestRowDetails.tsx * Fix agentIndex * Update agentIndex * Fix more things * Update user cache * Fix * Fix account load/start/restart/run
This commit is contained in:
137
src/Managing.Domain/Indicators/Context/StDevContext.cs
Normal file
137
src/Managing.Domain/Indicators/Context/StDevContext.cs
Normal file
@@ -0,0 +1,137 @@
|
||||
using Managing.Core;
|
||||
using Managing.Domain.Candles;
|
||||
using Managing.Domain.Indicators;
|
||||
using Managing.Domain.Shared.Rules;
|
||||
using Managing.Domain.Strategies.Base;
|
||||
using Skender.Stock.Indicators;
|
||||
using static Managing.Common.Enums;
|
||||
|
||||
namespace Managing.Domain.Strategies.Context;
|
||||
|
||||
public class StDevContext : IndicatorBase
|
||||
{
|
||||
public List<LightSignal> Signals { get; set; }
|
||||
|
||||
public StDevContext(string name, int period) : base(name, IndicatorType.StDev)
|
||||
{
|
||||
Signals = new List<LightSignal>();
|
||||
Period = period;
|
||||
}
|
||||
|
||||
public override List<LightSignal> Run(HashSet<Candle> candles)
|
||||
{
|
||||
if (candles.Count <= Period)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
var stDev = candles.GetStdDev(Period.Value).ToList();
|
||||
var stDevCandles = MapStDev(stDev, candles.TakeLast(Period.Value));
|
||||
|
||||
if (stDev.Count == 0)
|
||||
return null;
|
||||
|
||||
var lastCandle = stDevCandles.Last();
|
||||
var zScore = lastCandle.ZScore ?? 0;
|
||||
|
||||
// Determine confidence based on Z-score ranges
|
||||
// Lower absolute Z-score = more normal volatility = higher confidence for trading
|
||||
// Higher absolute Z-score = more extreme volatility = lower confidence for trading
|
||||
Confidence confidence;
|
||||
|
||||
if (Math.Abs(zScore) <= 0.5)
|
||||
{
|
||||
// Very low volatility - ideal conditions for trading
|
||||
confidence = Confidence.High;
|
||||
}
|
||||
else if (Math.Abs(zScore) <= 1.0)
|
||||
{
|
||||
// Normal volatility - good conditions for trading
|
||||
confidence = Confidence.Medium;
|
||||
}
|
||||
else if (Math.Abs(zScore) <= 1.5)
|
||||
{
|
||||
// Elevated volatility - caution advised
|
||||
confidence = Confidence.Low;
|
||||
}
|
||||
else
|
||||
{
|
||||
// High volatility - trading not recommended
|
||||
confidence = Confidence.None;
|
||||
}
|
||||
|
||||
// Context strategies always return TradeDirection.None
|
||||
// The confidence level indicates the quality of market conditions
|
||||
AddSignal(lastCandle, TradeDirection.None, confidence);
|
||||
|
||||
return Signals.Where(s => s.Confidence != Confidence.None).OrderBy(s => s.Date).ToList();
|
||||
}
|
||||
catch (RuleException)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public override IndicatorsResultBase GetIndicatorValues(HashSet<Candle> candles)
|
||||
{
|
||||
var test = new IndicatorsResultBase()
|
||||
{
|
||||
StdDev = candles.GetStdDev(Period.Value).ToList()
|
||||
};
|
||||
|
||||
return test;
|
||||
}
|
||||
|
||||
private List<CandleStDev> MapStDev(List<StdDevResult> stDev, IEnumerable<Candle> candles)
|
||||
{
|
||||
var sctList = new List<CandleStDev>();
|
||||
foreach (var candle in candles)
|
||||
{
|
||||
var currentSct = stDev.Find(candle.Date);
|
||||
if (currentSct != null)
|
||||
{
|
||||
sctList.Add(new CandleStDev()
|
||||
{
|
||||
Close = candle.Close,
|
||||
Open = candle.Open,
|
||||
Date = candle.Date,
|
||||
Ticker = candle.Ticker,
|
||||
Exchange = candle.Exchange,
|
||||
StDev = currentSct.StdDev,
|
||||
ZScore = currentSct.ZScore,
|
||||
StdDevSma = currentSct.StdDevSma,
|
||||
Mean = currentSct.Mean
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
return sctList;
|
||||
}
|
||||
|
||||
private void AddSignal(CandleStDev candleSignal, TradeDirection direction,
|
||||
Confidence confidence)
|
||||
{
|
||||
var signal = new LightSignal(
|
||||
MiscExtensions.ParseEnum<Ticker>(candleSignal.Ticker),
|
||||
direction,
|
||||
confidence,
|
||||
candleSignal,
|
||||
candleSignal.Date,
|
||||
candleSignal.Exchange,
|
||||
Type, SignalType, Name);
|
||||
if (!Signals.Any(s => s.Identifier == signal.Identifier))
|
||||
{
|
||||
Signals.AddItem(signal);
|
||||
}
|
||||
}
|
||||
|
||||
private class CandleStDev : Candle
|
||||
{
|
||||
public double? StDev { get; internal set; }
|
||||
public double? ZScore { get; internal set; }
|
||||
public double? StdDevSma { get; internal set; }
|
||||
public double? Mean { get; internal set; }
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user