How to insert delay - Please help.

GuluGulu

Well-Known Member
#1
Dear All,

Happy Dushera to you all.

I need a little for 'AlertIF' function help as I am very weak in Amibroker coding.

Consider the following example formula:

Code:
Buy = Cross( MACD(), Signal() );
Sell = Cross( Signal(), MACD() );
Short = Sell;
Cover = Buy;

barcomplete = BarIndex() < LastValue(BarIndex());


AlertIf( barcomplete AND Short, "EXEC Calc.exe", "Launching external application", 3);
AlertIf( barcomplete AND Cover, "EXEC Notepad.exe", "Launching external application", 3);
Here an external application, for example, Calc.exe or Notepad.exe will execute when the signal bar will complete, i.e., at the beginning of the next bar.

Now what I want is to insert some delay before this EXE triggeres, i.e. Signal bar completes, wait 5 seconds, and then execute the EXE files; something like:

Code:
Buy = Cross( MACD(), Signal() );
Sell = Cross( Signal(), MACD() );
Short = Sell;
Cover = Buy;

barcomplete = BarIndex() < LastValue(BarIndex());

[COLOR="Blue"]WAIT 5000 MILISECOND[/COLOR]

AlertIf( barcomplete AND Short, "EXEC Calc.exe", "Launching external application", 3);
AlertIf( barcomplete AND Cover, "EXEC Notepad.exe", "Launching external application", 3);
How to achieve this? Please help.

May God Bless all of us,

Warm Regards,
GG
 

KelvinHand

Well-Known Member
#2
Dear All,

Happy Dushera to you all.

I need a little for 'AlertIF' function help as I am very weak in Amibroker coding.

Consider the following example formula:

Code:
Buy = Cross( MACD(), Signal() );
Sell = Cross( Signal(), MACD() );
Short = Sell;
Cover = Buy;

barcomplete = BarIndex() < LastValue(BarIndex());


AlertIf( barcomplete AND Short, "EXEC Calc.exe", "Launching external application", 3);
AlertIf( barcomplete AND Cover, "EXEC Notepad.exe", "Launching external application", 3);
Here an external application, for example, Calc.exe or Notepad.exe will execute when the signal bar will complete, i.e., at the beginning of the next bar.

Now what I want is to insert some delay before this EXE triggeres, i.e. Signal bar completes, wait 5 seconds, and then execute the EXE files; something like:

Code:
Buy = Cross( MACD(), Signal() );
Sell = Cross( Signal(), MACD() );
Short = Sell;
Cover = Buy;

barcomplete = BarIndex() < LastValue(BarIndex());

[COLOR="Blue"]WAIT 5000 MILISECOND[/COLOR]

AlertIf( barcomplete AND Short, "EXEC Calc.exe", "Launching external application", 3);
AlertIf( barcomplete AND Cover, "EXEC Notepad.exe", "Launching external application", 3);
How to achieve this? Please help.

May God Bless all of us,

Warm Regards,
GG

Theoretically

Try ThreadSleep( )

OR

GetPerformanceCounter()
 
Last edited:

KelvinHand

Well-Known Member
#6
Kelvin

It doesn't work. I have just tried. Amibroker website says maximum 100ms too. How to apply "GetPerformanceCounter" here? Any insight? I went to Ami website for details, but didn't understand how to implement.

Thanks.
if ThreadSleep(100) is the max limit. then try put 5 ThreadSleep(100).

This one work
PHP:
//Reset Timer
GetPerformanceCounter( True );

//--- Use this way -----------
ThreadSleep(100);
ThreadSleep(100);
ThreadSleep(100);
ThreadSleep(100);
ThreadSleep(100);

//---  OR this way, also work
// for(i=0; i<5; i++)  ThreadSleep(100);

elapsed =GetPerformanceCounter();

_TRACE("elapsed = " + elapsed);

Try the JScript in the AFL.
The GetPerformanceCounter() use to measure the timing.

This one also work.
PHP:
//Reset Timer
GetPerformanceCounter( True );

//--- Kelvinhand:  Use this part
EnableScript("jscript");

<%
function sleep(milliseconds) {
  var start = new Date().getTime();
  for (var i = 0; i < 1e7; i++) {
    if ((new Date().getTime() - start) > milliseconds){
      break;
    }
  }
}
sleep(500);

%>

//--- Kelvinhand -----
elapsed =GetPerformanceCounter();

_TRACE("elapsed = " + elapsed);

Try GetPerformanceCounter()
http://www.amibroker.org/userkb/2007/11/10/high-precision-delay-and-interval-timing/
 
Last edited:

GuluGulu

Well-Known Member
#7
if ThreadSleep(100) is the max limit. then try put 5 ThreadSleep(100).

This one work
PHP:
//Reset Timer
GetPerformanceCounter( True );

//--- Use this way -----------
ThreadSleep(100);
ThreadSleep(100);
ThreadSleep(100);
ThreadSleep(100);
ThreadSleep(100);

//---  OR this way, also work
// for(i=0; i<5; i++)  ThreadSleep(100);

elapsed =GetPerformanceCounter();

_TRACE("elapsed = " + elapsed);

Try the JScript in the AFL.
The GetPerformanceCounter() use to measure the timing.

This one also work.
PHP:
//Reset Timer
GetPerformanceCounter( True );

//--- Kelvinhand:  Use this part
EnableScript("jscript");

<%
function sleep(milliseconds) {
  var start = new Date().getTime();
  for (var i = 0; i < 1e7; i++) {
    if ((new Date().getTime() - start) > milliseconds){
      break;
    }
  }
}
sleep(500);

%>

//--- Kelvinhand -----
elapsed =GetPerformanceCounter();

_TRACE("elapsed = " + elapsed);
Hello Kelvin,


Thank you for your help. Anyway, I just experimented with the Getperformancecounter Javascript part. Put it before the 'AlertIF...' line. It seems the whole system is slowed down by amount of time specified...but it did not accomplish the job.

I know I have done a mistake putting the code. Can you please once again tell me WHERE EXACTLY to put what code in the below example script.:

Code:
Buy = Cross( MACD(), Signal() );
Sell = Cross( Signal(), MACD() );
Short = Sell;
Cover = Buy;

barcomplete = BarIndex() < LastValue(BarIndex());

[COLOR="Blue"]WHERE EXACTLY TO PUT CODE PLEASE[/COLOR]

AlertIf( barcomplete AND Short, "EXEC Calc.exe", "Launching external application", 3);
AlertIf( barcomplete AND Cover, "EXEC Notepad.exe", "Launching external application", 3);
 

GuluGulu

Well-Known Member
#10
Kelvin,

In this example page:
http://www.amibroker.com/library/detail.php?id=1186&hilite=GetPerformanceCounter

I got the following script snippet:

Code:
// Wait until order actually PreSubmitted - this will prevent
submitting another order too quickly
             //     hopefully stops TWS/IB getting in a knot

            tradetime=GetPerformanceCounter()/1000; 
            while ((GetPerformanceCounter()/1000 - tradetime) < 5) // give up
after 5 seconds
            {
                ibc.Reconnect();  //Refreshes ibc, and gets accurate status
                ORderStatus = ibc.GetStatus( OID, True);
                if (ORderStatus == "PreSubmitted" || ORderStatus == "Submitted"
|| ORderStatus == "Filled")
                    break;
Here that GetPerformanceCounter is used for 5 second time. Can similar things be used here too?
 

Similar threads

Broker Special Offers

Intraday Higher Leverage

Save up to 90% in brokerage and get higher leverage for intraday trades.

Name:Phone:
Email:City:
State:
Are you a day trader?