Lavoro terminato
Specifiche
I have wirtten a simple ea for mt4 which reads a file an executes an order. Now I need this ea for mt5.
The structure of the file is:
2012.10.25 09:00;EURUSD;LL;0.01;1.4200;2;0.003500;0.005500;0.005500;Runner ;12345;
ORDERTIME;Symbol;ORDERTYPE;Volume;Price ;Slippage;StopBreakEven;StopLoss;TakeProfit;Comment;MagicNumber;
ORDERTIME: Is used to detect outdated orders; Format 'YYYY.MM.DD HH:MM'.
ORDERTYPE: LL : Long Limit
LM : Long Entry
LS : Long Stop
SL : Short Limit
SM : Short Market
SS : Short Stop
XX : Exit Any
Here is part of the code which has to be translated:
//+------------------------------------------------------------------+
//| expert start function |
//+------------------------------------------------------------------+
void start()
{
string Buffer;
string FileNameX = FileName + "(X)";
int Handle;
string Order_Comment = NULL;
datetime Order_Expiration = NULL;
double Order_Lots = NULL;
int Order_MagicNumber = NULL;
double Order_Price = NULL;
int Order_Slippage = NULL;
double Order_StopBreakEven = NULL;
double Order_StopLoss = NULL;
string Order_Symbol = NULL;
datetime Order_OpenTime = NULL;
double Order_TakeProfit = NULL;
int Order_Ticket = NULL;
int Order_Type = NULL;
Buffer = "";
if ( FileCode == "ASCII" )
{
Handle = FileOpen( FileName, FILE_BIN|FILE_READ );
if ( ! ( Handle > 0 ) ) return;
if ( FileSize( Handle ) > 0 ) Buffer = FileReadString( Handle, FileSize( Handle ) );
FileClose( Handle );
}
if ( FileCode == "UNICODE" )
{
Buffer = ReadFile( TerminalPath() + "\\experts\\files\\" + FileName );
Buffer = StringSubstr( Buffer, 2 );
}
if ( Buffer == "" ) return;
Handle = FileOpen( FileNameX, FILE_BIN|FILE_WRITE );
FileWriteString( Handle, Buffer, StringLen( Buffer ) );
FileClose( Handle );
if ( ( Period() * 60 ) > 600 ) Order_Expiration = Time[ 0 ] + Period() * 60;
Handle = FileOpen( FileNameX, FILE_CSV|FILE_READ, ';' );
if ( ! ( Handle > 0 ) ) return;
if ( FileSize( Handle ) == NULL ) { FileClose( Handle ); return; }
Order_OpenTime = StrToTime( StringTrimRight( StringTrimLeft( FileReadString( Handle ) ) ) );
Order_Symbol = StringTrimRight( StringTrimLeft( FileReadString( Handle ) ) );
Order_Type = IO;
Buffer = StringTrimRight( StringTrimLeft( FileReadString( Handle ) ) );
if ( Buffer == "LL" ) Order_Type = LL;
if ( Buffer == "LM" ) Order_Type = LM;
if ( Buffer == "LS" ) Order_Type = LS;
if ( Buffer == "SL" ) Order_Type = SL;
if ( Buffer == "SM" ) Order_Type = SM;
if ( Buffer == "SS" ) Order_Type = SS;
if ( Buffer == "XX" ) Order_Type = XX;
Order_Lots = StrToDouble( StringTrimRight( StringTrimLeft( FileReadString( Handle ) ) ) );
if ( MarketInfo( Symbol(), MODE_LOTSTEP ) == 0.1 ) Order_Lots = NormalizeDouble( Order_Lots, 1 );
Order_Price = StrToDouble( StringTrimRight( StringTrimLeft( FileReadString( Handle ) ) ) );
Order_Slippage = StrToInteger( StringTrimRight( StringTrimLeft( FileReadString( Handle ) ) ) );
Order_StopBreakEven = StrToDouble( StringTrimRight( StringTrimLeft( FileReadString( Handle ) ) ) );
Order_StopLoss = StrToDouble( StringTrimRight( StringTrimLeft( FileReadString( Handle ) ) ) );
Order_TakeProfit = StrToDouble( StringTrimRight( StringTrimLeft( FileReadString( Handle ) ) ) );
Order_Comment = StringTrimRight( StringTrimLeft( StringTrimRight( StringTrimLeft( FileReadString( Handle ) ) ) ) );
Order_MagicNumber = StrToInteger( StringTrimRight( StringTrimLeft( FileReadString( Handle ) ) ) );
FileClose( Handle );
if ( ( TimeOut > 0 ) && ( Order_OpenTime < ( TimeCurrent() + TimeOffset * 60 - TimeOut * 60 ) ) )
{
Handle = FileOpen( FileNameX, FILE_BIN|FILE_READ );
LogEvent( LOGERROR, "TimeOut: " + FileReadString( Handle, FileSize( Handle ) ) );
FileClose( Handle );
return;
}
for ( int Order = OrdersTotal(); Order > 0; Order-- )
{
if ( OrderSelect( ( Order - 1 ), SELECT_BY_POS, MODE_TRADES ) != true ) continue;
if ( OrderSymbol() != Symbol() ) continue;
if ( ( Order_MagicNumber == NULL ) || ( Order_MagicNumber == OrderMagicNumber() ) ) { Order_Ticket = OrderTicket(); break; }
}
switch ( Order_Type )
{
case LL: Order_Ticket = ResetOrder( Order_Ticket, OP_BUYLIMIT, Order_Slippage );
if ( Order_Price == NULL ) Order_Price = Ask;
if ( CurrentEntries( Order_MagicNumber ) < PositionMax )
Order_Ticket = SendOrder( OP_BUYLIMIT, Order_Lots, Order_Price, NULL, Order_Comment, Order_MagicNumber, Order_Expiration );
if ( OrderSelect( Order_Ticket, SELECT_BY_TICKET ) != true ) break;
// ???
Order_Ticket = ModifyOrder( Order_Ticket, ( Order_Price - Order_StopLoss ), ( Order_Price + Order_TakeProfit ) );
break;
case LM: Order_Ticket = ResetOrder( Order_Ticket, OP_BUY, Order_Slippage );
if ( CurrentEntries( Order_MagicNumber ) < PositionMax )
Order_Ticket = SendOrder( OP_BUY, Order_Lots, Order_Price, Order_Slippage, Order_Comment, Order_MagicNumber, NULL );
if ( OrderSelect( Order_Ticket, SELECT_BY_TICKET ) != true ) break;
if ( ( ( Order_StopLoss > 0 ) && ( OrderStopLoss() == NULL ) ) || ( ( Order_TakeProfit > 0 ) && ( OrderTakeProfit() == NULL ) ) )
{
if ( Order_StopLoss > 0 ) Order_StopLoss = OrderOpenPrice() - Order_StopLoss; else Order_StopLoss = OrderStopLoss();
if ( Order_TakeProfit > 0 ) Order_TakeProfit = OrderOpenPrice() + Order_TakeProfit; else Order_TakeProfit = OrderTakeProfit();
Order_Ticket = ModifyOrder( Order_Ticket, Order_StopLoss, Order_TakeProfit );
}
if ( ( Order_StopBreakEven > 0 ) && ( Bid > ( OrderOpenPrice() + Order_StopBreakEven ) ) )
Order_Ticket = ModifyOrder( Order_Ticket, OrderOpenPrice(), OrderTakeProfit() );
break;
case LS: Order_Ticket = ResetOrder( Order_Ticket, OP_BUYSTOP, Order_Slippage );
if ( Order_Price == NULL ) Order_Price = Ask;
if ( CurrentEntries( Order_MagicNumber ) < PositionMax )
Order_Ticket = SendOrder( OP_BUYSTOP, Order_Lots, Order_Price, NULL, Order_Comment, Order_MagicNumber, Order_Expiration );
if ( OrderSelect( Order_Ticket, SELECT_BY_TICKET ) != true ) break;
// ???
Order_Ticket = ModifyOrder( Order_Ticket, ( Order_Price - Order_StopLoss ), ( Order_Price + Order_TakeProfit ) );
break;
case SL: Order_Ticket = ResetOrder( Order_Ticket, OP_SELLLIMIT, Order_Slippage );
if ( Order_Price == NULL ) Order_Price = Bid;
if ( CurrentEntries( Order_MagicNumber ) < PositionMax )
Order_Ticket = SendOrder( OP_SELLLIMIT, Order_Lots, Order_Price, NULL, Order_Comment, Order_MagicNumber, Order_Expiration );
if ( OrderSelect( Order_Ticket, SELECT_BY_TICKET ) != true ) break;
// ???
Order_Ticket = ModifyOrder( Order_Ticket, ( Order_Price + Order_StopLoss ), ( Order_Price - Order_TakeProfit ) );
break;
case SM: Order_Ticket = ResetOrder( Order_Ticket, OP_SELL, Order_Slippage );
if ( CurrentEntries( Order_MagicNumber ) < PositionMax )
Order_Ticket = SendOrder( OP_SELL, Order_Lots, Order_Price, Order_Slippage, Order_Comment, Order_MagicNumber, NULL );
if ( OrderSelect( Order_Ticket, SELECT_BY_TICKET ) != true ) break;
if ( ( ( Order_StopLoss > 0 ) && ( OrderStopLoss() == NULL ) ) || ( ( Order_TakeProfit > 0 ) && ( OrderTakeProfit() == NULL ) ) )
{
if ( Order_StopLoss > 0 ) Order_StopLoss = OrderOpenPrice() + Order_StopLoss; else Order_StopLoss = OrderStopLoss();
if ( Order_TakeProfit > 0 ) Order_TakeProfit = OrderOpenPrice() - Order_TakeProfit; else Order_TakeProfit = OrderTakeProfit();
Order_Ticket = ModifyOrder( Order_Ticket, Order_StopLoss, Order_TakeProfit );
}
if ( ( Order_StopBreakEven > 0 ) && ( Ask < ( OrderOpenPrice() - Order_StopBreakEven ) ) )
Order_Ticket = ModifyOrder( Order_Ticket, OrderOpenPrice(), OrderTakeProfit() );
break;
case SS: Order_Ticket = ResetOrder( Order_Ticket, OP_SELLSTOP, Order_Slippage );
if ( Order_Price == NULL ) Order_Price = Bid;
if ( CurrentEntries( Order_MagicNumber ) < PositionMax )
Order_Ticket = SendOrder( OP_SELLSTOP, Order_Lots, Order_Price, NULL, Order_Comment, Order_MagicNumber, Order_Expiration );
if ( OrderSelect( Order_Ticket, SELECT_BY_TICKET ) != true ) break;
// ???
Order_Ticket = ModifyOrder( Order_Ticket, ( Order_Price + Order_StopLoss ), ( Order_Price - Order_TakeProfit ) );
break;
case XX: Order_Ticket = ResetOrder( Order_Ticket, -1, Order_Slippage );
break;
default: Handle = FileOpen( FileNameX, FILE_BIN|FILE_READ );
LogEvent( LOGERROR, "Invalid OrderType: " + FileReadString( Handle, FileSize( Handle ) ) );
FileClose( Handle );
}
if ( Debug )
{
Handle = FileOpen( FileNameX, FILE_BIN|FILE_READ );
LogEvent( LOGDEBUG, "FileSize = " + FileSize( Handle ) + "; '" + FileReadString( Handle, FileSize( Handle ) ) );
FileClose( Handle );
}
}