Status
Open
Budget
100.00 USD
Payment Method
Direct Payment
Job Description
using cAlgo.API;
using cAlgo.API.Indicators;
using cAlgo.API.Trading;
namespace cAlgo.Robots
{
[Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
public class XAGUSD_ScalperBot : Robot
{
[Parameter("EMA Fast Period", DefaultValue = 9)]
public int FastPeriod { get; set; }
[Parameter("EMA Slow Period", DefaultValue = 21)]
public int SlowPeriod { get; set; }
[Parameter("EMA Trend Period", DefaultValue = 200)]
public int TrendPeriod { get; set; }
[Parameter("RSI Period", DefaultValue = 14)]
public int RsiPeriod { get; set; }
[Parameter("Overbought Level", DefaultValue = 70)]
public int Overbought { get; set; }
[Parameter("Oversold Level", DefaultValue = 30)]
public int Oversold { get; set; }
[Parameter("Volume (Lots)", DefaultValue = 0.01)]
public double VolumeInLots { get; set; }
[Parameter("Stop Loss (pips)", DefaultValue = 15)]
public int StopLoss { get; set; }
[Parameter("Take Profit (pips)", DefaultValue = 30)]
public int TakeProfit { get; set; }
[Parameter("Trailing Stop Trigger (pips)", DefaultValue = 10)]
public int TrailingTrigger { get; set; }
[Parameter("Trailing Stop Step (pips)", DefaultValue = 5)]
public int TrailingStep { get; set; }
private ExponentialMovingAverage emaFast;
private ExponentialMovingAverage emaSlow;
private ExponentialMovingAverage emaTrend;
private RelativeStrengthIndex rsi;
protected override void OnStart()
{
emaFast = Indicators.ExponentialMovingAverage(Bars.ClosePrices, FastPeriod);
emaSlow = Indicators.ExponentialMovingAverage(Bars.ClosePrices, SlowPeriod);
emaTrend = Indicators.ExponentialMovingAverage(Bars.ClosePrices, TrendPeriod);
rsi = Indicators.RelativeStrengthIndex(Bars.ClosePrices, RsiPeriod);
}
protected override void OnBar()
{
if (SymbolName != "XAGUSD")
return;
bool isBullishCross = emaFast.Result.Last(1) < emaSlow.Result.Last(1) && emaFast.Result.Last(0) > emaSlow.Result.Last(0);
bool isBearishCross = emaFast.Result.Last(1) > emaSlow.Result.Last(1) && emaFast.Result.Last(0) < emaSlow.Result.Last(0);
bool isTrendUp = Bars.ClosePrices.Last(1) > emaTrend.Result.Last(1);
bool isTrendDown = Bars.ClosePrices.Last(1) < emaTrend.Result.Last(1);
bool isRsiAbove50 = rsi.Result.Last(1) > 50;
bool isRsiBelow50 = rsi.Result.Last(1) < 50;
if (isBullishCross && isTrendUp && isRsiAbove50)
{
ExecuteTrade(TradeType.Buy);
}
else if (isBearishCross && isTrendDown && isRsiBelow50)
{
ExecuteTrade(TradeType.Sell);
}
}
private void ExecuteTrade(TradeType tradeType)
{
double volumeInUnits = Symbol.QuantityToVolumeInUnits(VolumeInLots);
var result = ExecuteMarketOrder(tradeType, SymbolName, volumeInUnits, "XAGUSD_ScalperBot", StopLoss, TakeProfit);
if (result.IsSuccessful)
{
Print("Trade opened successfully!");
}
else
{
Print("Failed to open trade: {0}", result.Error);
}
}
protected override void OnPositionOpened(PositionOpenedEventArgs args)
{
// Start Trailing Stop if needed
if (args.Position.SymbolName == SymbolName)
{
Positions.Find("XAGUSD_ScalperBot")?.ModifyTrailingStop(TrailingTrigger, TrailingStep);
}
}
}
}