Taking trade based on time

Hi,
I am coding a strategy which is supposed to fire a buy/sell only after end of every hour based of some conditions. How can I fix the times at which an order can be fired

You can fix the time in a number of ways.

  1. If you just want to fire at the close of hourly bar, then just Ref() the trade by one bar
someBuy = 1;
Buy = Ref( someBuy, -1);
  1. If you want to FIX the times, as in hard-coded by some logic, explain it in detail.

You can use TimeNum() or write logic based on Exchange/System/Local time.
Eg.

// Exit all Short positions before market close
tn = TimeNum();
someCover = // Cover condition.
Cover = someCover OR tn >= 155000;
1 Like

@klivish

Check out this page
http://www.amibroker.com/kb/2015/11/29/how-to-execute-part-of-the-formula-only-when-new-bar-is-added/

I tried using TimeNum() function but it does not seem to be working in backtest.

tn=TimeNum();
BUY= Cross(MA(c,5), MA(c,10)) and tn=140000

From what I understood, this should execute order only when the crossover condition is true and time is 2pm, right?

Your code is incorrect

= is the ASSIGNMENT operator, comparison is ==

http://www.amibroker.com/guide/a_mistakes.html