Simple Coding Help - No Promise.

Hi Ami Experts...

I am facing Problem in Exporting the USDINR and other currency futures data. When I use the below given AFL for Exporting the Intraday data in 1 minute TF , for all instruments the output file is fine but when I export the USDINR data , the output data is faulty. The data should have 4 decimal places as in case of currency futures but it is showing only 2 decimal places thus creating a faulty data.

Like the quote should be 67.0175 but the output shows it as 67.01




Please help ...

Thanks.
simple exploration code also will do.
Code:
// You can choose ticker and time range by setting "Apply to" an "Range"
// Explore data by execute exploration in Automatic Analysis window
// Export the exploration result by clicking "File --> Export"


Filter =  C>0 ;

AddColumn(O,"Open",1.4); 
AddColumn(H,"High",1.4); 
AddColumn(L,"Low",1.4); 
AddColumn(C,"Close",1.4); 
AddColumn(V,"Volume",1.0,1.0);
 
Hi Ami Experts...

I am facing Problem in Exporting the USDINR and other currency futures data. When I use the below given AFL for Exporting the Intraday data in 1 minute TF , for all instruments the output file is fine but when I export the USDINR data , the output data is faulty. The data should have 4 decimal places as in case of currency futures but it is showing only 2 decimal places thus creating a faulty data.

Like the quote should be 67.0175 but the output shows it as 67.01
I deal with this problem by having a utility function in a utility header file that gives me precision upto 4 places for CDS (INR) contracts and 2 places for others (NFO). See the code below inside "util.afl":

Code:
function getPrecision() {
	precision = 2;
	if(StrFind(Name(), "INR")) {
		precision = 4;
	}
	return precision;
}

function getFormatWithPrecision() {
	return (1 + getPrecision() * 0.1);
}
Thereafter I use it as follows:

Code:
#include <util.afl>
WriteVal(C, getFormatWithPrecision(), True);
You will have to modify the above a bit to suit your C style formatting requirement.

Regards
Curious_Trader
 
Last edited:

trash

Well-Known Member
Hi Ami Experts...

I am facing Problem in Exporting the USDINR and other currency futures data. When I use the below given AFL for Exporting the Intraday data in 1 minute TF , for all instruments the output file is fine but when I export the USDINR data , the output data is faulty. The data should have 4 decimal places as in case of currency futures but it is showing only 2 decimal places thus creating a faulty data.

Like the quote should be 67.0175 but the output shows it as 67.01

Code:
fmkdir( "C:\\SaveData" );
Buy = ( (DateNum() >= 1151222) AND (DateNum() <= 1160204) );

//if we want to export data for a single Day, we modify the Second line like this
//Buy = ( (DateNum() == 1070129));

for( i = 0; i < BarCount; i++ )
if( Buy[i] )
{
  //fh = fopen( "C:\\SaveData\\intraday.csv", "a");//if we want to export all data in a single file//
    fh = fopen( "C:\\SaveData\\"+Name()+".csv", "a");// if we want to export data in an individual file// 
  if( fh ) 
   { 
     y = Year(); 
     m = Month(); 
     d = Day(); 
     r = Hour();
     e = Minute();
   
   for( i = 0; i < BarCount; i++ )
   if( Buy[i] )

   { 
      fputs( Name() + "," , fh );
      ds = StrFormat("%02.0f/%02.0f/%02.0f%02,", 
                     d[ i ], m[ i ], y[ i ] ); 
      fputs( ds, fh ); 
     
      ts = StrFormat("%02.0f:%02.0f,", 
                     r[ i ],e[ i ]); 
      fputs( ts, fh ); 

      qs = StrFormat("%.2f,%.2f,%.2f,%.2f,%.0f\n", 
                     O[ i ],H[ i ],L[ i ],C[ i ],V[ i ] ); 
      fputs( qs, fh );
   }
   fclose( fh );
   }
}

Please help ...

Thanks.
The code you have there is a failure. It's a code floating around in Indian forums made by some newbie. It should not be used the way it is because it will raise few problems.

First of all it is insane using two loops. The first loop is completely redundant and does not add anything other than slow execution. Also you should not add array variables within barcount loop. It's amateurish. So in short just remove the first loop statement.

Secondly care should be taken if you use general file names other than a one with Name() function in it (i.e. intraday_data.csv). In such case you would have to add critical section. Since new analysis is multi threaded doing otherwise you will get corrupted data if writing data of multiple symbols to one single file without adding critical section code. So see example 2 here https://www.amibroker.com/guide/afl/staticvarcompareexchange.html on how to add critical section

Thirdly as for your actual "problem".
Instead of

Code:
qs = StrFormat("%.2f,%.2f,%.2f,%.2f,%.0f\n", 
                     O[ i ],H[ i ],L[ i ],C[ i ],V[ i ] );
simply write

Code:
qs = StrFormat("%g,%g,%g,%g,%1.0f\n", 
                     O[ i ],H[ i ],L[ i ],C[ i ],V[ i ] );

Another hint. Instead of such inflexible stuff like

Code:
Buy = ( (DateNum() >= 1151222) AND (DateNum() <= 1160204) );
Rather use

Code:
Buy = Status( "barinrange" );
because then any set range of analysis tool bar will be exported.
 
Last edited:

trash

Well-Known Member
Secondly care should be taken if you use general file names other than a one with Name() function in it (i.e. intraday_data.csv). In such case you would have to add critical section. Since new analysis is multi threaded doing otherwise you will get corrupted data if writing data of multiple symbols to one single file without adding critical section code. So see example 2 here https://www.amibroker.com/guide/afl/staticvarcompareexchange.html on how to add critical section
An alternative way to that one would be adding

Code:
#pragma maxthreads 1
at the top of file export code if you intend to write multiple symbols data to one single file.

Upper line would use just one thread because N = 1. So basically it would disable multi threading.

https://www.amibroker.com/guide/afl/_pragma.html

EDIT: maxthreads mode of #pragma was added to AB 5.95
 
Last edited:

cloudTrader

Well-Known Member
The code you have there is a failure. It's a code floating around in Indian forums made by some newbie. It should not be used the way it is because it will raise few problems.

First of all it is insane using two loops. The first loop is completely redundant and does not add anything other than slow execution. Also you should not add array variables within barcount loop. It's amateurish. So in short just remove the first loop statement.

Secondly care should be taken if you use general file names other than a one with Name() function in it (i.e. intraday_data.csv). In such case you would have to add critical section. Since new analysis is multi threaded doing otherwise you will get corrupted data if writing data of multiple symbols to one single file without adding critical section code. So see example 2 here https://www.amibroker.com/guide/afl/staticvarcompareexchange.html on how to add critical section

Thirdly as for your actual "problem".
Instead of

Code:
qs = StrFormat("%.2f,%.2f,%.2f,%.2f,%.0f\n", 
                     O[ i ],H[ i ],L[ i ],C[ i ],V[ i ] );
simply write

Code:
qs = StrFormat("%g,%g,%g,%g,%1.0f\n", 
                     O[ i ],H[ i ],L[ i ],C[ i ],V[ i ] );

Another hint. Instead of such inflexible stuff like

Code:
Buy = ( (DateNum() >= 1151222) AND (DateNum() <= 1160204) );
Rather use

Code:
Buy = Status( "barinrange" );
because then any set range of analysis tool bar will be exported.
Thanks for your inputs. The hints given by you have helped a lot as in the afl changes were to be made in the date every time data was to be exported.

Coding is Greek to me as have no technical background so cannot modify [barring minuscule obvious faults] things on my own.

Regards.
 
_SECTION_BEGIN("Price");
SetChartOptions(0,chartShowArrows|chartShowDates);
_N(Title = StrFormat("{{NAME}} - {{INTERVAL}} {{DATE}} O %g, H %g, L %g, C %g ", O, H, L, C, SelectedValue( ROC( C, 1 ) ) ));
Plot( C, "Close", ParamColor("Color", colorDefault ), styleNoTitle | ParamStyle("Style") | GetPriceStyle() );
Buy=Sell=Short=Cover=0;
_SECTION_END();

mktclose=Param("Market Close Time",152500,000000,235959,1 );

MA1 = MA(C,15);
MA2 = MA(C,20);

Buy = Cross(MA1,MA2);
Short = Cross(MA2,MA1);

Cover = TimeNum() == mktclose OR Buy ;
Sell = TimeNum() == mktclose OR Short ;

PlotShapes(IIf(Buy==1, shapeUpArrow , shapeNone), colorGreen, 0,Low, Offset=-20);
PlotShapes(IIf(Sell==1, shapeStar, shapeNone), colorRed, 0,High, Offset=30);
PlotShapes(IIf(Short==1, shapeDownArrow, shapeNone), colorRed, 0,High, Offset=-20);
PlotShapes(IIf(Cover==1, shapeStar, shapeNone), colorGreen, 0,Low, Offset=-30);

ApplyStop( 2, 1, 0.66, 0 );
ApplyStop( 0, 1, 0.33, 1 );

above system is simple crossover system. all signals are on candle close basis. i have added 0.33% fixed stoploss and 0.66% trailing stoploss by this

ApplyStop( 2, 1, 0.66, 0 );
ApplyStop( 0, 1, 0.33, 1 );

so in backtesting there is 0.33% fixed stoploss and 0.66% trailing stoploss.

what i want in instead of adding fixed stoploss and trailing stoploss by this

ApplyStop( 2, 1, 0.66, 0 );
ApplyStop( 0, 1, 0.33, 1 );

can i add this fixed stoploss and trailing stoploss in cover and short directly. i am working on semi auto system so i need these stoplosses add in cover and short signal.
can any one help me.
 

LOVEENAJYOTHI

Well-Known Member
Dearies,
Is there an AFL or Procedure to detect missing Bars (called Data Holes ? , not sure) in a Ticker/Symbol's data sample in a chosen range/date say a day ?

P.S :: TF of the Bar is Tick/Sec

Kindly share
Thank you
regards
 

Similar threads