SOD or EOD trading

Hi everyone,

I've tried looking through the forums and can't seem to find my problem asked anywhere else, although I will admit I'm not exactly sure what the problem I'm currently facing is. Essentially I've got trading strategy that are meant to generate buy/sell/short/cover signals either right when the market opens or right before the market closes. I've been trying to use the TimeNum function with AND statements to ensure the time range is in the first/last 15 seconds of the trading day so that signals are only generated at the open or close. I've tried attaching the time function to part of an If statement and also to part of the buy command but once I do signals are no longer generated (even during the proper time). I believe the issue is that no value is actually being added to my variable TN but I'm not sure how to check/fix this problem. Any suggestions would be fantastic!

Just trading strategy (generic strategy used)

tn = TimeNum ();
opentime = 093015 ;
closetime = 160000 ;
starttime = 083000 ;
endtime = 155945 ;
_buy = 0 ;
_sell = 0 ;
_short = 0;
_cover = 0;
         
 
 
if (Name () == "SPY")
 
{
 
 
_buy = Close > 0 AND tn > closetime AND tn < endtime ;
 
_sell = Close>Ref(H,-1) AND tn > closetime AND tn < endtime ;
}

To get better understanding of what is happening in your code and how functions work, use advice given here: How do I debug my formula?

Thank you for the suggestion I've now identified the problem is that TimeNum () is returning 0 (or is not actually finding the current time).

Does anyone have any ideas why this may be occurring? I'm currently using data from IB and have everything set to daily candles. I'm trying to code so that my trades either buy or sell at the open/close (using a time function I would be selling in the first/last 15 seconds of the day).

Thank you in advance

It would return 0 if you run on DAILY bars. You need intraday intervals to get TimeNum() returning non-zero values.

Hi Tomasz, is there a way to return the current time while still using daily bars?

Hi everyone,

I've written some code that runs a strategy on daily bars. I want to initiate the trades at the open or close of the trading day. I wanted to use the TimeNum() function but because I'm using daily bars it returns 0. Does anyone have any possible work arounds that would allow me to get the time? I'm specifically looking for the time at my computers location (computer is in New York).

Thanks!

Hi everyone,

I've written some code that runs a strategy on daily bars. I want to initiate the trades at the open or close of the trading day. I wanted to use the TimeNum() function but because I'm using daily bars it returns 0. Does anyone have any possible work arounds that would allow me to get the time? I'm specifically looking for the time at my computers location (computer is in New York).

Thanks!

TimeNum returns BAR timestamp. Obviously EOD bars come without time.

TimeNum is only needed when you trade INTRADAY.

To trade on EOD open or on EOD close, you DO NOT NEED TimeNum at all.

Simply use

BuyPrice = Open; // buy on open

Please do read the manual. It is essential to read the docs
https://www.amibroker.com/guide/h_backtest.html

and
https://www.amibroker.com/guide/h_portfolio.html

Hi Tomasz,

I have reviewed the manuals and I'm sorry I had not realized daily bars come without time.
Thank you for the recommendation but isn't the

BuyPrice = Open

Function only used for backtesting? This strategy I'm working on is for live/automated trading. Or can I use the BuyPrice function to automatically buy at the open/Close for live trading?

Thank you!

It is for backtesting. As for "live trading" it depends how do you "live trade". You did not provide that information in your posts. It is really super-difficult to answer questions that lack basic information. I don't know what is in your mind.

IF you want to trade with Interactive Brokers, you can simply place MOO (Market On Open) or MOC (Market On Close) order. You can do this manually or automatically.
If you are into automated trading, you should read IBController 1.3.8 READ ME that covers PlaceOrder function and order types including MOO and MOC.
Supported order types are listed in TWS API docs: TWS API v9.72+: Basic Orders

You just pass MOO or MOC as order type in PlaceOrder call. It does NOT require you to use TimeNum().

You might however use Now() function because it gives you current CLOCK time if you want to act only in certain moments of the day.

It is however strongly discouraged to directly jump to automated trading for absolute beginners in coding.

1 Like