diff --git a/README.md b/README.md index 72d3530..249e206 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,4 @@ # cTrader Algo API Samples cBot / Indicator Samples of cTrader Algo API + +## Custom Samples diff --git a/Robots/Advanced Gold Strategy/Advanced Gold Strategy.sln b/Robots/Advanced Gold Strategy/Advanced Gold Strategy.sln new file mode 100644 index 0000000..b733f3b --- /dev/null +++ b/Robots/Advanced Gold Strategy/Advanced Gold 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 Advanced Strategy", "Advanced Gold Strategy\AdvancedGoldStrategy.csproj", "{7C9AEC3E-8925-4BA9-9CD3-0D026EB8B452}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {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 + EndGlobalSection +EndGlobal diff --git a/Robots/Advanced Gold Strategy/Advanced Gold Strategy/AdvancedGoldStrategy.cs b/Robots/Advanced Gold Strategy/Advanced Gold Strategy/AdvancedGoldStrategy.cs new file mode 100644 index 0000000..72a57df --- /dev/null +++ b/Robots/Advanced Gold Strategy/Advanced Gold Strategy/AdvancedGoldStrategy.cs @@ -0,0 +1,154 @@ +// ------------------------------------------------------------------------------------------------- +// +// 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 GoldAdvancedStrategy : Robot + { + private double _volumeInUnits; + private Supertrend _supertrend; + private MacdHistogram _macd; + private RelativeStrengthIndex _rsi; + + [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 = "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") + Print("Warning: This cBot is designed for XAUUSD. Current symbol is {0}.", SymbolName); + + if (!UseDynamicVolume) + _volumeInUnits = Symbol.QuantityToVolumeInUnits(VolumeInLots); + + _supertrend = Indicators.Supertrend(SupertrendPeriods, SupertrendMultiplier); + _macd = Indicators.MacdHistogram(Bars.ClosePrices, MacdLongCycle, MacdShortCycle, MacdSignalPeriods); + _rsi = Indicators.RelativeStrengthIndex(Bars.ClosePrices, RsiPeriod); + } + + protected override void OnBarClosed() + { + 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 (upTrend && macdCrossUp && _rsi.Result.LastValue < Oversold) + { + ClosePositions(TradeType.Sell); + ExecuteMarketOrder(TradeType.Buy, SymbolName, volume, Label, StopLossInPips, TakeProfitInPips); + } + else if (downTrend && 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? newStop; + if (position.TradeType == TradeType.Buy) + newStop = Symbol.Bid - TrailingStopInPips * Symbol.PipSize; + else + newStop = Symbol.Ask + TrailingStopInPips * Symbol.PipSize; + + 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); + } + } + + 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/Advanced Gold Strategy/Advanced Gold Strategy/AdvancedGoldStrategy.csproj b/Robots/Advanced Gold Strategy/Advanced Gold Strategy/AdvancedGoldStrategy.csproj new file mode 100644 index 0000000..b6c33ed --- /dev/null +++ b/Robots/Advanced Gold Strategy/Advanced Gold Strategy/AdvancedGoldStrategy.csproj @@ -0,0 +1,25 @@ + + + + net6.0 + False + False + + + 7.2 + Debug + AnyCPU + Properties + cAlgo + GoldAdvancedStrategy + 512 + True + + + + + + + + + diff --git a/Robots/Advanced Gold Strategy/Advanced Gold Strategy/Properties/AssemblyInfo.cs b/Robots/Advanced Gold Strategy/Advanced Gold Strategy/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..7953a1c --- /dev/null +++ b/Robots/Advanced Gold Strategy/Advanced Gold Strategy/Properties/AssemblyInfo.cs @@ -0,0 +1,20 @@ +using System.Diagnostics; +using System.Reflection; +using System.Runtime.InteropServices; + +[assembly: AssemblyTitle("GoldAdvancedStrategy")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyProduct("GoldAdvancedStrategy")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +[assembly: ComVisible(false)] + +[assembly: Guid("a602b2fa-d783-4cf2-881e-3d927e3b6a8e")] + +[assembly: AssemblyVersion("1.0.0.0")] +[assembly: AssemblyFileVersion("1.0.0.0")] +#if DEBUG + [assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations)] +#endif 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..93a85ba --- /dev/null +++ b/Robots/XAUUSD Trend Strategy/XAUUSD Trend Strategy/GoldTrendStrategy.cs @@ -0,0 +1,169 @@ +// ------------------------------------------------------------------------------------------------- +// +// 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 = position.TradeType == TradeType.Buy + ? Symbol.Bid - TrailingStopInPips * Symbol.PipSize + : 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) + 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