I need write some function on mql4 code to open trading position on forex chart. In this case I write mql4 code for open BUY and SELL order by OrderSend () function - open market order. There are incliding parameters whitch I want to use. This parameters are specified in "()". I can use all parameters including function "OrderSend".
For example: type of order, lot amount, StopLoss, TakeProfit, magic number of order. However I can use all paremeters.
I write:
int OpenPendingOrder( int pType,double pLots,double pLevel,int sp,double pr,int sl,int tp,string comm,int pMagic,color pColor ){
int ticket = 0;
if ( pType == 0) /*0 - BUY order according mql4 including function*/
{
ticket = OrderSend( Symbol(), OP_BUY, pLots, Ask, sp, Bid, sl, comm, pMagic, 0, pColor );
}
else /* 1 - SELL order according mql4 including function */
{
ticket = OrderSend( Symbol(), OP_SELL, pLots, Bid, sp, Ask, sl, comm, pMagic, 0, pColor);
}
return ( ticket );
}
We have many open orders and is need to know how much it is. So, I need calculate amount of open orders. I do this by OrderSelect () function.
I write:
int CountTrades() // the name of my function
{
int count = 0; // the value
for (int trade = OrdersTotal() - 1; trade >= 0; trade--) // I do recalculation of all open positions
{
if (OrderSelect(trade, SELECT_BY_POS, MODE_TRADES) == TRUE)
if (OrderMagicNumber() != MagicNumber) continue; // if order magic number is not equal Magic Number - go to next
if (OrderMagicNumber() == MagicNumber) // yea! order magic number is select
if (OrderType() == OP_SELL || OrderType() == OP_BUY) count++; // do count +1;
}
return ( count ); // after the function is entering amount of all orders
}
In other time I need to calculate not only the number of orders, but also the amount of profit by each open order (it means OrderProfit() + OrderCommision() + OrderSwap()). I do the calculation by one of magic numbers. For example, only one magic number using one expert advisor (One expert advisor can control many orders, many magic numbers). So, I write next:
double CalculateProfit()
{
double Profit = 0;
for (cnt = OrdersTotal() - 1; cnt >= 0; cnt--)
{
if (OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES) == TRUE)
if (OrderMagicNumber() != MagicNumber) continue;
if (OrderMagicNumber() == MagicNumber)
if (OrderType() == OP_BUY || OrderType() == OP_SELL) Profit += OrderProfit() + OrderCommision() + OrderSwap();
}
return ( Profit );
}
I have one function on H4 graph, which open the orders by iMA signals ( iMA - Calculates the Moving Average indicator and returns its value ). Today I wrote a function to close this orders by iMA too. This function is located inside the mane code. So, what I have?
The simple function to close orders by iMA by market price.
bool order_close_position = TRUE;
...
if ( order_close_position )
{
for (int trade = OrdersTotal()-1; trade >= 0; trade--)
{
if (OrderSelect( trade, SELECT_BY_POS, MODE_TRADES) )
{
if (OrderSymbol() == MY_SYMBOL )
{
if ( OrderMagicNumber() == mg_b && OrderType() == OP_BUY)
{
if ( OrderProfit() > 0 && Ask > OrderOpenPrice() )
{
if (
iMA (MY_SYMBOL, 240, 13, 0, 0, 0, 1) < iOpen(MY_SYMBOL, 240, 1) && iMA (MY_SYMBOL, 240, 13, 0, 0, 0, 1) > iClose(MY_SYMBOL, 240, 1)
)
if ( Open[1] > Close[1] )
{
//if ( (Minute() == 0 || Minute() == 30 /*|| Minute() == 15 || Minute() == 45*/) )
{
if (OrderClose( OrderTicket() , OrderLots(), MarketInfo( MY_SYMBOL,MODE_BID ), slip, Blue) == true) break;
}
}
}
}
if ( OrderType() == OP_SELL && OrderMagicNumber() == mg_s )
{
if ( OrderProfit() > 0 && Bid < OrderOpenPrice() )
{
if (
iMA (MY_SYMBOL, 240, 9, 0, 0, 0, 1) < iClose(MY_SYMBOL, 240, 1) && iMA (MY_SYMBOL, 240, 9, 0, 0, 0, 1) > iClose(MY_SYMBOL, 240, 1)
)
if ( Close[1] > Open[1] )
{
//if ( (Minute() == 0 || Minute() == 30 /*|| Minute() == 15 || Minute() == 45*/) )
{
if (OrderClose( OrderTicket() , OrderLots(), MarketInfo( MY_SYMBOL,MODE_ASK ), slip, Blue) == true) break;
}
}
}
}
}
}
}
}
...
Using the function I have been wrote before in this page I can create a new function to calculate profit of all closed orders in the trade history. For example, I need to know how many profit I received up over the last 2 weeks. So, I need 2 time: the time begining and the time end.
In this case I used "OrderSelect ()" function with a pool parameter: "MODE_HISTORY".
double Result_2 ( datetime dt1, datetime dt2 )
{
double profit = 0;
for ( int i = 0; i < OrdersHistoryTotal(); i++ )
{
if ( OrderSelect( i, SELECT_BY_POS, MODE_HISTORY ))
{
if ( OrderClosePrice() == 0 ) continue;
if ( OrderCloseTime() < dt1 || OrderCloseTime() > dt2 ) continue;
profit += OrderProfit() + OrderCommission() + OrderSwap() ;
}
}
return ( profit );
}
If I need amount of profit only one symbol current - I'll add one parameter to select what I need: "string symbol" and use it inside this function.
double Result_2 ( datetime dt1, datetime dt2, string symbol )
{
profit = 0;
for ( int i = 0; i < OrdersHistoryTotal(); i++ )
{
if ( OrderSelect( i, SELECT_BY_POS, MODE_HISTORY ))
{
if ( OrderClosePrice() == 0 ) continue;
if ( OrderCloseTime() < dt1 || OrderCloseTime() > dt2 ) continue;
if ( OrderSymbol() == symbol ) // here use "EURUSD", "GBPUSD", "USDJPY", ..
profit += OrderProfit() + OrderCommission() + OrderSwap() ;
}
}
return ( profit );
}
In this case are calculated orders by OrderCloseTime() parameter in history. All orders have many parameters: ticket, orderopentime, price, lot, stoploss and other.
How can I use this function? very simple:
Result_2 ( iTime( Symbol(), PERIOD_W1, 1 ), iTime( Symbol(), PERIOD_W1, 0 ), Symbol() );
If I write " Symbol() " - it will be current symbol on chart, but I can use "USDCHF" or another symbol.
Why iTime? - because this function returns Time value for the bar of specified symbol with timeframe and shift.
So, iTime( Symbol(), PERIOD_W1, 1 ) - return a value the beginning of last week from first seconds. iTime( Symbol(), PERIOD_W1, 0 ) - return a value current time.
And, Result_2 ( iTime( Symbol(), PERIOD_W1, 1 ), iTime( Symbol(), PERIOD_W1, 0 ), Symbol() ) can be Result_2 ( iTime( Symbol(), 10080, 1 ), iTime( Symbol(), 10080, 0 ), Symbol() ). That's simple, right?
An expert advisor trade on my forex trading account and sometime I open orders by myself. How I can discern these orders? I can write comment (in special field "Comment") for each market order OR I can use MagicNumber for each orders.
MQL4 code be able to set the special parameter for each market order ( except was opened by hand, by myself ), like MagicNumber. In another case the Forex Broker can to see all of orders which have been open by expert or client ( which have been open from client's MT4 terminal ). They have special notes "expert" or "client" in MetaTrader 4 Manager.
In short. If order have been open by myself = MagicNumber is "0".
I write:
if ( OrderMagicNumber() == 0 ) OR I can set this parameter in name of this function: double Result_2 ( datetime dt1, datetime dt2, string symbol, int MagicNumber )
double Result_2 ( datetime dt1, datetime dt2, string symbol, int MagicNumber )
{
profit = 0;
for ( int i = 0; i < OrdersHistoryTotal(); i++ )
{
if ( OrderSelect(i, SELECT_BY_POS, MODE_HISTORY ))
{
if ( OrderClosePrice() == 0 ) continue;
if ( OrderCloseTime() < dt1 || OrderCloseTime() > dt2 ) continue;
if ( MagicNumber == 0 )
profit += OrderProfit() + OrderCommission() + OrderSwap() ;
}
}
return ( profit );
}
Good. But I want to calculate all my withdrawals of profit in History. I know that OrderType() == OP_BUY - that "0", OrderType() == OP_SELL - that "1".
So, what I have now? if ( OrderType () == 6 && OrderProfit () < 0 ) - good.
In this case a function return the amount calculation of balance operations in History which less then 0 - that is withdrawals orders.
double Result_2 ( datetime dt1, datetime dt2, string symbol, int MagicNumber )
{
profit = 0;
for ( int i = 0; i < OrdersHistoryTotal(); i++ )
{
if ( OrderSelect(i, SELECT_BY_POS, MODE_HISTORY ))
{
if ( OrderClosePrice() == 0 ) continue;
if ( OrderCloseTime() < dt1 || OrderCloseTime() > dt2 ) continue;
if ( MagicNumber == 0 )
if ( OrderType () == 6 && OrderProfit () < 0 )
profit += OrderProfit();
}
}
return ( profit );
}
Many time ago I looking for ideas to create new code for expert advisor. In this time a forex robot work. One time in the week I chech closed orders in the history and to calculate on paper amount of profits. A wile I create a simple mql4 function to calculate automatically.
It's so simple for mql4 code.
This function return a double type value ( 78.454534, 12.475435435, 78.6267567 ).
I write:
double Result_of_closed_positions ( datetime dt1, datetime dt2 )
{
double r = 0;
for ( int i = 0; i < OrdersHistoryTotal(); i++ )
{
if ( OrderSelect ( i, SELECT_BY_POS, MODE_HISTORY ) )
{
if ( OrderClosePrice() == 0 ) continue;
if ( ( OrderCloseTime() < dt1 ) || ( OrderCloseTime() > dt2 ) ) continue;
r = NormalizeDouble ( r + OrderProfit() + OrderCommission() + OrderSwap(), 2 );
}
}
return(r);
}
I use NormalizeDouble () function to rounding floating point number to a specified accuracy. I can set a parrameter digits what I want.
So, how can I use this function in my robot code? look on
double Result_of_closed_positions ( iTime ( Symbol (), PERIOD_W1, 1 ), iTime ( Symbol (), PERIOD_W1, 0 ) )
iTime ( Symbol (), PERIOD_W1, 1 ) - the start time of the last week
iTime ( Symbol (), PERIOD_W1, 0 ) - the start time of current week
or
iTime ( Symbol (), PERIOD_D1, 1 ) - the start time of the last day
iTime ( Symbol (), PERIOD_D1, 0 ) - the start time of the tooday
or
TimeCurrent () - time is now, this is mql4 built-in function
And when I write:
double Result_of_closed_positions ( iTime ( Symbol (), PERIOD_W1, 1 ), iTime ( Symbol (), PERIOD_W1, 0 ) ) / AccountBalance () * 100 % - I found a percent of profit money in this time.
For example, my profit $784, Account Depo is $5671, so = 784 / 5671 * 100 % = 13.82 %