public class TradingStrategy { public decimal LastPrice { get; set; } public decimal TickSize { get; set; } public decimal LowerCircuitLimit { get; set; }
public (decimal StopLoss, decimal Target) CalculateStopLossAndTarget() { decimal stopLoss; decimal target; decimal minStopLoss; decimal buffer = 0.002m; // Adding a small buffer to ensure compliance
// Determine the minimum stop-loss based on the last price if (LastPrice <= 50) { minStopLoss = 0.09m + buffer; } else { minStopLoss = LastPrice * 0.0025m + buffer; // 0.25% of the last price }
// Calculate the stop-loss and ensure it respects the minimum stop-loss stopLoss = LastPrice - minStopLoss; stopLoss = Math.Floor(stopLoss / TickSize) * TickSize;
// Ensure the stop-loss is above the lower circuit price if (stopLoss < LowerCircuitLimit) { stopLoss = Math.Ceiling(LowerCircuitLimit / TickSize) * TickSize; }
// Calculate the target price and adjust for tick size target = LastPrice * 1.02m; target = Math.Ceiling(target / TickSize) * TickSize;
// Logging or comments for better understanding and maintenance Console.WriteLine($"LastPrice: {LastPrice}, MinStopLoss: {minStopLoss}, CalculatedStopLoss: {stopLoss}, Target: {target}");
return (stopLoss, target); } }
public static int BuyShareAndSetStoplossAndTargetPrice(out int returnQuantity) { decimal totalAvilableAmount = 0; int quantity = 0; string exchange = "NSE"; string tradingSymbol = ""; Console.WriteLine("Enter Trading Symbol value"); tradingSymbol = Console.ReadLine().Trim();
// Place buy order PlaceBuyOrder(tradingSymbol, quantity, lastPrice);
////Set Stop Loss and Target Price using GTT var gttTriggerId = SetStopLossAndTargetPrice(tradingSymbol, lastPrice, result.StopLoss, result.Target, quantity);