Ask! - page 157

 

Try something like this :

string uniqueFileName()

{

string sname=TimeToStr(TimeCurrent(),TIME_DATE|TIME_SECONDS);

string tname="";

for (int i=0;i<StringLen(sname);i++)

{

string char = StringSubstr(sname,i,1);

if (char!="." && char!=":")

if (char==" ")

tname = tname+"_";

else tname = tname+char;

}

return(tname+"_"+Symbol()+Period()+".csv");

}[/php]this one adds hour, minute and seconds in the middle to make it more or less unique (so something like this "20090924_102541_EURUSD15.csv"). To make it really sure that the file does not exist it would have to be a bit more complicated (something like this :

[php]string uniqueFileName()

{

string sname=TimeToStr(TimeCurrent(),TIME_DATE|TIME_SECONDS);

string tname="";

int thand;

for (int i=0;i<StringLen(sname);i++)

{

string char = StringSubstr(sname,i,1);

if (char!="." && char!=":")

if (char==" ")

tname = tname+"_";

else tname = tname+char;

}

//

//

//

//

//

tname = tname+"_"+Symbol()+Period()+".csv";

thand = FileOpen(tname,FILE_BIN|FILE_READ);

if (thand==-1)

return(tname);

else

{

FileClose(thand);

return("failed to make unique name");

}

}

It will return the "failed to make unique name" string if it fails, in which case you have to decide of how many times you want it to try again

PS: the usage would be someFileName=uniqueFileName();

regards

mladen

tedalagi:
hi all,

I am a trader but I don't know about mq4 language and have been trying to do this since tuesday ...

I need to save my chart in csv format using a script. I got a nice script that I can simply drag it on any chart and any timeframe and it does just nice, except the filename.

I wish that it can automatically generate unique out filename every time I save the chart so i dont have to rename it every time I save another file in the same folder.

string fname = [date in YYYYMMDD format] + "_"+[date - hour only]+"_" + Symbol() + Period() + ".csv";

The code in question are in bold.

When I got the code right, I think the output filename will look like this

20090924_10_GBPJPY15.csv

Sorry that I can't spend much time to read 157 pages of this huge thread (I had tried and my head gone dizzy)

Thank you in advance.
 
 
 
mladen:
Use it like this (mql does not allow nested functions or procedures so the function or procedure must be a separate one)

It works perfectly, thank you very much.

mladen:

Of saving files any where : yes, it can be done using windows API. Here is a link with a good example and a good explanation how to do that : File Operations via WinAPI - MQL4 Articles

I will learn about it, and I sure return to this thread afterward

May God bless you.

 

"Duplicate entry": Why my program does not work?

Sorry: I posted this earlier, but I cannot relocate the post on the Forum.

(It also does not allow "bookmarks" from my computer history...)

Anyhow: wrote a simple prog, that tests if two consecutive bars are "up" or "down".

Ie: on "up", the high on bar1 is higher than on bar2, and the low on bar 1

is also higher than on bar2. (Down is symetrical.)

The prog works w/ breakouts, so on "up", the high of bar1 minus the low

of bar2 should be above some threshold. ("Down" is symetric.)

Now working w/ M30 GBPUSD. Picked this ""breakout threshold" by eyeball,

as did for the TakeProfit. StopLoss is as indicated in the prog.

So: trying to gain experience w/ MetaTrader & mql4.

Then, can try other progs, optimize things, etc.

So: got as far as having the prog compile, & run on the Strategy Tester.

But, it never gets down to the second half of the prog, or print that it

has entered an order.

For somebody experienced, whatever error I have is probably obvious.

It is not self evident to me.

So: could you help w/ this?

Thanks.

Prog follows.

//+------------------------------------------------------------------+

//| expert initialization function |

//+------------------------------------------------------------------+

int init()

{

//----

//----

return(0);

}

//+------------------------------------------------------------------+

//| expert deinitialization function |

//+------------------------------------------------------------------+

int deinit()

{

//----

//----

return(0);

}

//+------------------------------------------------------------------+

//| expert start function |

//+------------------------------------------------------------------+

int start()

{

//----

bool TrendUp1; double TakeProfitUp; double EE = .00500;

bool OrdTot_OK;double lot;double slip;

double SL_Trailing_Up;bool High2Up;bool Low2Up;

int ticketup;int ticketdown;bool DownAndBig; bool UpAndBig;

bool TrendDown; double SL_Trailing_Down;double TakeProfitDown;

bool High2down; bool Low2down;

int ABC = OrdersTotal();

double Size1 = .00630;bool BigUp; double A = High[1];double B= Low[2];

double C= High[2]; double D= Low[1];bool BigDown;

//------------------------------------down order start

if (ABC == 0)// no orders out is true

OrdTot_OK = true;

if (ABC == 0)// no orders out is true

Print ("on down order sequence, no orders were out");

if ((C-D) > Size1)//high of 2 less low of 1 > .0063

BigDown = true;

if ((C-D) > Size1)//high of 2 less low of 1 > .0063

Print ("on down order sequence, found a big breakout down");

if (High[1]< High[2])

High2down=true;

if (High[1]< High[2])

Print ("on down order sequence, the consecutive bars have decreasing highs");

if(Low[1]< Low[2])

Low2down=true;

if(Low[1]< Low[2])

Print ("on down order sequence, the consecutive bars have decreasing lows");

if (High2down && Low2down)

TrendDown=true;

if (High2down && Low2down)

Print ("on down order sequence, TrendDown flag is set");

if (TrendDown)

SL_Trailing_Down = High[1];

if (TrendDown)

Print ("on down order sequence, did set the stop_loss");

if (TrendDown)

TakeProfitDown = Bid - (.005);

if (TrendDown)

Print ("on down order sequence, got a takeprofit", TakeProfitDown);

if (TrendDown && BigDown)

DownAndBig = true;

if (DownAndBig && OrdTot_OK)

ticketdown= OrderSend(Symbol(),OP_SELL,lot, Bid,slip,SL_Trailing_Down,TakeProfitDown,Blue);

if (DownAndBig && OrdTot_OK)

Print ("on down order sequence, tried to enter a ticket", ticketdown);

if (ticketdown < 0)

Print("OrderSend_Down failed with error #",GetLastError());

return(0);

//----------------------------------------------down order done

//---------------------------------------------------up order start

if (ABC == 0)

OrdTot_OK = true;

if (ABC == 0)

Print ("on the going up sequence, no orders were out");

if ((A - B) > Size1)

BigUp = true;

if ((A - B) > Size1)

Print ("on the going up sequence, the size test for the two bars was ok");

if (High[1] > High[2])

High2Up = true;//44

if (High[1] > High[2])

Print ("on the going up sequence, the test for increasing highs was ok");

if(Low[1] > Low[2])

Low2Up=true;

if(Low[1] > Low[2])

Print ("on the going up sequence, the test for increasing lows was ok");

if (High2Up && Low2Up)

TrendUp1=true;

if (High2Up && Low2Up)

Print ("on the going up sequence, did set the flag for going up");

if (TrendUp1)

SL_Trailing_Up = Low[1] ;

if (TrendUp1)

Print ("on the going up sequence, established the stoploss");

if (TrendUp1)

TakeProfitUp = Ask + (.005);

if (TrendUp1)

Print ("on the going up sequence, established the takeprofit", TakeProfitUp);

if (TrendUp1 && BigUp)

UpAndBig = true;

if ( UpAndBig && OrdTot_OK)

ticketup = OrderSend(Symbol(), OP_BUY,lot, Ask,slip, SL_Trailing_Up,TakeProfitUp,Red);

if (UpAndBig && OrdTot_OK)

Print ("on the going up sequence, tried to send in an order", ticketup);

if (ticketup < 0)

Print("OrderSend_Up failed with error #",GetLastError());

return(0);

//-------------------------------------------------up order done

//----

return(0);// this line might be superfluous

//+------------------------------------------------------------------+

//----

return(0);

}

//+------------------------------------------------------------------+

 

Using multiple CharToStr????

I would like to place two wingding characters. One on top of the other. This is the code I am using to set the first character-----ObjectSetText("signal"+x+"0",CharToStr(104),symbolSize,"Wingdings",Yellow);.

I would like to add (236) and use a different color for it.

Is it possible to do? If it is, please help.

Thanks for looking,

gever17

 

Sleep | IsTradeContextBusy

Greeting to CodeMasters and all the rest!

I have an EA based on Fractals, it is used on 1 MT4 platform, and on multiple currency pairs, on D1 timeframe.

Since more than one copy of EA trades at the same moment, I get ''Trade Context Is Busy'' notification in my Journal.

I would like to insert Sleep or IsTradeContextBusy function into my EA, but do not know where to put it:

- after Send order function, separately for Sell and Buy function?

- after both Sell and Buy Send order function?

- how to solve this problem - with

if (IsTradeContextBusy() == true) Sleep (5000); return;

???

Please advise or point me to solution.

Thank you very much for your help,

Have fun,

Simon

EA attached.

 

Advise needed !

Hello,

I would really appreciate a programmers help with a problem I'm losing money over. It's just that I have an EA that should open positions at a certain price level, but sometimes whipsaw causes it to open too many positions. I'd like it to open only one. Can anyone tell me could this be fixed with one line of code or do I need to hire a programmer ?

Thank you.

 

Pls adv

Hi Guys, i m really dont unstd how MagicNumber works, bcos it seems like DOESNT WORK???? I hav made 2 simple EA attached, I m still learning.

1. Buy when price above 100SMA

2. Sell when price also above 100SMA

it mean when u attach them to 2 diffrent Eurusd chart, it should one Buy and one Sell same time. As theory, each EA works indepently .

But it doesnt... It seems like when Sell executed 1st, the Buy EA won't work, and vise-versa.

Can anyone recode my EA, the controlling MagicNumber ? Any wrong?

Thanks a lot

Files:
buy100ma.mq4  9 kb
sell100ma.mq4  9 kb
 
Ghosthand:
Hi Guys, i m really dont unstd how MagicNumber works, bcos it seems like DOESNT WORK???? I hav made 2 simple EA attached, I m still learning.

1. Buy when price above 100SMA

2. Sell when price also above 100SMA

it mean when u attach them to 2 diffrent Eurusd chart, it should one Buy and one Sell same time. As theory, each EA works indepently .

But it doesnt... It seems like when Sell executed 1st, the Buy EA won't work, and vise-versa.

Can anyone recode my EA, the controlling MagicNumber ? Any wrong?

Thanks a lot

Looks like you put magic number correctly. The only problem is that you are not checking IF the order with the same magic number exists in current trade pool. You should walk through all open orders and check if OrderMagicNumber() == Your magic number before entering new trade.