ANY ERROR ON THIS CBOT CODE.-

Created at 21 Oct 2024, 08:43
How’s your experience with the cTrader Platform?
Your feedback is crucial to cTrader's development. Please take a few seconds to share your opinion and help us improve your trading experience. Thanks!
AT

athanasopoulos684

Joined 21.10.2024

ANY ERROR ON THIS CBOT CODE.-
21 Oct 2024, 08:43


The problem I am facing is that while I have implemented this strategy when it is SET UP to take a buy or sell position it does not take these positions. Can you tell me the reason?

 

 

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

namespace cAlgo.Robots
{
   [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
   public class MovingAverageCrossoverBot : Robot
   {
       // Declare parameters for the fast and slow moving averages
       [Parameter("Fast MA Period", DefaultValue = 9)]
       public int FastMAPeriod { get; set; }

       [Parameter("Slow MA Period", DefaultValue = 21)]
       public int SlowMAPeriod { get; set; }

       // Adjust volume for a 1000 euro account
       [Parameter("Volume (Units)", DefaultValue = 1000)] // Equivalent to 0.01 lots
       public int Volume { get; set; }

       [Parameter("Stop Loss (pips)", DefaultValue = 30)]
       public double StopLossPips { get; set; }

       [Parameter("Take Profit (pips)", DefaultValue = 60)]
       public double TakeProfitPips { get; set; }

       // Declare the moving averages
       private MovingAverage fastMA;
       private MovingAverage slowMA;

       // Initialize the cBot and indicators
       protected override void OnStart()
       {
           // Initialize fast and slow moving averages using Bars.ClosePrices
           fastMA = Indicators.MovingAverage(Bars.ClosePrices, FastMAPeriod, MovingAverageType.Simple);
           slowMA = Indicators.MovingAverage(Bars.ClosePrices, SlowMAPeriod, MovingAverageType.Simple);
       }

       // Main function that runs on every tick
       protected override void OnTick()
       {
           // Get the current and previous moving average values
           double fastMA_Current = fastMA.Result.LastValue;
           double slowMA_Current = slowMA.Result.LastValue;
           double fastMA_Previous = fastMA.Result[1];
           double slowMA_Previous = slowMA.Result[1];

           // Check for buy signal: Fast MA crosses above Slow MA
           if (fastMA_Previous < slowMA_Previous && fastMA_Current > slowMA_Current)
           {
               if (Positions.Count == 0 || Positions.Find("buy") == null)
               {
                   Print("Buy signal detected.");
                   ClosePositions(TradeType.Sell); // Close any open sell positions
                   ExecuteMarketOrder(TradeType.Buy, SymbolName, Volume, "buy", StopLossPips, TakeProfitPips);
               }
           }

           // Check for sell signal: Fast MA crosses below Slow MA
           if (fastMA_Previous > slowMA_Previous && fastMA_Current < slowMA_Current)
           {
               if (Positions.Count == 0 || Positions.Find("sell") == null)
               {
                   Print("Sell signal detected.");
                   ClosePositions(TradeType.Buy); // Close any open buy positions
                   ExecuteMarketOrder(TradeType.Sell, SymbolName, Volume, "sell", StopLossPips, TakeProfitPips);
               }
           }
       }

       // Function to close positions of a specific trade type
       private void ClosePositions(TradeType tradeType)
       {
           foreach (var position in Positions)
           {
               if (position.TradeType == tradeType)
               {
                   ClosePosition(position);
               }
           }
       }

       // Optional: Stop method, executed when the bot is stopped
       protected override void OnStop()
       {
           // Clean up any resources if necessary
       }
   }
}
 


@athanasopoulos684
Replies

PanagiotisCharalampous
22 Oct 2024, 05:25

Hi there,

This code part

           double fastMA_Previous = fastMA.Result[1];
           double slowMA_Previous = slowMA.Result[1];

does not return the previous value. If you are looking for the previous value, use the below

           double fastMA_Previous = fastMA.Result.Last(1);
           double slowMA_Previous = slowMA.Result.Last(1);

Best regards,

Panagiotis


@PanagiotisCharalampous