Does LUA language have sleep() function?

Moderator: admin

Does LUA language have sleep() function?

Postby 215607364@qq.com » Tue Dec 21, 2010 3:46 am

Does LUA language have sleep() function ?
if Not,is there any function instead it ?
sleep() function is very important for a precision strategy.
215607364@qq.com
 
Posts: 16
Joined: Mon Oct 11, 2010 9:56 pm

Re: Does LUA language have sleep() function?

Postby Apprentice » Tue Dec 21, 2010 3:54 am

For this purpose command host: execute ("stop ") is added,
http://www.fxcodebase.com/documents/Ind ... _stop.html
It is Currently available in beta version
and from January 9. in the regular distribution.
User avatar
Apprentice
FXCodeBase: Confirmed User
 
Posts: 23173
Joined: Thu Dec 31, 2009 11:59 am
Location: Zagreb, Croatia

Re: Does LUA language have sleep() function?

Postby 215607364@qq.com » Tue Dec 21, 2010 4:21 am

but I want to sleep only 10 seconds.not stop the strategy.
I did not find host:execute("run") function.how can I do this?
215607364@qq.com
 
Posts: 16
Joined: Mon Oct 11, 2010 9:56 pm

Re: Does LUA language have sleep() function?

Postby Timon55 » Tue Dec 21, 2010 4:56 am

If you need a sleep() function you can use an additional Lua module ExtensionProposal
http://lua-users.org/wiki/ExtensionProposal
Timon55
FXCodeBase: Confirmed User
 
Posts: 43
Joined: Thu Jul 15, 2010 5:35 am
Location: Omsk

Re: Does LUA language have sleep() function?

Postby Nikolay.Gekht » Wed Dec 22, 2010 10:35 am

sleep-like functionality is extremely not recommended for strategies and indicators. Yielding of the strategy will also yield the whole Trading Station and Marketscope. So, the common rule for the strategies and indicators must be "release the control flow as soon as possible".

If you need to delay some actions for N seconds, I strongly recommend to use timers (setTimer, killTimer calles of core.host:execute("") functions.

So, when you need to yield a strategy for 10 seconds I recommend the following approach (I don't have debugger right now, so probably this code must be polished before using, but idea is the same):

Code: Select all

local yielded = false;
local yieldTimerStart;
local yieldTimer;
local yieldTimerID = 1000; -- 1000 must be unique value amount all "ExtSubscribe", "setTimer", terminal:executeOrder calls

function Update()
    if yielded then
       return ;
   end

   ...
   -- need to yield:
  yielded = true;
  yieldTimerStart = core.now() * 86400;  -- start time in seconds
  yieldTimer = core.host:execute("setTimer", yieldTimerID, 1);
  return ;
end

function AsyncOperationFinished(cookie, success, msg)
   if cookie == yieldTimerID then
       local now = core.now() * 86400;
       if now >= yieldTimerStart + 10 then
           core.host:execute("killTimer", yieldTimer);
           yielded = false;
           ... continue actions after yield
       end
   end
end



I hope this help. Should you have any questions please do not hesitate to ask them.

p.s. If you use helper.lua for strategy - use ExtUpdate and ExtAsyncOperationFinished instead of Update and AsyncOperationFinished.
p.p.s. Moved to development forum.
Nikolay.Gekht
FXCodeBase: Site Admin
 
Posts: 1223
Joined: Wed Dec 16, 2009 6:39 pm
Location: Cary, NC

Re: Does LUA language have sleep() function?

Postby Timon55 » Wed Dec 22, 2010 1:28 pm

Additionaly I want to show two examples of how you can realize sleep in your lua programm.

1. If you need a delay for 10 seconds between Update() function executing.

Code: Select all
local timerId;
local bIsNeedContinue = true;

function Update()
    if bIsNeedContinue then
        bIsNeedContinue = false;
        -- Place your code here
        timerId = core.host:execute("setTimer",101,10);
    end
end

function AsyncOperationFinished(cookie, success, message)
    if cookie == 101 then
        core.host:execute("killTimer",timerId);
        bIsNeedContinue = true;
    end
end


In this example code placed instead of "Place your code here" comment will be executed on next tick with 10 seconds delay

2. You need to delay you program somewhere inside of function

Code: Select all
local co;
local timerId;
local bIsNeedContinue = true;

function Update()
    if bIsNeedContinue then
        bIsNeedContinue = false;
        co = coroutine.create(cor);
        coroutine.resume(co);
        timerId = core.host:execute("setTimer",101,10);
    end
end

function cor()
    -- First part of the function.
    core.host:trace("First part");
    coroutine.yield();
    -- Second part of the function. It will be executed only after 10 seconds
    core.host:trace("Second part");
end

function AsyncOperationFinished(cookie, success, message)
    if cookie == 101 then
        core.host:execute("killTimer",timerId);
        coroutine.resume(co);
        bIsNeedContinue = true;
    end
end


In this example Second part of cor() function will be executed only after 10 seconds delay. Second example is very similar to Nickolay's approach but there is no necessity to divide function for two parts.
Timon55
FXCodeBase: Confirmed User
 
Posts: 43
Joined: Thu Jul 15, 2010 5:35 am
Location: Omsk

Re: Does LUA language have sleep() function?

Postby Karlo_Karlo » Fri Dec 11, 2015 6:45 am

Hello Timon55,

I tried your code #2, but the error comes out: "attempt to yield across C-call boundary".

How can i fix it?

Thank you
Karlo
Karlo_Karlo
Karlo_Karlo
 
Posts: 11
Joined: Fri Jan 07, 2011 4:48 am


Return to Indicator Development

Who is online

Users browsing this forum: No registered users and 2 guests