Files
spark/src/client/spark/Modules/Spreader/SyntheticDataHelper.cs
T

171 lines
9.0 KiB
C#

using bts.api;
using bts.spark;
using mbts.spark.utilities;
using System;
using System.Collections.Generic;
using System.Linq;
namespace bts.shell {
static class SyntheticDataHelper {
public static void sanitize(ExecutionStrategy data) {
foreach (var kv in data.LegParamsMap) sanitize(kv.Value);
}
public static void sanitize(LegSpreaderParams data) {
sanitize(data.QuoteParams);
sanitize(data.PreHedgeParams);
sanitize(data.PostHedgeParams);
}
#region Quote
public static void sanitize(QuoteParams data) {
if (data.QuoteInsideMarketDistance == null) data.QuoteInsideMarketDistance = Defaults.QuoteInsideMarketDistance;
else sanitize(data.QuoteInsideMarketDistance);
if (data.QuoteInsideMarketDistanceWithTravel == null) data.QuoteInsideMarketDistanceWithTravel = Defaults.QuoteInsideMarketDistanceWithTravel;
else sanitize(data.QuoteInsideMarketDistanceWithTravel);
if (data.QuoteMuteZone == null) data.QuoteMuteZone = Defaults.QuoteMuteZone;
else sanitize(data.QuoteMuteZone);
if (data.QuoteRepriceThrottle == null) data.QuoteRepriceThrottle = Defaults.QuoteRepriceThrottle;
else sanitize(data.QuoteRepriceThrottle);
if (data.QuoteMaxOrderMove == null) data.QuoteMaxOrderMove = Defaults.QuoteMaxOrderMove;
}
public static void sanitize(QuoteInsideMarketDistance data) {
if (data.Priority == null) data.Priority = Defaults.RulePriority;
}
public static void sanitize(QuoteInsideMarketDistanceWithTravel data) {
if (data.Priority == null) data.Priority = Defaults.RulePriority;
}
public static void sanitize(QuoteMuteZone data) {
if (data.Priority == null) data.Priority = Defaults.RulePriority;
}
public static void sanitize(QuoteRepriceThrottle data) {
if (data.Priority == null) data.Priority = Defaults.RulePriority;
}
#endregion
#region PreHedge
public static void sanitize(PreHedgeParams data) {
if (data.QuantityBasedPaylessTicks == null) data.QuantityBasedPaylessTicks = Defaults.PreHedgeQuantityBasedPayLessTicks;
else sanitize(data.QuantityBasedPaylessTicks);
if (data.QuantityBasedPayupTicks == null) data.QuantityBasedPayupTicks = Defaults.PreHedgeQuantityBasedPayUpTicks;
else sanitize(data.QuantityBasedPayupTicks);
if (data.SendHedgeAsMarketOrder == null) data.SendHedgeAsMarketOrder = Defaults.PreHedgeSendHedgeAsMarketOrder;
else data.SendHedgeAsMarketOrder = new PreHedgeSendHedgeAsMarketOrder();
if (data.MaxPriceFromInsideMarket == null) data.MaxPriceFromInsideMarket = Defaults.PreHedgeMaxPriceFromInsideMarket;
else data.MaxPriceFromInsideMarket = new PreHedgeMaxPriceFromInsideMarket();
}
public static void sanitize(PreHedgeQuantityBasedPayLessTicks data) {
if (data.QuantityThreshold == null) data.QuantityThreshold = OrderDataHelper.Defaults.QuantityMeasure;
}
public static void sanitize(PreHedgeQuantityBasedPayupTicks data) {
if (data.QuantityThreshold == null) data.QuantityThreshold = OrderDataHelper.Defaults.QuantityMeasure;
}
#endregion
#region PostHedge
public static void sanitize(PostHedgeParams data) {
if (data.PriceBasedCross == null) data.PriceBasedCross = Defaults.PostHedgePriceBasedForcedCross;
if (data.TimeBasedForcedCross == null) data.TimeBasedForcedCross = Defaults.PostHedgeTimeBasedForcedCross;
}
#endregion
public static class Defaults {
public static LegSpreaderParams LegSpreaderParams => new LegSpreaderParams {
IsActiveQuoting = true,
EnableHedging = true,
QuoteParams = QuoteParams,
PreHedgeParams = PreHedgeParams,
PostHedgeParams = PostHedgeParams,
};
public static RulePriority RulePriority => new RulePriority();
#region QuoteParams
public static QuoteParams QuoteParams => new QuoteParams {
LeanQuantityRatio = 1,
//QuoteInsideMarketDistance = QuoteInsideMarketDistance,
QuoteMaxOrderMove = QuoteMaxOrderMove,
//QuoteMuteZone = QuoteMuteZone,
QuoteRepriceThrottle = QuoteRepriceThrottle,
QuoteInsideMarketDistanceWithTravel = QuoteInsideMarketDistanceWithTravel,
};
public static QuoteMaxOrderMove QuoteMaxOrderMove => new QuoteMaxOrderMove {
MaxOrderMoveThreshold = 1
};
public static QuoteRepriceThrottle QuoteRepriceThrottle {
get {
var rv = new QuoteRepriceThrottle { Priority = RulePriority };
rv.TicksAwayToSensitivity.Add(1, 1);
rv.TicksAwayToSensitivity.Add(5, 3);
return rv;
}
}
public static QuoteMuteZone QuoteMuteZone => new QuoteMuteZone {
InsideTicks = 2,
OutsideTicks = 3,
Priority = RulePriority
};
public static ConditionalQuoting ConditionalQuoting => new ConditionalQuoting {
NTicks = 2,
PriceType = ConditionalQuoting.Types.PriceType.Leg
};
public static NullableQuantity ShowQuantity => new NullableQuantity {
Quantity = 1
};
public static QuoteInsideMarketDistanceWithTravel QuoteInsideMarketDistanceWithTravel => new QuoteInsideMarketDistanceWithTravel { Priority = RulePriority };
public static QuoteInsideMarketDistance QuoteInsideMarketDistance => new QuoteInsideMarketDistance { Priority = RulePriority };
public static QuoteInsideMarketDistanceThrottle QuoteInsideMarketDistanceThrottle => new QuoteInsideMarketDistanceThrottle();
#endregion
#region PreHedge
public static PreHedgeParams PreHedgeParams => new PreHedgeParams {
MaxPriceFromInsideMarket = PreHedgeMaxPriceFromInsideMarket,
QuantityBasedPaylessTicks = null,
QuantityBasedPayupTicks = null,
SendHedgeAsMarketOrder = PreHedgeSendHedgeAsMarketOrder
};
public static PreHedgeSendHedgeAsMarketOrder PreHedgeSendHedgeAsMarketOrder => new PreHedgeSendHedgeAsMarketOrder();
public static PreHedgeMaxPriceFromInsideMarket PreHedgeMaxPriceFromInsideMarket => new PreHedgeMaxPriceFromInsideMarket();
public static PreHedgeQuantityBasedPayLessTicks PreHedgeQuantityBasedPayLessTicks => new PreHedgeQuantityBasedPayLessTicks { QuantityThreshold = OrderDataHelper.Defaults.QuantityMeasure };
public static PreHedgeQuantityBasedPayupTicks PreHedgeQuantityBasedPayUpTicks => new PreHedgeQuantityBasedPayupTicks { QuantityThreshold = OrderDataHelper.Defaults.QuantityMeasure };
#endregion
#region PostHedge
public static PostHedgeParams PostHedgeParams => new PostHedgeParams {
PriceBasedCross = null,
TimeBasedForcedCross = null,
};
public static PostHedgePriceBasedForcedCross PostHedgePriceBasedForcedCross => new PostHedgePriceBasedForcedCross();
public static PostHedgeTimeBasedForcedCross PostHedgeTimeBasedForcedCross => new PostHedgeTimeBasedForcedCross();
public static PostHedgeOppositeMarketVolumeBasedForcedCross PostHedgeOppositeMarketVolumeBasedForcedCross =>
new PostHedgeOppositeMarketVolumeBasedForcedCross {
QuantityThreshold = OrderDataHelper.Defaults.QuantityMeasure
};
#endregion
public static SyntheticContract SyntheticContract(string name, IEnumerable<ContractLeg> legs) {
var rv = new SyntheticContract { Name = name };
foreach (var l in legs) rv.Legs.Add(l);
return rv;
}
public static ContractLeg ContractLeg(SecurityImmutable security) =>
new ContractLeg { Security = security.CloneToMutable(), PriceMultiplier = 1, Quantity = 1, Side = Side.Buy };
public static ExecutionStrategy ExecutionStrategy(SecurityImmutable security) {
switch (security.ContractCase) {
case Security.ContractOneofCase.Spread: return ExecutionStrategy(security.Spread.Legs.Select(l => l.Security));
case Security.ContractOneofCase.Synthetic: return ExecutionStrategy(security.Synthetic.Legs.Select(l => l.Security));
default: throw new ArgumentException("Must be a Spread, or Synthetic", nameof(security));
}
}
public static ExecutionStrategy ExecutionStrategy(IEnumerable<SecurityImmutable> securities) { return ExecutionStrategy(securities.Select(s => s.Id)); }
public static ExecutionStrategy ExecutionStrategy(IEnumerable<uint> security_keys) {
var rv = new ExecutionStrategy();
foreach (var sk in security_keys) rv.LegParamsMap.Add(sk, LegSpreaderParams);
return rv;
}
}
}
}