Daily indicator values in intraday trade list

I followed the instructions of this article and it worked for me.

Article

In contrast to this article I want to plot DAILY indicator values in the trade list of an INTRADAY strategy. So if a trade happened on 7/12/2017 I’d like to plot the indicator value (e.g. ATR) as of the close of 7/11/2017.

In the main section of the code if define my Daily indicator as follows:

if(dreportingOn)
{// Compress to Daily
                TimeFrameSet (inDaily);
                dATR20=ATR(20);
                dClose=C;
                TimeFrameRestore();
                
                StaticVarSet(Name()+"dATR20", datr20,False);
                StaticVarSet(Name()+"dClose",dclose,false);
                StaticVarSet(Name()+"dClose1",ref(dclose,-1),False);
  }

.....

The backtesting interface should pick the correct values like this:

if(Status("action")==actionPortfolio)

....
  for (trade=bo.GetFirstTrade(); trade; trade=bo.GetNextTrade())
   {

       if(dreportingOn)
       {                              
            mydatr=StaticVarGet(trade.Symbol+"dATR20");
            trade.AddCustomMetric("dATR20",Lookup(mydatr, trade.EntryDateTime,-1));
            mydclose=StaticVarGet(trade.Symbol+"dClose");
            trade.AddCustomMetric("dClose",Lookup(mydclose, trade.EntryDateTime,-1));
            mydclose1=StaticVarGet(trade.Symbol+"dClose1");
            trade.AddCustomMetric("dClose1",Lookup(mydclose1, trade.EntryDateTime,-1));
         }
  }
  bo.ListTrades();
}

However the lookup function seems to return wrong values, when used in combination with compressed arrays. Any ideas how to solve this ?

Thanks.

Never store compressed array in static variable.
You must call TimeFrameExpand() to expand array prior to calling StaticVarSet():

StaticVarSet( Name()+"dATR20", TimeFrameExpand(datr20, inDaily) );

Always, I mean always use TimeFrameExpand().

Required reading:
http://www.amibroker.com/guide/h_timeframe.html

I mentioned many times that each sentence in manual is for the purpose. The manual is filled with important information and you can’t just scan it. You need to read it with understanding.

Excerpt from manual:

How does it work internally ?

Time-frame functions do not change the BarCount - they just squeeze the arrays so you have first N-bars filled with NULL values and then - last part of the array contains the actual time-compressed values. This is why it is essential to expand the data back to the original frame with TimeFrameExpand.

1 Like

Thanks Tomasz, this solved the problem.

I agree to you that RTFM should be the first way to get support.