Delay Order (On Purpose)  [SOLVED]

Questions about MultiCharts and user contributed studies.
Zoakes
Posts: 14
Joined: 23 Jul 2019
Has thanked: 1 time

Delay Order (On Purpose)

Postby Zoakes » 23 Jul 2019

I'm doing some testing to determine latency requirements for capacity, and want to include a delay into my order to determine the effects.

I'm currently using something I found on the forum, a DLL function import of MS 'Sleep([seconds])' function.
Wanted to make sure this was correct, I kind of doubled up on logic to be sure. I'm trying to use microseconds, and eventually add in variance to the latency randomly to simulate actual conditions. First things first :

Code: Select all

//[function_name = sleep_delay, BOOLEAN]

inputs:
delay_in_us(numericsimple);
//varBool(0),
//var_plus_minus(50);

vars:
s(0),
beg_delay(0),
end_delay(0);

//Sleep Function import from DLL
DEFINEDLLFUNC: ThreadSafe,"Kernel32.dll",void,"Sleep",dword;
//In Milliseconds, divide by 1k to convert to microseconds
Sleep(delay_in_us/1000);


if time <> time[1] then
begin
beg_delay = millisecondsfromdatetime(computerdatetime);
end_delay = beg_delay+.25 //250us in milliseconds
Sleep(delay_in_us);
if millisecondsfromdatetime(computerdatetime) > end_delay then sleep_delay = True;
end
else
sleep_delay = False;

// In Strategy Code (Tick data):

vars: run(false);

if LongCondition then
begin
run = sleep_delay(250);
if run then buy this bar at close;
end;
//Same for short.

Let me know if I'm missing something -- My thoughts:
If it's buying on a close, I may be waiting for a close regardless?
(I've heard there's ms data somewhere, don't know where to find if so)
Want to confirm it's running before every order, not just once then caching.

Thanks!

Zach

User avatar
Svetlana MultiCharts
Posts: 645
Joined: 19 Oct 2017
Has thanked: 3 times
Been thanked: 163 times

Re: Delay Order (On Purpose)  [SOLVED]

Postby Svetlana MultiCharts » 02 Aug 2019

Hello, Zach,

I’m afraid, this method is not suitable for MultiCharts, as tick cache feature doesn’t let studies to skip ticks. Therefore, a study will calculate on all ticks, and a delay will not be emulated.
You can set your own communication delay in the Paper Trader and use it as a simulated broker to achieve your goal:
https://www.multicharts.com/trading-sof ... per_Trader

Zoakes
Posts: 14
Joined: 23 Jul 2019
Has thanked: 1 time

Re: Delay Order (On Purpose)

Postby Zoakes » 02 Aug 2019

Thanks ! This is perfect.


well that was alot of code for nothing : /

Zach

amw_775
Posts: 23
Joined: 19 Apr 2020
Has thanked: 1 time
Been thanked: 12 times

Re: Delay Order (On Purpose)

Postby amw_775 » 02 Jul 2022

Hi Thats really simple. You can use the code below

For Delaying in Seconds

Code: Select all

inputs: SecondDelay(3); vars: start(0), end(0) ; start= currenttime_s; end= currenttime_s; While end - start > SecondDelay begin end = currenttime_s; end; // Place your Order code here // Buy Next bar at market

For Delay in MilliSeconds:

Code: Select all

DEFINEDLLFUNC: "kernel32.dll", int, "GetTickCount"; inputs: milliSecondDelay(3000); vars: start(0), end(0) ; start= GetTickCount; end= GetTickCount; While end - start > milliSecondDelay begin end= GetTickCount; end; // Place your Order code here // Buy Next bar at market ;


Return to “MultiCharts”