Initial commit

This commit is contained in:
Michael Schimmel
2026-01-28 09:10:52 +01:00
commit a1d2e96f8a
513 changed files with 19503 additions and 0 deletions
+1
View File
@@ -0,0 +1 @@
{"version":"2.0.0","tasks":[{"label":"build","command":"dotnet","type":"process","args":["build","${workspaceFolder}","/property:GenerateFullPaths=true","/consoleLoggerParameters:NoSummary"],"problemMatcher":"$msCompile"}]}
@@ -0,0 +1,22 @@
Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.30011.22
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "HmaStructureBot", "HmaStructureBot\HmaStructureBot.csproj", "{9f6f411d-ba95-4e92-853b-10e6a1a8c98c}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{9f6f411d-ba95-4e92-853b-10e6a1a8c98c}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{9f6f411d-ba95-4e92-853b-10e6a1a8c98c}.Debug|Any CPU.Build.0 = Debug|Any CPU
{9f6f411d-ba95-4e92-853b-10e6a1a8c98c}.Release|Any CPU.ActiveCfg = Release|Any CPU
{9f6f411d-ba95-4e92-853b-10e6a1a8c98c}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
EndGlobal
@@ -0,0 +1,227 @@
using System;
using System.Collections.Generic;
using System.Linq;
using cAlgo.API;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;
namespace cAlgo.Robots
{
[Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
public class HmaStructureBot : Robot
{
// --- Parameters ---
[Parameter("HMA Period", DefaultValue = 25)]
public int HmaPeriod { get; set; }
[Parameter("Risk % per Trade", DefaultValue = 1.0, MinValue = 0.01)]
public double RiskPercent { get; set; }
[Parameter("Order Label", DefaultValue = "HMA_Struct")]
public string OrderLabel { get; set; }
// Expiration removed
// --- Indicators ---
private HullMovingAverage _hma;
// --- State Management ---
private DateTime _lastSetupTime;
// --- Data Structures ---
private enum ExtremumType { High, Low }
private struct Extremum
{
public ExtremumType Type;
public double Value;
public int Index; // Index relative to logic
public DateTime Time; // Unique identifier for the extremum
}
protected override void OnStart()
{
_hma = Indicators.HullMovingAverage(Bars.ClosePrices, HmaPeriod);
}
protected override void OnBarClosed()
{
// 1. Manage existing pending orders
ManageExistingOrders();
// 2. Find Extrema
var extrema = GetExtrema(200, 5);
if (extrema.Count < 5)
return;
// 3. Check for duplicate execution on the same setup
// The setup is defined by the latest extremum (extrema[0])
if (extrema[0].Time == _lastSetupTime)
return;
bool orderPlaced = false;
// --- Short Logic ---
// Last extremum must be a Trough (T0)
if (extrema[0].Type == ExtremumType.Low)
{
var t0 = extrema[0].Value;
var h0 = extrema[1].Value;
var t1 = extrema[2].Value;
var h1 = extrema[3].Value;
var t2 = extrema[4].Value;
// Condition: T1 < T2 and H0 < T2
if (t1 < t2 && h0 < t2)
{
double entryPrice = 0.5 * (t2 + h0);
double stopLossPrice = h1;
double takeProfitPrice = Math.Max(t0, t1);
PlaceStructureOrder(TradeType.Sell, entryPrice, stopLossPrice, takeProfitPrice);
orderPlaced = true;
}
}
// --- Long Logic ---
// Last extremum must be a Peak (H0)
else if (extrema[0].Type == ExtremumType.High)
{
var h0 = extrema[0].Value;
var t0 = extrema[1].Value;
var h1 = extrema[2].Value;
var t1 = extrema[3].Value;
var h2 = extrema[4].Value;
// Condition: H1 > H2 and T0 > H2
if (h1 > h2 && t0 > h2)
{
double entryPrice = 0.5 * (h2 + t0);
double stopLossPrice = t1;
double takeProfitPrice = Math.Min(h0, h1);
PlaceStructureOrder(TradeType.Buy, entryPrice, stopLossPrice, takeProfitPrice);
orderPlaced = true;
}
}
// 4. Mark Setup as handled if we placed an order
if (orderPlaced)
{
_lastSetupTime = extrema[0].Time;
}
}
private void PlaceStructureOrder(TradeType type, double entryPrice, double slPrice, double tpPrice)
{
// Calculate Risk
double riskAmount = Account.Balance * (RiskPercent / 100.0);
// Note: VolumeForFixedRisk still requires SL in Pips calculation
double slDistancePips = Math.Abs(entryPrice - slPrice) / Symbol.PipSize;
if (slDistancePips <= 0) return;
double volume = Symbol.VolumeForFixedRisk(riskAmount, slDistancePips);
volume = Symbol.NormalizeVolumeInUnits(volume, RoundingMode.Down);
// Place Limit Order using Absolute Prices
// Expiry is null (GTC - Good Till Cancelled)
PlaceLimitOrder(type, SymbolName, volume, entryPrice, OrderLabel, slPrice, tpPrice, ProtectionType.Absolute, null);
}
private void ManageExistingOrders()
{
foreach (var order in PendingOrders.ToArray())
{
if (order.Label != OrderLabel || order.SymbolName != SymbolName)
continue;
var barLow = Bars.LowPrices.Last(0);
var barHigh = Bars.HighPrices.Last(0);
bool isCancelled = false;
// --- Check SL Hit ---
if (order.StopLoss.HasValue)
{
if (order.TradeType == TradeType.Buy)
{
if (barLow <= order.StopLoss.Value)
{
order.Cancel();
isCancelled = true;
}
}
else // Sell
{
if (barHigh >= order.StopLoss.Value)
{
order.Cancel();
isCancelled = true;
}
}
}
if (isCancelled) continue;
// --- Check TP Extension ---
if (order.TakeProfit.HasValue)
{
if (order.TradeType == TradeType.Sell)
{
// If price went lower than TP, move TP to Low
if (barLow < order.TakeProfit.Value)
{
ModifyPendingOrder(order, order.TargetPrice, order.StopLoss, barLow, order.ProtectionType);
}
}
else // Buy
{
// If price went higher than TP, move TP to High
if (barHigh > order.TakeProfit.Value)
{
ModifyPendingOrder(order, order.TargetPrice, order.StopLoss, barHigh, order.ProtectionType);
}
}
}
}
}
private List<Extremum> GetExtrema(int searchDepth, int minCount)
{
var list = new List<Extremum>();
var hma = _hma.Result;
// i starts at 1 to compare with confirmed i+1 and i-1 (where i-1 is closer to closed bar Last(0))
for (int i = 1; i < searchDepth && i < hma.Count - 1; i++)
{
double current = hma.Last(i);
double prev = hma.Last(i + 1); // Older
double next = hma.Last(i - 1); // Newer
if (current > prev && current > next)
{
list.Add(new Extremum {
Type = ExtremumType.High,
Value = current,
Index = i,
Time = Bars.OpenTimes.Last(i)
});
}
else if (current < prev && current < next)
{
list.Add(new Extremum {
Type = ExtremumType.Low,
Value = current,
Index = i,
Time = Bars.OpenTimes.Last(i)
});
}
if (list.Count >= minCount)
break;
}
return list;
}
}
}
@@ -0,0 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="cTrader.Automate" Version="*" />
</ItemGroup>
</Project>
@@ -0,0 +1,4 @@
// <autogenerated />
using System;
using System.Reflection;
[assembly: global::System.Runtime.Versioning.TargetFrameworkAttribute(".NETCoreApp,Version=v6.0", FrameworkDisplayName = "")]
@@ -0,0 +1,22 @@
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by a tool.
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------
using System;
using System.Reflection;
[assembly: System.Reflection.AssemblyCompanyAttribute("HmaStructureBot")]
[assembly: System.Reflection.AssemblyConfigurationAttribute("Debug")]
[assembly: System.Reflection.AssemblyFileVersionAttribute("1.0.0.0")]
[assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0")]
[assembly: System.Reflection.AssemblyProductAttribute("HmaStructureBot")]
[assembly: System.Reflection.AssemblyTitleAttribute("HmaStructureBot")]
[assembly: System.Reflection.AssemblyVersionAttribute("1.0.0.0")]
// Generated by the MSBuild WriteCodeFragment class.
@@ -0,0 +1 @@
7c0c361c93e8c6d8fca2b9bb1c31f57269d6fb5c
@@ -0,0 +1,10 @@
is_global = true
build_property.TargetFramework = net6.0
build_property.TargetPlatformMinVersion =
build_property.UsingMicrosoftNETSdkWeb =
build_property.ProjectTypeGuids =
build_property.InvariantGlobalization =
build_property.PlatformNeutralAssembly =
build_property._SupportedPlatformList = Linux,macOS,Windows
build_property.RootNamespace = HmaStructureBot
build_property.ProjectDir = C:\Users\Brummel\Documents\cAlgo\Sources\Robots\HmaStructureBot\HmaStructureBot\
@@ -0,0 +1,66 @@
{
"format": 1,
"restore": {
"C:\\Users\\Brummel\\Documents\\cAlgo\\Sources\\Robots\\HmaStructureBot\\HmaStructureBot\\HmaStructureBot.csproj": {}
},
"projects": {
"C:\\Users\\Brummel\\Documents\\cAlgo\\Sources\\Robots\\HmaStructureBot\\HmaStructureBot\\HmaStructureBot.csproj": {
"version": "1.0.0",
"restore": {
"projectUniqueName": "C:\\Users\\Brummel\\Documents\\cAlgo\\Sources\\Robots\\HmaStructureBot\\HmaStructureBot\\HmaStructureBot.csproj",
"projectName": "HmaStructureBot",
"projectPath": "C:\\Users\\Brummel\\Documents\\cAlgo\\Sources\\Robots\\HmaStructureBot\\HmaStructureBot\\HmaStructureBot.csproj",
"packagesPath": "C:\\Users\\Brummel\\.nuget\\packages\\",
"outputPath": "C:\\Users\\Brummel\\Documents\\cAlgo\\Sources\\Robots\\HmaStructureBot\\HmaStructureBot\\obj\\",
"projectStyle": "PackageReference",
"configFilePaths": [
"C:\\Users\\Brummel\\AppData\\Roaming\\NuGet\\NuGet.Config"
],
"originalTargetFrameworks": [
"net6.0"
],
"sources": {
"https://api.nuget.org/v3/index.json": {}
},
"frameworks": {
"net6.0": {
"targetAlias": "net6.0",
"projectReferences": {}
}
},
"warningProperties": {
"warnAsError": [
"NU1605"
]
}
},
"frameworks": {
"net6.0": {
"targetAlias": "net6.0",
"dependencies": {
"cTrader.Automate": {
"target": "Package",
"version": "[*, )"
}
},
"imports": [
"net461",
"net462",
"net47",
"net471",
"net472",
"net48"
],
"assetTargetFallback": true,
"warn": true,
"frameworkReferences": {
"Microsoft.NETCore.App": {
"privateAssets": "all"
}
},
"runtimeIdentifierGraphPath": "C:\\Program Files\\dotnet\\sdk\\6.0.200\\RuntimeIdentifierGraph.json"
}
}
}
}
}
@@ -0,0 +1,21 @@
<?xml version="1.0" encoding="utf-8" standalone="no"?>
<Project ToolsVersion="14.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup Condition=" '$(ExcludeRestorePackageImports)' != 'true' ">
<RestoreSuccess Condition=" '$(RestoreSuccess)' == '' ">True</RestoreSuccess>
<RestoreTool Condition=" '$(RestoreTool)' == '' ">NuGet</RestoreTool>
<ProjectAssetsFile Condition=" '$(ProjectAssetsFile)' == '' ">$(MSBuildThisFileDirectory)project.assets.json</ProjectAssetsFile>
<NuGetPackageRoot Condition=" '$(NuGetPackageRoot)' == '' ">$(UserProfile)\.nuget\packages\</NuGetPackageRoot>
<NuGetPackageFolders Condition=" '$(NuGetPackageFolders)' == '' ">C:\Users\Brummel\.nuget\packages\</NuGetPackageFolders>
<NuGetProjectStyle Condition=" '$(NuGetProjectStyle)' == '' ">PackageReference</NuGetProjectStyle>
<NuGetToolVersion Condition=" '$(NuGetToolVersion)' == '' ">6.1.0</NuGetToolVersion>
</PropertyGroup>
<ItemGroup Condition=" '$(ExcludeRestorePackageImports)' != 'true' ">
<SourceRoot Include="C:\Users\Brummel\.nuget\packages\" />
</ItemGroup>
<ImportGroup Condition=" '$(ExcludeRestorePackageImports)' != 'true' ">
<Import Project="$(NuGetPackageRoot)ctrader.automate\1.0.14\build\cTrader.Automate.props" Condition="Exists('$(NuGetPackageRoot)ctrader.automate\1.0.14\build\cTrader.Automate.props')" />
</ImportGroup>
<PropertyGroup Condition=" '$(ExcludeRestorePackageImports)' != 'true' ">
<PkgcTrader_Automate Condition=" '$(PkgcTrader_Automate)' == '' ">C:\Users\Brummel\.nuget\packages\ctrader.automate\1.0.14</PkgcTrader_Automate>
</PropertyGroup>
</Project>
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8" standalone="no"?>
<Project ToolsVersion="14.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ImportGroup Condition=" '$(ExcludeRestorePackageImports)' != 'true' ">
<Import Project="$(NuGetPackageRoot)ctrader.automate\1.0.14\build\cTrader.Automate.targets" Condition="Exists('$(NuGetPackageRoot)ctrader.automate\1.0.14\build\cTrader.Automate.targets')" />
</ImportGroup>
</Project>
@@ -0,0 +1,139 @@
{
"version": 3,
"targets": {
"net6.0": {
"cTrader.Automate/1.0.14": {
"type": "package",
"compile": {
"lib/net6.0/cAlgo.API.dll": {}
},
"runtime": {
"lib/net6.0/cAlgo.API.dll": {}
},
"build": {
"build/cTrader.Automate.props": {},
"build/cTrader.Automate.targets": {}
}
}
}
},
"libraries": {
"cTrader.Automate/1.0.14": {
"sha512": "eNwE7WL90MGBKb5MuLAtZLdQy0vxkI5EVhLWAQ9S83EAdYkGAzdccvGFLk2oqmtSGBtD+gEpyrJUW/ej4dI4jw==",
"type": "package",
"path": "ctrader.automate/1.0.14",
"hasTools": true,
"files": [
".nupkg.metadata",
".signature.p7s",
"build/cTrader.Automate.props",
"build/cTrader.Automate.targets",
"ctrader.automate.1.0.14.nupkg.sha512",
"ctrader.automate.nuspec",
"eula.md",
"icon.png",
"lib/net40/cAlgo.API.dll",
"lib/net40/cAlgo.API.xml",
"lib/net6.0/cAlgo.API.dll",
"lib/net6.0/cAlgo.API.xml",
"tools/net472/Core.AlgoFormat.Compose.Reflection.dll",
"tools/net472/Core.AlgoFormat.Writer.dll",
"tools/net472/Core.AlgoFormat.dll",
"tools/net472/Core.Domain.Primitives.dll",
"tools/net472/Newtonsoft.Json.dll",
"tools/net472/System.Buffers.dll",
"tools/net472/System.Collections.Immutable.dll",
"tools/net472/System.Memory.dll",
"tools/net472/System.Numerics.Vectors.dll",
"tools/net472/System.Reflection.Metadata.dll",
"tools/net472/System.Reflection.MetadataLoadContext.dll",
"tools/net472/System.Runtime.CompilerServices.Unsafe.dll",
"tools/net472/cTrader.Automate.Sdk.Tasks.dll",
"tools/net6.0/Core.AlgoFormat.Compose.Reflection.dll",
"tools/net6.0/Core.AlgoFormat.Writer.dll",
"tools/net6.0/Core.AlgoFormat.dll",
"tools/net6.0/Core.Connection.Protobuf.Common.dll",
"tools/net6.0/Core.Domain.Primitives.dll",
"tools/net6.0/Microsoft.Win32.SystemEvents.dll",
"tools/net6.0/Newtonsoft.Json.dll",
"tools/net6.0/System.Drawing.Common.dll",
"tools/net6.0/System.Reflection.MetadataLoadContext.dll",
"tools/net6.0/System.Security.Permissions.dll",
"tools/net6.0/System.Windows.Extensions.dll",
"tools/net6.0/cTrader.Automate.Sdk.Tasks.dll",
"tools/net6.0/protobuf-net.Core.dll",
"tools/net6.0/protobuf-net.dll",
"tools/net6.0/runtimes/unix/lib/net6.0/System.Drawing.Common.dll",
"tools/net6.0/runtimes/win/lib/net6.0/Microsoft.Win32.SystemEvents.dll",
"tools/net6.0/runtimes/win/lib/net6.0/System.Drawing.Common.dll",
"tools/net6.0/runtimes/win/lib/net6.0/System.Windows.Extensions.dll"
]
}
},
"projectFileDependencyGroups": {
"net6.0": [
"cTrader.Automate >= *"
]
},
"packageFolders": {
"C:\\Users\\Brummel\\.nuget\\packages\\": {}
},
"project": {
"version": "1.0.0",
"restore": {
"projectUniqueName": "C:\\Users\\Brummel\\Documents\\cAlgo\\Sources\\Robots\\HmaStructureBot\\HmaStructureBot\\HmaStructureBot.csproj",
"projectName": "HmaStructureBot",
"projectPath": "C:\\Users\\Brummel\\Documents\\cAlgo\\Sources\\Robots\\HmaStructureBot\\HmaStructureBot\\HmaStructureBot.csproj",
"packagesPath": "C:\\Users\\Brummel\\.nuget\\packages\\",
"outputPath": "C:\\Users\\Brummel\\Documents\\cAlgo\\Sources\\Robots\\HmaStructureBot\\HmaStructureBot\\obj\\",
"projectStyle": "PackageReference",
"configFilePaths": [
"C:\\Users\\Brummel\\AppData\\Roaming\\NuGet\\NuGet.Config"
],
"originalTargetFrameworks": [
"net6.0"
],
"sources": {
"https://api.nuget.org/v3/index.json": {}
},
"frameworks": {
"net6.0": {
"targetAlias": "net6.0",
"projectReferences": {}
}
},
"warningProperties": {
"warnAsError": [
"NU1605"
]
}
},
"frameworks": {
"net6.0": {
"targetAlias": "net6.0",
"dependencies": {
"cTrader.Automate": {
"target": "Package",
"version": "[*, )"
}
},
"imports": [
"net461",
"net462",
"net47",
"net471",
"net472",
"net48"
],
"assetTargetFallback": true,
"warn": true,
"frameworkReferences": {
"Microsoft.NETCore.App": {
"privateAssets": "all"
}
},
"runtimeIdentifierGraphPath": "C:\\Program Files\\dotnet\\sdk\\6.0.200\\RuntimeIdentifierGraph.json"
}
}
}
}
@@ -0,0 +1,10 @@
{
"version": 2,
"dgSpecHash": "+LfUhmmRyp/da3uLv4sl9ySOIaNqbWkyyWrG6dFoxe2QtHmfBoTXxKOgzeDDVjvn++es3OTLB7yxGQK8b5/qMg==",
"success": true,
"projectFilePath": "C:\\Users\\Brummel\\Documents\\cAlgo\\Sources\\Robots\\HmaStructureBot\\HmaStructureBot\\HmaStructureBot.csproj",
"expectedPackageFiles": [
"C:\\Users\\Brummel\\.nuget\\packages\\ctrader.automate\\1.0.14\\ctrader.automate.1.0.14.nupkg.sha512"
],
"logs": []
}