Login Page - Create Account

Support Board


Date/Time: Mon, 21 Apr 2025 05:21:22 +0000



Post From: Request for Feature Improvement - Custom Trading Command - Offset from Reference Price

[2025-03-14 15:54:56]
E1ite51 - Posts: 3
I've tried to create a custom study script, it is buggy, but works. For some reason though, it doesn't work for a quantity greater than 1. Any help would be appreciated. I'd like it to use the quantity from the trade window. It is successfully using the bracket from the trade window, but any quantity more than 1, does not enter. And I'd like it to stop running after 1 order is entered. For now, it doesn't keep placing more orders, but the script still keeps running with messages of failed further error placements. Any help in bringing this script to the finish line would be appreciated.

---------------------------------------------------------------------------------------

#include "sierrachart.h" // Include the Sierra Chart API

SCDLLName("One-Time Buy Limit Chase Order") // Define the study/DLL name

SCSFExport scsf_BuyLimitChase(SCStudyInterfaceRef sc) // Main study function
{
// ------------------- Setting Defaults -------------------
if (sc.SetDefaults) // This block runs once when the study is loaded to set defaults
{
sc.GraphName = "One-Time Buy Limit Chase Order";
// Name of the study as it appears in the Studies window
sc.StudyDescription = "Places a Buy Limit Chase Order at Last Price - 40 Ticks using Trade Window configuration for quantity and attached orders.";
// Description for documentation purposes
sc.AutoLoop = 1;
// Enable AutoLoop so the study is executed on every new bar/tick
sc.FreeDLL = 1;
// Unload this study from memory after execution (if applicable)

// Enable usage of Trade Window configuration:
sc.SupportAttachedOrdersForTrading = 1;
// Allow the study to use attached (bracket) orders as configured in the Trade Window
sc.UseGUIAttachedOrderSetting = 1;
// Instruct Sierra Chart to use the GUI (Trade Window) settings for bracket orders

return; // Exit the defaults block
}
// ------------------- End Defaults -------------------

// Prevent repeated execution in the same bar
if (sc.LastCallToFunction)
{
return; // Exit if this is the final call for the bar
}

// Only execute the order logic on the most recent bar
if (sc.Index == sc.ArraySize - 1)
{
// Retrieve the last traded price from the current bar
double LastPrice = sc.Close[sc.Index];
// Calculate the offset for the order (40 ticks) - we ignore C4244 warnings as requested
double TickOffset = 40.0 * sc.TickSize;
// Determine the price at which to place the order (40 ticks below last price)
double OrderPrice = LastPrice - TickOffset;

// Use a persistent variable to ensure only one order is placed per study load
int &OrderPlaced = sc.GetPersistentInt(1);
if (OrderPlaced == 1)
{
return; // Exit if an order has already been placed
}

// ------------------- Create New Order -------------------
s_SCNewOrder NewOrder;
// Create a new order object to be submitted

// Set the order quantity to use the value configured in the Trade Window:
NewOrder.OrderQuantity = sc.TradeWindowOrderQuantity;
// This ensures that the quantity from the Trade Window is used (do not hardcode)

// Specify the order type as Limit Chase
NewOrder.OrderType = SCT_ORDERTYPE_LIMIT_CHASE;

// Set the order price calculated above (40 ticks below the last traded price)
NewOrder.Price1 = OrderPrice;

// Set the order's time in force to Good-Til-Canceled (GTC)
NewOrder.TimeInForce = SCT_TIF_GOOD_TILL_CANCELED;
// ------------------- End New Order Creation -------------------

// Submit the order using BuyEntry. The attached orders (bracket orders) and quantity will be taken from the Trade Window settings.
int Result = sc.BuyEntry(NewOrder);

if (Result > 0)
{
sc.AddMessageToLog("✅ Buy Limit Chase Order Placed at Last Price - 40 Ticks (Using Trade Window Config)", 1);
// Log success if the order submission returns a positive result
OrderPlaced = 1;
// Mark that an order has been placed so that duplicate orders are prevented
}
else
{
sc.AddMessageToLog("❌ Order Submission Failed", 1);
// Log failure if the order submission returns 0 or a negative value
}
}
}