116 lines
3.9 KiB
C#
116 lines
3.9 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using YLErp.Abstract.DataProviders;
|
|
using YLErp.Modules.DataProviderModule;
|
|
|
|
namespace YLErp.BLL.MarginCalculation.DongWu
|
|
{
|
|
public class PriceFactory : IPriceFactory
|
|
{
|
|
private IUpDownLimitProvider upDownLimitProvider;
|
|
private InnerUpDownLimit innerUpDownLimit;
|
|
private DateTime settleDate;
|
|
private double? realSettlePricePrice;
|
|
private string underlyingCode;
|
|
|
|
private PriceFactory() { }
|
|
public PriceFactory(DateTime settleDate, double? realSettlePricePrice, string underlyingCode, IUpDownLimitProvider upDownLimitProvider)
|
|
{
|
|
this.settleDate = settleDate;
|
|
this.realSettlePricePrice = realSettlePricePrice;
|
|
this.underlyingCode = underlyingCode;
|
|
this.upDownLimitProvider = upDownLimitProvider;
|
|
this.innerUpDownLimit = upDownLimitProvider.GetUpDownLimit();
|
|
}
|
|
|
|
public PriceProviderWrap Generate(int priceCount, string preName)
|
|
{
|
|
var settlementDayEodPrice = EodPriceQueryService.GetEodPrice(settleDate, underlyingCode);
|
|
var settlePrice = realSettlePricePrice ?? (settlementDayEodPrice?.SettlePrice??0);
|
|
var prices = GeneratePrices(priceCount, settlePrice);
|
|
PriceProviderWrap priceProviderWrap = new PriceProviderWrap()
|
|
{
|
|
Name = preName,
|
|
SettlePrice= settlePrice,
|
|
UpLimitRate= innerUpDownLimit.UplimitRate,
|
|
DownLimitRate= innerUpDownLimit.DownLimitRate,
|
|
IsCover= innerUpDownLimit.IsCover,
|
|
IsTouch= innerUpDownLimit.IsTouch,
|
|
priceProviders=new Dictionary<string, IPriceProvider>(),
|
|
};
|
|
int index = 1;
|
|
foreach (var price in prices)
|
|
{
|
|
priceProviderWrap.priceProviders.Add(GenerateName(preName,index, price), new ManulPriceProvider(price));
|
|
index++;
|
|
}
|
|
return priceProviderWrap;
|
|
}
|
|
|
|
private List<double> GeneratePrices(int priceCount,double settlePrice)
|
|
{
|
|
var upLimitRate = innerUpDownLimit.UplimitRate;
|
|
var downLimitRate = innerUpDownLimit.DownLimitRate;
|
|
|
|
var upLimitPrice = settlePrice * (1+ Math.Abs(upLimitRate));
|
|
var downPrice = settlePrice * (1-Math.Abs(downLimitRate));
|
|
if (downPrice < 0)
|
|
{
|
|
// 最低价为0
|
|
downPrice = 0;
|
|
}
|
|
double step = (upLimitPrice - downPrice) / (priceCount-1);
|
|
List<double> prices = new List<double>();
|
|
for (int i = 0; i < priceCount; i++)
|
|
{
|
|
prices.Add(downPrice + (step * i));
|
|
}
|
|
return prices;
|
|
}
|
|
private string GenerateName(string preName,int index,double price)
|
|
{
|
|
return $"{preName}_{index}_{price}";
|
|
}
|
|
}
|
|
|
|
public interface IPriceFactory
|
|
{
|
|
PriceProviderWrap Generate(int priceCount,string preName);
|
|
}
|
|
|
|
public class PriceProviderWrap
|
|
{
|
|
public string Name { get; set; }
|
|
public double SettlePrice { get; set; }
|
|
public double UpLimitRate { get; set; }
|
|
public double DownLimitRate { get; set; }
|
|
public bool IsCover { get; set; }
|
|
public bool IsTouch { get; set; }
|
|
public Dictionary<string, IPriceProvider> priceProviders { get; set; }
|
|
}
|
|
|
|
public class ManulPriceProvider : IPriceProvider
|
|
{
|
|
private double _price;
|
|
|
|
public ManulPriceProvider(double price)
|
|
{
|
|
this._price = price;
|
|
}
|
|
|
|
public double GetPrice(string instrumentCode)
|
|
{
|
|
return _price;
|
|
}
|
|
|
|
public bool TryGetPrice(string instrumentCode, out double price)
|
|
{
|
|
price = _price;
|
|
return true;
|
|
}
|
|
}
|
|
}
|