New to Algos in cTrade need coding help please
New to Algos in cTrade need coding help please
07 Nov 2024, 06:00
Hi,
I've worked with algos a bit, but i'm no coder, and i'm new to cTrader algo and c#. I used chat GPT and then just seaarched and fixed build errors to create my first simple algo, but it is not trading. I've looked at a sample algo and there about a million more lines of code i dont understand, and probably never will. I'm sure i'm missing setup code. Can someone please help?
// Import necessary namespaces
using cAlgo.API;
using cAlgo.API.Indicators;
namespace cAlgo.Robots
{
// Define the bot class
[Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
public class EMAcBotPreviousBarWithSLTP : Robot
{
// Define the EMAs as indicators
private ExponentialMovingAverage _ema20;
private ExponentialMovingAverage _ema50;
private ExponentialMovingAverage _ema100;
// Define parameters for the bot
[Parameter("Volume", DefaultValue = 1, MinValue = 1)]
public int Volume { get; set; }
[Parameter("Stop Loss (pips)", DefaultValue = 1.00, MinValue = 0.1)]
public double StopLoss { get; set; } // Stop loss in pips
[Parameter("Take Profit (pips)", DefaultValue = 3.00, MinValue = 0.1)]
public double TakeProfit { get; set; } // Take profit in pips
// Called when the bot starts
protected override void OnStart()
{
// Initialize the EMAs with the specified periods
_ema20 = Indicators.ExponentialMovingAverage(Bars.ClosePrices, 20);
_ema50 = Indicators.ExponentialMovingAverage(Bars.ClosePrices, 50);
_ema100 = Indicators.ExponentialMovingAverage(Bars.ClosePrices, 100);
}
// Called on each new tick (every time a new bar is formed)
protected override void OnBar()
{
// Make sure there are at least two bars (to check the previous one)
if (Bars.ClosePrices.Count < 2)
return;
// Get values of the previous bar (bar at index 1)
double previousClose = Bars.ClosePrices[1]; // Close of the previous bar
double previousEma20 = _ema20.Result[1]; // 20-period EMA on the previous bar
double previousEma50 = _ema50.Result[1]; // 50-period EMA on the previous bar
double previousEma100 = _ema100.Result[1]; // 100-period EMA on the previous bar
// Check if the conditions for a buy are met
if (previousClose > previousEma20 && // Previous close > 20 EMA
previousEma20 > previousEma50 && // 20 EMA > 50 EMA
previousEma50 > previousEma100) // 50 EMA > 100 EMA
{
// If conditions are met, execute a buy order
if (Positions.Find("BuyPosition") == null) // Prevent opening multiple buy positions
{
// Calculate the Stop Loss and Take Profit in price units (not pips)
double stopLossPrice = Symbol.Bid - StopLoss * Symbol.PipSize;
double takeProfitPrice = Symbol.Bid + TakeProfit * Symbol.PipSize;
// Open a market buy order with the specified volume, SL and TP
ExecuteMarketOrder(TradeType.Buy, SymbolName, Volume, "BuyPosition", stopLossPrice, takeProfitPrice);
}
}
}
}
}
PanagiotisCharalampous
07 Nov 2024, 06:58
Hi there,
If you don't know how to program, it's better to assign the job to a professional.
Best regards,
Panagiotis
@PanagiotisCharalampous