How do I create an daily ATR indicator as a trigger for automatic trading

Hello amibroker community!
Thanks for reading my help request.

For my automatic trading system with IB I need an ATR indicator as a entry trigger signal.
I want to trade almost on MOC. Therefore my entry trading time started at 3:55 p.m. based on S&P 500 stocks.
How do I get a realtime daily ATR(20) indicator which I can use as a entry trigger. The ATR(20) should be calculate based on the last 19 trading days plus todays intraday data.

Here my AFL-Code and of course not correct :

// automatic trading system
// amibroker 6.40.4
// broker.exe and IB TWS
// realtime data from IB
// 1 Minutes bars standard timeframe
// daily ATR as trigger
// entry almost on MOC     
    
// trading time intraday    
StartTradingTime    = 155500;   // start trading
EndTradingtime      = 155900;   // end trading

// trading time  
tn = TimeNum();   
trading_time = tn >= StartTradingTime AND tn <= EndTradingTime;     

TimeFrameSet( inDaily ); // switch to daily time frame    

// calculate real time daily ATR for last 19 days plus today intraday data 
myATR = ATR(20);  // ?

TimeFrameRestore();  // restore time frame to 1 minute    

// expand for 1 minute timeframe
myATR = TimeFrameExpand(myATR, inDaily, mode = expandLast);  // mode = ?

// trigger rule 1 - with realtime daily ATR(20)
trigger_1 = myATR > Close - TimeFrameGetPrice( "close", inDaily, -1 );   

// trigger rule 2
trigger_2 = trading_time; 
  
// trigger  
trigger = trigger_1 AND trigger_2;  
 
// buy    
Buy = trigger;

// Sell  
Sell = 0 ;  
 
// automatic place order at Interactive Brokers 
if ( LastValue(Buy) )  
  {
   // connect to IB 
   ibc = GetTradingInterface("IB");
   

There are two alternative solutions:

  1. You can use
TimeFrameSet( inDaily ); // switch to daily time frame    

// calculate real time daily ATR for last 19 days plus today intraday data 
myATR = ATR(20);  // ?

TimeFrameRestore();  // restore time frame to 1 minute    

// expand for 1 minute timeframe
myATR = TimeFrameExpand(myATR, inDaily, mode = expandLast);  // mode = ?

where expandLast guarantees that you are getting yesterday values until the very end of session

  1. or you can use:
TimeFrameSet( inDaily ); // switch to daily time frame    

// calculate real time daily ATR for last 19 days plus today intraday data 
myATR = Ref( ATR(20), -1 );  // yesterday

TimeFrameRestore();  // restore time frame to 1 minute    

// expand for 1 minute timeframe
myATR = TimeFrameExpand(myATR, inDaily, mode = expandFirst); 

where expandFirst expands yesterday data from the very first bar of today.

This topic was automatically closed 100 days after the last reply. New replies are no longer allowed.