Please help me to fix the code error.

Created at 29 Feb 2024, 09:12
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!
AS

asnavaneeth1

Joined 29.02.2024

Please help me to fix the code error.
29 Feb 2024, 09:12


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

namespace cAlgo.Robots
{
   [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
   public class RenkoTrendBot : Robot
   {
       [Parameter("Base Lot Size", DefaultValue = 0.01, MinValue = 0.01)]
       public double BaseLotSize { get; set; }

       [Parameter("Max Lot Size", DefaultValue = 0.1, MinValue = 0.01)]
       public double MaxLotSize { get; set; }

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

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

       private double currentLotSize;
       private bool isLastTradeWinner;

       protected override void OnStart()
       {
           currentLotSize = BaseLotSize;
       }

       protected override void OnBar()
       {
           int greenBars = 0;
           int redBars = 0;

           for (int i = 1; i <= 6; i++)
           {
               if (Bars.Last(i).Close > Bars.Last(i).Open)
               {
                   greenBars++;
               }
               else if (Bars.Last(i).Close < Bars.Last(i).Open)
               {
                   redBars++;
               }
           }

           if (greenBars == 6)
           {
               ExecuteMarketOrder(TradeType.Buy, SymbolName, Symbol.QuantityToVolume(currentLotSize), "RenkoBuy", StopLossPips, TakeProfitPips);
           }
           else if (redBars == 6)
           {
               ExecuteMarketOrder(TradeType.Sell, SymbolName, Symbol.QuantityToVolume(currentLotSize), "RenkoSell", StopLossPips, TakeProfitPips);
           }
       }

       protected override void OnPositionClosed(PositionClosedEventArgs args)
       {
           isLastTradeWinner = args.Position.GrossProfit > 0;

           if (isLastTradeWinner)
           {
               currentLotSize = Math.Min(currentLotSize + BaseLotSize, MaxLotSize);
           }
           else
           {
               currentLotSize = Math.Max(currentLotSize - BaseLotSize, BaseLotSize);
           }
       }

       protected override void OnError(Error error)
       {
           switch (error.Code)
           {
               case ErrorCode.BadVolume:
                   Print("Invalid Volume amount");
                   Stop();
                   break;
               case ErrorCode.NoMoney:
                   Print("Not enough money to trade");
                   break;
               case ErrorCode.InvalidStopLossTakeProfit:
                   Print("The invalid Stop Loss or Take Profit");
                   break;
               case ErrorCode.TechnicalError:
                   Print("Technical Error. Confirm that the trade command parameters are valid");
                   break;
               case ErrorCode.UnknownSymbol:
                   Print("Unknown symbol");
                   break;
               case ErrorCode.Timeout:
                   Print("Operation timed out");
                   break;
               case ErrorCode.Disconnected:
                   Print("The server is disconnected");
                   break;
               case ErrorCode.InvalidRequest:
                   Print("The invalid request");
                   break;
               default:
                   Print("An unexpected error occurred: ", error.Code);
                   break;
           }
       }
   }
}

 

Please help me what is wrong with this code


@asnavaneeth1
Replies

PanagiotisCharalampous
01 Mar 2024, 08:56

Hi there,

As per the message there is no such position to override. Use Position.Closed method instead.

Best regards,

Panagiotis


@PanagiotisCharalampous