Tying to get Foreign O, C Daily Data in 15 sec TimeFrame

I am trying to utilize foreign daily OHLC data in my program that is set at a 15 sec base time interval in the database settings.

Below is the code I am using...

// compress data to daily interval  
SetForeign( "SPY", True, True ); 
TimeFrameSet( inDaily );  
// assign daily values to custom variables  
SPYOpenDaily = Open;  
SPYPrevClose1 = Ref( Close, -1 ); 
// restore original arrays (back to the primary symbol)  
TimeFrameRestore(); 
RestorePriceArrays( True );

It turns out that this code works:

SPYOpenDaily = Open; 

as I get the daily SPY Open data.

But, this code does not - I get erroneous data that is not correct...

SPYPrevClose1 = Ref( Close, -1 ); 

Any ideas as to what I am doing wrong would be appreciated.

hello @pmfiorini

i didnt see your code

Another idea…. if this of what you try to do, is only to have for 2-3 days on daily basis data of few Symbols. Maybe is better to run an exploration only ONCE per day, and then store that data to the statickvar.
I prefer this way, as an easy and fast solution. What you think?
That means we donot have to mess about SetForeign /TimeFrameSet.

Also in your code i just saw you donot have any TimeFrameExpand()

many examples below link
https://forum.amibroker.com/tags/c/afl/timeframe

@pmfiorini,

Try this code - I think you need to use SetForeign() inside the TimeFrameSet()/...Restore(), otherwise, its acting on your current timeframe/ symbol.

 // AB tutorial on multi-timeframe support: http://www.amibroker.com/guide/h_timeframe.html

// Something to look at!!!, because nothing was provided by the OP!!!
_SECTION_BEGIN("Price");
SetChartOptions(0,chartShowArrows|chartShowDates);
_N(Title = StrFormat("{{NAME}} - {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo %g, Close %g (%.1f%%) {{VALUES}}", O, H, L, C, SelectedValue( ROC( C, 1 ) ) ));
Plot( C, "Close", ParamColor("Color", colorDefault ), styleNoTitle | ParamStyle("Style") | GetPriceStyle() ); 
_SECTION_END();


// User interactions
selForeignSym	= ParamStr("selForeignSym", "SPY") ;
selForeignActive	= ParamToggle("selForeignActive", "1st|2nd", 0) ;


// compress data to daily interval  
//SetForeign( "SPY", True, True ); 
if (selForeignActive == 0)
	SetForeign(selForeignSym, True, True ); 	// Executing this "outside" the ```TimeFrameSet()``` has no effect on what happens inside it!
	
TimeFrameSet( inDaily );  

if (selForeignActive == 1)
	SetForeign(selForeignSym, True, True ); 		// This on the other hand, seems to be doing what you want

// assign daily values to custom variables  
SPYOpenDaily = Open;  
SPYPrevClose1 = Ref( Close, -1 ); 
// restore original arrays (back to the primary symbol)  
TimeFrameRestore(); 
RestorePriceArrays( True );

// Show the result
Plot(SPYOpenDaily, "SPYOpenDaily", ParamColor("SPYOpenDaily color", colorCycle), ParamStyle("SPYOpenDaily style", styleLine + styleOwnScale, maskAll)) ;
Plot(SPYPrevClose1, "SPYPrevClose1", ParamColor("SPYPrevClose1 color", colorCycle), ParamStyle("SPYPrevClose1 style", styleLine + styleOwnScale, maskAll)) ;

I copied your code, and plotted it. The values for SpyPrevClose1 on the graph are not the previous close. Is your graph working?

Thanks for the response, but I'm trying to do things programmatically within the code w/o using external data

@pmfiorini, Perhaps you should provide all the code for a minimal reproducible/ usable example, not just a fragment, so that we can replicate exactly what you're seeing, and include a chart to illustrate what is/ not working.

Ok - Thanks for your effort - Here is all the code...

function GetSecondNum()  
{  
    Time         = Now( 4 );  
    Seconds     = int( Time % 100 );  
    Minutes     = int( Time / 100 % 100 );  
    Hours     = int( Time / 10000 % 100 );  
    SecondNum = int( Hours * 60 * 60 + Minutes * 60 + Seconds );  
    return SecondNum;  
}  
  
RequestTimedRefresh( 1 );  
TimeFrame = Interval();  
SecNumber = GetSecondNum();  
Newperiod = SecNumber % TimeFrame == 0;  
SecsLeft     = SecNumber - int( SecNumber / TimeFrame ) * TimeFrame;  
SecsToGo     = TimeFrame - SecsLeft;  
  
if ( NewPeriod )  
{  
    //Say( "New period" );  
    Plot( 1, "", colorYellow, styleArea | styleOwnScale, 0, 1 );  
}  
  
Title = "\n" +  
  
        "  Current Time: " + Now( 2 ) + "\n" +  
        "Chart Interval: " + NumToStr( TimeFrame, 1.0 ) + " Seconds\n" +  
        " Second Number: " + NumToStr( SecNumber, 1.0, False ) + "\n" +  
        "  Seconds Left: " + NumToStr( SecsLeft, 1.0, False ) + "\n" +  
        " Seconds To Go: " + NumToStr( SecsToGo, 1.0, False );  
  
Plot( C, "", 1, 128 );  
 
 
PosSize = 4000;   
   
maxpos =  40; // maximum number of open positions   
SetOption("InitialEquity", 160000 ); // set initial equity = 50K   
SetOption( "MaxOpenPositions", maxpos );   
//SetPositionSize( 5.0, spsPercentOfEquity );   
//SetPositionSize( 4000, spsValue );    
SetPositionSize( 3.333333333, spsPercentOfEquity );   
//SetOption( "PriceBoundChecking", False );// disable price bound checking   
SetOption( "ReverseSignalForcesExit", False); 
 //SetOption("GenerateReport", 1 );
   
RoundLotSize = 5;   
   
   
DailyOpen = TimeFrameGetPrice( "O", inDaily, -0 );   
DailyHigh = TimeFrameGetPrice( "H", inDaily, -0 );  
PrevDailyClose = TimeFrameGetPrice( "C", inDaily, -1 ); 
PrevDailyOpen =  TimeFrameGetPrice( "O", inDaily, -1 );  
PrevPrevDailyClose = TimeFrameGetPrice( "C", inDaily, -2 ); 
PrevPrevDailyOpen =  TimeFrameGetPrice( "O", inDaily, -2 );  
PrevDailyHigh = TimeFrameGetPrice( "H", inDaily, -1 );   
PrevDailyLow = TimeFrameGetPrice( "L", inDaily, -1 );   
 
 
TimeFrameSet( in15Minute ); // switch to 5 minute frame   
SMA200 = MA( C, 200 );   
SMA50 = MA( C, 50 );   
SMA20 = MA( C, 20 );   
SMA05 = MA( C, 5 );   
ATRinDaily = ATR( 14 );  
Slope = LinRegSlope( C, 30 );   
LinRegInter = LinRegIntercept( C, 30 );   
DeltaDaily = Open - Close;   
meanDaily = MA( Close, 520 );     
stdDaily  = StDev( Close, 520 );  
TimeFrameRestore(); // restore time frame to original   
SMA20 = TimeFrameExpand(SMA20, inDaily, expandLast);   
SMA50 = TimeFrameExpand(SMA50, inDaily, expandLast);   
Slope = TimeFrameExpand(Slope, inDaily, expandLast);   
LinRegInter = TimeFrameExpand(LinRegInter, inDaily, expandLast);   
meanDaily = TimeFrameExpand(meanDaily, inDaily, expandLast);      
stdDaily = TimeFrameExpand(stdDaily, inDaily, expandLast);   
 
 
TimeFrameSet( inDaily );  
// assign daily values to custom variables  
meanDaily = MA( Close, 20 );     
stdDaily  = StDev( Close, 30 );  
// restore original arrays (back to the primary symbol)  
// RestorePriceArrays() function is an equivalent  
TimeFrameRestore(); 
  
  
 
// compress data to daily interval  
//SetForeign( "SPY", True, True ); 
//TimeFrameSet( inDaily );  
// assign daily values to custom variables  
//SPYOpenDaily = Open;  
//SPYPrevClose1 = Ref( Close, -1 ); 
// restore original arrays (back to the primary symbol)  
//TimeFrameRestore(); 
//RestorePriceArrays(True); //function is an equivalent  

// User interactions
selForeignSym	= ParamStr("selForeignSym", "SPY") ;
selForeignActive	= ParamToggle("selForeignActive", "1st|2nd", 0) ;


// compress data to daily interval  
//SetForeign( "SPY", True, True ); 
if (selForeignActive == 0)
	SetForeign(selForeignSym, True, True ); 	// Executing this "outside" the ```TimeFrameSet()``` has no effect on what happens inside it!
	
TimeFrameSet( inDaily );  

if (selForeignActive == 1)
	SetForeign(selForeignSym, True, True ); 		// This on the other hand, seems to be doing what you want

// assign daily values to custom variables  
SPYOpenDaily = Open;  
SPYPrevClose1 = Ref( Close, -1 ); 
// restore original arrays (back to the primary symbol)  
TimeFrameRestore(); 
RestorePriceArrays( True );

 
  
SetForeign( "DIA" );  
// compress data to daily interval  
TimeFrameSet( inDaily );  
// assign daily values to custom variables  
DIAOpenDaily = Open;  
// restore original arrays (back to the primary symbol)  
// RestorePriceArrays() function is an equivalent  
TimeFrameRestore();  
  
  
SetForeign( "VIX" );  
// compress data to daily interval  
TimeFrameSet( inDaily );  
// assign daily values to custom variables  
VIXOpenDaily = Open;  
// restore original arrays (back to the primary symbol)  
// RestorePriceArrays() function is an equivalent  
TimeFrameRestore();  
  
 
  
SystemBeginTime = DateTimeConvert( 1, DateTimeAdd( Now( Format = 5 ), -5, in1Minute ) ); // Works for 5 min project     
SystemBeginTime = 040000;     
SystemEndTime = 160000;      
     
     
   
SetTradeDelays( 1, 1, 1, 1 );   
SetOption( "ActivateStopsImmediately", True );  
  
SPYOpen = Foreign( "SPY", "Open" );     
SPYClose = Foreign( "SPY", "Close" );   
SPYDelta = SpyClose - SPYOpen;  
  
  
DIAOpen = Foreign( "DIA", "Open" );     
DIAClose = Foreign( "DIA", "Close" );   
DIADelta = SpyClose - SPYOpen;  
 
 
QQQOpen = Foreign( "QQQ", "Open" );     
QQQClose = Foreign( "QQQ", "Close" );   
QQQDelta = QQQClose - QQQOpen;  
  
  
SPYOpenDaily = Ref( SPYClose, -BarsSince( TimeNum() <= 093000 ) );  
DIAOpenDaily = Ref( DIAClose, -BarsSince( TimeNum() <= 093000 ) );  

  
BarsSinceOpen = Max( BarsSince( TimeNum() >= 093000 ), 10 );     


/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 
///						LONG 
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
BBWidth = 2.0;
MARange = 20;
Lag = 5;
          
          
BuySignal  = ( (  Close > BBandBot( Close, Lag, BBWidth ) AND MA( Close, Lag ) < Close )  
          OR ( Close < BBandTop( Close, Lag, BBWidth ) AND MA( Close, Lag ) > Close ) );//AND SPYClose - Ref( SPYClose, -3) > 0;     
  
ShortSignal =  ( ( Close > BBandTop( Close, Lag, BBWidth ) AND MA( Close, Lag ) < Close ) 
          OR ( Close < BBandBot( Close, Lag, BBWidth ) AND MA( Close, Lag ) > Close ) ); //AND SPYClose - Ref( SPYClose, -3) < 0;  
          
				 		  		 
Buy = BuySignal AND TimeNum() >= SystemBeginTime AND TimeNum() <= 153000; // AND LongOrShort <= PrGoLong;  
BuyPrice = Min( Open, Ref( Close, -1) ) * 1.0000; 
Buy = Buy AND BuyPrice > Low;  
 
 
Sell = ( Close > BBandTop( Close, Lag, BBWidth ) AND MA( Close, Lag ) > Close ) OR TimeNum() >= 155000; 
SellPrice = Max( Open, Ref( Close, -1 ) ); 
Sell = Sell AND SellPrice < High; 
 
 
 
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 
///						SHORT 
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 
                          			 
				 
			  
Short = ( ShortSignal AND TimeNum() >= SystemBeginTime AND TimeNum() <= 153000 ); // AND LongOrShort > PrGoLong;  
ShortPrice = Max( Open, Ref( Close, -1 ) ) * 1.0000;
Short = Short AND ShortPrice < High; 
 
 
Cover = ( Close < BBandBot( Close, Lag, BBWidth ) AND MA( Close, Lag ) < Close ) OR TimeNum() >= 155000; 
CoverPrice = Min( Open, Ref( Close, -1 ) ); 
Cover = Cover AND CoverPrice > Low; 




PositionScore = mtRandom();
 
 
//Sell = Cover = 0;
//ApplyStop( stopTypeProfit, stopModePercent , 2.50, ExitAtStop = 1);         
ApplyStop( stopTypeLoss,  stopModePercent, 2.50, ExitAtStop = 1 ); 
 
 

 
Filter = Buy OR Short; 
 
 
//Filter = 1; 
 
ibc = GetTradingInterface( "IB" );    
   
nm = Name();   
openpos = ibc.GetPositionList();	          
numOpenPos = StrCount( openpos, "," );   
posExists = StrFind( openpos, nm );      
SymPos = StrFind( openpos, nm );	   
pendList = ibc.GetPendingList( 1, "Filled" );   
PendSymbolFound = StrFind(pendList, nm + "-" );   
   
   
GMTShift = 18000; //  60 * 60 * 5 :: NH = -5 hr difference, so need to add 5 hrs to GTDDateTime   
GTDDateTime = DateTimeAdd( Now( Format = 5 ), GMTShift + SecsToGo, in1Second );    
GTDTime = DateTimeFormat("GTD %Y%m%d %H:%M:%S GMT", GTDDateTime );   
//GTDTime = "GTC";   
   
        
if ( LastValue( Short ) && !posExists && numOpenPos <= 40 && GetRTData( "Last" ) > 0 ) {      
   
	Price = LastValue( ShortPrice );      
	Price = Max( GetRTData( "Last" ), GetRTData( "Bid" ) ) * 1.0005; 
	Price = GetRTData( "Last" ) * 1.0000; 
	Price = Max( GetRTData( "Last" ), GetRTData( "Bid" ) ); 
	Shares = LastValue( Round( ( PosSize / Price ) / 5) * 5 );	  
	  
	if ( LastValue( TimeNum() ) >= 093000 && LastValue( TimeNum() ) <= 160000 ) {	  
		parentID = ibc.PlaceOrder(nm, "SELL", Shares, "LMT", Price, 0, GTDTime, False ); 
		//ibc.PlaceOrder(nm, "BUY", Shares, "LMT", Price * 0.9750, Price * 0.9750, "GTC", False, 100, "", parentID ); 
		ibc.PlaceOrder(nm, "BUY", Shares, "STP", Price * 1.0250, Price * 1.0250, "GTC", True, 100, "", parentID ); 
	}	   
  
	if ( ( LastValue( TimeNum() ) >= 040000 && LastValue( TimeNum() ) <= 093000 ) || LastValue( TimeNum() ) > 160000 ) {  
		//parentID = ibc.PlaceOrder(nm, "SELL", Shares, "LMT", Price, 0, GTDTime, False, 100, "outsideRTH" ); 
		//ibc.PlaceOrder(nm, "BUY", Shares, "LMT", Price * 0.9750, Price * 0.9750, "GTC", False, 100, "outsideRTH", parentID ); 
		//ibc.PlaceOrder(nm, "BUY", Shares, "STP", Price * 1.0250, Price * 1.0250, "GTC", True, 100, "outsideRTH", parentID ); 
	}  
	  
}     
   
   
//if ( LastValue( Buy ) && !posExists && numOpenPos <= 40 && GetRTData( "Last" ) > 0 ) {      
   
//	Price = LastValue( BuyPrice );    
//	Price = Min( GetRTData( "Last" ), GetRTData( "Ask" ) ) * 0.9992;   
//	Price = GetRTData( "Last" ) * 1.0000; 
//	Price = Min( GetRTData( "Last" ), GetRTData( "Ask" ) ); 
//	Shares = LastValue( Round( ( PosSize / Price ) / 5 ) * 5 );	  
	  
//	if ( LastValue( TimeNum() ) >= 093000 && LastValue( TimeNum() ) <= 160000 ) {	  
//		parentID = ibc.PlaceOrder(nm, "BUY", Shares, "LMT", Price, 0, GTDTime, False ); 
		//ibc.PlaceOrder(nm, "SELL", Shares, "LMT", Price * 1.0250, Price * 1.0250, "GTC", False, 100, "", parentID ); 
//		ibc.PlaceOrder(nm, "SELL", Shares, "STP", Price * 0.9750, Price * 0.9750, "GTC", True, 100, "", parentID ); 
//	}	   
	  
//	if ( ( LastValue( TimeNum() ) >= 040000 && LastValue( TimeNum() ) <= 093000 ) || LastValue( TimeNum() ) > 160000 ) {	  
//		parentID = ibc.PlaceOrder(nm, "BUY", Shares, "LMT", Price, 0, GTDTime, False, 100, "outsideRTH" ); 
		//ibc.PlaceOrder(nm, "SELL", Shares, "LMT", Price * 1.0250, Price * 1.0250, "GTC", False, 100, "outsideRTH", parentID ); 
//		ibc.PlaceOrder(nm, "SELL", Shares, "STP", Price * 0.9750, Price * 0.9750, "GTC", True, 100, "outsideRTH", parentID ); 
//	}	   
	   
//}      
 
//Bound = 2000; 
//UnrealizedPNL = ibc.GetPositionInfo( nm, "Unrealized PNL" ); 
//GTDTime = "GTC"; 
 
//if ( ibc.GetPositionSize( Name() ) > 0 && ( UnrealizedPNL >= Bound || UnrealizedPNL <= -Bound || LastValue( Sell ) ) ) {   
   
//	Price = Max( GetRTData( "Last" ), GetRTData( "Bid" ) );  
//	Price = GetRTData( "Last" ) * 1.0000;  
//	if ( LastValue( TimeNum() ) >= 093000 && LastValue( TimeNum() ) <= 160000 ) {	  
//		ibc.PlaceOrder( Name() , "SELL",  ibc.GetPositionSize( Name() ), "LMT", LastValue( Price ), 0, GTDTime, True );  
//	}   
	  
//	if ( ( LastValue( TimeNum() ) >= 040000 && LastValue( TimeNum() ) <= 093000 ) || LastValue( TimeNum() ) > 160000 ) {	  
//		ibc.PlaceOrder( Name() , "SELL",  ibc.GetPositionSize( Name() ), "LMT", LastValue( Price ), 0, GTDTime, True, 100, "outsideRTH" );   
//	}  
  
//}   
   
   
//if ( ibc.GetPositionSize( Name() ) < 0 && ( UnrealizedPNL >= Bound || UnrealizedPNL <= -Bound || LastValue( Cover ) ) ) {   
   
//	Price = Min( GetRTData( "Last" ), GetRTData( "Ask" ) );  
//	Price = GetRTData( "Last" );  
//	if ( LastValue( TimeNum() ) >= 093000 && LastValue( TimeNum() ) <= 160000 ) {	  
//		ibc.PlaceOrder( Name() , "BUY",  -ibc.GetPositionSize( Name() ), "LMT", LastValue( Price ), 0, GTDTime, True );   
//	}  
  
//	if ( ( LastValue( TimeNum() ) >= 040000 && LastValue( TimeNum() ) <= 093000 ) || LastValue( TimeNum() ) > 160000 ) {	  
//		ibc.PlaceOrder( Name() , "BUY",  -ibc.GetPositionSize( Name() ), "LMT", LastValue( Price ), 0, GTDTime, True, 100, "outsideRTH" );   
//	}  
	   
//}   
   
//if ( LastValue( TimeNum() ) >= 155500 ) {   
   
	//ibc.CloseAllOpenPositions();   
	   
//}   
 
 
Filter = TimeNum() >= SystemBeginTime OR ( Short OR Buy );  
 
//Filter = TimeNum() >= SystemBeginTime; 
 
 
      
//AddTextColumn( NumToStr( SystemBeginTime ), "SystemBeginTime");      
AddTextColumn( Now(), "Current time");  
AddColumn( SPYOpenDaily, "SPYOpenDaily", 1.2 );
AddColumn( SPYPrevClose1, "SPYPrevClose1", 1.2 );
AddColumn( PrevDailyOpen, "PrevDailyOpen", 1.2 );
AddColumn( PrevDailyClose, "PrevDailyClose", 1.2 );
//AddColumn( Close - Ref( Close, -3 ) > 0, "Sht Term Trend", 1.2 ); 
AddColumn( Open - Close < 0, "O - C < 0", 1.2 ); 
//AddColumn( LinearReg( SPYClose, 5 ), "LR Est", 1.6 ); 
//AddColumn( PrLong, "PrLong", 1.6 ); 
//delta = Sum( Close - Open, 390 ); 
//AddColumn( delta, "Delta", 1.6 ); 
//deltaSigma = sqrt( Sum( StDev( Close - Open, 390 )^2, 60 ) ); 
//AddColumn( deltaSigma, "deltaSigma", 1.6 ); 
//Z = (Sum( Close - Open, 390 ) - delta ) / deltaSigma; 
//AddColumn( Z, "Z", 1.6 ); 
//AddColumn( Close + ( delta + 2 * deltaSigma ), "MaxEst", 1.6 ); 
//AddColumn( Close, "Close", 1.2 ); 
//AddColumn( DailyHigh, "DailyHigh", 1.2 ); 
AddColumn( LastValue( Close ), "Close", 1.2 ); 
AddColumn( LastValue( Close ) - GetRTData( "Last" ), "LV C - Last", 1.2 ); 
//AddColumn( Ref( Close, -1 ), "Ref( Close, -1 )", 1.2 ); 
//AddColumn( HHV( High, TimeBound ), "HHV( High, TimeBound )", 1 );      
AddColumn( Buy, "Buy", 1.0 ); 
AddColumn( BuyPrice, "BuyPrice", 1.2 );      
//AddColumn( Min( Last, Ref( LLV( Low, TimeBound ), -1 ) ) * 0.9990, "BuyPriceAnal", 1.2 ); 
//AddColumn( Sell, "Sell", 1.0 );   
//AddColumn( SellPrice, "SellPrice", 1.2 );   
AddColumn( Short, "Short", 1 );      
AddColumn( ShortPrice, "ShortPrice", 1.2 );      
//AddColumn( Max( Last, Ref( HHV( High, TimeBound ), -1 ) ) * 1.0010 , "ShortPriceAnal", 1.2 ); 
AddColumn( LastValue( Open ), "Open", 1.2 ); 
AddColumn( LastValue( Open ) - GetRTData( "Last" ), "LV O - Last", 1.2 );      
//AddColumn( Low, "Low", 1.2 );      
//AddColumn( High, "High", 1.2 ); 
AddColumn( LastValue( Open ) - LastValue( Close ), "O - C", 1.2 );      
 
AddColumn( GetRTData( "Bid" ), "Bid", 1.2 ); 
AddColumn( GetRTData( "Ask" ), "Ask", 1.2 ); 
AddColumn( GetRTData( "Last" ), "Last", 1.2 ); 
 
//AddColumn( Ref( High, -1 ), "Ref( High, -1 )", 1.2 );      
//AddColumn( Close, "Close", 1.2 );      
//AddColumn( GetRTData("Bid"), "Bid", 1.2 );      
//AddColumn( GetRTData("Ask"), "Ask", 1.2 );      
//AddColumn( GetRTData("Last"), "Last", 1.2 );      
//AddColumn( Open - GetRTData("Last"), "Open - Last", 1.2 );   
//AddColumn( Min( Open, GetRTData( "Last" ) ), "Min( Open, Last )", 1.2 );  
//AddColumn( LastValue( Close ), "LastValue( C )", 1.2 );  
//AddColumn( GetRTData( "Last" ) - LastValue( Close ), "RTLast - LVClose", 1.2 );  
//AddColumn( LastValue( Open ), "LV(Open)", 1.2 );  
//AddSummaryRows( 31, 1.6 ); 
1 Like

Also, I am not using charts. I am comparing and analyzing data using Explore.

You need to use TimeFrameExpand() on variables like SPYOpenDaily that were assigned inside your TimeFrameSet()... TimeFrameRestore() block. Otherwise they will still be compressed to daily bars and misaligned with your other arrays.

Then a screen capture of what you're seeing, so that we can also see it - use descriptions, code and illustrations to explain what you want help with:

It's obvious that you can't answer the question. My post was clear explaining EXACTLY what was needed my friend. So don't lecture me about anything. Thank you.

@phase21, @pmfiorini From the manual, TimeFrameRestore() and RestorePriceArrays() is essentially the same function and also has the same function definition (parameters).
They both change the interval and Arrays back to the original so calling them consecutively isn't required.
One less line of code every time :slight_smile:

TimeFrameRestore and RestorePriceArrays is essentially the same function.....

@pmfiorini the code in your first post works fine at my end.
Regarding TimeFrameExpand(), this is contextual and may not always be necessary.

If I need to just refer to individual Daily bars and its previous data, then one can directly index them and avoid Time Compress/Expand altogether.
Only if you have some operation with two different TF, then there is a compulsion to align the data.

Can you instead post the actual data values and see if you're missing something there.

@pmfiorini
In your code block of multiple SetForeign of lines 124 to 141 you did not expand a single time after that.
For proper functioning in ALL cases you better do not forget to expand in order to align data.

Please look into KB article
http://www.amibroker.com/kb/2014/10/20/foreign-timeframeset/
In addition that article mentions that TimeFrameSet and RestorePriceArrays are equivalent.

/// look here
/// @link http://www.amibroker.com/kb/2014/10/20/foreign-timeframeset/
//
// Setforeign and compressing data to daily interval  
//
// you do not have trade signals in there so tradeprices = false
// Read here about setforeign in general
/// @link https://www.amibroker.com/guide/afl/setforeign.html
timeframe = inDaily;

SetForeign( "SPY", True, tradeprices = False);
	TimeFrameSet( timeframe );  
		// assign daily values to custom variables  
		SPYOpenDaily = Open;  
		SPYPrevClose1 = Ref( Close, -1 ); 
	// restore original arrays (back to the primary symbol)  
	TimeFrameRestore( tradeprices ); 
	//RestorePriceArrays( tradeprices );// not required because of being equivalent to TimeFrameRestore

// EXPAND after Restore!
/// @link https://www.amibroker.com/guide/afl/timeframeexpand.html
expandmode = expandLast;// expand modes, READ about in upper link
SPYOpenDaily = TimeFrameExpand(SPYOpenDaily , timeframe, expandmode );
SPYPrevClose1 = TimeFrameExpand(SPYPrevClose1, timeframe, expandmode );

Filter = 1;  
 
AddColumn( SPYOpenDaily, "SPYOpenDaily", 1.2 );
AddColumn( SPYPrevClose1, "SPYPrevClose1", 1.2 );

If using timeframeset etc then in addition to that you better do use TimeFrameExpand avoiding bad habits (e.g. by newbies) and avoiding reporting to support ("...does not work") because of forgetting to expand (and as such leading to non alignments)! This is one of the most common mistakes done by (new) people in AmBroker universe... forgetting to expand. So please stop encouraging people to start applying bad habits.

He has forgotten to use TimeFrameExpand.


BTW the thread subject mentions two time frame operation.

Tying to get Foreign O, C Daily Data in 15 sec TimeFrame

So again @pmfiorini better follow AB manual instead of doing experiments (such as thinking it could be a "good" idea to not use TimeFrameExpand). If you use shorter TF (15 sec) and switch to longer TF (daily) from shorter interval then in order to align data to shorter TF you have to add expansion code.

2 Likes

@fxshrat Agreed, but going by his code posted later on in the thread, he has used TimeFrameExpand() at several places so it was taken by me that he has done that and still getting trouble.

That's where the part comes, so if one needed only the recent most previous close for example, then LastValue() of a Daily compressed Array Ref(C, -1) or the Ref() itself would given him that value instead of taking the Expand route. That was the intention here.

The original code is incorrect.

Proper usage is shown in the Knowledge Base:
http://www.amibroker.com/kb/2014/10/20/foreign-timeframeset/

Also as per User's Guide http://www.amibroker.com/guide/h_timeframe.html
If you only want to get yesterdays close from intraday chart there is much easier function that does not require as much code. You can just make it single line using http://www.amibroker.com/f?timeframegetprice

The example on how to use these functions are already in the documentation so instead of "reinventing the wheel" and creating your own incorrect code, just copy-pase from manual or from Knowledge Base.

2 Likes