Additions

This commit is contained in:
2026-02-19 09:13:15 -05:00
parent 69c8736bf3
commit 6d966c4f28
12 changed files with 849 additions and 0 deletions

View File

@@ -0,0 +1,66 @@
using MarketData.Utils;
using System;
using System.Collections.Generic;
namespace MarketData.Generator.MGSHMomentum
{
public class MGSHPricingExceptions:List<MGSHPricingException>
{
public NVPCollections ToNVPCollections()
{
NVPCollections nvpCollections=new NVPCollections();
foreach(MGSHPricingException pricingException in this)
{
nvpCollections.Add(pricingException.ToNVPCollection());
}
return nvpCollections;
}
public static MGSHPricingExceptions FromNVPCollections(NVPCollections nvpCollections)
{
MGSHPricingExceptions pricingExcpetions=new MGSHPricingExceptions();
foreach(NVPCollection nvpCollection in nvpCollections)
{
pricingExcpetions.Add(MGSHPricingException.FromNVPCollection(nvpCollection));
}
return pricingExcpetions;
}
public void AddFromNVPCollection(NVPCollection nvpCollection)
{
Add(MGSHPricingException.FromNVPCollection(nvpCollection));
}
}
public class MGSHPricingException
{
public MGSHPricingException()
{
}
public MGSHPricingException(MGSHPricingException pricingException)
{
this.Symbol=pricingException.Symbol;
this.ExceptionCount=pricingException.ExceptionCount;
}
public MGSHPricingException(String symbol,int exceptionCount)
{
this.Symbol=symbol;
this.ExceptionCount=exceptionCount;
}
public String Symbol { get; set; }
public int ExceptionCount { get; set; }
public virtual NVPCollection ToNVPCollection()
{
NVPCollection nvpCollection=new NVPCollection();
nvpCollection.Add(new NVP("Symbol",Symbol.ToString()));
nvpCollection.Add(new NVP("ExceptionCount",ExceptionCount.ToString()));
return nvpCollection;
}
public static MGSHPricingException FromNVPCollection(NVPCollection nvpCollection)
{
MGSHPricingException pricingException=new MGSHPricingException();
NVPDictionary nvpDictionary=nvpCollection.ToDictionary();
pricingException.Symbol=nvpDictionary["Symbol"].Get<String>();
pricingException.ExceptionCount=nvpDictionary["ExceptionCount"].Get<int>();
return pricingException;
}
}
}

View File

@@ -0,0 +1,16 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MarketData.Generator.Model
{
public class RealtimeGainLoss
{
public double Exposure{get;set;}
public double MarketValue{get;set;}
public double GainLoss{get{return MarketValue-Exposure;}}
public double GainLossPercent{get{return Exposure==0?0:(MarketValue-Exposure)/Exposure;}}
}
}

View File

@@ -0,0 +1,125 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using System.Threading.Tasks;
using MarketData.MarketDataModel;
using MarketData.Utils;
using MarketData.DataAccess;
namespace MarketData.Generator
{
public class RSIGenerator
{
private RSIGenerator()
{
}
public static RSICollection GenerateRSI(String symbol,int priceCount,int rsiDayCount=14)
{
if(priceCount<rsiDayCount)priceCount=(rsiDayCount*2)+1;
Prices prices=PricingDA.GetPrices(symbol,priceCount);
if(null==prices||priceCount!=prices.Count)return null;
RSICollection rsiCollection=new RSICollection();
for(int index=prices.Count-1;index>=0;index--)
{
Price price=prices[index];
RSIElement rsiElement=new RSIElement();
rsiElement.Symbol=price.Symbol;
rsiElement.RSIDays=rsiDayCount;
rsiElement.Date=price.Date;
rsiElement.Close=price.Close;
if(index==prices.Count-1)continue;
rsiElement.Change=price.Close-prices[index+1].Close;
if(rsiElement.Change<0){rsiElement.Loss=Math.Abs(rsiElement.Change);rsiElement.Gain=0.00;}
else if(rsiElement.Change>0){rsiElement.Gain=rsiElement.Change;rsiElement.Loss=0.00;}
else{rsiElement.Loss=0.00;rsiElement.Gain=0.00;}
rsiCollection.Add(rsiElement);
}
RSICollection topCollection=rsiCollection.Top(rsiDayCount,1);
rsiCollection[rsiDayCount].AverageGain=topCollection.AverageGain();
rsiCollection[rsiDayCount].AverageLoss=topCollection.AverageLoss();
if(0.00==rsiCollection[rsiDayCount].AverageLoss)
{
rsiCollection[rsiDayCount].RS=0.00;
rsiCollection[rsiDayCount].RSI=100.00;
}
else
{
rsiCollection[rsiDayCount].RS=rsiCollection[rsiDayCount].AverageGain/rsiCollection[rsiDayCount].AverageLoss;
rsiCollection[rsiDayCount].RSI=100.00-(100.00/(1.00+rsiCollection[rsiDayCount].RS));
}
for(int index=rsiDayCount+1;index<rsiCollection.Count;index++)
{
rsiCollection[index].AverageGain=(rsiCollection[index-1].AverageGain*(rsiDayCount-1)+rsiCollection[index].Gain)/(double)rsiDayCount;
rsiCollection[index].AverageLoss=(rsiCollection[index-1].AverageLoss*(rsiDayCount-1)+rsiCollection[index].Loss)/(double)rsiDayCount;
if(0.00==rsiCollection[index].AverageLoss)
{
rsiCollection[index].RS=0.00;
rsiCollection[index].RSI=100.00;
}
else
{
rsiCollection[index].RS=rsiCollection[index].AverageGain/rsiCollection[index].AverageLoss;
rsiCollection[index].RSI=100.00-(100.00/(1.00+rsiCollection[index].RS));
}
}
rsiCollection=new RSICollection(rsiCollection.Skip(rsiDayCount).ToList());
return rsiCollection;
// return rsiCollection;
}
public static RSICollection GenerateRSI(String symbol,DateTime analysisDate,int priceCount,int rsiDayCount=14)
{
if(priceCount<rsiDayCount)priceCount=(rsiDayCount*2)+1;
Prices prices=PricingDA.GetPrices(symbol,analysisDate,priceCount);
if(null==prices||priceCount!=prices.Count)return null;
RSICollection rsiCollection=new RSICollection();
for(int index=prices.Count-1;index>=0;index--)
{
Price price=prices[index];
RSIElement rsiElement=new RSIElement();
rsiElement.Symbol=price.Symbol;
rsiElement.RSIDays=rsiDayCount;
rsiElement.Date=price.Date;
rsiElement.Close=price.Close;
if(index==prices.Count-1)continue;
rsiElement.Change=price.Close-prices[index+1].Close;
if(rsiElement.Change<0){rsiElement.Loss=Math.Abs(rsiElement.Change);rsiElement.Gain=0.00;}
else if(rsiElement.Change>0){rsiElement.Gain=rsiElement.Change;rsiElement.Loss=0.00;}
else{rsiElement.Loss=0.00;rsiElement.Gain=0.00;}
rsiCollection.Add(rsiElement);
}
RSICollection topCollection=rsiCollection.Top(rsiDayCount,1);
rsiCollection[rsiDayCount].AverageGain=topCollection.AverageGain();
rsiCollection[rsiDayCount].AverageLoss=topCollection.AverageLoss();
if(0.00==rsiCollection[rsiDayCount].AverageLoss)
{
rsiCollection[rsiDayCount].RS=0.00;
rsiCollection[rsiDayCount].RSI=100.00;
}
else
{
rsiCollection[rsiDayCount].RS=rsiCollection[rsiDayCount].AverageGain/rsiCollection[rsiDayCount].AverageLoss;
rsiCollection[rsiDayCount].RSI=100.00-(100.00/(1.00+rsiCollection[rsiDayCount].RS));
}
for(int index=rsiDayCount+1;index<rsiCollection.Count;index++)
{
rsiCollection[index].AverageGain=(rsiCollection[index-1].AverageGain*(rsiDayCount-1)+rsiCollection[index].Gain)/(double)rsiDayCount;
rsiCollection[index].AverageLoss=(rsiCollection[index-1].AverageLoss*(rsiDayCount-1)+rsiCollection[index].Loss)/(double)rsiDayCount;
if(0.00==rsiCollection[index].AverageLoss)
{
rsiCollection[index].RS=0.00;
rsiCollection[index].RSI=100.00;
}
else
{
rsiCollection[index].RS=rsiCollection[index].AverageGain/rsiCollection[index].AverageLoss;
rsiCollection[index].RSI=100.00-(100.00/(1.00+rsiCollection[index].RS));
}
}
rsiCollection=new RSICollection(rsiCollection.Skip(rsiDayCount).ToList());
return rsiCollection;
// return rsiCollection;
}
}
}