Skip to content

Commit 52b2d99

Browse files
authored
Merge branch 'master' into o82xrr-codex/create-gold-trading-algo-bot-for-ctrader
2 parents 53faede + 581f67b commit 52b2d99

File tree

1 file changed

+25
-2
lines changed

1 file changed

+25
-2
lines changed

Robots/XAUUSD Trend Strategy/XAUUSD Trend Strategy/GoldTrendStrategy.cs

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,12 @@
66
// profit of any kind. Use it at your own risk.
77
//
88
// -------------------------------------------------------------------------------------------------
9-
109
using System;
1110
using cAlgo.API;
1211
using cAlgo.API.Indicators;
1312
using cAlgo.API.Internals;
13+
using cAlgo.API;
14+
using cAlgo.API.Indicators;
1415

1516
namespace cAlgo.Robots
1617
{
@@ -23,7 +24,7 @@ public class GoldTrendStrategy : Robot
2324
private RelativeStrengthIndex _rsi;
2425
private Supertrend _supertrend;
2526
private MacdCrossOver _macd;
26-
27+
2728
[Parameter("Fast MA Source", Group = "Fast MA")]
2829
public DataSeries FastMaSource { get; set; }
2930

@@ -75,6 +76,9 @@ public class GoldTrendStrategy : Robot
7576
[Parameter("Trailing Stop (Pips)", DefaultValue = 50, Group = "Trade", MinValue = 0)]
7677
public double TrailingStopInPips { get; set; }
7778

79+
[Parameter("Volume (Lots)", DefaultValue = 0.01, Group = "Trade")]
80+
public double VolumeInLots { get; set; }
81+
7882
[Parameter("Stop Loss (Pips)", DefaultValue = 100, Group = "Trade", MinValue = 1)]
7983
public double StopLossInPips { get; set; }
8084

@@ -96,6 +100,10 @@ protected override void OnStart()
96100
_rsi = Indicators.RelativeStrengthIndex(Bars.ClosePrices, RsiPeriod);
97101
_supertrend = Indicators.Supertrend(SupertrendPeriods, SupertrendMultiplier);
98102
_macd = Indicators.MacdCrossOver(Bars.ClosePrices, MacdLongCycle, MacdShortCycle, MacdSignalPeriods);
103+
_volumeInUnits = Symbol.QuantityToVolumeInUnits(VolumeInLots);
104+
_fastMa = Indicators.SimpleMovingAverage(FastMaSource, FastMaPeriod);
105+
_slowMa = Indicators.SimpleMovingAverage(SlowMaSource, SlowMaPeriod);
106+
_rsi = Indicators.RelativeStrengthIndex(MarketSeries.Close, RsiPeriod);
99107

100108
_fastMa.Result.Line.Color = Color.Gold;
101109
_slowMa.Result.Line.Color = Color.DarkOrange;
@@ -156,6 +164,21 @@ private double GetTradeVolume()
156164
return Symbol.NormalizeVolumeInUnits(units, RoundingMode.ToNearest);
157165
}
158166

167+
var inUptrend = _fastMa.Result.LastValue > _slowMa.Result.LastValue;
168+
var inDowntrend = _fastMa.Result.LastValue < _slowMa.Result.LastValue;
169+
170+
if (inUptrend && _rsi.Result.LastValue < Oversold)
171+
{
172+
ClosePositions(TradeType.Sell);
173+
ExecuteMarketOrder(TradeType.Buy, SymbolName, _volumeInUnits, Label, StopLossInPips, TakeProfitInPips);
174+
}
175+
else if (inDowntrend && _rsi.Result.LastValue > Overbought)
176+
{
177+
ClosePositions(TradeType.Buy);
178+
ExecuteMarketOrder(TradeType.Sell, SymbolName, _volumeInUnits, Label, StopLossInPips, TakeProfitInPips);
179+
}
180+
}
181+
159182
private void ClosePositions(TradeType tradeType)
160183
{
161184
foreach (var position in Positions.FindAll(Label))

0 commit comments

Comments
 (0)