Skip to content

Enhance XAUUSD bot #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -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.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@
// profit of any kind. Use it at your own risk.
//
// -------------------------------------------------------------------------------------------------

using System;
using cAlgo.API;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;
using cAlgo.API;
using cAlgo.API.Indicators;

Expand All @@ -19,7 +22,9 @@ public class GoldTrendStrategy : Robot
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; }

Expand All @@ -41,6 +46,36 @@ public class GoldTrendStrategy : Robot
[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("Volume (Lots)", DefaultValue = 0.01, Group = "Trade")]
public double VolumeInLots { get; set; }

Expand All @@ -58,6 +93,13 @@ 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);
_volumeInUnits = Symbol.QuantityToVolumeInUnits(VolumeInLots);
_fastMa = Indicators.SimpleMovingAverage(FastMaSource, FastMaPeriod);
_slowMa = Indicators.SimpleMovingAverage(SlowMaSource, SlowMaPeriod);
Expand All @@ -69,6 +111,59 @@ protected override void OnStart()

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);
}

var inUptrend = _fastMa.Result.LastValue > _slowMa.Result.LastValue;
var inDowntrend = _fastMa.Result.LastValue < _slowMa.Result.LastValue;

Expand Down