Take profit price and trailing stop not working
Take profit price and trailing stop not working
12 Feb 2025, 18:03
Considering the code below: whenever the bot places a buy order and applies the values for take profit and trailing stop, the values don't work, but the ctrader platform shows a Take profit value that is twice the entry price. Example Entry price = 6000 and TP = 12000. See attached screenshot
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 US500DropBuyBot : Robot
{
private double _entryPrice;
private double _priceDrop;
private double _takeProfitDistance;
private double _trailingStopDistance;
private AverageTrueRange _atr;
private bool _isWaitingForClose;
protected override void OnStart()
{
_atr = Indicators.AverageTrueRange(14, MovingAverageType.Simple);
Positions.Closed += OnPositionClosedHandler;
_isWaitingForClose = false;
}
protected override void OnTick()
{
if (_isWaitingForClose)
return;
double currentPrice = Symbol.Bid;
double atrValue = _atr.Result.LastValue;
double pipSize = Symbol.PipSize;
// Adjust Take Profit & Trailing Stop Based on Market Volatility
if (atrValue > 50) // High Volatility Market
{
_takeProfitDistance = atrValue * 0.50;
_trailingStopDistance = atrValue * 0.30;
}
else if (atrValue >= 10 && atrValue <= 50) // Moderate Volatility Market
{
_takeProfitDistance = atrValue * 0.30;
_trailingStopDistance = atrValue * 0.20;
}
else // Low Volatility Market
{
_takeProfitDistance = atrValue * 0.15;
_trailingStopDistance = atrValue * 0.10;
}
// Convert ATR values into actual price distances
_takeProfitDistance *= pipSize;
_trailingStopDistance *= pipSize;
_priceDrop = atrValue * 1.50 * pipSize; // ATR multiplier for price drop
Print($"ATR: {atrValue}, TP: {_takeProfitDistance}, SL: {_trailingStopDistance}");
try
{
if (_entryPrice == 0 || _entryPrice - currentPrice >= _priceDrop)
{
double volumeInUnits = Symbol.QuantityToVolumeInUnits(0.1);
double takeProfitPrice = currentPrice + _takeProfitDistance;
var position = ExecuteMarketOrder(TradeType.Buy, SymbolName, volumeInUnits, "Buy", null, takeProfitPrice);
if (position == null)
{
Print("Error: Order execution failed.");
}
else
{
_entryPrice = currentPrice;
_isWaitingForClose = true;
}
}
}
catch (Exception ex)
{
Print("An error occurred during order execution: " + ex.Message);
}
try
{
foreach (var position in Positions)
{
if (position.TradeType == TradeType.Buy)
{
double newStopLossPrice = currentPrice - _trailingStopDistance;
if (position.TakeProfit.HasValue && newStopLossPrice >= position.TakeProfit.Value)
{
newStopLossPrice = position.TakeProfit.Value - pipSize;
}
if (currentPrice > position.EntryPrice + _trailingStopDistance &&
(position.StopLoss == null || position.StopLoss < newStopLossPrice))
{
position.ModifyStopLossPrice(newStopLossPrice);
}
}
}
}
catch (Exception ex)
{
Print("An error occurred while modifying the stop loss: " + ex.Message);
}
}
private void OnPositionClosedHandler(PositionClosedEventArgs args)
{
_entryPrice = 0;
_isWaitingForClose = false;
Print($"Position closed: {args.Position.Id}, P/L: {args.Position.GrossProfit}, Reason: {args.Reason}");
}
protected override void OnStop()
{
Positions.Closed -= OnPositionClosedHandler;
}
}
}
Any help, please.
firemyst
18 Feb 2025, 01:10
You didn't show the output of:
Print($"ATR: {atrValue}, TP: {_takeProfitDistance}, SL: {_trailingStopDistance}");
So basically, nobody knows what the numbers are along the way as it calculates the TP price you're setting. Have you tried debugging the code and/or used Visual studio to run in debug mode to see what the values are along the way to make sure they tee up to what you're expecting?
@firemyst