Login Page - Create Account

Support Board


Date/Time: Sun, 11 May 2025 04:10:06 +0000



[Programming Help] - Dynamically Update Tick Size

View Count: 253

[2024-09-05 14:46:36]
User471063 - Posts: 7
Hello SC Engineers,

I'm trying to update Tick Size dynamucally that part working but when I place order Target Price Offset and Stop Price Offset stay static not updating but tick size updating correctly.

I think something with order. Can you please help me target and Stop update dynamically?

Thanks for help.

#include "sierrachart.h"
SCDLLName("Dynamically Update Tick Size");

SCSFExport scsf_DynamicallyUpdateTickSize(SCStudyInterfaceRef sc)
{
if (sc.SetDefaults)
{
sc.GraphName = "Dynamically Update Tick Size";
sc.AutoLoop = 1; // Enable automatic looping
sc.Subgraph[0].Name = "ATR"; // Example subgraph name for ATR
sc.Subgraph[0].DrawStyle = DRAWSTYLE_LINE; // Draw style for ATR
sc.Subgraph[0].PrimaryColor = RGB(255, 0, 0); // ATR color
sc.Input[0].Name = "ATR Length";
sc.Input[0].SetInt(14);
sc.Input[1].Name = "ATR Multiplier";
sc.Input[1].SetFloat(1.5f);
sc.Input[2].Name = "Use Bracket Orders";
sc.Input[2].SetYesNo(true);
return;
}

// Input Parameters
int atrLength = sc.Input[0].GetInt();
float atrMultiplier = sc.Input[1].GetFloat();
bool useBracketOrders = sc.Input[2].GetYesNo();

// Get ATR value
float ATR = sc.Subgraph[0][sc.Index]; // Example: Get ATR from Subgraph[0]

// Retrieve Entry Price and calculate target and stop prices
float EntryPrice = sc.BaseData[SC_LAST][sc.Index]; // Example for entry price
float TargetPrice = 5658.50; // Updated target price
float StopPrice = 5629.00; // Updated stop price

// Calculate offsets
float targetPriceOffset = EntryPrice - TargetPrice;
float stopPriceOffset = EntryPrice - StopPrice;

// Convert offsets to ticks
float tickSize = sc.TickSize; // Retrieve tick size from chart
float targetOffsetInTicks = targetPriceOffset / tickSize;
float stopOffsetInTicks = stopPriceOffset / tickSize;

// Create and populate the new order structure
s_SCNewOrder newOrder;
newOrder.OCOGroup1Quantity = sc.TradeWindowOrderQuantity;

// Set order types
newOrder.AttachedOrderStop1Type = SCT_ORDERTYPE_STOP;
newOrder.AttachedOrderTarget1Type = SCT_ORDERTYPE_LIMIT;

// Calculate prices with offsets
newOrder.Target1Price = EntryPrice - targetPriceOffset;
newOrder.Stop1Price = EntryPrice + stopPriceOffset;

// Log message
SCString logMessage;
logMessage.Format("Entry Price: %.2f, Target Price: %.2f, Stop Price: %.2f", EntryPrice, TargetPrice, StopPrice);
logMessage += SCString().Format("Target Offset: %.2f, Stop Offset: %.2f", targetPriceOffset, stopPriceOffset);
logMessage += SCString().Format("Target Offset in Ticks: %.2f, Stop Offset in Ticks: %.2f", targetOffsetInTicks, stopOffsetInTicks);
logMessage += SCString().Format("Target1 Price: %.2f, Stop1 Price: %.2f", newOrder.Target1Price, newOrder.Stop1Price);
sc.AddMessageToLog(logMessage, 0);

// Set the attached orders if configured
if (useBracketOrders)
{
sc.SetAttachedOrders(newOrder);
}
}
[2024-09-06 20:01:24]
ForgivingComputers.com - Posts: 1055
Try this:

// Set price offsets
newOrder.Target1Offset = targetPriceOffset;
newOrder.Stop1Offset = stopPriceOffset;

To post a message in this thread, you need to log in with your Sierra Chart account:

Login

Login Page - Create Account