Login Page - Create Account

Support Board


Date/Time: Mon, 21 Apr 2025 16:59:26 +0000



Post From: Code error

[2025-02-13 16:34:45]
cmet - Posts: 690
How are you entering the other two orders?

Looks like you're going long and entering a Sell stop with a Buy order? Also try adding stop price.

This might point you in the right direction:

// Trailing Stop Logic
if (PositionData.PositionQuantity != 0 && sc.Index > 0)
{
if (PositionData.PositionQuantity > 0) // Long position
{
float newStopPrice = sc.Low[sc.Index - 1]; // Previous bar's low

// Place initial stop if it hasn’t been set yet
if (r_TrailingStopPrice == 0.0f)
{
r_TrailingStopPrice = newStopPrice; // Set initial stop
}

// Only move stop up
if (newStopPrice > r_TrailingStopPrice)
{
s_SCNewOrder UpdatedStop;
UpdatedStop.OrderQuantity = PositionData.PositionQuantity;
UpdatedStop.OrderType = SCT_ORDERTYPE_STOP;
UpdatedStop.TimeInForce = SCT_TIF_GOOD_TILL_CANCELED;
UpdatedStop.Price1 = newStopPrice; // Set Stop Price

// Submit stop order
int Result = sc.SellOrder(UpdatedStop);

if (Result > 0)
{
r_TrailingStopPrice = newStopPrice; // Update stop price

SCString message;
message.Format("Updated Long Stop to %.2f at index %d", newStopPrice, sc.Index);
sc.AddMessageToLog(message.GetChars(), 0);
}
else
{
SCString errorMsg;
errorMsg.Format("Trailing Stop FAILED at index %d with result %d", sc.Index, Result);
sc.AddMessageToLog(errorMsg.GetChars(), 1);
}
}

// Check if stop is hit
if (sc.Low[sc.Index] <= r_TrailingStopPrice)
{
sc.FlattenPosition();
}
}
}

Date Time Of Last Edit: 2025-02-13 18:01:56