From 2474089b517303653c68e7efb36a3d8ea7379a9e Mon Sep 17 00:00:00 2001 From: Martin <51429321+martcklm@users.noreply.github.com> Date: Fri, 13 Jun 2025 02:32:33 +0530 Subject: [PATCH 1/5] Add GoldTrendStrategy cBot for XAUUSD --- .../XAUUSD Trend Strategy.sln | 20 ++++ .../GoldTrendStrategy.cs | 98 +++++++++++++++++++ .../GoldTrendStrategy.csproj | 25 +++++ .../Properties/AssemblyInfo.cs | 20 ++++ 4 files changed, 163 insertions(+) create mode 100644 Robots/XAUUSD Trend Strategy/XAUUSD Trend Strategy.sln create mode 100644 Robots/XAUUSD Trend Strategy/XAUUSD Trend Strategy/GoldTrendStrategy.cs create mode 100644 Robots/XAUUSD Trend Strategy/XAUUSD Trend Strategy/GoldTrendStrategy.csproj create mode 100644 Robots/XAUUSD Trend Strategy/XAUUSD Trend Strategy/Properties/AssemblyInfo.cs diff --git a/Robots/XAUUSD Trend Strategy/XAUUSD Trend Strategy.sln b/Robots/XAUUSD Trend Strategy/XAUUSD Trend Strategy.sln new file mode 100644 index 0000000..53a816d --- /dev/null +++ b/Robots/XAUUSD Trend Strategy/XAUUSD Trend Strategy.sln @@ -0,0 +1,20 @@ + +Microsoft Visual Studio Solution File, Format Version 11.00 +# Visual Studio 2010 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Gold Trend Strategy", "XAUUSD Trend Strategy\GoldTrendStrategy.csproj", "{9AE93258-B8C0-4BB7-9848-7E4F7C43B555}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {9AE93258-B8C0-4BB7-9848-7E4F7C43B555}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {9AE93258-B8C0-4BB7-9848-7E4F7C43B555}.Debug|Any CPU.Build.0 = Debug|Any CPU + {9AE93258-B8C0-4BB7-9848-7E4F7C43B555}.Release|Any CPU.ActiveCfg = Release|Any CPU + {9AE93258-B8C0-4BB7-9848-7E4F7C43B555}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection +EndGlobal diff --git a/Robots/XAUUSD Trend Strategy/XAUUSD Trend Strategy/GoldTrendStrategy.cs b/Robots/XAUUSD Trend Strategy/XAUUSD Trend Strategy/GoldTrendStrategy.cs new file mode 100644 index 0000000..074dbbd --- /dev/null +++ b/Robots/XAUUSD Trend Strategy/XAUUSD Trend Strategy/GoldTrendStrategy.cs @@ -0,0 +1,98 @@ +// ------------------------------------------------------------------------------------------------- +// +// This code is a cTrader Algo API example. +// +// This cBot is intended to be used as a sample and does not guarantee any particular outcome or +// profit of any kind. Use it at your own risk. +// +// ------------------------------------------------------------------------------------------------- + +using cAlgo.API; +using cAlgo.API.Indicators; + +namespace cAlgo.Robots +{ + [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None, AddIndicators = true)] + public class GoldTrendStrategy : Robot + { + private double _volumeInUnits; + private SimpleMovingAverage _fastMa; + private SimpleMovingAverage _slowMa; + private RelativeStrengthIndex _rsi; + + [Parameter("Fast MA Source", Group = "Fast MA")] + public DataSeries FastMaSource { get; set; } + + [Parameter("Fast MA Period", DefaultValue = 50, Group = "Fast MA", MinValue = 1)] + public int FastMaPeriod { get; set; } + + [Parameter("Slow MA Source", Group = "Slow MA")] + public DataSeries SlowMaSource { get; set; } + + [Parameter("Slow MA Period", DefaultValue = 200, Group = "Slow MA", MinValue = 1)] + public int SlowMaPeriod { get; set; } + + [Parameter("RSI Period", DefaultValue = 14, Group = "RSI", MinValue = 1)] + public int RsiPeriod { get; set; } + + [Parameter("RSI Oversold", DefaultValue = 30, Group = "RSI", MinValue = 1, MaxValue = 50)] + public int Oversold { get; set; } + + [Parameter("RSI Overbought", DefaultValue = 70, Group = "RSI", MinValue = 50, MaxValue = 100)] + public int Overbought { get; set; } + + [Parameter("Volume (Lots)", DefaultValue = 0.01, Group = "Trade")] + public double VolumeInLots { get; set; } + + [Parameter("Stop Loss (Pips)", DefaultValue = 100, Group = "Trade", MinValue = 1)] + public double StopLossInPips { get; set; } + + [Parameter("Take Profit (Pips)", DefaultValue = 200, Group = "Trade", MinValue = 1)] + public double TakeProfitInPips { get; set; } + + [Parameter("Label", DefaultValue = "GoldTrendStrategy", Group = "Trade")] + public string Label { get; set; } + + protected override void OnStart() + { + if (SymbolName != "XAUUSD") + Print("Warning: This cBot is designed for XAUUSD. Current symbol is {0}.", SymbolName); + + _volumeInUnits = Symbol.QuantityToVolumeInUnits(VolumeInLots); + _fastMa = Indicators.SimpleMovingAverage(FastMaSource, FastMaPeriod); + _slowMa = Indicators.SimpleMovingAverage(SlowMaSource, SlowMaPeriod); + _rsi = Indicators.RelativeStrengthIndex(MarketSeries.Close, RsiPeriod); + + _fastMa.Result.Line.Color = Color.Gold; + _slowMa.Result.Line.Color = Color.DarkOrange; + } + + protected override void OnBarClosed() + { + var inUptrend = _fastMa.Result.LastValue > _slowMa.Result.LastValue; + var inDowntrend = _fastMa.Result.LastValue < _slowMa.Result.LastValue; + + if (inUptrend && _rsi.Result.LastValue < Oversold) + { + ClosePositions(TradeType.Sell); + ExecuteMarketOrder(TradeType.Buy, SymbolName, _volumeInUnits, Label, StopLossInPips, TakeProfitInPips); + } + else if (inDowntrend && _rsi.Result.LastValue > Overbought) + { + ClosePositions(TradeType.Buy); + ExecuteMarketOrder(TradeType.Sell, SymbolName, _volumeInUnits, Label, StopLossInPips, TakeProfitInPips); + } + } + + private void ClosePositions(TradeType tradeType) + { + foreach (var position in Positions.FindAll(Label)) + { + if (position.TradeType != tradeType) + continue; + + ClosePosition(position); + } + } + } +} diff --git a/Robots/XAUUSD Trend Strategy/XAUUSD Trend Strategy/GoldTrendStrategy.csproj b/Robots/XAUUSD Trend Strategy/XAUUSD Trend Strategy/GoldTrendStrategy.csproj new file mode 100644 index 0000000..29f8790 --- /dev/null +++ b/Robots/XAUUSD Trend Strategy/XAUUSD Trend Strategy/GoldTrendStrategy.csproj @@ -0,0 +1,25 @@ + + + + net6.0 + False + False + + + 7.2 + Debug + AnyCPU + Properties + cAlgo + Gold Trend Strategy + 512 + True + + + + + + + + + diff --git a/Robots/XAUUSD Trend Strategy/XAUUSD Trend Strategy/Properties/AssemblyInfo.cs b/Robots/XAUUSD Trend Strategy/XAUUSD Trend Strategy/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..61b8d57 --- /dev/null +++ b/Robots/XAUUSD Trend Strategy/XAUUSD Trend Strategy/Properties/AssemblyInfo.cs @@ -0,0 +1,20 @@ +using System.Diagnostics; +using System.Reflection; +using System.Runtime.InteropServices; + +[assembly: AssemblyTitle("Gold Trend Strategy")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyProduct("Gold Trend Strategy")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +[assembly: ComVisible(false)] + +[assembly: Guid("9f04b8d8-0ddb-4e77-bb31-abbf3a30c91b")] + +[assembly: AssemblyVersion("1.0.0.0")] +[assembly: AssemblyFileVersion("1.0.0.0")] +#if DEBUG + [assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations)] +#endif From 53faede1c9695416a3e4d39aa2f6aa127eb8a7e4 Mon Sep 17 00:00:00 2001 From: Martin <51429321+martcklm@users.noreply.github.com> Date: Fri, 13 Jun 2025 02:48:21 +0530 Subject: [PATCH 2/5] Improve gold strategy --- README.md | 4 + .../XAUUSD Trend Strategy.sln | 20 +++ .../GoldTrendStrategy.cs | 170 ++++++++++++++++++ .../GoldTrendStrategy.csproj | 25 +++ .../Properties/AssemblyInfo.cs | 20 +++ 5 files changed, 239 insertions(+) create mode 100644 Robots/XAUUSD Trend Strategy/XAUUSD Trend Strategy.sln create mode 100644 Robots/XAUUSD Trend Strategy/XAUUSD Trend Strategy/GoldTrendStrategy.cs create mode 100644 Robots/XAUUSD Trend Strategy/XAUUSD Trend Strategy/GoldTrendStrategy.csproj create mode 100644 Robots/XAUUSD Trend Strategy/XAUUSD Trend Strategy/Properties/AssemblyInfo.cs diff --git a/README.md b/README.md index 72d3530..b984483 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,6 @@ # cTrader Algo API Samples cBot / Indicator Samples of cTrader Algo API + +## Custom Samples + +- **GoldTrendStrategy**: Trend-following cBot for XAUUSD using Supertrend, MACD, RSI and optional dynamic volume management. diff --git a/Robots/XAUUSD Trend Strategy/XAUUSD Trend Strategy.sln b/Robots/XAUUSD Trend Strategy/XAUUSD Trend Strategy.sln new file mode 100644 index 0000000..53a816d --- /dev/null +++ b/Robots/XAUUSD Trend Strategy/XAUUSD Trend Strategy.sln @@ -0,0 +1,20 @@ + +Microsoft Visual Studio Solution File, Format Version 11.00 +# Visual Studio 2010 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Gold Trend Strategy", "XAUUSD Trend Strategy\GoldTrendStrategy.csproj", "{9AE93258-B8C0-4BB7-9848-7E4F7C43B555}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {9AE93258-B8C0-4BB7-9848-7E4F7C43B555}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {9AE93258-B8C0-4BB7-9848-7E4F7C43B555}.Debug|Any CPU.Build.0 = Debug|Any CPU + {9AE93258-B8C0-4BB7-9848-7E4F7C43B555}.Release|Any CPU.ActiveCfg = Release|Any CPU + {9AE93258-B8C0-4BB7-9848-7E4F7C43B555}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection +EndGlobal diff --git a/Robots/XAUUSD Trend Strategy/XAUUSD Trend Strategy/GoldTrendStrategy.cs b/Robots/XAUUSD Trend Strategy/XAUUSD Trend Strategy/GoldTrendStrategy.cs new file mode 100644 index 0000000..b9d0f3c --- /dev/null +++ b/Robots/XAUUSD Trend Strategy/XAUUSD Trend Strategy/GoldTrendStrategy.cs @@ -0,0 +1,170 @@ +// ------------------------------------------------------------------------------------------------- +// +// This code is a cTrader Algo API example. +// +// This cBot is intended to be used as a sample and does not guarantee any particular outcome or +// profit of any kind. Use it at your own risk. +// +// ------------------------------------------------------------------------------------------------- + +using System; +using cAlgo.API; +using cAlgo.API.Indicators; +using cAlgo.API.Internals; + +namespace cAlgo.Robots +{ + [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None, AddIndicators = true)] + public class GoldTrendStrategy : Robot + { + private double _volumeInUnits; + private SimpleMovingAverage _fastMa; + private SimpleMovingAverage _slowMa; + private RelativeStrengthIndex _rsi; + private Supertrend _supertrend; + private MacdCrossOver _macd; + + [Parameter("Fast MA Source", Group = "Fast MA")] + public DataSeries FastMaSource { get; set; } + + [Parameter("Fast MA Period", DefaultValue = 50, Group = "Fast MA", MinValue = 1)] + public int FastMaPeriod { get; set; } + + [Parameter("Slow MA Source", Group = "Slow MA")] + public DataSeries SlowMaSource { get; set; } + + [Parameter("Slow MA Period", DefaultValue = 200, Group = "Slow MA", MinValue = 1)] + public int SlowMaPeriod { get; set; } + + [Parameter("RSI Period", DefaultValue = 14, Group = "RSI", MinValue = 1)] + public int RsiPeriod { get; set; } + + [Parameter("RSI Oversold", DefaultValue = 30, Group = "RSI", MinValue = 1, MaxValue = 50)] + public int Oversold { get; set; } + + [Parameter("RSI Overbought", DefaultValue = 70, Group = "RSI", MinValue = 50, MaxValue = 100)] + public int Overbought { get; set; } + + [Parameter("Supertrend Periods", DefaultValue = 10, Group = "Supertrend", MinValue = 1)] + public int SupertrendPeriods { get; set; } + + [Parameter("Supertrend Multiplier", DefaultValue = 3.0, Group = "Supertrend", MinValue = 0.1)] + public double SupertrendMultiplier { get; set; } + + [Parameter("MACD Long Cycle", DefaultValue = 26, Group = "MACD", MinValue = 1)] + public int MacdLongCycle { get; set; } + + [Parameter("MACD Short Cycle", DefaultValue = 12, Group = "MACD", MinValue = 1)] + public int MacdShortCycle { get; set; } + + [Parameter("MACD Signal Periods", DefaultValue = 9, Group = "MACD", MinValue = 1)] + public int MacdSignalPeriods { get; set; } + + [Parameter("Volume (Lots)", DefaultValue = 0.01, Group = "Trade")] + public double VolumeInLots { get; set; } + + [Parameter("Use Dynamic Volume", DefaultValue = false, Group = "Trade")] + public bool UseDynamicVolume { get; set; } + + [Parameter("Risk Per Trade (%)", DefaultValue = 1.0, Group = "Trade", MinValue = 0.1)] + public double RiskPercent { get; set; } + + [Parameter("Max Spread (pips)", DefaultValue = 50, Group = "Trade", MinValue = 0)] + public double MaxSpreadInPips { get; set; } + + [Parameter("Trailing Stop (Pips)", DefaultValue = 50, Group = "Trade", MinValue = 0)] + public double TrailingStopInPips { get; set; } + + [Parameter("Stop Loss (Pips)", DefaultValue = 100, Group = "Trade", MinValue = 1)] + public double StopLossInPips { get; set; } + + [Parameter("Take Profit (Pips)", DefaultValue = 200, Group = "Trade", MinValue = 1)] + public double TakeProfitInPips { get; set; } + + [Parameter("Label", DefaultValue = "GoldTrendStrategy", Group = "Trade")] + public string Label { get; set; } + + protected override void OnStart() + { + if (SymbolName != "XAUUSD") + Print("Warning: This cBot is designed for XAUUSD. Current symbol is {0}.", SymbolName); + + if (!UseDynamicVolume) + _volumeInUnits = Symbol.QuantityToVolumeInUnits(VolumeInLots); + _fastMa = Indicators.SimpleMovingAverage(FastMaSource, FastMaPeriod); + _slowMa = Indicators.SimpleMovingAverage(SlowMaSource, SlowMaPeriod); + _rsi = Indicators.RelativeStrengthIndex(Bars.ClosePrices, RsiPeriod); + _supertrend = Indicators.Supertrend(SupertrendPeriods, SupertrendMultiplier); + _macd = Indicators.MacdCrossOver(Bars.ClosePrices, MacdLongCycle, MacdShortCycle, MacdSignalPeriods); + + _fastMa.Result.Line.Color = Color.Gold; + _slowMa.Result.Line.Color = Color.DarkOrange; + } + + protected override void OnBarClosed() + { + var trendUp = _supertrend.UpTrend.Last(0) < Bars.LowPrices.Last(0) && _supertrend.DownTrend.Last(1) > Bars.HighPrices.Last(1); + var trendDown = _supertrend.DownTrend.Last(0) > Bars.HighPrices.Last(0) && _supertrend.UpTrend.Last(1) < Bars.LowPrices.Last(1); + + var macdCrossUp = _macd.MACD.Last(0) > _macd.Signal.Last(0) && _macd.MACD.Last(1) <= _macd.Signal.Last(1); + var macdCrossDown = _macd.MACD.Last(0) < _macd.Signal.Last(0) && _macd.MACD.Last(1) >= _macd.Signal.Last(1); + + if (Symbol.Spread / Symbol.PipSize > MaxSpreadInPips) + return; + + var volume = GetTradeVolume(); + + if (trendUp && macdCrossUp && _rsi.Result.LastValue < Oversold) + { + ClosePositions(TradeType.Sell); + ExecuteMarketOrder(TradeType.Buy, SymbolName, volume, Label, StopLossInPips, TakeProfitInPips); + } + else if (trendDown && macdCrossDown && _rsi.Result.LastValue > Overbought) + { + ClosePositions(TradeType.Buy); + ExecuteMarketOrder(TradeType.Sell, SymbolName, volume, Label, StopLossInPips, TakeProfitInPips); + } + } + + protected override void OnTick() + { + foreach (var position in Positions.FindAll(Label)) + { + double? newStopLoss; + + if (position.TradeType == TradeType.Buy) + newStopLoss = Symbol.Bid - TrailingStopInPips * Symbol.PipSize; + else + newStopLoss = Symbol.Ask + TrailingStopInPips * Symbol.PipSize; + + if (position.TradeType == TradeType.Buy && (position.StopLoss == null || newStopLoss > position.StopLoss)) + ModifyPosition(position, newStopLoss, position.TakeProfit); + else if (position.TradeType == TradeType.Sell && (position.StopLoss == null || newStopLoss < position.StopLoss)) + ModifyPosition(position, newStopLoss, position.TakeProfit); + } + } + + private double GetTradeVolume() + { + if (!UseDynamicVolume) + return _volumeInUnits; + + var riskAmount = Account.Balance * RiskPercent / 100.0; + var volumeInLots = riskAmount / (StopLossInPips * Symbol.PipValue); + var units = Symbol.QuantityToVolumeInUnits(volumeInLots); + units = Math.Max(Symbol.VolumeInUnitsMin, Math.Min(Symbol.VolumeInUnitsMax, units)); + return Symbol.NormalizeVolumeInUnits(units, RoundingMode.ToNearest); + } + + private void ClosePositions(TradeType tradeType) + { + foreach (var position in Positions.FindAll(Label)) + { + if (position.TradeType != tradeType) + continue; + + ClosePosition(position); + } + } + } +} diff --git a/Robots/XAUUSD Trend Strategy/XAUUSD Trend Strategy/GoldTrendStrategy.csproj b/Robots/XAUUSD Trend Strategy/XAUUSD Trend Strategy/GoldTrendStrategy.csproj new file mode 100644 index 0000000..29f8790 --- /dev/null +++ b/Robots/XAUUSD Trend Strategy/XAUUSD Trend Strategy/GoldTrendStrategy.csproj @@ -0,0 +1,25 @@ + + + + net6.0 + False + False + + + 7.2 + Debug + AnyCPU + Properties + cAlgo + Gold Trend Strategy + 512 + True + + + + + + + + + diff --git a/Robots/XAUUSD Trend Strategy/XAUUSD Trend Strategy/Properties/AssemblyInfo.cs b/Robots/XAUUSD Trend Strategy/XAUUSD Trend Strategy/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..61b8d57 --- /dev/null +++ b/Robots/XAUUSD Trend Strategy/XAUUSD Trend Strategy/Properties/AssemblyInfo.cs @@ -0,0 +1,20 @@ +using System.Diagnostics; +using System.Reflection; +using System.Runtime.InteropServices; + +[assembly: AssemblyTitle("Gold Trend Strategy")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyProduct("Gold Trend Strategy")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +[assembly: ComVisible(false)] + +[assembly: Guid("9f04b8d8-0ddb-4e77-bb31-abbf3a30c91b")] + +[assembly: AssemblyVersion("1.0.0.0")] +[assembly: AssemblyFileVersion("1.0.0.0")] +#if DEBUG + [assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations)] +#endif From 51f265586fe57181be0f5fcea47ac3345cefd2b5 Mon Sep 17 00:00:00 2001 From: Martin <51429321+martcklm@users.noreply.github.com> Date: Fri, 13 Jun 2025 03:21:44 +0530 Subject: [PATCH 3/5] Fix trailing stop handling --- README.md | 4 + .../XAUUSD Trend Strategy.sln | 20 ++ .../GoldTrendStrategy.cs | 173 ++++++++++++++++++ .../GoldTrendStrategy.csproj | 25 +++ .../Properties/AssemblyInfo.cs | 20 ++ 5 files changed, 242 insertions(+) create mode 100644 Robots/XAUUSD Trend Strategy/XAUUSD Trend Strategy.sln create mode 100644 Robots/XAUUSD Trend Strategy/XAUUSD Trend Strategy/GoldTrendStrategy.cs create mode 100644 Robots/XAUUSD Trend Strategy/XAUUSD Trend Strategy/GoldTrendStrategy.csproj create mode 100644 Robots/XAUUSD Trend Strategy/XAUUSD Trend Strategy/Properties/AssemblyInfo.cs diff --git a/README.md b/README.md index 72d3530..b984483 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,6 @@ # cTrader Algo API Samples cBot / Indicator Samples of cTrader Algo API + +## Custom Samples + +- **GoldTrendStrategy**: Trend-following cBot for XAUUSD using Supertrend, MACD, RSI and optional dynamic volume management. diff --git a/Robots/XAUUSD Trend Strategy/XAUUSD Trend Strategy.sln b/Robots/XAUUSD Trend Strategy/XAUUSD Trend Strategy.sln new file mode 100644 index 0000000..53a816d --- /dev/null +++ b/Robots/XAUUSD Trend Strategy/XAUUSD Trend Strategy.sln @@ -0,0 +1,20 @@ + +Microsoft Visual Studio Solution File, Format Version 11.00 +# Visual Studio 2010 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Gold Trend Strategy", "XAUUSD Trend Strategy\GoldTrendStrategy.csproj", "{9AE93258-B8C0-4BB7-9848-7E4F7C43B555}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {9AE93258-B8C0-4BB7-9848-7E4F7C43B555}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {9AE93258-B8C0-4BB7-9848-7E4F7C43B555}.Debug|Any CPU.Build.0 = Debug|Any CPU + {9AE93258-B8C0-4BB7-9848-7E4F7C43B555}.Release|Any CPU.ActiveCfg = Release|Any CPU + {9AE93258-B8C0-4BB7-9848-7E4F7C43B555}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection +EndGlobal diff --git a/Robots/XAUUSD Trend Strategy/XAUUSD Trend Strategy/GoldTrendStrategy.cs b/Robots/XAUUSD Trend Strategy/XAUUSD Trend Strategy/GoldTrendStrategy.cs new file mode 100644 index 0000000..cfa5a96 --- /dev/null +++ b/Robots/XAUUSD Trend Strategy/XAUUSD Trend Strategy/GoldTrendStrategy.cs @@ -0,0 +1,173 @@ +// ------------------------------------------------------------------------------------------------- +// +// This code is a cTrader Algo API example. +// +// This cBot is intended to be used as a sample and does not guarantee any particular outcome or +// profit of any kind. Use it at your own risk. +// +// ------------------------------------------------------------------------------------------------- + +using System; +using cAlgo.API; +using cAlgo.API.Indicators; +using cAlgo.API.Internals; + +namespace cAlgo.Robots +{ + [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None, AddIndicators = true)] + public class GoldTrendStrategy : Robot + { + private double _volumeInUnits; + private SimpleMovingAverage _fastMa; + private SimpleMovingAverage _slowMa; + private RelativeStrengthIndex _rsi; + private Supertrend _supertrend; + private MacdCrossOver _macd; + + [Parameter("Fast MA Source", Group = "Fast MA")] + public DataSeries FastMaSource { get; set; } + + [Parameter("Fast MA Period", DefaultValue = 50, Group = "Fast MA", MinValue = 1)] + public int FastMaPeriod { get; set; } + + [Parameter("Slow MA Source", Group = "Slow MA")] + public DataSeries SlowMaSource { get; set; } + + [Parameter("Slow MA Period", DefaultValue = 200, Group = "Slow MA", MinValue = 1)] + public int SlowMaPeriod { get; set; } + + [Parameter("RSI Period", DefaultValue = 14, Group = "RSI", MinValue = 1)] + public int RsiPeriod { get; set; } + + [Parameter("RSI Oversold", DefaultValue = 30, Group = "RSI", MinValue = 1, MaxValue = 50)] + public int Oversold { get; set; } + + [Parameter("RSI Overbought", DefaultValue = 70, Group = "RSI", MinValue = 50, MaxValue = 100)] + public int Overbought { get; set; } + + [Parameter("Supertrend Periods", DefaultValue = 10, Group = "Supertrend", MinValue = 1)] + public int SupertrendPeriods { get; set; } + + [Parameter("Supertrend Multiplier", DefaultValue = 3.0, Group = "Supertrend", MinValue = 0.1)] + public double SupertrendMultiplier { get; set; } + + [Parameter("MACD Long Cycle", DefaultValue = 26, Group = "MACD", MinValue = 1)] + public int MacdLongCycle { get; set; } + + [Parameter("MACD Short Cycle", DefaultValue = 12, Group = "MACD", MinValue = 1)] + public int MacdShortCycle { get; set; } + + [Parameter("MACD Signal Periods", DefaultValue = 9, Group = "MACD", MinValue = 1)] + public int MacdSignalPeriods { get; set; } + + [Parameter("Volume (Lots)", DefaultValue = 0.01, Group = "Trade")] + public double VolumeInLots { get; set; } + + [Parameter("Use Dynamic Volume", DefaultValue = false, Group = "Trade")] + public bool UseDynamicVolume { get; set; } + + [Parameter("Risk Per Trade (%)", DefaultValue = 1.0, Group = "Trade", MinValue = 0.1)] + public double RiskPercent { get; set; } + + [Parameter("Max Spread (pips)", DefaultValue = 50, Group = "Trade", MinValue = 0)] + public double MaxSpreadInPips { get; set; } + + [Parameter("Trailing Stop (Pips)", DefaultValue = 50, Group = "Trade", MinValue = 0)] + public double TrailingStopInPips { get; set; } + + [Parameter("Stop Loss (Pips)", DefaultValue = 100, Group = "Trade", MinValue = 1)] + public double StopLossInPips { get; set; } + + [Parameter("Take Profit (Pips)", DefaultValue = 200, Group = "Trade", MinValue = 1)] + public double TakeProfitInPips { get; set; } + + [Parameter("Label", DefaultValue = "GoldTrendStrategy", Group = "Trade")] + public string Label { get; set; } + + protected override void OnStart() + { + if (SymbolName != "XAUUSD") + Print("Warning: This cBot is designed for XAUUSD. Current symbol is {0}.", SymbolName); + + if (!UseDynamicVolume) + _volumeInUnits = Symbol.QuantityToVolumeInUnits(VolumeInLots); + _fastMa = Indicators.SimpleMovingAverage(FastMaSource, FastMaPeriod); + _slowMa = Indicators.SimpleMovingAverage(SlowMaSource, SlowMaPeriod); + _rsi = Indicators.RelativeStrengthIndex(Bars.ClosePrices, RsiPeriod); + _supertrend = Indicators.Supertrend(SupertrendPeriods, SupertrendMultiplier); + _macd = Indicators.MacdCrossOver(Bars.ClosePrices, MacdLongCycle, MacdShortCycle, MacdSignalPeriods); + + _fastMa.Result.Line.Color = Color.Gold; + _slowMa.Result.Line.Color = Color.DarkOrange; + } + + protected override void OnBarClosed() + { + var trendUp = _supertrend.UpTrend.Last(0) < Bars.LowPrices.Last(0) && _supertrend.DownTrend.Last(1) > Bars.HighPrices.Last(1); + var trendDown = _supertrend.DownTrend.Last(0) > Bars.HighPrices.Last(0) && _supertrend.UpTrend.Last(1) < Bars.LowPrices.Last(1); + + var macdCrossUp = _macd.MACD.Last(0) > _macd.Signal.Last(0) && _macd.MACD.Last(1) <= _macd.Signal.Last(1); + var macdCrossDown = _macd.MACD.Last(0) < _macd.Signal.Last(0) && _macd.MACD.Last(1) >= _macd.Signal.Last(1); + + if (Symbol.Spread / Symbol.PipSize > MaxSpreadInPips) + return; + + var volume = GetTradeVolume(); + + if (trendUp && macdCrossUp && _rsi.Result.LastValue < Oversold) + { + ClosePositions(TradeType.Sell); + ExecuteMarketOrder(TradeType.Buy, SymbolName, volume, Label, StopLossInPips, TakeProfitInPips); + } + else if (trendDown && macdCrossDown && _rsi.Result.LastValue > Overbought) + { + ClosePositions(TradeType.Buy); + ExecuteMarketOrder(TradeType.Sell, SymbolName, volume, Label, StopLossInPips, TakeProfitInPips); + } + } + + protected override void OnTick() + { + if (TrailingStopInPips <= 0) + return; + + foreach (var position in Positions.FindAll(Label)) + { + double? newStopLoss; + + if (position.TradeType == TradeType.Buy) + newStopLoss = Symbol.Bid - TrailingStopInPips * Symbol.PipSize; + else + newStopLoss = Symbol.Ask + TrailingStopInPips * Symbol.PipSize; + + if (position.TradeType == TradeType.Buy && (position.StopLoss == null || newStopLoss > position.StopLoss)) + ModifyPosition(position, newStopLoss, position.TakeProfit); + else if (position.TradeType == TradeType.Sell && (position.StopLoss == null || newStopLoss < position.StopLoss)) + ModifyPosition(position, newStopLoss, position.TakeProfit); + } + } + + private double GetTradeVolume() + { + if (!UseDynamicVolume) + return _volumeInUnits; + + var riskAmount = Account.Balance * RiskPercent / 100.0; + var volumeInLots = riskAmount / (StopLossInPips * Symbol.PipValue); + var units = Symbol.QuantityToVolumeInUnits(volumeInLots); + units = Math.Max(Symbol.VolumeInUnitsMin, Math.Min(Symbol.VolumeInUnitsMax, units)); + return Symbol.NormalizeVolumeInUnits(units, RoundingMode.ToNearest); + } + + private void ClosePositions(TradeType tradeType) + { + foreach (var position in Positions.FindAll(Label)) + { + if (position.TradeType != tradeType) + continue; + + ClosePosition(position); + } + } + } +} diff --git a/Robots/XAUUSD Trend Strategy/XAUUSD Trend Strategy/GoldTrendStrategy.csproj b/Robots/XAUUSD Trend Strategy/XAUUSD Trend Strategy/GoldTrendStrategy.csproj new file mode 100644 index 0000000..29f8790 --- /dev/null +++ b/Robots/XAUUSD Trend Strategy/XAUUSD Trend Strategy/GoldTrendStrategy.csproj @@ -0,0 +1,25 @@ + + + + net6.0 + False + False + + + 7.2 + Debug + AnyCPU + Properties + cAlgo + Gold Trend Strategy + 512 + True + + + + + + + + + diff --git a/Robots/XAUUSD Trend Strategy/XAUUSD Trend Strategy/Properties/AssemblyInfo.cs b/Robots/XAUUSD Trend Strategy/XAUUSD Trend Strategy/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..61b8d57 --- /dev/null +++ b/Robots/XAUUSD Trend Strategy/XAUUSD Trend Strategy/Properties/AssemblyInfo.cs @@ -0,0 +1,20 @@ +using System.Diagnostics; +using System.Reflection; +using System.Runtime.InteropServices; + +[assembly: AssemblyTitle("Gold Trend Strategy")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyProduct("Gold Trend Strategy")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +[assembly: ComVisible(false)] + +[assembly: Guid("9f04b8d8-0ddb-4e77-bb31-abbf3a30c91b")] + +[assembly: AssemblyVersion("1.0.0.0")] +[assembly: AssemblyFileVersion("1.0.0.0")] +#if DEBUG + [assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations)] +#endif From ce27749e301b733c3a20532b0630f66d82bb91bf Mon Sep 17 00:00:00 2001 From: Martin <51429321+martcklm@users.noreply.github.com> Date: Fri, 13 Jun 2025 10:02:11 +0530 Subject: [PATCH 4/5] Refine gold strategy --- README.md | 4 + .../XAUUSD Trend Strategy.sln | 20 +++ .../GoldTrendStrategy.cs | 152 ++++++++++++++++++ .../GoldTrendStrategy.csproj | 25 +++ .../Properties/AssemblyInfo.cs | 20 +++ 5 files changed, 221 insertions(+) create mode 100644 Robots/XAUUSD Trend Strategy/XAUUSD Trend Strategy.sln create mode 100644 Robots/XAUUSD Trend Strategy/XAUUSD Trend Strategy/GoldTrendStrategy.cs create mode 100644 Robots/XAUUSD Trend Strategy/XAUUSD Trend Strategy/GoldTrendStrategy.csproj create mode 100644 Robots/XAUUSD Trend Strategy/XAUUSD Trend Strategy/Properties/AssemblyInfo.cs diff --git a/README.md b/README.md index 72d3530..341576a 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,6 @@ # cTrader Algo API Samples cBot / Indicator Samples of cTrader Algo API + +## Custom Samples + +- **GoldTrendStrategy**: Moving average crossover strategy for XAUUSD with RSI confirmation and optional dynamic volume control. diff --git a/Robots/XAUUSD Trend Strategy/XAUUSD Trend Strategy.sln b/Robots/XAUUSD Trend Strategy/XAUUSD Trend Strategy.sln new file mode 100644 index 0000000..53a816d --- /dev/null +++ b/Robots/XAUUSD Trend Strategy/XAUUSD Trend Strategy.sln @@ -0,0 +1,20 @@ + +Microsoft Visual Studio Solution File, Format Version 11.00 +# Visual Studio 2010 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Gold Trend Strategy", "XAUUSD Trend Strategy\GoldTrendStrategy.csproj", "{9AE93258-B8C0-4BB7-9848-7E4F7C43B555}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {9AE93258-B8C0-4BB7-9848-7E4F7C43B555}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {9AE93258-B8C0-4BB7-9848-7E4F7C43B555}.Debug|Any CPU.Build.0 = Debug|Any CPU + {9AE93258-B8C0-4BB7-9848-7E4F7C43B555}.Release|Any CPU.ActiveCfg = Release|Any CPU + {9AE93258-B8C0-4BB7-9848-7E4F7C43B555}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection +EndGlobal diff --git a/Robots/XAUUSD Trend Strategy/XAUUSD Trend Strategy/GoldTrendStrategy.cs b/Robots/XAUUSD Trend Strategy/XAUUSD Trend Strategy/GoldTrendStrategy.cs new file mode 100644 index 0000000..6d99877 --- /dev/null +++ b/Robots/XAUUSD Trend Strategy/XAUUSD Trend Strategy/GoldTrendStrategy.cs @@ -0,0 +1,152 @@ +// ------------------------------------------------------------------------------------------------- +// +// This code is a cTrader Algo API example. +// +// This cBot is intended to be used as a sample and does not guarantee any particular outcome or +// profit of any kind. Use it at your own risk. +// +// ------------------------------------------------------------------------------------------------- + +using System; +using cAlgo.API; +using cAlgo.API.Indicators; +using cAlgo.API.Internals; + +namespace cAlgo.Robots +{ + [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None, AddIndicators = true)] + public class GoldTrendStrategy : Robot + { + private double _volumeInUnits; + private SimpleMovingAverage _fastMa; + private SimpleMovingAverage _slowMa; + private RelativeStrengthIndex _rsi; + + [Parameter("Fast MA Source", Group = "Fast MA")] + public DataSeries FastMaSource { get; set; } + + [Parameter("Fast MA Period", DefaultValue = 20, Group = "Fast MA", MinValue = 1)] + public int FastMaPeriod { get; set; } + + [Parameter("Slow MA Source", Group = "Slow MA")] + public DataSeries SlowMaSource { get; set; } + + [Parameter("Slow MA Period", DefaultValue = 50, Group = "Slow MA", MinValue = 1)] + public int SlowMaPeriod { get; set; } + + [Parameter("RSI Period", DefaultValue = 14, Group = "RSI", MinValue = 1)] + public int RsiPeriod { get; set; } + + [Parameter("RSI Oversold", DefaultValue = 45, Group = "RSI", MinValue = 1, MaxValue = 50)] + public int Oversold { get; set; } + + [Parameter("RSI Overbought", DefaultValue = 55, Group = "RSI", MinValue = 50, MaxValue = 100)] + public int Overbought { get; set; } + + + [Parameter("Volume (Lots)", DefaultValue = 0.01, Group = "Trade")] + public double VolumeInLots { get; set; } + + [Parameter("Use Dynamic Volume", DefaultValue = false, Group = "Trade")] + public bool UseDynamicVolume { get; set; } + + [Parameter("Risk Per Trade (%)", DefaultValue = 1.0, Group = "Trade", MinValue = 0.1)] + public double RiskPercent { get; set; } + + [Parameter("Max Spread (pips)", DefaultValue = 50, Group = "Trade", MinValue = 0)] + public double MaxSpreadInPips { get; set; } + + [Parameter("Trailing Stop (Pips)", DefaultValue = 50, Group = "Trade", MinValue = 0)] + public double TrailingStopInPips { get; set; } + + [Parameter("Stop Loss (Pips)", DefaultValue = 100, Group = "Trade", MinValue = 1)] + public double StopLossInPips { get; set; } + + [Parameter("Take Profit (Pips)", DefaultValue = 200, Group = "Trade", MinValue = 1)] + public double TakeProfitInPips { get; set; } + + [Parameter("Label", DefaultValue = "GoldTrendStrategy", Group = "Trade")] + public string Label { get; set; } + + protected override void OnStart() + { + if (SymbolName != "XAUUSD") + Print("Warning: This cBot is designed for XAUUSD. Current symbol is {0}.", SymbolName); + + if (!UseDynamicVolume) + _volumeInUnits = Symbol.QuantityToVolumeInUnits(VolumeInLots); + _fastMa = Indicators.SimpleMovingAverage(FastMaSource, FastMaPeriod); + _slowMa = Indicators.SimpleMovingAverage(SlowMaSource, SlowMaPeriod); + _rsi = Indicators.RelativeStrengthIndex(Bars.ClosePrices, RsiPeriod); + + _fastMa.Result.Line.Color = Color.Gold; + _slowMa.Result.Line.Color = Color.DarkOrange; + } + + protected override void OnBarClosed() + { + var crossUp = _fastMa.Result.HasCrossedAbove(_slowMa.Result, 0); + var crossDown = _fastMa.Result.HasCrossedBelow(_slowMa.Result, 0); + + if (Symbol.Spread / Symbol.PipSize > MaxSpreadInPips) + return; + + var volume = GetTradeVolume(); + + if (crossUp && _rsi.Result.LastValue > Overbought) + { + ClosePositions(TradeType.Sell); + ExecuteMarketOrder(TradeType.Buy, SymbolName, volume, Label, StopLossInPips, TakeProfitInPips); + } + else if (crossDown && _rsi.Result.LastValue < Oversold) + { + ClosePositions(TradeType.Buy); + ExecuteMarketOrder(TradeType.Sell, SymbolName, volume, Label, StopLossInPips, TakeProfitInPips); + } + } + + protected override void OnTick() + { + if (TrailingStopInPips <= 0) + return; + + foreach (var position in Positions.FindAll(Label)) + { + double? newStopLoss; + + if (position.TradeType == TradeType.Buy) + newStopLoss = Symbol.Bid - TrailingStopInPips * Symbol.PipSize; + else + newStopLoss = Symbol.Ask + TrailingStopInPips * Symbol.PipSize; + + if (position.TradeType == TradeType.Buy && (position.StopLoss == null || newStopLoss > position.StopLoss)) + ModifyPosition(position, newStopLoss, position.TakeProfit); + else if (position.TradeType == TradeType.Sell && (position.StopLoss == null || newStopLoss < position.StopLoss)) + ModifyPosition(position, newStopLoss, position.TakeProfit); + } + } + + private double GetTradeVolume() + { + if (!UseDynamicVolume) + return _volumeInUnits; + + var riskAmount = Account.Balance * RiskPercent / 100.0; + var volumeInLots = riskAmount / (StopLossInPips * Symbol.PipValue); + var units = Symbol.QuantityToVolumeInUnits(volumeInLots); + units = Math.Max(Symbol.VolumeInUnitsMin, Math.Min(Symbol.VolumeInUnitsMax, units)); + return Symbol.NormalizeVolumeInUnits(units, RoundingMode.ToNearest); + } + + private void ClosePositions(TradeType tradeType) + { + foreach (var position in Positions.FindAll(Label)) + { + if (position.TradeType != tradeType) + continue; + + ClosePosition(position); + } + } + } +} diff --git a/Robots/XAUUSD Trend Strategy/XAUUSD Trend Strategy/GoldTrendStrategy.csproj b/Robots/XAUUSD Trend Strategy/XAUUSD Trend Strategy/GoldTrendStrategy.csproj new file mode 100644 index 0000000..29f8790 --- /dev/null +++ b/Robots/XAUUSD Trend Strategy/XAUUSD Trend Strategy/GoldTrendStrategy.csproj @@ -0,0 +1,25 @@ + + + + net6.0 + False + False + + + 7.2 + Debug + AnyCPU + Properties + cAlgo + Gold Trend Strategy + 512 + True + + + + + + + + + diff --git a/Robots/XAUUSD Trend Strategy/XAUUSD Trend Strategy/Properties/AssemblyInfo.cs b/Robots/XAUUSD Trend Strategy/XAUUSD Trend Strategy/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..61b8d57 --- /dev/null +++ b/Robots/XAUUSD Trend Strategy/XAUUSD Trend Strategy/Properties/AssemblyInfo.cs @@ -0,0 +1,20 @@ +using System.Diagnostics; +using System.Reflection; +using System.Runtime.InteropServices; + +[assembly: AssemblyTitle("Gold Trend Strategy")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyProduct("Gold Trend Strategy")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +[assembly: ComVisible(false)] + +[assembly: Guid("9f04b8d8-0ddb-4e77-bb31-abbf3a30c91b")] + +[assembly: AssemblyVersion("1.0.0.0")] +[assembly: AssemblyFileVersion("1.0.0.0")] +#if DEBUG + [assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations)] +#endif From d43272d58ee3ace45f23422fd187c5376e4f387a Mon Sep 17 00:00:00 2001 From: Martin <51429321+martcklm@users.noreply.github.com> Date: Fri, 13 Jun 2025 10:19:57 +0530 Subject: [PATCH 5/5] Replace GoldTrendStrategy with advanced version --- README.md | 2 +- .../Advanced Gold Strategy.sln} | 10 +-- .../AdvancedGoldStrategy.cs} | 90 ++++++++++--------- .../AdvancedGoldStrategy.csproj} | 4 +- .../Properties/AssemblyInfo.cs | 6 +- 5 files changed, 57 insertions(+), 55 deletions(-) rename Robots/{XAUUSD Trend Strategy/XAUUSD Trend Strategy.sln => Advanced Gold Strategy/Advanced Gold Strategy.sln} (59%) rename Robots/{XAUUSD Trend Strategy/XAUUSD Trend Strategy/GoldTrendStrategy.cs => Advanced Gold Strategy/Advanced Gold Strategy/AdvancedGoldStrategy.cs} (64%) rename Robots/{XAUUSD Trend Strategy/XAUUSD Trend Strategy/GoldTrendStrategy.csproj => Advanced Gold Strategy/Advanced Gold Strategy/AdvancedGoldStrategy.csproj} (89%) rename Robots/{XAUUSD Trend Strategy/XAUUSD Trend Strategy => Advanced Gold Strategy/Advanced Gold Strategy}/Properties/AssemblyInfo.cs (75%) diff --git a/README.md b/README.md index 341576a..044b49b 100644 --- a/README.md +++ b/README.md @@ -3,4 +3,4 @@ cBot / Indicator Samples of cTrader Algo API ## Custom Samples -- **GoldTrendStrategy**: Moving average crossover strategy for XAUUSD with RSI confirmation and optional dynamic volume control. +- **GoldAdvancedStrategy**: Combines Supertrend trend detection, MACD momentum confirmation, and RSI filters with dynamic volume and trailing stops for trading XAUUSD. diff --git a/Robots/XAUUSD Trend Strategy/XAUUSD Trend Strategy.sln b/Robots/Advanced Gold Strategy/Advanced Gold Strategy.sln similarity index 59% rename from Robots/XAUUSD Trend Strategy/XAUUSD Trend Strategy.sln rename to Robots/Advanced Gold Strategy/Advanced Gold Strategy.sln index 53a816d..b733f3b 100644 --- a/Robots/XAUUSD Trend Strategy/XAUUSD Trend Strategy.sln +++ b/Robots/Advanced Gold Strategy/Advanced Gold Strategy.sln @@ -1,7 +1,7 @@ Microsoft Visual Studio Solution File, Format Version 11.00 # Visual Studio 2010 -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Gold Trend Strategy", "XAUUSD Trend Strategy\GoldTrendStrategy.csproj", "{9AE93258-B8C0-4BB7-9848-7E4F7C43B555}" +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Gold Advanced Strategy", "Advanced Gold Strategy\AdvancedGoldStrategy.csproj", "{7C9AEC3E-8925-4BA9-9CD3-0D026EB8B452}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -9,10 +9,10 @@ Global Release|Any CPU = Release|Any CPU EndGlobalSection GlobalSection(ProjectConfigurationPlatforms) = postSolution - {9AE93258-B8C0-4BB7-9848-7E4F7C43B555}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {9AE93258-B8C0-4BB7-9848-7E4F7C43B555}.Debug|Any CPU.Build.0 = Debug|Any CPU - {9AE93258-B8C0-4BB7-9848-7E4F7C43B555}.Release|Any CPU.ActiveCfg = Release|Any CPU - {9AE93258-B8C0-4BB7-9848-7E4F7C43B555}.Release|Any CPU.Build.0 = Release|Any CPU + {7C9AEC3E-8925-4BA9-9CD3-0D026EB8B452}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {7C9AEC3E-8925-4BA9-9CD3-0D026EB8B452}.Debug|Any CPU.Build.0 = Debug|Any CPU + {7C9AEC3E-8925-4BA9-9CD3-0D026EB8B452}.Release|Any CPU.ActiveCfg = Release|Any CPU + {7C9AEC3E-8925-4BA9-9CD3-0D026EB8B452}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/Robots/XAUUSD Trend Strategy/XAUUSD Trend Strategy/GoldTrendStrategy.cs b/Robots/Advanced Gold Strategy/Advanced Gold Strategy/AdvancedGoldStrategy.cs similarity index 64% rename from Robots/XAUUSD Trend Strategy/XAUUSD Trend Strategy/GoldTrendStrategy.cs rename to Robots/Advanced Gold Strategy/Advanced Gold Strategy/AdvancedGoldStrategy.cs index 6d99877..72a57df 100644 --- a/Robots/XAUUSD Trend Strategy/XAUUSD Trend Strategy/GoldTrendStrategy.cs +++ b/Robots/Advanced Gold Strategy/Advanced Gold Strategy/AdvancedGoldStrategy.cs @@ -15,35 +15,13 @@ namespace cAlgo.Robots { [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None, AddIndicators = true)] - public class GoldTrendStrategy : Robot + public class GoldAdvancedStrategy : Robot { private double _volumeInUnits; - private SimpleMovingAverage _fastMa; - private SimpleMovingAverage _slowMa; + private Supertrend _supertrend; + private MacdHistogram _macd; private RelativeStrengthIndex _rsi; - [Parameter("Fast MA Source", Group = "Fast MA")] - public DataSeries FastMaSource { get; set; } - - [Parameter("Fast MA Period", DefaultValue = 20, Group = "Fast MA", MinValue = 1)] - public int FastMaPeriod { get; set; } - - [Parameter("Slow MA Source", Group = "Slow MA")] - public DataSeries SlowMaSource { get; set; } - - [Parameter("Slow MA Period", DefaultValue = 50, Group = "Slow MA", MinValue = 1)] - public int SlowMaPeriod { get; set; } - - [Parameter("RSI Period", DefaultValue = 14, Group = "RSI", MinValue = 1)] - public int RsiPeriod { get; set; } - - [Parameter("RSI Oversold", DefaultValue = 45, Group = "RSI", MinValue = 1, MaxValue = 50)] - public int Oversold { get; set; } - - [Parameter("RSI Overbought", DefaultValue = 55, Group = "RSI", MinValue = 50, MaxValue = 100)] - public int Overbought { get; set; } - - [Parameter("Volume (Lots)", DefaultValue = 0.01, Group = "Trade")] public double VolumeInLots { get; set; } @@ -65,9 +43,33 @@ public class GoldTrendStrategy : Robot [Parameter("Take Profit (Pips)", DefaultValue = 200, Group = "Trade", MinValue = 1)] public double TakeProfitInPips { get; set; } - [Parameter("Label", DefaultValue = "GoldTrendStrategy", Group = "Trade")] + [Parameter("Label", DefaultValue = "GoldAdvancedStrategy", Group = "Trade")] public string Label { get; set; } + [Parameter("Supertrend Periods", DefaultValue = 10, Group = "Supertrend", MinValue = 1)] + public int SupertrendPeriods { get; set; } + + [Parameter("Supertrend Multiplier", DefaultValue = 3.0, Group = "Supertrend", MinValue = 0.1)] + public double SupertrendMultiplier { get; set; } + + [Parameter("MACD Long Cycle", DefaultValue = 26, Group = "MACD", MinValue = 1)] + public int MacdLongCycle { get; set; } + + [Parameter("MACD Short Cycle", DefaultValue = 12, Group = "MACD", MinValue = 1)] + public int MacdShortCycle { get; set; } + + [Parameter("MACD Signal Periods", DefaultValue = 9, Group = "MACD", MinValue = 1)] + public int MacdSignalPeriods { get; set; } + + [Parameter("RSI Period", DefaultValue = 14, Group = "RSI", MinValue = 1)] + public int RsiPeriod { get; set; } + + [Parameter("RSI Oversold", DefaultValue = 30, Group = "RSI", MinValue = 1, MaxValue = 50)] + public int Oversold { get; set; } + + [Parameter("RSI Overbought", DefaultValue = 70, Group = "RSI", MinValue = 50, MaxValue = 100)] + public int Overbought { get; set; } + protected override void OnStart() { if (SymbolName != "XAUUSD") @@ -75,30 +77,31 @@ protected override void OnStart() if (!UseDynamicVolume) _volumeInUnits = Symbol.QuantityToVolumeInUnits(VolumeInLots); - _fastMa = Indicators.SimpleMovingAverage(FastMaSource, FastMaPeriod); - _slowMa = Indicators.SimpleMovingAverage(SlowMaSource, SlowMaPeriod); - _rsi = Indicators.RelativeStrengthIndex(Bars.ClosePrices, RsiPeriod); - _fastMa.Result.Line.Color = Color.Gold; - _slowMa.Result.Line.Color = Color.DarkOrange; + _supertrend = Indicators.Supertrend(SupertrendPeriods, SupertrendMultiplier); + _macd = Indicators.MacdHistogram(Bars.ClosePrices, MacdLongCycle, MacdShortCycle, MacdSignalPeriods); + _rsi = Indicators.RelativeStrengthIndex(Bars.ClosePrices, RsiPeriod); } protected override void OnBarClosed() { - var crossUp = _fastMa.Result.HasCrossedAbove(_slowMa.Result, 0); - var crossDown = _fastMa.Result.HasCrossedBelow(_slowMa.Result, 0); - if (Symbol.Spread / Symbol.PipSize > MaxSpreadInPips) return; + var upTrend = _supertrend.UpTrend.Last(0) < Bars.LowPrices.Last(0) && _supertrend.DownTrend.Last(1) > Bars.HighPrices.Last(1); + var downTrend = _supertrend.DownTrend.Last(0) > Bars.HighPrices.Last(0) && _supertrend.UpTrend.Last(1) < Bars.LowPrices.Last(1); + + var macdCrossUp = _macd.Histogram.Last(0) > 0 && _macd.Histogram.Last(1) <= 0; + var macdCrossDown = _macd.Histogram.Last(0) < 0 && _macd.Histogram.Last(1) >= 0; + var volume = GetTradeVolume(); - if (crossUp && _rsi.Result.LastValue > Overbought) + if (upTrend && macdCrossUp && _rsi.Result.LastValue < Oversold) { ClosePositions(TradeType.Sell); ExecuteMarketOrder(TradeType.Buy, SymbolName, volume, Label, StopLossInPips, TakeProfitInPips); } - else if (crossDown && _rsi.Result.LastValue < Oversold) + else if (downTrend && macdCrossDown && _rsi.Result.LastValue > Overbought) { ClosePositions(TradeType.Buy); ExecuteMarketOrder(TradeType.Sell, SymbolName, volume, Label, StopLossInPips, TakeProfitInPips); @@ -112,17 +115,16 @@ protected override void OnTick() foreach (var position in Positions.FindAll(Label)) { - double? newStopLoss; - + double? newStop; if (position.TradeType == TradeType.Buy) - newStopLoss = Symbol.Bid - TrailingStopInPips * Symbol.PipSize; + newStop = Symbol.Bid - TrailingStopInPips * Symbol.PipSize; else - newStopLoss = Symbol.Ask + TrailingStopInPips * Symbol.PipSize; + newStop = Symbol.Ask + TrailingStopInPips * Symbol.PipSize; - if (position.TradeType == TradeType.Buy && (position.StopLoss == null || newStopLoss > position.StopLoss)) - ModifyPosition(position, newStopLoss, position.TakeProfit); - else if (position.TradeType == TradeType.Sell && (position.StopLoss == null || newStopLoss < position.StopLoss)) - ModifyPosition(position, newStopLoss, position.TakeProfit); + if (position.TradeType == TradeType.Buy && (position.StopLoss == null || newStop > position.StopLoss)) + ModifyPosition(position, newStop, position.TakeProfit); + else if (position.TradeType == TradeType.Sell && (position.StopLoss == null || newStop < position.StopLoss)) + ModifyPosition(position, newStop, position.TakeProfit); } } diff --git a/Robots/XAUUSD Trend Strategy/XAUUSD Trend Strategy/GoldTrendStrategy.csproj b/Robots/Advanced Gold Strategy/Advanced Gold Strategy/AdvancedGoldStrategy.csproj similarity index 89% rename from Robots/XAUUSD Trend Strategy/XAUUSD Trend Strategy/GoldTrendStrategy.csproj rename to Robots/Advanced Gold Strategy/Advanced Gold Strategy/AdvancedGoldStrategy.csproj index 29f8790..b6c33ed 100644 --- a/Robots/XAUUSD Trend Strategy/XAUUSD Trend Strategy/GoldTrendStrategy.csproj +++ b/Robots/Advanced Gold Strategy/Advanced Gold Strategy/AdvancedGoldStrategy.csproj @@ -11,7 +11,7 @@ AnyCPU Properties cAlgo - Gold Trend Strategy + GoldAdvancedStrategy 512 True @@ -19,7 +19,7 @@ - + diff --git a/Robots/XAUUSD Trend Strategy/XAUUSD Trend Strategy/Properties/AssemblyInfo.cs b/Robots/Advanced Gold Strategy/Advanced Gold Strategy/Properties/AssemblyInfo.cs similarity index 75% rename from Robots/XAUUSD Trend Strategy/XAUUSD Trend Strategy/Properties/AssemblyInfo.cs rename to Robots/Advanced Gold Strategy/Advanced Gold Strategy/Properties/AssemblyInfo.cs index 61b8d57..7953a1c 100644 --- a/Robots/XAUUSD Trend Strategy/XAUUSD Trend Strategy/Properties/AssemblyInfo.cs +++ b/Robots/Advanced Gold Strategy/Advanced Gold Strategy/Properties/AssemblyInfo.cs @@ -2,16 +2,16 @@ using System.Reflection; using System.Runtime.InteropServices; -[assembly: AssemblyTitle("Gold Trend Strategy")] +[assembly: AssemblyTitle("GoldAdvancedStrategy")] [assembly: AssemblyDescription("")] [assembly: AssemblyConfiguration("")] -[assembly: AssemblyProduct("Gold Trend Strategy")] +[assembly: AssemblyProduct("GoldAdvancedStrategy")] [assembly: AssemblyTrademark("")] [assembly: AssemblyCulture("")] [assembly: ComVisible(false)] -[assembly: Guid("9f04b8d8-0ddb-4e77-bb31-abbf3a30c91b")] +[assembly: Guid("a602b2fa-d783-4cf2-881e-3d927e3b6a8e")] [assembly: AssemblyVersion("1.0.0.0")] [assembly: AssemblyFileVersion("1.0.0.0")]