Login Page - Create Account

Support Board


Date/Time: Mon, 21 Apr 2025 13:11:44 +0000



[Programming Help] - SOLVED Issue with function sc.GetOrderForSymbolAndAccountByIndex(...)

View Count: 860

[2025-02-20 19:29:25]
LTSys - Posts: 68
Hi,

Is this the best approach to check for new orders? I was looking for some order event I can listen for or subscribe to but I can't find anything like that in the docs.

TIA


bool firstFunctionCall = true;

char *tradeAccount; // global

SCSFExport scsf_MainFunction(SCStudyInterfaceRef sc) {

// some other code

if(firstFunctionCall) {

tradeAccount = accountInput.GetSelectedCustomString().GetChars();

firstFunctionCall = false;
}

int orderIndex = 0;

s_SCTradeOrder order;

while(sc.GetOrderForSymbolAndAccountByIndex(EMPTY_STRING, tradeAccount, orderIndex, order) != SCTRADING_ORDER_ERROR) {

orderIndex++;

// do something
}
}

Date Time Of Last Edit: 2025-02-25 12:36:41
[2025-02-21 14:42:47]
Sierra_Chart Engineering - Posts: 19285
Do you want to get orders, for the Symbol and Trade Account of the particular chart, or all of them?
Sierra Chart Support - Engineering Level

Your definitive source for support. Other responses are from users. Try to keep your questions brief and to the point. Be aware of support policy:
https://www.sierrachart.com/index.php?l=PostingInformation.php#GeneralInformation

For the most reliable, advanced, and zero cost futures order routing, use the Teton service:
Sierra Chart Teton Futures Order Routing
[2025-02-21 14:57:38]
LTSys - Posts: 68
Do you want to get orders, for the Symbol and Trade Account of the particular chart, or all of them?

Hi, thanks for replying...

I'd like to get instant notification of any type of order action, for all symbols, for a specific account.

At the moment I just put the above study code on any chart... but I don't do any calculations or trade from the study so is a study the wrong approach?

TIA
Date Time Of Last Edit: 2025-02-21 19:47:09
[2025-02-24 18:21:14]
LTSys - Posts: 68
Hi, I noticed the "sc.GetOrderForSymbolAndAccountByIndex(...)" function will all of a sudden just stop looping through orders. If I play around with the chart update interval setting or the Minimum chart update interval setting for ACSIL UpdateAlways the orders will start looping again. Clicking the Apply All or the OK button in the chart settings also seems to get the loop working again. Anyone have any possible ideas why that is happening? Seems like a bug but perhaps some additional flags needs to be set in the default section of the study?



if(sc.SetDefaults) {

sc.AutoLoop = 0;
sc.UpdateAlways = 1;
}

TIA
Date Time Of Last Edit: 2025-02-24 18:50:46
[2025-02-24 19:20:48]
cmet - Posts: 690
In the Trade Service Log, do you see this error: SCTRADING_ORDER_ERROR?
[2025-02-24 19:59:46]
LTSys - Posts: 68
In the Trade Service Log, do you see this error: SCTRADING_ORDER_ERROR?

Hi, thanks for the reply. I don't see a file called "Trade Service Log"?
Date Time Of Last Edit: 2025-02-24 21:02:38
[2025-02-24 20:10:32]
cmet - Posts: 690
main menus Trade > Trade Service Log (Ctrl-Shift-L)

I'm sure there's better solutions but I gotta bail. Here's a couple things to look at:

Beginning from 0, you can increment the OrderIndex parameter by 1 and keep calling sc.GetOrderForSymbolAndAccountByIndex until the function returns SCTRADING_ORDER_ERROR indicating there are no more orders.

Options:

Add sc.RequestTradeOrders();

or maybe

sc.FlagFullRecalculate = 1;
Date Time Of Last Edit: 2025-02-24 20:31:59
[2025-02-24 20:42:13]
LTSys - Posts: 68
I added that flag to the defaults...


if(sc.SetDefaults) {

sc.AutoLoop = 0;
sc.UpdateAlways = 1;
sc.FlagFullRecalculate = 1;
}

And I am now calling "sc.RequestTradeOrders();" before the loop containing "sc.GetOrderForSymbolAndAccountByIndex(...)".

It worked for about 5 loops and then it stopped looping.
Date Time Of Last Edit: 2025-02-24 20:58:28
[2025-02-24 21:01:31]
LTSys - Posts: 68
Look like the function doesn't like the trade account parameter.

If I change this...

while(sc.GetOrderForSymbolAndAccountByIndex(EMPTY_STRING, tradeAccount, orderIndex, order) != SCTRADING_ORDER_ERROR)

To a hard coded trade account it works.

while(sc.GetOrderForSymbolAndAccountByIndex(EMPTY_STRING, "Sim1", orderIndex, order) != SCTRADING_ORDER_ERROR)

This isn't a solution but at least narrowed it down to where the problem is.
Date Time Of Last Edit: 2025-02-24 21:01:52
[2025-02-24 22:56:55]
cmet - Posts: 690
Can see if this keeps the loop from exiting. Other than that, I'm outta ideas. Maybe someone else will jump in.

SCSFExport scsf_PersistentOrderScan(SCStudyInterfaceRef& sc) {
if (sc.SetDefaults) {
sc.AutoLoop = 0;
sc.UpdateAlways = 1;
return;
}

sc.RefreshTradeData();

int orderIndex = 0;
s_SCTradeOrder order;
SCString emptyString;
bool ordersFound = false;

while (true) {
if (sc.GetOrderForSymbolAndAccountByIndex(emptyString, emptyString, orderIndex, order) == SCTRADING_ORDER_ERROR) {
if (!ordersFound) {
sc.FlagFullRecalculate = 1;
}
orderIndex = 0;
continue;
}

ordersFound = true;
orderIndex++;
}
}

[2025-02-24 23:11:42]
LTSys - Posts: 68
see if this keeps the loop from exiting. Other than that, I'm outta ideas. Maybe someone else will jump in.

Thanks for your help, your ideas help me to find the issue.

The value of the global tradeAccount variable was being re-assigned a new value after a while. Hence why it worked sporadically and put me on this lost path trying to find the cause.

The fix was to do a new char[64], a memset, and a memcpy.

Appreciate your effort alot... hopefully I can return the effort some day.


Here is the code that fixed the issue...


bool firstFunctionCall = true;

char *tradeAccount = new char[128]; // global

SCSFExport scsf_MainFunction(SCStudyInterfaceRef sc) {

// some other code

if(firstFunctionCall) {

const char *tmp = accountInput.GetSelectedCustomString().GetChars();
      
memset(tradeAccount, 0x00, 128); // reset buffer
      
memcpy(tradeAccount, tmp, strlen(tmp));

firstFunctionCall = false;
}

// get orders loop...
}

Date Time Of Last Edit: 2025-02-25 12:37:13
[2025-02-25 06:29:11]
seandunaway - Posts: 348
that's really weird i've never seen an input value changing like that

when you step through it what are the tradeAccount and accountInput values changing to?
Date Time Of Last Edit: 2025-02-25 06:31:39
[2025-02-25 12:14:09]
LTSys - Posts: 68
what are the tradeAccount and accountInput values changing to?

The accountInput selected was always "Sim1" and assigned to tradeAccount (which I verified with a log message) but then tradeAccount would have a value of "Stop" or the path to the c++ notepad editor or some other value in memory. Playing around with the chart interval setting would get the function looping through orders again so I thought the issue was related to chart settings.
Date Time Of Last Edit: 2025-02-25 12:27:11
[2025-02-25 13:56:36]
seandunaway - Posts: 348
sounds like your running off the stack frame or static space in your code

my guess is you're using globals instead of persistent variables and different instances of the code are overwriting the values

globals are shared across every instance of your study which is rarely a good idea

are you doing any weird pointer arithmetic (like tradeAccount++) or have dangling pointers?

edit: oh yeah also make sure your tradeAccount string is null terminated
Date Time Of Last Edit: 2025-02-25 14:31:11
[2025-02-26 17:01:18]
LTSys - Posts: 68
edit: oh yeah also make sure your tradeAccount string is null terminated

This was part of the issue as well. Allocating a buffer for tradeAccount and mem setting the buffer to null fixed it.

globals are shared across every instance of your study which is rarely a good idea

I've now taken an OOP approach and moved all logic into classes. The study is now very minimal with a single global variable pointer that points to a main class. If I put that pointer into a persistent variable it would only be visible to that study instance correct?
[2025-02-26 17:53:13]
seandunaway - Posts: 348
correct!

here's a sierrachart snake game example showing similar, putting all variables into a struct (or class) and just persisting a pointer
attachmentsierrasnake.cpp - Attached On 2025-02-26 17:53:08 UTC - Size: 12.68 KB - 63 views
[2025-02-26 22:17:38]
LTSys - Posts: 68
putting all variables into a struct (or class) and just persisting a pointer

Thanks, just the example I was looking for.

The game demo of what sierra chart can do was interesting.

I'm building a trade copier to copy to/from sierra chart to a bunch of other platforms.

https://lumens.llc/trade_copier_sierrachart.html
Date Time Of Last Edit: 2025-03-01 02:48:37
[2025-02-26 22:20:13]
seandunaway - Posts: 348
looks great and i'm sure many people will find useful!

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

Login

Login Page - Create Account