Support Board
Date/Time: Mon, 21 Apr 2025 23:45:07 +0000
Converting From MetaTrader EA (Init, DeInit, OnTick) to Sierra Chart study?
View Count: 560
[2025-02-17 19:27:18] |
LTSys - Posts: 68 |
Hi, I am converting some EA from MetaTrader to Sierra Chart and I'm struggling to find some examples of how to replicate a few functions on the Sierra Chart Study side. Basically in MetaTrader they have several functions called: OnInit() // executes when script is started OnDeinit() // executes when script is stopped OnTimer() // executes on an specified interval OnTick() // executes every tick Can someone direct me to some examples of how to do the same in a custom Study? TIA |
[2025-02-17 20:36:06] |
cmet - Posts: 690 |
These might point you in the right direction. OnInit() - sc.SetDefaults OnDeinit() - sc.LastCallToFunction OnTick() - sc.UpdateStartIndex OnTimer() - if there's a built in timer function in ACSIL, I don't know what it is. Probably have to count manually. Maybe something like this: SCSFExport scsf_ModifiableTimer(SCStudyInterfaceRef sc)
{ SCInputRef IntervalSeconds = sc.Input[0]; if (sc.SetDefaults) { sc.GraphName = "Custom Timer Function"; sc.AutoLoop = 0; sc.UpdateAlways = 1; IntervalSeconds.Name = "Interval)"; IntervalSeconds.SetInt(10); // interval (10 seconds) IntervalSeconds.SetIntLimits(1, 3600); // max interval return; } static SCDateTime LastExecutionTime; SCDateTime CurrentTime = sc.CurrentSystemDateTime; int Interval = IntervalSeconds.GetInt(); if (CurrentTime.GetTimeInSeconds() - LastExecutionTime.GetTimeInSeconds() >= Interval) { ExecuteTimedFunction(); LastExecutionTime = CurrentTime; // Reset } } |
[2025-02-17 21:36:47] |
LTSys - Posts: 68 |
Thanks... those 3 worked. Looks like you example for a timed method call may do the trick. Basically I need to call a function every 250 milliseconds. Is the study itself called on a regular interval? Date Time Of Last Edit: 2025-02-17 21:43:39
|
[2025-02-17 21:55:35] |
cmet - Posts: 690 |
If using milliseconds, you want to use SCDateTimeMS. Also, change Seconds to Milliseconds throughout. (IntervalMilliseconds, GetTimeInMilliseconds) ExecuteTimedFunction is just a placeholder for your function/logic. You would replace that with whatever you needed to call. Is the study itself called on a regular interval?
Could you explain what you mean there? From above with a few more notes: SCSFExport scsf_ModifiableTimer(SCStudyInterfaceRef sc)
{ //modifiable interval (milliseconds) SCInputRef IntervalMilliseconds = sc.Input[0]; if (sc.SetDefaults) { sc.GraphName = "Modifiable Timer Function"; sc.AutoLoop = 0; sc.UpdateAlways = 1; IntervalMilliseconds.Name = "Interval Milliseconds"; IntervalMilliseconds.SetInt(250); // interval 250ms IntervalMilliseconds.SetIntLimits(1, 60000); // max ms return; } // store last execution time static SCDateTimeMS LastExecutionTime; SCDateTimeMS CurrentTime = sc.CurrentSystemDateTimeMS; int Interval = IntervalMilliseconds.GetInt(); // get interval in ms // execute on specified interval if ((CurrentTime - LastExecutionTime).GetMilliSeconds() >= Interval) { ExecuteTimedFunction(); // replace with your actual function LastExecutionTime = CurrentTime; // reset } } Date Time Of Last Edit: 2025-02-17 22:01:59
|
[2025-02-17 22:03:48] |
LTSys - Posts: 68 |
Is the study itself called on a regular interval?
These SCSFExport functions seem to be called on some interval when attached to a chart. I assume with every tick? SCSFExport scsf_SomeFunction(SCStudyInterfaceRef sc) { // code } |
[2025-02-17 22:07:28] |
cmet - Posts: 690 |
Yeah, if you have real time data, should be every tick. That's the default.
|
[2025-02-17 22:13:59] |
LTSys - Posts: 68 |
Thanks for clarifying... so back to my other question about some function on a timed 250 millisecond interval. How would that work if these SCSFExport functions only fire on a new tick? Would I have to create a separate thread using C++ to call the function? I appreciate the help... Date Time Of Last Edit: 2025-02-17 22:20:31
|
[2025-02-17 22:18:22] |
cmet - Posts: 690 |
That's what sc.UpdateAlways = 1; is for. Since it's tracking time manually, it doesn't rely on tick interval. Instead, it relies on chart update interval. Just make sure that includes the interval you want to use. |
[2025-02-17 22:26:15] |
LTSys - Posts: 68 |
Instead, it relies on chart update interval.
So that setting is controlled through the chart itself? Or can I control that through the study code example you gave? |
[2025-02-17 22:30:43] |
cmet - Posts: 690 |
Yeah, you can find chart update interval here: Global Settings > General Settings > General There's also a separate setting at the chart level for ACSIL here: Chart Settings > Performance > ACSIL Performance Don't know if you can manage that in study, but not sure why you would want to. Date Time Of Last Edit: 2025-02-17 22:36:15
|
[2025-02-17 22:38:01] |
LTSys - Posts: 68 |
Thanks for clarifying. So the only other way (aside from the chart setting) to get a function to run every 250 milliseconds is to spawn off a separate timed thread using standard C++ when the study is added to a chart? |
[2025-02-17 23:02:33] |
cmet - Posts: 690 |
Other than chart update interval, SCDateTime and scUpdateAlways, yeah. The function above doesn't spawn any additional threads though. ACSIL studies rely on the main chart update thread. It doesn't support multi-threading. std::thread can be hit or miss. You might want to get additional input on that, but that's how I understand it. Date Time Of Last Edit: 2025-02-17 23:02:47
|
[2025-02-17 23:22:56] |
LTSys - Posts: 68 |
Thanks for the info... I'll give the thread approach a try. I'd like to poll the account every 250 milliseconds and catch when trades are executed. The best solution is not to run a study on a chart but instead to have a Start/Stop button in Sierra Chart somewhere that can start/stop this account polling process on a separate thread. In NinjaTrader you can make your own custom addon GUI window... does Sierra Chart have this ability as well? |
[2025-02-17 23:36:59] |
cmet - Posts: 690 |
Don't think you can tie your own background process into SC like that. Might be able to create an ACSIL study that doesn't rely on a chart and add a start/stop button to the right click menu. Other than that, maybe a custom app that polls the API. More pain than it's worth most likely. Anyways, gotta bail. Hope you get it sorted. |
[2025-02-17 23:40:42] |
LTSys - Posts: 68 |
Thanks for the help today... I appreciate it.
|
[2025-02-18 05:40:08] |
seandunaway - Posts: 348 |
with sierrachart you have the entire win32 sdk and c++. you can literally do anything including all of those things the acsil documentation is great and worth a thorough read Date Time Of Last Edit: 2025-02-18 05:40:55
|
[2025-02-18 14:03:27] |
LTSys - Posts: 68 |
with sierrachart you have the entire win32 sdk and c++
Yup, I added my custom win32 dll to the study with zero issues. the acsil documentation is great and worth a thorough read
I'm looking through it, just struggling a bit to find some examples ACSIL study that doesn't rely on a chart
That sounds like what I need for what I'm building. I did a search but couldn't find an example of how to do it... if anyone could share a link it would help alot. TIA Date Time Of Last Edit: 2025-02-18 14:04:09
|
[2025-02-18 14:10:06] |
seandunaway - Posts: 348 |
Advanced Custom Study Interaction With Menus, Control Bar Buttons, Pointer Events Automated Trading From an Advanced Custom Study: sc.GetTradePosition |
[2025-02-18 14:17:35] |
seandunaway - Posts: 348 |
here's a study which does similar things like using a toggle button to enable or disable the study, polling with a desire delay, and querying the position data if you look closely, it shows how to replicate the functionality of each of these from post #1 OnInit() // executes when script is started
OnDeinit() // executes when script is stopped OnTimer() // executes on an specified interval OnTick() // executes every tick relevant: ACSIL Programming Concepts: Study Initialization/Unitialization Date Time Of Last Edit: 2025-02-18 14:56:25
|
![]() |
[2025-02-18 14:25:01] |
LTSys - Posts: 68 |
here's a study which does similar things
Thanks alot, I appreciate your time to help. I'll check it out. Is it an "ACSIL study that doesn't rely on a chart"? From what I understand these studies fire on tick or at some interval based on the chart settings... but what I am trying to do is just start/stop a thread that polls the account's trade positions. So having a study fire off all the time is not required. Date Time Of Last Edit: 2025-02-18 14:25:17
|
[2025-02-18 14:28:41] |
seandunaway - Posts: 348 |
just persist the timestamp of the last call into the study and return early if enough time hasn't elapsed. you're handed full control of the program with c++ and a very generous sc execution context - you can literally do anything including spawn threads, create windows, daemonize servers, write to files, etc (but you won't need to) the werld is your oyster Date Time Of Last Edit: 2025-02-18 14:32:49
|
[2025-02-18 15:53:52] |
LTSys - Posts: 68 |
Thanks...
Date Time Of Last Edit: 2025-02-18 16:08:53
|
[2025-02-18 17:44:52] |
seandunaway - Posts: 348 |
@LTSystems, i doubt you're really wanting another thread here is a more concrete example, that matches your metatrader question #include <sierrachart.h>
#define timer_interval_ms 5000 void OnInit (SCStudyGraphRef sc) { sc.AddMessageToLog("OnInit", 1); } void OnDeinit (SCStudyInterfaceRef sc) { sc.AddMessageToLog("OnDeinit", 1); } void OnTimer (SCStudyInterfaceRef sc) { sc.AddMessageToLog("OnTimer", 1); // add logic to get position data every 5 seconds here } void OnTick (SCStudyInterfaceRef sc) { sc.AddMessageToLog("OnTick", 1); } SCDLLName("concepts") SCSFExport scsf_concepts (SCStudyInterfaceRef sc) { if (sc.SetDefaults) { sc.GraphName = "concepts"; sc.GraphRegion = 0; sc.UpdateAlways = 1; return; } // OnInit int& initialized = sc.GetPersistentIntFast(0); if (!initialized) { OnInit(sc); initialized = 1; } // OnDeinit if (sc.LastCallToFunction) OnDeinit(sc); // OnTimer SCDateTimeMS& last_datetime = sc.GetPersistentSCDateTimeFast(0); SCDateTimeMS now = sc.CurrentSystemDateTimeMS; if (now >= last_datetime + SCDateTime::MILLISECONDS(timer_interval_ms)) { OnTimer(sc); last_datetime = now; } // OnTick OnTick(sc); } Date Time Of Last Edit: 2025-02-18 17:45:37
|
[2025-02-18 17:47:26] |
cmet - Posts: 690 |
Looks like seandunaway gave you the goods. But, with regards to this: ACSIL study that doesn't rely on a chart
Should've said doesn't rely on price updates. You still need a "dummy" chart and can use something static (like INX EOD). You could detach the window and use right click start stop menu item. Verify in message log. Attached is an example of this. Not very sophisticated but might spawn some ideas if you need them. |
![]() |
[2025-02-18 17:57:14] |
cmet - Posts: 690 |
Here's a threaded version from a friend of mine too.
Date Time Of Last Edit: 2025-02-18 17:58:43
|
![]() |
To post a message in this thread, you need to log in with your Sierra Chart account: