Saturday, September 21, 2013

EXCEL Text/CSV File Import: How to prevent Excel from converting a text value to numeric value

Came across this irritating problem. I have a text file (csv) that I want to import into excel. Some of the columns in the text are numbers but they have leading zeros.
For example:
UserID,UserName,UserAge
00123,TestUser01,21
01221,User02,23

When you import such a text file into Excel, it will convert the UserID into NUMERIC value instead of a TEXT value. Thus resulting in the truncation of the leading zero.

This is also true when you are trying to import a DATE value. Excel will automatically convert it into a date, but if your text file does not use the standard MM/DD/YYYY format, it will screw up.

To prevent Excel from automatically converting these data, you will need to tell Excel to maintain them as TEXT field. To do this, put the text value into a ="" qualifier. This is what you do in the .csv file:

UserID,UserName,UserAge
="00123",TestUser01,21
="01221",User02,23

When you import it, the 00123 will remain as 00123. Or if it is a custom date field, it will remain as text value.

Hope this helps someone out there. You can always contact me at joseph.lee (at) f s.com.my (it is done this way purposely from preventing spam email crawler).

Wednesday, September 18, 2013

MQL4 (Forex): EA to show Price Range of Currency Pair

Ever wanted to know what is the maximum price range for a particular currency pair?
For example: You want to know the largest/average monthly price range for GBPUSD for the past 12 months?

Here's a code for this. I've also added the LongSwap and ShortSwap interests. For those who are looking for a cross-broker Swap Hedge (one-side pay swap interest, one-side do not charge swap interest), this is useful to know which currency pair pays the highest interest.

I've also included a SwpIndex especially for those who are exploring the Interest Swap Hedging trying to figure out which currency pair is the best for that purpose. Basically, it takes into consideration the Interest Swap paid/charge, the longest price range for that period and also the (current) spread. These are the factors that determine the most suitable pair for this purpose.

Drop this EA on each of the currency pair chart in MT4 (MetaTrader 4).

//+------------------------------------------------------------------+
//|                                            PriceRangeOfChart.mq4 |
//|                                             http://www.fs.com.my |
//+------------------------------------------------------------------+
#property copyright "Copyright 2013, EMail: joseph.lee [a.t.] f s.c o m.my"
#property link      "http://www.fs.com.my"

//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
double viPricePerPip = 0.0001;
double viPointsToPip = 1;
string vsLeftTab = "                                   ";

//=================================================
int init() {
if((Digits == 2) || (Digits == 3)) {viPricePerPip = 0.01;}
if((Digits == 3) || (Digits == 5)) {viPointsToPip = 10;}
}
//=================================================
int start() {
int viIndex;
double viLength;
double viStartTick = GetTickCount();

double viSpread = (Ask-Bid)/viPricePerPip;
double viPipValue = MarketInfo( Symbol(), MODE_TICKVALUE) * viPointsToPip;

int pvaiCandlePeriods[5];
pvaiCandlePeriods[0] = 7;
pvaiCandlePeriods[1] = 12;
pvaiCandlePeriods[2] = 14;
pvaiCandlePeriods[3] = 24;
pvaiCandlePeriods[4] = 30;

double viSwapLongInPips = MarketInfo( Symbol(), MODE_SWAPLONG );
double viSwapShortInPips= MarketInfo( Symbol(), MODE_SWAPSHORT );
string pvsDisplay = "";
for(viIndex=0; viIndex viLength = fniGetAvgCandleLen(pvaiCandlePeriods[viIndex]);
pvsDisplay = pvsDisplay + "\n" + vsLeftTab + "Avg Candle (" + pvaiCandlePeriods[viIndex] + "): " + DoubleToStr(viLength,1) + "pips, ";
pvsDisplay = pvsDisplay + "SWPIndex: ";
if( viLength != 0 ) {
pvsDisplay = pvsDisplay + DoubleToStr(1000*viSwapLongInPips/(viLength*viSpread), 2 ) + " / ";
pvsDisplay = pvsDisplay + DoubleToStr(1000*viSwapShortInPips/(viLength*viSpread), 2 );
}
}
pvsDisplay = pvsDisplay + "\n";
for(viIndex=0; viIndex viLength = fniGetLongestCandleLen(pvaiCandlePeriods[viIndex]);
pvsDisplay = pvsDisplay + "\n" + vsLeftTab + "Longest Candle (" + pvaiCandlePeriods[viIndex] + "): " + DoubleToStr(viLength,1) + "pips, ";
pvsDisplay = pvsDisplay + "SWPIndex: ";
if( viLength != 0 ) {
pvsDisplay = pvsDisplay + DoubleToStr(1000*viSwapLongInPips/(viLength*viSpread), 2 ) + " / ";
pvsDisplay = pvsDisplay + DoubleToStr(1000*viSwapShortInPips/(viLength*viSpread), 2 );
}
}

Comment(
 "\n" + vsLeftTab + "Chart Candle LENGTH (" + Symbol() + ")\n" + vsLeftTab + "======================"
, "\n" + vsLeftTab + "Current Ask/Bid: "
+ DoubleToStr( Ask, Digits ) + " / "
+ DoubleToStr( Bid, Digits )
+ " (" + DoubleToStr(viSpread,1) + "pips Spread)"
+ " Pip Value: $" + DoubleToStr(viPipValue,2)
, "\n" + vsLeftTab + "Swap Long/Short: "
+ DoubleToStr(viSwapLongInPips, 2) + "pips / "
+ DoubleToStr(viSwapShortInPips, 2) + "pips"
, "\n"
, pvsDisplay
);
return(0);
}
//====================================================================
double fniGetAvgCandleLen( int pviCount ) {
double viTotalValue = 0.0;
for( int viIndex=1; viIndex<=pviCount; viIndex++ ) {
viTotalValue += (iHigh( Symbol(), 0, viIndex ) - iLow( Symbol(), 0, viIndex ))/viPricePerPip;
}
if( pviCount != 0 )
return( viTotalValue/pviCount );
else
return( 0 );
}
double fniGetLongestCandleLen( int pviCount ) {
double viLength = 0.0;
double viLongest = 0.0;
for( int viIndex=1; viIndex<=pviCount; viIndex++ ) {
viLength = (iHigh( Symbol(), 0, viIndex ) - iLow( Symbol(), 0, viIndex ))/viPricePerPip;
if( viLength > viLongest ) viLongest = viLength;
}
return( viLongest );
}
double fniGetShortestCandleLen( int pviCount ) {
double viLength = 0.0;
double viShortest = 0.0;
for( int viIndex=1; viIndex<=pviCount; viIndex++ ) {
viLength = (iHigh( Symbol(), 0, viIndex ) - iLow( Symbol(), 0, viIndex ))/viPricePerPip;
if( viLength < viShortest ) viShortest = viLength;
}
return( viShortest );
}
//====================================================================