Forex



Go Back   Forex Trading > Programming > MetaTrader
Forex Forum Register More recent Calendar Advertising Others Help






Register
Welcome to Forex-TSD!, one of the largest Forex forums worldwide, where you will be able to find the most complete and reliable Forex information imaginable.

From the list below, select the forum that you want to visit and register to post, as many times you want. It’s absolutely free. Click here for registering on Forex-TSD.

Exclusive Forum
The Exclusive Forum is the only paid section. Once you subscribe, you will get free access to real cutting-edge Trading Systems (automated and not), Indicators, Signals, Articles, etc., that will help and guide you, in ways that you could only imagine, with your Forex trading.
  • Elite Section
    Get access to private discussions, specialized support, indicators and trading systems reported every week.
  • Advanced Elite Section
    For professional traders, trading system developers and any other member who may need to use and/or convert, the most cutting-edge exclusive indicators and trading systems for MT4 and MT5.
See more

Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old 03-29-2008, 03:18 PM
MiniMe's Avatar
Senior Member
 
Join Date: Nov 2006
Location: Montréal
Posts: 1,451
MiniMe is an unknown quantity at this point
Red face Close orders with magic number at certain profit

I need help please,

I am trying to close orders from different currency pairs ( long and short ) but with the same magic number when the profit reach define target " lets say 15 pips" without touching the other orders with different magic number

so I wrote this code to check open trades and count the profit on trades with magic number 111
PHP Code:
//+------------------------------------------------------------------+
//|  check profitable positions with magic 111                       |
//+------------------------------------------------------------------+
for(cnt=OrdersTotal();cnt>=0;cnt--)
      {
         
OrderSelect(cntSELECT_BY_POSMODE_TRADES);
         if( 
OrderMagicNumber()==111 )
            {
               if(
OrderType()==OP_BUY)
                  {
                     
CurrentProfitB+=Bid-OrderOpenPrice() ;
                  }
//if(OrderType()==OP_BUY)
               
if(OrderType()==OP_SELL)
                  {
                     
CurrentProfitS+=OrderOpenPrice()-Ask;
                 }
//if(OrderType()==OP_SELL)
            
//if( OrderSymbol()==Symbol1 && OrderMagicNumber()==111 ) 
          
CurrentProfit=CurrentProfitB+CurrentProfitS;    
       }
// for(cnt=OrdersTotal();cnt>=0;cnt--)        

//====================== 
then I check if we make profit
PHP Code:
// Did we make a profit
//======================
if(profit>&& CurrentProfit>=(profit*Point))
  {
   while(
true)
     {
      
CloseAllOrd(111);               
     }
//while
  
}//if(profit>0 && CurrentProfit>=(profit*Point)) 
if we make a profit I call the function CloseAllOrd
PHP Code:
//+------------------------------------------------------------------+
//|                              Close all                           |
//+------------------------------------------------------------------+
void CloseAllOrd(int magic)
{
      
RefreshRates();
      
int total  OrdersTotal();
      for (
int cnt cnt total cnt++)
      {
         
OrderSelect(0,SELECT_BY_POS,MODE_TRADES);
         if (
OrderMagicNumber() == magic)
         if(
IsTradeContextBusy()) Sleep(1000);
         if(
IsTradeContextBusy()) Sleep(2000);
         
            if(
OrderType()==OP_BUY)
             
OrderClose(OrderTicket(),OrderLots(),Bid,slippage,White);
            if(
OrderType()==OP_SELL)   
             
OrderClose(OrderTicket(),OrderLots(),Ask,slippage,White);
      } 


however this will only close EURUSD after it reach 1 pip of profit sometimes 2

I need to close all orders( different currency pairs ) with magic number 111 at lets say 15 pips of profit , without touching other orders

can someone please help me
__________________

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!StumbleUpon this Post!Reddit this Post!Facebook this Post!BlinkList this Post!Google Bookmarks this Post!Yahoo! My Web this Post!
Reply With Quote
  #2 (permalink)  
Old 03-29-2008, 03:48 PM
Mistigri's Avatar
Member
 
Join Date: Mar 2006
Posts: 34
Mistigri is on a distinguished road
Hi There ...

Because you are working with any symbol you can not use things such as bid , ask, point but rather MarketInfo( OrderSymbol(), MODE_BID ), MarketInfo( OrderSymbol(), MODE_ASK ) etc ...

Try updating to this:
PHP Code:
//+------------------------------------------------------------------+
//|  check profitable positions with magic 111                       |
//+------------------------------------------------------------------+
for(cnt=OrdersTotal();cnt>=0;cnt--)
      {
         
OrderSelect(cntSELECT_BY_POSMODE_TRADES);
         if( 
OrderMagicNumber()==111 )
            {
               if(
OrderType()==OP_BUY)
                  {
                     
CurrentProfitB+= MarketInfoOrderSymbol(), MODE_BID)-OrderOpenPrice() ;
                  }
//if(OrderType()==OP_BUY)
               
if(OrderType()==OP_SELL)
                  {
                     
CurrentProfitS+=OrderOpenPrice()- MarketInfoOrderSymbol(), MODE_ASK );
                 }
//if(OrderType()==OP_SELL)
            
//if( OrderSymbol()==Symbol1 && OrderMagicNumber()==111 ) 
          
CurrentProfit=CurrentProfitB+CurrentProfitS;    
       }
// for(cnt=OrdersTotal();cnt>=0;cnt--)        

//====================== 
PHP Code:
//+------------------------------------------------------------------+
//|                              Close all                           |
//+------------------------------------------------------------------+
void CloseAllOrd(int magic)
{
      
RefreshRates();
      
int total  OrdersTotal();
      for (
int cnt cnt total cnt++)
      {
         
OrderSelect(0,SELECT_BY_POS,MODE_TRADES);
         if (
OrderMagicNumber() == magic)
         if(
IsTradeContextBusy()) Sleep(1000);
         if(
IsTradeContextBusy()) Sleep(2000);
         
            if(
OrderType()==OP_BUY)
             
OrderClose(OrderTicket(),OrderLots(),MarketInfoOrderSymbol(), MODE_BID),slippage,White);
            if(
OrderType()==OP_SELL)   
             
OrderClose(OrderTicket(),OrderLots(),MarketInfoOrderSymbol(), MODE_ASK),slippage,White);
      } 

You may want to check your log for more errors but that should do it.

Patrick
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!StumbleUpon this Post!Reddit this Post!Facebook this Post!BlinkList this Post!Google Bookmarks this Post!Yahoo! My Web this Post!
Reply With Quote
  #3 (permalink)  
Old 03-29-2008, 03:56 PM
mikkom's Avatar
Senior Member
 
Join Date: Oct 2007
Posts: 285
mikkom is on a distinguished road
Quote:
Originally Posted by MiniMe View Post
I need help please,

I am trying to close orders from different currency pairs ( long and short ) but with the same magic number when the profit reach define target " lets say 15 pips" without touching the other orders with different magic number
Here is some code that should work

Code:
void closeProfitable(int tpPips, int magic) {
   for(i=0; i<OrdersTotal(); i++) {
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false)        
         break;
      if(OrderMagicNumber() != magic) 
         continue;

        double oAsk = MarketInfo(OrderSymbol(), MODE_ASK);
        double oBid = MarketInfo(OrderSymbol(), MODE_BID);

        if(OrderType() == OP_BUY &&
           oBid > OrderOpenPrice() + tpPips * Point) {
               OrderClose(OrderTicket(),OrderLots(),oBid,0);
               i--;               
            }

        if(OrderType() == OP_SELL &&
           oAsk < OrderOpenPrice() - tpPips * Point) {
               OrderClose(OrderTicket(),OrderLots(),oAsk,0);
               i--;               
            }
   }
}
ps. that code is not tested, I wrote it to this post directly but it should work

Last edited by mikkom; 03-29-2008 at 03:58 PM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!StumbleUpon this Post!Reddit this Post!Facebook this Post!BlinkList this Post!Google Bookmarks this Post!Yahoo! My Web this Post!
Reply With Quote
  #4 (permalink)  
Old 03-29-2008, 03:57 PM
MiniMe's Avatar
Senior Member
 
Join Date: Nov 2006
Location: Montréal
Posts: 1,451
MiniMe is an unknown quantity at this point
Thanks a lot Patrick, I think you are right. I will change the code and try it
__________________

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!StumbleUpon this Post!Reddit this Post!Facebook this Post!BlinkList this Post!Google Bookmarks this Post!Yahoo! My Web this Post!
Reply With Quote
  #5 (permalink)  
Old 03-29-2008, 04:02 PM
MiniMe's Avatar
Senior Member
 
Join Date: Nov 2006
Location: Montréal
Posts: 1,451
MiniMe is an unknown quantity at this point
Yes but that would close trades on profit , I am looking at the combined trades menaing that EURUSD could be in 30pips+ and USDCHF could be in -15 pips so I look at the overall profit of trades with magic number 111 if the total profit from all those trades reach 15 pips I close all orders of magic number 111 only at combined profit of 15 pips


Quote:
Originally Posted by mikkom View Post
Here is some code that should work

Code:
void closeProfitable(int tpPips, int magic) {
   for(i=0; i<OrdersTotal(); i++) {
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false)        
         break;
      if(OrderMagicNumber() != magic) 
         continue;

        double oAsk = MarketInfo(OrderSymbol(), MODE_ASK);
        double oBid = MarketInfo(OrderSymbol(), MODE_BID);

        if(OrderType() == OP_BUY &&
           oBid > OrderOpenPrice() + tpPips * Point) {
               OrderClose(OrderTicket(),OrderLots(),oBid,0);
               i--;               
            }

        if(OrderType() == OP_SELL &&
           oAsk < OrderOpenPrice() - tpPips * Point) {
               OrderClose(OrderTicket(),OrderLots(),oAsk,0);
               i--;               
            }
   }
}
ps. that code is not tested, I wrote it to this post directly but it should work
__________________

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!StumbleUpon this Post!Reddit this Post!Facebook this Post!BlinkList this Post!Google Bookmarks this Post!Yahoo! My Web this Post!
Reply With Quote
  #6 (permalink)  
Old 03-29-2008, 04:09 PM
mikkom's Avatar
Senior Member
 
Join Date: Oct 2007
Posts: 285
mikkom is on a distinguished road
Quote:
Originally Posted by MiniMe View Post
Yes but that would close trades on profit , I am looking at the combined trades menaing that EURUSD could be in 30pips+ and USDCHF could be in -15 pips so I look at the overall profit of trades with magic number 111 if the total profit from all those trades reach 15 pips I close all orders of magic number 111 only at combined profit of 15 pips
Okay now I get your point.. If you don't mind using a big array..

Code:
#define MAX_MAGIC_NUMBER 100 // or whatever

void closeProfitable(int tpPips, int magic) {
   double profit[MAX_MAGIC_NUMBER];
   for(i=0; i<OrdersTotal(); i++) {
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false)        
         break;
      if(OrderMagicNumber() != magic) 
         continue;

        double oAsk = MarketInfo(OrderSymbol(), MODE_ASK);
        double oBid = MarketInfo(OrderSymbol(), MODE_BID);

        if(OrderType() == OP_BUY) 
           profit[OrderMagicNumber()] += oBid - OrderOpenPrice();
        if(OrderType() == OP_SELL) 
           profit[OrderMagicNumber()] += OrderOpenPrice() - oAsk;

   }
   for(i=0; i<OrdersTotal(); i++) {
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false)        
         break;
      if(OrderMagicNumber() != magic) 
         continue;

        double oAsk = MarketInfo(OrderSymbol(), MODE_ASK);
        double oBid = MarketInfo(OrderSymbol(), MODE_BID);

        if(profit[OrderMagicNumber()] >= tpPips*Point) {
            if(OrderType() == OP_BUY) {
               OrderClose(OrderTicket(),OrderLots(),oBid,0);
               i--;               
            }
            if(OrderType() == OP_SELL) {
               OrderClose(OrderTicket(),OrderLots(),oAsk,0);
               i--;               
            }
        }
}
same disclaimers as above, I didn't test this at all
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!StumbleUpon this Post!Reddit this Post!Facebook this Post!BlinkList this Post!Google Bookmarks this Post!Yahoo! My Web this Post!
Reply With Quote
  #7 (permalink)  
Old 03-29-2008, 04:54 PM
MiniMe's Avatar
Senior Member
 
Join Date: Nov 2006
Location: Montréal
Posts: 1,451
MiniMe is an unknown quantity at this point
what does
#define MAX_MAGIC_NUMBER 100 // or whatever
do ?

also this line
double profit[MAX_MAGIC_NUMBER];

do I just put 111 there ? I am a bit confused as I am new in programming

Thanks a lot for your help
__________________

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!StumbleUpon this Post!Reddit this Post!Facebook this Post!BlinkList this Post!Google Bookmarks this Post!Yahoo! My Web this Post!
Reply With Quote
  #8 (permalink)  
Old 03-30-2008, 11:23 AM
Senior Member
 
Join Date: May 2006
Posts: 156
waltini is on a distinguished road
Will this help?

Have a look at this ea, which appears to do what you want.
Attached Files
File Type: mq4 MultiPositionExpert.mq4 (6.2 KB, 38 views)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!StumbleUpon this Post!Reddit this Post!Facebook this Post!BlinkList this Post!Google Bookmarks this Post!Yahoo! My Web this Post!
Reply With Quote
  #9 (permalink)  
Old 03-30-2008, 06:47 PM
MiniMe's Avatar
Senior Member
 
Join Date: Nov 2006
Location: Montréal
Posts: 1,451
MiniMe is an unknown quantity at this point
Thumbs up

Quote:
Originally Posted by waltini View Post
Have a look at this ea, which appears to do what you want.
yes I think I can use part of the code it seems to do what I want, now I had made 3 experts , each one based on a reply I got , lucky me.

I will test them this week , I thank you Mistigri , mikkom and waltini your help is really appricated
__________________


Last edited by MiniMe; 03-30-2008 at 06:49 PM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!StumbleUpon this Post!Reddit this Post!Facebook this Post!BlinkList this Post!Google Bookmarks this Post!Yahoo! My Web this Post!
Reply With Quote
  #10 (permalink)  
Old 03-30-2008, 09:50 PM
wolfe's Avatar
Senior Member
 
Join Date: Jan 2006
Posts: 820
wolfe is on a distinguished road
Minime,

I created the following piece of code to get the total profit of all orders with the same magic number:

PHP Code:
double OPBM(int intMagic)//OrderProfitByMagic
{
double dblProfit=0;
int intPOS=0;
bool boolTerm=false;
while(
boolTerm==false)
{
if(
OrderSelect(intPOS,SELECT_BY_POS))
{
if(
OrderMagicNumber()==intMagicdblProfit=dblProfit+OrderProfit();
intPOS++;
}
else
boolTerm=true;
}
return(
dblProfit);

All you have to do is copy the above code and paste it in your EA, I usually put it at the bottom, after the
PHP Code:
return(0);
  } 
You will also want to use a Close By Magic function:
PHP Code:
int CBM(int intMagic)//CloseByMagic
{
int intOffset=0;
int Count OTBM(intMagic);

while(
OTBM(intMagic)>&& Count 0)
{
OrderSelect(intOffset,SELECT_BY_POS);
if(
OrderMagicNumber()==intMagic)
{
if(
OrderType()==OP_BUYOrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_BID),999,Red);
else if(
OrderType()==OP_SELLOrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_ASK),999,Orange);
Count--;
}
else {
intOffset++;
}
}
return(
0);

Also include this Orders Total By Magic Function:
PHP Code:
int OTBM(int intMagic)//OrdersTotalByMagic
{
int intCount=0;
int intPOS=0;
bool boolTerm=false;
while(
boolTerm==false)
{
if(
OrderSelect(intPOS,SELECT_BY_POS))
{
if(
OrderMagicNumber()==intMagicintCount++;
intPOS++;
}
else
boolTerm=true;
}
return(
intCount);

Then all you need to code is:

PHP Code:
if (OPBM(Your_Magic_Number) >= Your_Profit_Target)
 {
   
CBM(Your_Magic_Number);
 } 
Hope this helps.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!StumbleUpon this Post!Reddit this Post!Facebook this Post!BlinkList this Post!Google Bookmarks this Post!Yahoo! My Web this Post!
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
magic number forextrend Metatrader 4 42 06-12-2009 10:25 AM
Magic Number question Yoda_Glenn Expert Advisors - Metatrader 4 1 02-22-2007 09:26 AM
MT4 Magic Number DeadEye96 Expert Advisors - Metatrader 4 2 09-19-2006 11:05 PM
magic number caldolegare Metatrader 4 4 03-19-2006 06:28 AM


All times are GMT. The time now is 03:27 AM.



Search Engine Friendly URLs by vBSEO 3.2.0 ©2008, Crawlability, Inc.