Created
August 1, 2025 02:19
-
-
Save joeybeninghove/7ce0160019a09c9ed6e2c71557d7923a to your computer and use it in GitHub Desktop.
Setting Item Group Example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Configuration : IConfiguration | |
{ | |
private readonly Risk riskSettings; | |
private readonly int sortIndex = -1999; | |
public IEnumerable<SettingItem> Settings => BuildSettings(); | |
public Configuration() | |
{ | |
riskSettings = new Risk(++sortIndex); | |
} | |
public void Update(IList<SettingItem> settingItems) | |
{ | |
riskSettings.Update(settingItems); | |
} | |
private IEnumerable<SettingItem> BuildSettings() | |
{ | |
return new List<SettingItem> | |
{ | |
riskSettings.Build() | |
}; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Risk(int sortIndex) : SettingsSection(GroupName, sortIndex) | |
{ | |
private int tickSize = 4000; | |
private int stopLoss = 20; | |
private int takeProfit = 60; | |
private int contracts = 1; | |
private double maxProfit = 1500.0; | |
private double maxLoss = 500.0; | |
private bool enforcePnlLimits = true; | |
private PnLType pnlType = PnLType.Gross; | |
public int TickSize => tickSize; | |
public int StopLoss => stopLoss; | |
public int TakeProfit => takeProfit; | |
public int Contracts => contracts; | |
public double MaxProfit => maxProfit; | |
public double MaxLoss => maxLoss; | |
public bool EnforcePnlLimits => enforcePnlLimits; | |
public PnLType PnLType => pnlType; | |
public override IList<SettingItem> SettingItems => | |
new List<SettingItem> | |
{ | |
new SettingItemInteger(Labels.TickSize, tickSize), | |
new SettingItemInteger(Labels.StopLoss, stopLoss), | |
new SettingItemInteger(Labels.TakeProfit, takeProfit), | |
new SettingItemInteger(Labels.Contracts, contracts), | |
new SettingItemDouble(Labels.MaxProfit, maxProfit), | |
new SettingItemDouble(Labels.MaxLoss, maxLoss), | |
new SettingItemSelector(Labels.PnLType, pnlType.ToString(), | |
[PnLType.Gross.ToString(), PnLType.Net.ToString()]), | |
new SettingItemBoolean(Labels.EnforcePnlLimits, enforcePnlLimits), | |
}; | |
public override void Update(IList<SettingItem> settingItems) | |
{ | |
if (!settingItems.TryGetValue(GroupName, out List<SettingItem> updatedSettings) || updatedSettings == null) | |
return; | |
if (updatedSettings.TryGetValue(Labels.TickSize, out int updatedTickSize)) | |
tickSize = updatedTickSize; | |
if (updatedSettings.TryGetValue(Labels.StopLoss, out int updatedStopLoss)) | |
stopLoss = updatedStopLoss; | |
if (updatedSettings.TryGetValue(Labels.TakeProfit, out int updatedTakeProfit)) | |
takeProfit = updatedTakeProfit; | |
if (updatedSettings.TryGetValue(Labels.Contracts, out int updatedContracts)) | |
contracts = updatedContracts; | |
if (updatedSettings.TryGetValue(Labels.MaxProfit, out double updatedMaxProfit)) | |
maxProfit = updatedMaxProfit; | |
if (updatedSettings.TryGetValue(Labels.MaxLoss, out double updatedMaxLoss)) | |
maxLoss = updatedMaxLoss; | |
if (updatedSettings.TryGetValue(Labels.PnLType, out string updatedPnLType)) | |
pnlType = Enum.Parse<PnLType>(updatedPnLType); | |
if (updatedSettings.TryGetValue(Labels.EnforcePnlLimits, out bool updatedEnforcePnlLimits)) | |
enforcePnlLimits = updatedEnforcePnlLimits; | |
} | |
private static class Labels | |
{ | |
public const string TickSize = "Tick Size"; | |
public const string StopLoss = "Stop Loss (in Ticks)"; | |
public const string TakeProfit = "Take Profit (in Ticks)"; | |
public const string Contracts = "Contracts"; | |
public const string MaxProfit = "Max Profit ($)"; | |
public const string MaxLoss = "Max Loss ($)"; | |
public const string PnLType = "P&L Type"; | |
public const string EnforcePnlLimits = "Enforce P&L Limits"; | |
} | |
private const string GroupName = "Risk"; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public abstract class SettingsSection(string separatorGroupName, int sortIndex) | |
: ISettingsSection | |
{ | |
private readonly SettingItemSeparatorGroup separatorGroup = new(separatorGroupName, sortIndex); | |
public string SeparatorGroupName => separatorGroup.Text; | |
public abstract IList<SettingItem> SettingItems { get; } | |
public virtual SettingItem Build() | |
{ | |
var updatedSettingItems = SettingItems | |
.Select(settingItem => | |
{ | |
settingItem.SeparatorGroup = separatorGroup; | |
return settingItem; | |
}) | |
.ToList(); | |
return new SettingItemGroup(separatorGroupName, updatedSettingItems); | |
} | |
public abstract void Update(IList<SettingItem> settingItems); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment