Please use the SRC button when posting code. I have done it for you this time.
To be honest, your code is not easy to read.
Please use the SRC button when posting code. I have done it for you this time.
To be honest, your code is not easy to read.
Thank Keith.
This is my very first code in mql4.
could you please correct me by assuming that there is only two orders for each magic number.
And the name of the order is int ticket1,ticket2
thank you
Done.
Thanks
- if(OrderMagicNumber()==magicnumber) dblProfit=dblProfit+OrderProfit();Total profit is OrderProfit() + OrderSwap() + OrderCommission()
Your code while(boolTerm==false)
{//4-2-1
if(OrderSelect(POS,SELECT_BY_POS))
{//4-2-2
if(OrderMagicNumber()==magicnumber) dblProfit=dblProfit+OrderProfit();
POS++;
}//4-2-2
else
boolTerm=true;
}//4-2-1Simplified for(int i=OrderTotal() - 1; i >= 0; --i) if(
OrderSelect(POS,SELECT_BY_POS)
&& OrderMagicNumber()==magicnumber
) dblProfit=dblProfit+OrderProfit();- Not testing for symbol means the code breaks unless you use a different MN on other pairs. Symbol Doesn't equal
Ordersymbol when another currency is added to another seperate chart . - MQL4
forum
void checkprofit()
{
double dblProfit=0;
for(int i=OrdersTotal()-1;i>=0;i--)
{
if(OrderSelect(i,SELECT_BY_POS))
{
if(OrderMagicNumber()==magicnumber && OrderSymbol()==Symbol())
dblProfit+=OrderProfit()+OrderSwap()+OrderCommission();
}
}
if(dblProfit>=takeprofit || dblProfit<=stoploss)
{//4-2-3
for(int i=OrdersTotal()-1;i>=0;i--)
{//4-2-4
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
if(OrderMagicNumber()==magicnumber && OrderSymbol()==Symbol())
{//4-2-5
bool result=OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),slippage,Red);
if(result==false)
{//4-2-7
Sleep(0);
}//4-2-7
}//4-2-5
} //4-2-4
}//4-2-3
}
Not tested
Note OrderClosePrice() works for both Buys and Sells
- if(OrderMagicNumber()==magicnumber) dblProfit=dblProfit+OrderProfit();Total profit is OrderProfit() + OrderSwap() + OrderCommission()
Your code while(boolTerm==false)
{//4-2-1
if(OrderSelect(POS,SELECT_BY_POS))
{//4-2-2
if(OrderMagicNumber()==magicnumber) dblProfit=dblProfit+OrderProfit();
POS++;
}//4-2-2
else
boolTerm=true;
}//4-2-1Simplified for(int i=OrderTotal() - 1; i >= 0; --i) if(
OrderSelect(POS,SELECT_BY_POS)
&& OrderMagicNumber()==magicnumber
) dblProfit=dblProfit+OrderProfit();- Not testing for symbol means the code breaks unless you use a different MN on other pairs. Symbol Doesn't equal
Ordersymbol when another currency is added to another seperate chart . - MQL4
forum
I have this warning for OrderSelect function in 4-2-4 section:
return value of 'OrderSelect' should be checked last.mq4 200 8
I have this warning for OrderSelect function in 4-2-4 section:
return value of 'OrderSelect' should be checked last.mq4 200 8
Have you looked at the code that I posted earlier?
Yes I checked and now I reduced the code to:
{
int mTotal = OrdersTotal();
double Profit = 0;
for(int i = mTotal-1; i>=0; i--)
{
if (OrderSelect(i, SELECT_BY_POS) && OrderMagicNumber()==magicnumber)
Profit += OrderProfit()+OrderSwap()+OrderCommission();
}
if (Profit>= takeprofit || Profit<=stoploss)
{//4-2-3
for(int i = mTotal-1; i>=0; i--)
{//4-2-4
if(OrderSelect(i,SELECT_BY_POS) && OrderMagicNumber()==magicnumber)
OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),slippage,Red);
} //4-2-4
//return(0);
}//4-2-3
I See one warning:
return value of 'OrderClose' should be checked last.mq4 198 8 (in section 4-2-4)
By the way in code I opened orders like this:
int ticket2=OrderSend(SecondSymbol,OP_BUY,lotsize,MarketInfo(SecondSymbol,MODE_ASK),slippage,0,0,NULL,magicnumber,0,clrBlue);
Yes I checked and now I reduced the code to:
{
int mTotal = OrdersTotal();
double Profit = 0;
for(int i = mTotal-1; i>=0; i--)
{
if (OrderSelect(i, SELECT_BY_POS) && OrderMagicNumber()==magicnumber)
Profit += OrderProfit()+OrderSwap()+OrderCommission();
}
if (Profit>= takeprofit || Profit<=stoploss)
{//4-2-3
for(int i = mTotal-1; i>=0; i--)
{//4-2-4
if(OrderSelect(i,SELECT_BY_POS) && OrderMagicNumber()==magicnumber)
OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),slippage,Red);
} //4-2-4
//return(0);
}//4-2-3
I See one warning:
return value of 'OrderClose' should be checked last.mq4 198 8 (in section 4-2-4)
By the way in code I opened orders like this:
int ticket2=OrderSend(SecondSymbol,OP_BUY,lotsize,MarketInfo(SecondSymbol,MODE_ASK),slippage,0,0,NULL,magicnumber,0,clrBlue);
Just added a minus sign before stoploss
if (Profit>= takeprofit || Profit<=(-stoploss))
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
{
double dblProfit=0;
int POS=0;
bool boolTerm=false;
while(boolTerm==false)
{//4-2-1
if(OrderSelect(POS,SELECT_BY_POS))
{//4-2-2
if(OrderMagicNumber()==magicnumber) dblProfit=dblProfit+OrderProfit();
POS++;
}//4-2-2
else
boolTerm=true;
}//4-2-1
if (dblProfit>= takeprofit || dblProfit<=stoploss)
{//4-2-3
for(int i=OrdersTotal()-1;i>=0;i--)
{//4-2-4
OrderSelect(i,SELECT_BY_TICKET,MODE_TRADES);
if(OrderMagicNumber()==magicnumber)
{//4-2-5
int type = OrderType();
bool result = false;
switch(type)
{//4-2-6
//Close opened long positions
case OP_BUY : result = OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_BID),slippage,Red);
break;
//Close opened short positions
case OP_SELL : result = OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_ASK),slippage,LimeGreen);
}//4-2-6
if(result == false)
{//4-2-7
Sleep(0);
}//4-2-7
}//4-2-5
} //4-2-4
//return(0);
}//4-2-3
}
Hi All
I have written an EA to open two order for each magic number ...
after opening the orders in every tick the Ea will check if the total profit or loss of two orders.. if they reach the target profit/loss the orders will close and open two new order.
I send the checkorder() function I wrote in my code.
My EA opens orders but it never close the orders after profit or loss is reached.
Could you please check the code and find the logic error/s.