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}") = "HmaSmaCrossoverBot", "HmaSmaCrossoverBot\HmaSmaCrossoverBot.csproj", "{b3f19675-15c1-4ecd-87db-b4e345e3d6f5}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{b3f19675-15c1-4ecd-87db-b4e345e3d6f5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{b3f19675-15c1-4ecd-87db-b4e345e3d6f5}.Debug|Any CPU.Build.0 = Debug|Any CPU
{b3f19675-15c1-4ecd-87db-b4e345e3d6f5}.Release|Any CPU.ActiveCfg = Release|Any CPU
{b3f19675-15c1-4ecd-87db-b4e345e3d6f5}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
EndGlobal
@@ -0,0 +1,198 @@
using cAlgo.API;
using cAlgo.API.Indicators;
using System;
namespace cAlgo.Robots
{
[Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
public class HmaSmaCrossoverBot : Robot
{
[Parameter("HMA A Period (Fast)", DefaultValue = 20, Group = "Periods")]
public int HmaAPeriod { get; set; }
[Parameter("HMA B Period (Slow)", DefaultValue = 250, Group = "Periods")]
public int HmaBPeriod { get; set; }
[Parameter("SMA C Period (Base)", DefaultValue = 200, Group = "Periods")]
public int SmaCPeriod { get; set; }
[Parameter("Lot Size", DefaultValue = 0.1, Group = "Volume")]
public double LotSize { get; set; }
[Parameter("Max Positions", DefaultValue = 4, Group = "Volume")]
public int MaxPositions { get; set; }
[Parameter("Stop Loss (Pips)", DefaultValue = 50, Group = "Protection")]
public double StopLossPips { get; set; }
// Label for position management
private const string BotLabel = "HmaSmaCrossoverBot";
private HullMovingAverage _hmaA;
private HullMovingAverage _hmaB;
private SimpleMovingAverage _smaC;
private double _volumeInUnits;
private TimeZoneInfo _germanZone; // Added for time filter
protected override void OnStart()
{
_hmaA = Indicators.HullMovingAverage(Bars.ClosePrices, HmaAPeriod);
_hmaB = Indicators.HullMovingAverage(Bars.ClosePrices, HmaBPeriod);
_smaC = Indicators.SimpleMovingAverage(Bars.ClosePrices, SmaCPeriod);
// Convert LotSize to normalized VolumeInUnits
double preciseVolume = Symbol.QuantityToVolumeInUnits(LotSize);
_volumeInUnits = Symbol.NormalizeVolumeInUnits(preciseVolume, RoundingMode.ToNearest);
if (_volumeInUnits < Symbol.VolumeInUnitsMin)
{
_volumeInUnits = Symbol.VolumeInUnitsMin;
Print($"Warning: Requested volume {preciseVolume} is below minimum. Using minimum: {_volumeInUnits} units.");
}
// Initialize German TimeZone
try
{
// "W. Europe Standard Time" is the ID for Berlin, Amsterdam, Rome (CET/CEST)
_germanZone = TimeZoneInfo.FindSystemTimeZoneById("W. Europe Standard Time");
}
catch (TimeZoneNotFoundException)
{
Print("Error: TimeZone 'W. Europe Standard Time' not found. Using Server TimeZone as fallback for time filter (might be incorrect).");
_germanZone = TimeZone; // Fallback to server zone (which is UTC here)
}
catch (Exception ex)
{
Print($"Error initializing TimeZone: {ex.Message}. Using Server TimeZone as fallback.");
_germanZone = TimeZone;
}
}
protected override void OnBar()
{
// Ensure indicators have enough data + 3 bars for peak/trough logic
int requiredBars = Math.Max(HmaAPeriod, Math.Max(HmaBPeriod, SmaCPeriod)) + 3;
if (Bars.Count < requiredBars)
{
return;
}
// Check exit conditions first
HandleExits();
// Check entry conditions
HandleEntries();
}
// Checks and executes exit logic based on rules
private void HandleExits()
{
// Get current and previous values (based on closed bars)
// Last(0) = current closed bar, Last(1) = previous bar
double hmaB_curr = _hmaB.Result.Last(0);
double smaC_curr = _smaC.Result.Last(0);
double hmaB_prev = _hmaB.Result.Last(1);
double low_curr = Bars.LowPrices.Last(0);
double low_prev = Bars.LowPrices.Last(1);
double high_curr = Bars.HighPrices.Last(0);
double high_prev = Bars.HighPrices.Last(1);
// --- Long Exit Conditions ---
// 1. B < C (at current close)
// 2. Low crosses below B (Currentbar.Low < Current.B AND PreviousBar.Low >= Previous.B)
bool longExitSignal = (hmaB_curr < smaC_curr) ||
((low_curr < hmaB_curr) && (low_prev >= hmaB_prev));
// --- Short Exit Conditions ---
// 1. B > C (at current close)
// 2. High crosses above B (Currentbar.High > Current.B AND PreviousBar.High <= Previous.B)
bool shortExitSignal = (hmaB_curr > smaC_curr) ||
((high_curr > hmaB_curr) && (high_prev <= hmaB_prev));
// Find all positions managed by this bot for the current symbol
var botPositions = GetPositions(null);
foreach (var position in botPositions)
{
if (position.TradeType == TradeType.Buy && longExitSignal)
{
ClosePosition(position);
}
else if (position.TradeType == TradeType.Sell && shortExitSignal)
{
ClosePosition(position);
}
}
}
// Checks and executes entry logic based on indicator signals
private void HandleEntries()
{
// --- Time Filter ---
// Use TimeInUtc for a reliable UTC baseline (as Robot TimeZone is UTC)
DateTime germanTime = TimeZoneInfo.ConvertTimeFromUtc(TimeInUtc, _germanZone);
// Only allow new trades between 7:00 (inclusive) and 17:00 (exclusive) German time
if (germanTime.Hour < 7 || germanTime.Hour >= 17)
{
return; // Outside trading hours
}
// --- End Time Filter ---
// Get values for signal bar (i-1) and confirmation bar (i)
// Last(0) = i (confirmation bar, just closed)
// Last(1) = i-1 (signal bar)
// Last(2) = i-2 (previous bar)
double hmaA_confirm = _hmaA.Result.Last(0);
double hmaA_signal = _hmaA.Result.Last(1);
double hmaA_prev = _hmaA.Result.Last(2);
// Get filter values at the signal bar (i-1)
double hmaB_signal = _hmaB.Result.Last(1);
double smaC_signal = _smaC.Result.Last(1);
// Peak/Trough detection at i-1 (Last(1)), confirmed by i (Last(0))
bool isPeak = (hmaA_prev < hmaA_signal) && (hmaA_signal > hmaA_confirm);
bool isTrough = (hmaA_prev > hmaA_signal) && (hmaA_signal < hmaA_confirm);
// --- Sell Signal Condition (at i-1) ---
// 1. B < C
// 2. A > C
// 3. A forms a peak
if ((hmaB_signal < smaC_signal) && (hmaA_signal > smaC_signal) && isPeak)
{
if (GetPositions(TradeType.Sell).Length < MaxPositions)
{
ExecuteMarketOrder(TradeType.Sell, SymbolName, _volumeInUnits, BotLabel, StopLossPips, null);
}
}
// --- Buy Signal Condition (at i-1) ---
// 1. B > C
// 2. A < C
// 3. A forms a trough
if ((hmaB_signal > smaC_signal) && (hmaA_signal < smaC_signal) && isTrough)
{
if (GetPositions(TradeType.Buy).Length < MaxPositions)
{
ExecuteMarketOrder(TradeType.Buy, SymbolName, _volumeInUnits, BotLabel, StopLossPips, null);
}
}
}
// Helper to get positions only for the current symbol and managed by this bot
private Position[] GetPositions(TradeType? tradeType)
{
if (tradeType == null)
{
// Get all Buy or Sell positions for this bot/symbol
return Positions.FindAll(BotLabel, SymbolName);
}
// Get specific TradeType positions for this bot/symbol
return Positions.FindAll(BotLabel, SymbolName, tradeType.Value);
}
}
}
@@ -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("HmaSmaCrossoverBot")]
[assembly: System.Reflection.AssemblyConfigurationAttribute("Debug")]
[assembly: System.Reflection.AssemblyFileVersionAttribute("1.0.0.0")]
[assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0")]
[assembly: System.Reflection.AssemblyProductAttribute("HmaSmaCrossoverBot")]
[assembly: System.Reflection.AssemblyTitleAttribute("HmaSmaCrossoverBot")]
[assembly: System.Reflection.AssemblyVersionAttribute("1.0.0.0")]
// Generated by the MSBuild WriteCodeFragment class.
@@ -0,0 +1 @@
4f125f90c408d20a05e42f8a6dc598f9c14b8e6f
@@ -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 = HmaSmaCrossoverBot
build_property.ProjectDir = C:\Users\Brummel\Documents\cAlgo\Sources\Robots\HmaSmaCrossoverBot\HmaSmaCrossoverBot\
@@ -0,0 +1,66 @@
{
"format": 1,
"restore": {
"C:\\Users\\Brummel\\Documents\\cAlgo\\Sources\\Robots\\HmaSmaCrossoverBot\\HmaSmaCrossoverBot\\HmaSmaCrossoverBot.csproj": {}
},
"projects": {
"C:\\Users\\Brummel\\Documents\\cAlgo\\Sources\\Robots\\HmaSmaCrossoverBot\\HmaSmaCrossoverBot\\HmaSmaCrossoverBot.csproj": {
"version": "1.0.0",
"restore": {
"projectUniqueName": "C:\\Users\\Brummel\\Documents\\cAlgo\\Sources\\Robots\\HmaSmaCrossoverBot\\HmaSmaCrossoverBot\\HmaSmaCrossoverBot.csproj",
"projectName": "HmaSmaCrossoverBot",
"projectPath": "C:\\Users\\Brummel\\Documents\\cAlgo\\Sources\\Robots\\HmaSmaCrossoverBot\\HmaSmaCrossoverBot\\HmaSmaCrossoverBot.csproj",
"packagesPath": "C:\\Users\\Brummel\\.nuget\\packages\\",
"outputPath": "C:\\Users\\Brummel\\Documents\\cAlgo\\Sources\\Robots\\HmaSmaCrossoverBot\\HmaSmaCrossoverBot\\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\\HmaSmaCrossoverBot\\HmaSmaCrossoverBot\\HmaSmaCrossoverBot.csproj",
"projectName": "HmaSmaCrossoverBot",
"projectPath": "C:\\Users\\Brummel\\Documents\\cAlgo\\Sources\\Robots\\HmaSmaCrossoverBot\\HmaSmaCrossoverBot\\HmaSmaCrossoverBot.csproj",
"packagesPath": "C:\\Users\\Brummel\\.nuget\\packages\\",
"outputPath": "C:\\Users\\Brummel\\Documents\\cAlgo\\Sources\\Robots\\HmaSmaCrossoverBot\\HmaSmaCrossoverBot\\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": "o2zbiJnQ5+mDJlvuxYW8+Qmxo/HzYxOwFc7BUoYdtbMdhQvLGFQJ9gkt6PTVAeX05JrpmDeuNnfj0iTviIs+iQ==",
"success": true,
"projectFilePath": "C:\\Users\\Brummel\\Documents\\cAlgo\\Sources\\Robots\\HmaSmaCrossoverBot\\HmaSmaCrossoverBot\\HmaSmaCrossoverBot.csproj",
"expectedPackageFiles": [
"C:\\Users\\Brummel\\.nuget\\packages\\ctrader.automate\\1.0.14\\ctrader.automate.1.0.14.nupkg.sha512"
],
"logs": []
}