Forex
Google

Go Back   Forex Trading > Programming > Metatrader Programming
Forex Forum Register FAQ Members List Calendar Today's Posts


Register in Forex TSD!
Trading Systems Leaders in this forum (automated trading systems) are winning more than 3000 pips in a month (30000$ investing one lot every time).
Click here to register and get more information

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 03-29-2008, 01:18 PM
MiniMe's Avatar
MiniMe MiniMe is offline
Senior Member
 
Join Date: Nov 2006
Location: Montréal
Posts: 1,025
MiniMe is on a distinguished road
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!
Reply With Quote
  #2 (permalink)  
Old 03-29-2008, 01:48 PM
Mistigri's Avatar
Mistigri Mistigri is offline
Junior Member
 
Join Date: Mar 2006
Location: SLC
Posts: 22
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!
Reply With Quote
  #3 (permalink)  
Old 03-29-2008, 01:56 PM
mikkom mikkom is offline
Senior Member
 
Join Date: Oct 2007
Posts: 165
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 01:58 PM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 03-29-2008, 01:57 PM
MiniMe's Avatar
MiniMe MiniMe is offline
Senior Member
 
Join Date: Nov 2006
Location: Montréal
Posts: 1,025
MiniMe is on a distinguished road
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!
Reply With Quote
  #5 (permalink)  
Old 03-29-2008, 02:02 PM
MiniMe's Avatar
MiniMe MiniMe is offline
Senior Member
 
Join Date: Nov 2006
Location: Montréal
Posts: 1,025
MiniMe is on a distinguished road
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!
Reply With Quote
  #6 (permalink)  
Old 03-29-2008, 02:09 PM
mikkom mikkom is offline
Senior Member
 
Join Date: Oct 2007
Posts: 165
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!
Reply With Quote
  #7 (permalink)  
Old 03-29-2008, 02:54 PM
MiniMe's Avatar
MiniMe MiniMe is offline
Senior Member
 
Join Date: Nov 2006
Location: Montréal
Posts: 1,025
MiniMe is on a distinguished road
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!
Reply With Quote
  #8 (permalink)  
Old 03-30-2008, 10:23 AM
waltini waltini is offline
Senior Member
 
Join Date: May 2006
Posts: 130
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, 21 views)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #9 (permalink)  
Old 03-30-2008, 05:47 PM
MiniMe's Avatar
MiniMe MiniMe is offline
Senior Member
 
Join Date: Nov 2006
Location: Montréal
Posts: 1,025
MiniMe is on a distinguished road
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 05:49 PM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #10 (permalink)  
Old 03-30-2008, 08:50 PM
wolfe's Avatar
wolfe wolfe is offline
Senior Member
 
Join Date: Jan 2006
Posts: 673
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!
Reply With Quote
Reply



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

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


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


All times are GMT. The time now is 03:43 PM.