Optimizing and Refactoring HmaClusterBot & -Indicator
This commit is contained in:
@@ -1,9 +1,8 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using cAlgo.API;
|
||||
using cAlgo.API.Indicators;
|
||||
using cAlgo.API.Internals;
|
||||
using Myc.HmaSma;
|
||||
|
||||
namespace cAlgo
|
||||
{
|
||||
@@ -39,7 +38,7 @@ namespace cAlgo
|
||||
private HullMovingAverage _hmaBiasIndicator;
|
||||
private HullMovingAverage _hmaClusterIndicator;
|
||||
|
||||
// Default values from prompt header
|
||||
// Default values
|
||||
private int _smaBiasLength = 200;
|
||||
private int _hmaBiasLength = 250;
|
||||
private int _hmaClusterLength = 25;
|
||||
@@ -48,7 +47,7 @@ namespace cAlgo
|
||||
|
||||
protected override void Initialize()
|
||||
{
|
||||
LoadConfiguration();
|
||||
ApplyExternalConfiguration();
|
||||
|
||||
// Initialize indicators with loaded or default values
|
||||
_smaBiasIndicator = Indicators.SimpleMovingAverage(Bars.ClosePrices, _smaBiasLength);
|
||||
@@ -62,73 +61,39 @@ namespace cAlgo
|
||||
{
|
||||
if (_smaBiasIndicator != null)
|
||||
SmaBiasOutput[index] = _smaBiasIndicator.Result[index];
|
||||
|
||||
|
||||
if (_hmaBiasIndicator != null)
|
||||
HmaBiasOutput[index] = _hmaBiasIndicator.Result[index];
|
||||
|
||||
|
||||
if (_hmaClusterIndicator != null)
|
||||
HmaClusterOutput[index] = _hmaClusterIndicator.Result[index];
|
||||
}
|
||||
|
||||
private void LoadConfiguration()
|
||||
private void ApplyExternalConfiguration()
|
||||
{
|
||||
if (!File.Exists(ConfigFilePath))
|
||||
{
|
||||
Print($"Config file not found at {ConfigFilePath}. Using defaults.");
|
||||
return;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
var lines = File.ReadAllLines(ConfigFilePath);
|
||||
var currentSymbol = SymbolName;
|
||||
// cTrader ShortName returns "m5", "h1", etc. which matches your file format
|
||||
var currentTimeframe = TimeFrame.ShortName;
|
||||
var loader = new HmaSmaLoader(ConfigFilePath);
|
||||
loader.Load();
|
||||
|
||||
foreach (var line in lines)
|
||||
// cTrader TimeFrame.ShortName returns values like "m5", "h1" which match the library expectation
|
||||
var parameters = loader.GetParameters(SymbolName, TimeFrame.ShortName);
|
||||
|
||||
if (parameters != null)
|
||||
{
|
||||
var trimmedLine = line.Trim();
|
||||
|
||||
// Skip comments and empty lines
|
||||
if (string.IsNullOrWhiteSpace(trimmedLine) || trimmedLine.StartsWith("#"))
|
||||
continue;
|
||||
|
||||
// Split by whitespace
|
||||
var columns = trimmedLine.Split(new[] { ' ', '\t' }, StringSplitOptions.RemoveEmptyEntries);
|
||||
|
||||
// Ensure we have enough columns (need at least up to index 6)
|
||||
if (columns.Length < 7)
|
||||
continue;
|
||||
|
||||
var cfgSymbol = columns[0];
|
||||
var cfgTimeframe = columns[1];
|
||||
|
||||
// Check for match (case-insensitive)
|
||||
if (string.Equals(cfgSymbol, currentSymbol, StringComparison.OrdinalIgnoreCase) &&
|
||||
string.Equals(cfgTimeframe, currentTimeframe, StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
// Parse values
|
||||
// Col 4: SmaBias
|
||||
// Col 5: HmaBias
|
||||
// Col 6: HmaCluster
|
||||
if (int.TryParse(columns[4], out int sBias) &&
|
||||
int.TryParse(columns[5], out int hBias) &&
|
||||
int.TryParse(columns[6], out int hCluster))
|
||||
{
|
||||
_smaBiasLength = sBias;
|
||||
_hmaBiasLength = hBias;
|
||||
_hmaClusterLength = hCluster;
|
||||
Print($"Configuration found for {currentSymbol} {currentTimeframe}");
|
||||
return; // Stop searching after match
|
||||
}
|
||||
}
|
||||
_smaBiasLength = parameters.SmaBias;
|
||||
_hmaBiasLength = parameters.HmaBias;
|
||||
_hmaClusterLength = parameters.HmaCluster;
|
||||
Print($"Configuration loaded for {SymbolName} {TimeFrame.ShortName}");
|
||||
}
|
||||
else
|
||||
{
|
||||
Print($"No specific config found for {SymbolName} {TimeFrame.ShortName}. Using defaults.");
|
||||
}
|
||||
|
||||
Print($"No specific config found for {currentSymbol} {currentTimeframe}. Using defaults.");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Print($"Error reading config file: {ex.Message}. Using defaults.");
|
||||
Print($"Error loading configuration: {ex.Message}. Using defaults.");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,9 +1,14 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net6.0</TargetFramework>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="cTrader.Automate" Version="*" />
|
||||
</ItemGroup>
|
||||
</Project>
|
||||
<ItemGroup>
|
||||
<Reference Include="MSLib, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null">
|
||||
<HintPath>..\..\..\Common\MSLib\obj\Debug\net6.0\MSLib.dll</HintPath>
|
||||
</Reference>
|
||||
</ItemGroup>
|
||||
</Project>
|
||||
Reference in New Issue
Block a user