Login Page - Create Account

Support Board


Date/Time: Mon, 21 Apr 2025 17:15:58 +0000



Post From: Block Invalid Orders

[2025-03-02 21:44:32]
Nacido_Del_Sol - Posts: 3
I have been attempting to block buy Limit orders above the current price, block buy stop orders below the current price, and the reverse for sell orders. Really I am doing this because I mess up and click the wrong one sometimes, which is annoying.

I was trying to do this through a study, but I have been unsuccessful. are there any plans to add this feature to the settings? I know it is just me not paying good enough attention to when I am placing orders, and I am trying to get better.

The code I was writing is below. If you also can see where I went wrong, that also would be helpful. It builds it fine, but every time I add it to my chart, it freezes up, which is why I was trying to exit the loop if no orders exist:

#include "sierrachart.h"

SCDLLName("Block Invalid Orders")

void OrderValidation(SCStudyInterfaceRef sc) {
SCString Message;
float LastPrice = sc.Close[sc.Index]; // Get last traded price

int i = 0; // Start index for orders
while (true) {
s_SCTradeOrder Order;
if (!sc.GetOrderByIndex(i, Order)) {
break; // Exit loop when no more orders exist
}

float OrderPrice = Order.Price1; // Order price
int OrderType = Order.OrderTypeAsInt; // Correct order type retrieval
int OrderSide = Order.BuySell; // Buy/Sell type

bool IsInvalidOrder = false;

// Block incorrect Buy and Sell orders
if (OrderType == SCT_ORDERTYPE_LIMIT && OrderSide == BSE_BUY && OrderPrice > LastPrice) {
IsInvalidOrder = true;
Message = "Blocked Buy Limit Order: Cannot place above current price!";
}
else if (OrderType == SCT_ORDERTYPE_STOP && OrderSide == BSE_BUY && OrderPrice < LastPrice) {
IsInvalidOrder = true;
Message = "Blocked Buy Stop Order: Cannot place below current price!";
}
else if (OrderType == SCT_ORDERTYPE_LIMIT && OrderSide == BSE_SELL && OrderPrice < LastPrice) {
IsInvalidOrder = true;
Message = "Blocked Sell Limit Order: Cannot place below current price!";
}
else if (OrderType == SCT_ORDERTYPE_STOP && OrderSide == BSE_SELL && OrderPrice > LastPrice) {
IsInvalidOrder = true;
Message = "Blocked Sell Stop Order: Cannot place above current price!";
}

// If the order is invalid, cancel it and log the message
if (IsInvalidOrder) {
sc.CancelOrder(Order.InternalOrderID);
sc.AddMessageToLog(Message, 1);
}

i++; // Move to the next order in the list
}
}

// Main function
SCSFExport scsf_BlockInvalidOrders(SCStudyInterfaceRef sc) {
if (sc.SetDefaults) {
sc.GraphName = "Block Invalid Orders";
sc.AutoLoop = 0; // Prevent execution on every tick
sc.UpdateAlways = 1; // Run periodically
return;
}

// Run validation every 500 milliseconds
if (sc.UpdateStartIndex == 0 || sc.LastCallToFunction) {
OrderValidation(sc);
}
}