CBOT is forcing 1 pip stop loss and take profit
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...");
}
}
}
firemyst
08 Jul 2024, 00:38 ( Updated at: 08 Jul 2024, 04:51 )
See thread here:
https://ctrader.com/forum/ctrader-algo/44331
@firemyst