Need help with sell orders...

 

Hello!

I have a problem with the sell orders. The orders buy it's good.

I've to searched for all sites, but I didn't find a solution.

Please, can anybody help me?

This is the code:

int MagicNumber = 77777;
int TotalOrders,cnt,Ticket;

// Globals
int Spread = 0;
string FX_Symbol = "";


//+------------------------------------------------------------------+
//| expert initialization function |
//+------------------------------------------------------------------+
int init()
{
FX_Symbol = Symbol();
Spread = MarketInfo(FX_Symbol,MODE_SPREAD);

return(0);
}

//+------------------------------------------------------------------+
//| expert deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
return(0);
}

//+------------------------------------------------------------------+
//| expert start function |
//+------------------------------------------------------------------+
int start() {

int handle,handle_op,handle_op_act,handle_op_act_buy,handle_op_act_sell;
string valor,valor_op,valor_op_act,valor_op_act_buy,valor_op_act_sell;
string _Symbol,valor_s;
string arch_log,arch_log_op,arch_log_op_act,arch_log_op_act_buy,arch_log_op_act_sell;


_Symbol = Symbol();
arch_log = StringConcatenate(_Symbol, "_AH.txt");

handle=FileOpen(arch_log, FILE_CSV|FILE_READ);
if(handle>0)
{
valor=FileReadString(handle);
FileClose(handle);
}
else
{
Print("Error open file: ", GetLastError());
}

_Symbol = Symbol();
arch_log_op = StringConcatenate(_Symbol,"_AH_OP.txt");

handle_op=FileOpen(arch_log_op, FILE_CSV|FILE_READ);
if(handle_op>0)
{
valor_op=FileReadString(handle_op);
FileClose(handle_op);
}
else
{
Print("Error open file: ", GetLastError());
}

if((valor != "") && (valor != valor_op)) {
// Open buy
if(valor == "buy") {
if(Ticket <= 0)
{
Ticket = OrderSend(Symbol(), OP_BUY, 0.10, Ask , 3, 50, 0, EA_Name, MagicNumber, 0, Green);
}
else
{
Print("Error open order BUY : ", GetLastError());
}

arch_log_op_act_buy = StringConcatenate(_Symbol,"_AH_OP.txt");
handle_op_act_buy=FileOpen(arch_log_op_act_buy, FILE_CSV|FILE_WRITE);
if(handle_op_act_buy>0)
{
valor_s=StringTrimRight(valor);
valor_op_act_buy=FileWrite(handle_op_act_buy, valor_s);
FileClose(handle_op_act_buy);
}
else
{
Print("Error open file BUY: ", GetLastError());
}

}

// Open sell
if(valor == "sell") {
RefreshRates();

if(Ticket <= 0)
{
Ticket = OrderSend(Symbol(), OP_SELL, 0.10, Bid , 3, 50, 0, EA_Name, MagicNumber, 0, Red);
}
else
{
Print("Error open order SELL : ", GetLastError());
}

arch_log_op_act_sell = StringConcatenate(_Symbol,"_AH_OP.txt");
handle_op_act_sell=FileOpen(arch_log_op_act_sell, FILE_CSV|FILE_WRITE);
if(handle_op_act_sell>0)
{
valor_s=StringSubstr(valor, 0, 4);
valor_op_act_sell=FileWrite(handle_op_act_sell, valor_s);
FileClose(handle_op_act_sell);
}
else
{
Print("Error open file SELL: ", GetLastError());
}


}

// Close buy
if(valor == "close_buy") {

TotalOrders = OrdersTotal();
for ( cnt = 0; cnt < TotalOrders; cnt++)
{
OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
if ( OrderSymbol() == _Symbol)
{
if(OrderType()==OP_BUY) // long position is opened
{
OrderClose(OrderTicket(), OrderLots(), Bid, 3,Violet); // close position
arch_log_op_act = StringConcatenate(_Symbol, "_AccountHistory_OP. txt");
handle_op_act=FileOpen(arch_log_op_act, FILE_CSV|FILE_WRITE);
if(handle_op_act>0)
{
valor_s=StringSubstr(valor, 0, 9);
valor_op_act=FileWrite(handle_op_act, valor);
FileClose(handle_op_act);
}
else
{
Print("Error open file CLOSE_BUY: ", GetLastError());
}

return(0); // exit
}
}
}

}
// Cerramos Venta
if(valor == "close_sell") {
TotalOrders = OrdersTotal();
for ( cnt = 0; cnt < TotalOrders; cnt++)
{
OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
if ( OrderSymbol() == _Symbol)
{
if(OrderType()==OP_SELL) // long position is opened
{
OrderClose(OrderTicket(), OrderLots(), Ask, 3,Violet);
arch_log_op_act = StringConcatenate(_Symbol, "_AccountHistory_OP. txt");
handle_op_act=FileOpen(arch_log_op_act, FILE_CSV|FILE_WRITE);
if(handle_op_act>0)
{
valor_s=StringSubstr(valor, 0, 10);
valor_op_act=FileWrite(handle_op_act, valor);
FileClose(handle_op_act);
}
else
{
Print("Error open file CLOSE_SELL: ", GetLastError());
}

return(0); // exit
}
}
}

}

}

}

return(0);

Thank you.

 
I know the platform is lacking debugging features. So we just have to be much more careful in coding our EA.
In your case, the problem seems to be the parameters for OrderSend.
OP_BUY buys at current market (ask) price, SL below market price, and TP above market price.
OP_SELL sells at current market (bid) price, SL above market price, and TP below market price.
 
fireflies:
I know the platform is lacking debugging features. So we just have to be much more careful in coding our EA.
In your case, the problem seems to be the parameters for OrderSend.
OP_BUY buys at current market (ask) price, SL below market price, and TP above market price.
OP_SELL sells at current market (bid) price, SL above market price, and TP below market price.


Ok, thanks.

I've two question more of MetaTrader 4.

Can an EA on real time start another EA from tester strategy?

I've an EA, in tester, can this EA in tester strategy write any file to some folder out of \tester\files?

Sorry for my english, i'm spanish.

Thanks fireflies :-)

 
No EA calls another EA.
Yes an EA can write to a file.
 
And see F1 in client terminal: TOOLS => Configuration at Startup
 

I know that an EA can write to a file, but...

An EA in tester strategy (MetaTrader/tester/files), can write into this folder --> MetaTrader/experts/files?

Or an EA on real time can read a file into the folder MetaTrader/tester/files?

Thanks.

 
jjperellos:

I know that an EA can write to a file, but...

An EA in tester strategy (MetaTrader/tester/files), can write into this folder --> MetaTrader/experts/files?

Or an EA on real time can read a file into the folder MetaTrader/tester/files?

Conceptually I'd consider the possibility of an EA to write/read outside its designated folders (ie. testers/files during testing or experts/files during production) as a big security hole. Just consider how many nasty things you can do if it's allowed to access outside its sandbox.
However surely I prefer metatrader to have a bigger sandbox. Ie, I'd like to see an EA is allowed to read (only) to news folder and/or mail folder, and an EA that can read its own code file (experts/ folder). But then, it's not for me to decide. I'm just as grateful for having access to metatrader for free. :)
But then EA can include a .dll. And a .dll can do many things :)
 

Can an EA on real time start another EA from tester strategy?


Theoretically it is possible to start another instance of MT4 with another EA attached by using WinAPI functions via imported dll. I don't know about tester though. I don't think that terminal can be parametered to run tester upon starting but Im not sure in this.
 
See help in MetaTrader4 Client terminal (F1)
 
Oh, yes. I've always knew that Mt4 can be set up/programmed for anything. (Thats why I wrote I wasn't sure. :) )
So the final answer is, yes, an EA on real time can start another EA from tester strategy, not directly though, but it can surely be done.
Reason: