CBOT is forcing 1 pip stop loss and take profit

Created at 07 Jul 2024, 09:01
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!
DA

darov97

Joined 07.07.2024

CBOT is forcing 1 pip stop loss and take profit
07 Jul 2024, 09:01


Hello, i'm writing a CBot with Chat GPT 4, i just asked to make a bot that buys market when a custom indicator (similar to RSI) gives a certain value and place 10 pip sl and 20 pips tp. but during backtetsing i see that the bot places tp and sl always 1 pip from entry. the code from chat gpt looks fine, i don't understand where is the problem, if is something in the settings or what. btw i will paste the code here using cAlgo.API;
using cAlgo.API.Internals;
using cAlgo.API.Indicators;
using System;

namespace cAlgo.Robots
{
   [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
   public class TRPTradingBot : Robot
   {
       private TripleRangePriceIndicator trpIndicator;

       // Parameters for stop loss and take profit
       [Parameter("Stop Loss Pips", DefaultValue = 10)]
       public int StopLossPips { get; set; }

       [Parameter("Take Profit Pips", DefaultValue = 20)]
       public int TakeProfitPips { get; set; }

       // Parameter for the TRP indicator
       [Parameter("Custom Timeframe", DefaultValue = "H1")]
       public string CustomTimeFrame { get; set; }

       private DateTime lastTradeTime;

       protected override void OnStart()
       {
           Print("Starting TRPTradingBot...");

           try
           {
               trpIndicator = Indicators.GetIndicator<TripleRangePriceIndicator>(CustomTimeFrame);
               Print("TRP Indicator initialized successfully.");
           }
           catch (Exception ex)
           {
               Print("Error initializing TRP Indicator: {0}", ex.Message);
               trpIndicator = null;
           }

           lastTradeTime = DateTime.MinValue;
       }

       protected override void OnBar()
       {
           Print("OnBar called. Current server time: {0}", Server.Time);

           if (trpIndicator == null)
           {
               Print("Error: TRP Indicator is not initialized.");
               return;
           }

           // Get the current TRP value at the close of the bar
           var trpValue = trpIndicator.Result.Last(1);
           Print("Current TRP Value: {0}", trpValue);

           // Check if the TRP value is below or equal to -8 and place a buy order if conditions are met
           if (trpValue <= -8 && Server.Time > lastTradeTime)
           {
               Print("TRP Value condition met. Attempting to execute trade...");
               ExecuteTrade();
               lastTradeTime = Server.Time;
           }
       }

       private void ExecuteTrade()
       {
           // Calculate stop loss and take profit prices
           var stopLossPrice = Symbol.Bid - (StopLossPips * Symbol.PipSize);
           var takeProfitPrice = Symbol.Bid + (TakeProfitPips * Symbol.PipSize);

           // Log calculated prices
           Print("Bid: {0}", Symbol.Bid);
           Print("Stop Loss Price: {0} (Pips: {1})", stopLossPrice, StopLossPips);
           Print("Take Profit Price: {0} (Pips: {1})", takeProfitPrice, TakeProfitPips);

           // Execute market order with calculated SL and TP
           var result = ExecuteMarketOrder(TradeType.Buy, SymbolName, Symbol.QuantityToVolumeInUnits(1), "TRPTradingBot", stopLossPrice, takeProfitPrice);

           if (result.IsSuccessful)
           {
               Print("Buy order placed successfully. Position ID: {0}", result.Position.Id);
           }
           else
           {
               Print("Error placing buy order: {0}", result.Error);
           }
       }

       protected override void OnStop()
       {
           Print("Stopping TRPTradingBot...");
       }
   }
}
 


@darov97
Replies

firemyst
08 Jul 2024, 00:37

Can you provide screen captures showing the placement of the SL and TP during your backtests?

Also, I would alter your print statement after a successful order to include the entry price, SL, and TP prices. 

For example:

 Print("Buy order placed successfully. Position ID: {0}, Entry Price {1}, SL {2}, TP {3}, calculated SL {4}, calculated TP {5}", result.Position.Id, result.EntryPrice, result.StopLoss.GetValueOrDefault(), result.TakeProfit.GetValueOrDefault(), stopLossPrice, takeProfitPrice);

@firemyst

PanagiotisCharalampous
08 Jul 2024, 04:54

Hi there,

ExecuteMarketOrder() doesn't take tp and sl price as a parameter but pips. 

Best regards,

Panagiotis


@PanagiotisCharalampous