How to reducing the code or simplify it?

Hi all
I would to ask question.
If I have the code as flow
macd_a = macd formula;
signal _line_a = formula;
hist_a = formula;
condition1 = formula; ( using (WriteIf ) )
condition1_color = formula; ( using (WriteIf ) )
condition1_color_font = formula; ( using (WriteIf ) )

condition2 = formula; ( using (WriteIf ) )
condition2_color = formula; ( using (WriteIf ) )
condition2_color_font = formula; ( using (WriteIf ) )

condition3 = formula; ( using (WriteIf ) )
condition3_color = formula; ( using (WriteIf ) )
condition3_color_font = formula ; ( using (WriteIf ) )

condition4 = formula; ( using (WriteIf ) )
condition4_color = formula; ( using (WriteIf ) )
condition4_color_font = formula; ( using (WriteIf ) )
ETC…….

When I use multiple timeframe
TF_1 = in5Minute;
TF_2 = in15Minute;
TF_3 = 2in15Minute;
TF_4 = inHourly;
TF_5 = 4
inHourly;
TF_6 = inWeekly;
TF_7 = inMonthly;

I repeat the conditions for every timeframe and I end up with more than 4500 lines of code .
My question is
Can I do the flowing way?
for example using ( TF_3 = 2*in15Minute ; )

TimeFrameSet(TF_3);

macd_a_ TF_3 = macd_a ;
signal line_a TF_3 = signal line_a ;
hist_a
TF_3 = hist_a ;
condition1_ TF_3 = condition1 ;
condition1_color_ TF_3 = condition1_color ;
condition1_color_font_ TF_3 = condition1_color_font ;

condition2_ TF_3 = condition2 ;
condition2_color_ TF_3 = condition2_color ;
condition2_color_font_ TF_3 = condition2_color_font ;
and so on ………………
TimeFrameRestore();
macd_a_TF_3 Expand = TimeFrameExpand ( macd_a TF_3 , TF_3 ) ;
signal_line_a_TF_3_Expand = TimeFrameExpand ( signal line_a TF_3 , TF_3 ) ;
hist_a_TF_3_Expand = TimeFrameExpand ( hist_a_ TF_3 , TF_3 ) ;
condition1_ TF_3_Expand = TimeFrameExpand ( condition1_ TF_3, TF_3 ) ;
condition1_color_ TF_3_Expand = TimeFrameExpand ( condition1_color_ TF_3 , TF_3 ) ;
condition1_color_ TF_3_Expand = TimeFrameExpand ( condition1_color_font_ TF_3 , TF_3 ) ;

or I should use

coded by fxshrat

procedure LTF_set( timeframe ) {
// formula
// condition
}
LTF_set( TF_3);
But how to apply the conditions for GUI

I will use GUI as below photo

And using exploration
I hope I explain my issue right
Thank you for your help

Here's a starting point for you. Use a loop for the timeframes.

TFList = "300,900,1800,14400"; // List of timeframes in seconds.
for( i = 0; ( TF = StrExtract( TFList, i ) ) != ""; i++ )
{
	TimeFrameSet( StrToNum(TF) );
	Cond1 	= MA(C,30); // your formula here
	//... repeat with your conditions...
	
	
	// Plot GUI stuff here...
	
	
	
	TimeFrameRestore();
}

1 Like

I would rather do like this

//https://forum.amibroker.com/t/how-to-reducing-the-code-or-simplify-it/36969/3

mx = Matrix(4,1);
rownum = MxGetSize(mx,0);
mx[0][0] = in5Minute;
mx[1][0] = in15Minute;
mx[2][0] = 2*in15Minute;
mx[3][0] = inDaily;

for ( i = 0; i < rownum; i++ )
{
   TimeFrameSet( mx[i][0] );  
   inter_val = Interval(2);
   mtf_indi1 = MA(C,30); // compressed indicator 1
    
   //... repeat indicators ....  
      
   TimeFrameRestore();
  
   mtf_expand1 = TimeFrameExpand(mtf_indi1, mx[i][0]);// expanded indicator 1
  
   Plot( mtf_expand1, "Plot of Indicator1_" + inter_val, colorRed + ((2*i) % 15), styleLine);
  
   mtf_selected1 = SelectedValue(mtf_expand1);// selected value of indicator1 for use in Gfx   
  
   // repeated Gfx stuff here...  
}
3 Likes

Thank you Mr . ( PeterD ) for the response and your efforts are appreciated .
Special thanks to the great Professor ( fxshrat ) for the response and the wonderful code .
Thank you all .

Great idea to use a matrix instead of string manipulation.

for give me
I am getting error 10


//https://forum.amibroker.com/t/how-to-reducing-the-code-or-simplify-it/36969/3

mx = Matrix(8,1);
rownum = MxGetSize(mx,0);
mx[0][0] = in5Minute;
mx[1][0] = in15Minute;
mx[2][0] = 2*in15Minute;
mx[3][0] = inHourly;
mx[4][0] = 4*inHourly;
mx[5][0] = inDaily;
mx[6][0] = inWeekly;
mx[7][0] = inMonthly;
for ( i = 0; i < rownum; i++ )
{
   TimeFrameSet( mx[i][5] );  
   inter_val = Interval(2);
   mtf_indi1 = MA(C,30); // compressed indicator 1
    
   //... repeat indicators ....  
      
   TimeFrameRestore();
   
     TimeFrameSet( mx[i][6] );  
   inter_val = Interval(2);
   mtf_indi2 = MA(C,30); // compressed indicator 1
    
   //... repeat indicators ....  
      
   TimeFrameRestore();
  
   mtf_expand1 = TimeFrameExpand(mtf_indi1, mx[i][5]);// expanded indicator 1
   mtf_expand2 = TimeFrameExpand(mtf_indi2, mx[i][6]);// expanded indicator 1
  
   Plot( mtf_expand1, "Plot of Indicator1" + inter_val, colorRed + ((2*i) % 15), styleLine);
     Plot( mtf_expand2, "Plot of Indicator2" + inter_val, colorBlue + ((2*i) % 15), styleLine);

   mtf_selected1 = SelectedValue(mtf_expand1);// selected value of indicator1 for use in Gfx   
   mtf_selected2 = SelectedValue(mtf_expand2);// selected value of indicator1 for use in Gfx   

   // repeated Gfx stuff here...  
}

but when I change less than sign to greater than sign
for ( i = 0; i > rownum; i++ )
the error disappear .

is it right ?

thank you

No, that's not correct what you do there.

You repeat code again and you exceed array size of matrix.

Instead...

mx = Matrix(4,1);
rownum = MxGetSize(mx,0);
mx[0][0] = in5Minute;
mx[1][0] = in15Minute;
mx[2][0] = 2*in15Minute;
mx[3][0] = inDaily;

for ( i = 0; i < rownum; i++ )
{
  TimeFrameSet( mx[i][0] );  
    inter_val = Interval(2);
    mtf_indi1 = MA(C,30); // compressed indicator 1
    mtf_indi2 = MA(C,60); // compressed indicator 2
    //... repeat indicators ....  
      
  TimeFrameRestore();

  mtf_expand1 = TimeFrameExpand(mtf_indi1, mx[i][0]);// expanded indicator 1
  mtf_expand2 = TimeFrameExpand(mtf_indi2, mx[i][0]);// expanded indicator 2

  if ( i == 2 OR i == 3 ) { // select intervals to be plotted
    Plot( mtf_expand1, "Plot of Indicator1_" + inter_val, colorRed + ((2*i) % 15), styleLine);
    Plot( mtf_expand2, "Plot of Indicator2_" + inter_val, colorBlue + ((2*i) % 15), styleLine);
  }

  mtf_selected1 = SelectedValue(mtf_expand1);// selected value of indicator1 for use in Gui/Gfx   
  mtf_selected2 = SelectedValue(mtf_expand2);// selected value of indicator2 for use in Gui/Gfx 
  // repeated GUI/Gfx stuff here...  
}
4 Likes

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