Skip to content

Commit ba659cf

Browse files
authored
Merge pull request #4 from martcklm/x5xkuk-codex/create-gold-trading-algo-bot-for-ctrader
Enhance XAUUSD bot
2 parents fd920f6 + 58a3548 commit ba659cf

File tree

1 file changed

+11
-35
lines changed

1 file changed

+11
-35
lines changed

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

Lines changed: 11 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,11 @@
66
// profit of any kind. Use it at your own risk.
77
//
88
// -------------------------------------------------------------------------------------------------
9+
910
using System;
1011
using cAlgo.API;
1112
using cAlgo.API.Indicators;
1213
using cAlgo.API.Internals;
13-
using cAlgo.API;
14-
using cAlgo.API.Indicators;
1514

1615
namespace cAlgo.Robots
1716
{
@@ -24,7 +23,7 @@ public class GoldTrendStrategy : Robot
2423
private RelativeStrengthIndex _rsi;
2524
private Supertrend _supertrend;
2625
private MacdCrossOver _macd;
27-
26+
2827
[Parameter("Fast MA Source", Group = "Fast MA")]
2928
public DataSeries FastMaSource { get; set; }
3029

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

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

@@ -95,15 +91,12 @@ protected override void OnStart()
9591

9692
if (!UseDynamicVolume)
9793
_volumeInUnits = Symbol.QuantityToVolumeInUnits(VolumeInLots);
94+
9895
_fastMa = Indicators.SimpleMovingAverage(FastMaSource, FastMaPeriod);
9996
_slowMa = Indicators.SimpleMovingAverage(SlowMaSource, SlowMaPeriod);
10097
_rsi = Indicators.RelativeStrengthIndex(Bars.ClosePrices, RsiPeriod);
10198
_supertrend = Indicators.Supertrend(SupertrendPeriods, SupertrendMultiplier);
10299
_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);
107100

108101
_fastMa.Result.Line.Color = Color.Gold;
109102
_slowMa.Result.Line.Color = Color.DarkOrange;
@@ -136,14 +129,14 @@ protected override void OnBarClosed()
136129

137130
protected override void OnTick()
138131
{
132+
if (TrailingStopInPips <= 0)
133+
return;
134+
139135
foreach (var position in Positions.FindAll(Label))
140136
{
141-
double? newStopLoss;
142-
143-
if (position.TradeType == TradeType.Buy)
144-
newStopLoss = Symbol.Bid - TrailingStopInPips * Symbol.PipSize;
145-
else
146-
newStopLoss = Symbol.Ask + TrailingStopInPips * Symbol.PipSize;
137+
double? newStopLoss = position.TradeType == TradeType.Buy
138+
? Symbol.Bid - TrailingStopInPips * Symbol.PipSize
139+
: Symbol.Ask + TrailingStopInPips * Symbol.PipSize;
147140

148141
if (position.TradeType == TradeType.Buy && (position.StopLoss == null || newStopLoss > position.StopLoss))
149142
ModifyPosition(position, newStopLoss, position.TakeProfit);
@@ -164,29 +157,12 @@ private double GetTradeVolume()
164157
return Symbol.NormalizeVolumeInUnits(units, RoundingMode.ToNearest);
165158
}
166159

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-
182160
private void ClosePositions(TradeType tradeType)
183161
{
184162
foreach (var position in Positions.FindAll(Label))
185163
{
186-
if (position.TradeType != tradeType)
187-
continue;
188-
189-
ClosePosition(position);
164+
if (position.TradeType == tradeType)
165+
ClosePosition(position);
190166
}
191167
}
192168
}

0 commit comments

Comments
 (0)