DU
Topics
24 Jan 2025, 16:17
62
3
Replies
duynguyenhl.bio
25 Jan 2025, 02:17
( Updated at: 14 Feb 2025, 18:17 )
Use ‘MACD’ instead of ‘Macd’
@duynguyenhl.bio
duynguyenhl.bio
24 Jan 2025, 16:41
( Updated at: 14 Feb 2025, 18:18 )
Your bot only opens Buy positions, and Buy orders are executed Ask price (Symbol.Ask). But the code uses Symbol.bid. You just need to replace symbol.bid with symbol.ask and the code will work
// 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
{
// Prevent opening multiple buy positions
if (Positions.Find("BuyPosition") == null)
{
// Calculate SL and TP based on Ask price for Buy orders
double stopLossPrice = Symbol.Ask - StopLoss * Symbol.PipSize;
double takeProfitPrice = Symbol.Ask + TakeProfit * Symbol.PipSize;
// Open a market buy order
ExecuteMarketOrder(
TradeType.Buy,
SymbolName,
Volume,
"BuyPosition",
stopLossPrice,
takeProfitPrice
);
}
}
}
}
}
@duynguyenhl.bio
duynguyenhl.bio
26 Feb 2025, 01:12
RE: Why is my cbot lagging a few candles than tradingview?
firemyst said:
I fixed it already. Btw, you can find the indicator add to chart on Trading View and click to Source code (nearby name of indicator) to knowing the code is.
@duynguyenhl.bio