Skip This Ad >>
 
Register
Forgotten your password?
The Facebook Platform
Forex-TSD - Powered by vBulletin
Loading search...
Advanced search
  • Home
  • Forums
    Free sections Exclusive sections All sections
  • Register
    Create new account
  • More Recent
    Latest Threads Today's Posts
  • Rankings
    Most popular threads this month Most popular threads created this month Most linked threads this month Most active users Most reputed users
  • Calendar
  • Advertising
  • Others
    Blogs Toolbar
  • Help
    Contact Us FAQ
  • Premium Forum
  • Home
  • Forum
  • Programming
  • MetaTrader Programming
  • Close orders with magic number at certain profit

Thread: Close orders with magic number at certain profit



  • LinkBack
    • LinkBack URL LinkBack URL
    • About LinkBacks About LinkBacks
    •  
    • Bookmark & Share
    • Digg this Thread!
    • Add Thread to del.icio.us
    • Bookmark in Technorati
    • Tweet this thread
  • Thread Tools
    • Show Printable Version
    • Email this Page…
    • Subscribe to this Thread…
  • Display
    • Switch to Hybrid Mode
    • Switch to Threaded Mode
Results 1 to 10 of 12
Page 1 of 2 12 Next LastLast
1 1 Attachment(s)

  1. 03-29-2008 02:18 PM #1
    MiniMe's Avatar
    MiniMe
    MiniMe is offline Senior Member
    Points: 14,197, Level: 15
    Level completed: 43%, Points required for next Level: 803
    Overall activity: 0%
    Join Date
    Nov 2006
    Location
    Montréal
    Posts
    1,457
    Points
    14,197
    Level
    15
    Achievements:
    Three FriendsRecommendation First ClassVeteran500 Experience Points

    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(cnt, SELECT_BY_POS, MODE_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>0 && 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 = 0 ; 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
    • Share
      • Share this post on
      • Digg
      • Del.icio.us
      • Technorati
      • Twitter

    Reply With Quote Reply With Quote

  2. 03-29-2008 02:48 PM #2
    Mistigri
    Mistigri is offline Member
    Points: 5,471, Level: 8
    Level completed: 7%, Points required for next Level: 1,029
    Overall activity: 0%
    Join Date
    Mar 2006
    Posts
    33
    Points
    5,471
    Level
    8
    Achievements:
    Veteran500 Experience Points
    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(cnt, SELECT_BY_POS, MODE_TRADES);
             if( 
    OrderMagicNumber()==111 )
                {
                   if(
    OrderType()==OP_BUY)
                      {
                         
    CurrentProfitB+= MarketInfo( OrderSymbol(), MODE_BID)-OrderOpenPrice() ;
                      }
    //if(OrderType()==OP_BUY)
                   
    if(OrderType()==OP_SELL)
                      {
                         
    CurrentProfitS+=OrderOpenPrice()- MarketInfo( OrderSymbol(), 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 = 0 ; 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(),MarketInfo( OrderSymbol(), MODE_BID),slippage,White);
                if(
    OrderType()==OP_SELL)   
                 
    OrderClose(OrderTicket(),OrderLots(),MarketInfo( OrderSymbol(), MODE_ASK),slippage,White);
          } 
    } 
    You may want to check your log for more errors but that should do it.

    Patrick
    • Share
      • Share this post on
      • Digg
      • Del.icio.us
      • Technorati
      • Twitter
    Reply With Quote Reply With Quote

  3. 03-29-2008 02:56 PM #3
    mikkom's Avatar
    mikkom
    mikkom is offline Senior Member
    Points: 92,631, Level: 44
    Level completed: 28%, Points required for next Level: 2,669
    Overall activity: 0%
    Join Date
    Oct 2007
    Posts
    295
    Points
    92,631
    Level
    44
    Achievements:
    Veteran500 Experience Points
    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 02:58 PM.
    • Share
      • Share this post on
      • Digg
      • Del.icio.us
      • Technorati
      • Twitter
    Reply With Quote Reply With Quote

  4. 03-29-2008 02:57 PM #4
    MiniMe's Avatar
    MiniMe
    MiniMe is offline Senior Member
    Points: 14,197, Level: 15
    Level completed: 43%, Points required for next Level: 803
    Overall activity: 0%
    Join Date
    Nov 2006
    Location
    Montréal
    Posts
    1,457
    Points
    14,197
    Level
    15
    Achievements:
    Three FriendsRecommendation First ClassVeteran500 Experience Points
    Thanks a lot Patrick, I think you are right. I will change the code and try it
    • Share
      • Share this post on
      • Digg
      • Del.icio.us
      • Technorati
      • Twitter

    Reply With Quote Reply With Quote

  5. 03-29-2008 03:02 PM #5
    MiniMe's Avatar
    MiniMe
    MiniMe is offline Senior Member
    Points: 14,197, Level: 15
    Level completed: 43%, Points required for next Level: 803
    Overall activity: 0%
    Join Date
    Nov 2006
    Location
    Montréal
    Posts
    1,457
    Points
    14,197
    Level
    15
    Achievements:
    Three FriendsRecommendation First ClassVeteran500 Experience Points
    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
    • Share
      • Share this post on
      • Digg
      • Del.icio.us
      • Technorati
      • Twitter

    Reply With Quote Reply With Quote

  6. 03-29-2008 03:09 PM #6
    mikkom's Avatar
    mikkom
    mikkom is offline Senior Member
    Points: 92,631, Level: 44
    Level completed: 28%, Points required for next Level: 2,669
    Overall activity: 0%
    Join Date
    Oct 2007
    Posts
    295
    Points
    92,631
    Level
    44
    Achievements:
    Veteran500 Experience Points
    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
    • Share
      • Share this post on
      • Digg
      • Del.icio.us
      • Technorati
      • Twitter
    Reply With Quote Reply With Quote

  7. 03-29-2008 03:54 PM #7
    MiniMe's Avatar
    MiniMe
    MiniMe is offline Senior Member
    Points: 14,197, Level: 15
    Level completed: 43%, Points required for next Level: 803
    Overall activity: 0%
    Join Date
    Nov 2006
    Location
    Montréal
    Posts
    1,457
    Points
    14,197
    Level
    15
    Achievements:
    Three FriendsRecommendation First ClassVeteran500 Experience Points
    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
    • Share
      • Share this post on
      • Digg
      • Del.icio.us
      • Technorati
      • Twitter

    Reply With Quote Reply With Quote

  8. 03-30-2008 10:23 AM #8
    waltini
    waltini is offline Senior Member
    Points: 28,671, Level: 24
    Level completed: 87%, Points required for next Level: 229
    Overall activity: 0%
    Join Date
    May 2006
    Posts
    161
    Points
    28,671
    Level
    24
    Achievements:
    Veteran500 Experience Points

    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, 111 views)
    • Share
      • Share this post on
      • Digg
      • Del.icio.us
      • Technorati
      • Twitter
    Reply With Quote Reply With Quote

  9. 03-30-2008 05:47 PM #9
    MiniMe's Avatar
    MiniMe
    MiniMe is offline Senior Member
    Points: 14,197, Level: 15
    Level completed: 43%, Points required for next Level: 803
    Overall activity: 0%
    Join Date
    Nov 2006
    Location
    Montréal
    Posts
    1,457
    Points
    14,197
    Level
    15
    Achievements:
    Three FriendsRecommendation First ClassVeteran500 Experience Points

    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.
    • Share
      • Share this post on
      • Digg
      • Del.icio.us
      • Technorati
      • Twitter

    Reply With Quote Reply With Quote

  10. 03-30-2008 08:50 PM #10
    wolfe's Avatar
    wolfe
    wolfe is offline Senior Member
    Points: 74,911, Level: 40
    Level completed: 15%, Points required for next Level: 3,589
    Overall activity: 0%
    Join Date
    Jan 2006
    Posts
    845
    Points
    74,911
    Level
    40
    Achievements:
    Veteran500 Experience Points
    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()==intMagic) dblProfit=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)>0 && Count > 0)
    {
    OrderSelect(intOffset,SELECT_BY_POS);
    if(
    OrderMagicNumber()==intMagic)
    {
    if(
    OrderType()==OP_BUY) OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_BID),999,Red);
    else if(
    OrderType()==OP_SELL) OrderClose(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()==intMagic) intCount++;
    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.
    • Share
      • Share this post on
      • Digg
      • Del.icio.us
      • Technorati
      • Twitter
    Reply With Quote Reply With Quote

Page 1 of 2 12 Next LastLast
1 1 Attachment(s)

Quick Navigation MetaTrader Programming Top
  • Site Areas
  • Settings
  • Private Messages
  • Subscriptions
  • Who's Online
  • Search Forums
  • Forums Home
  • Forums
  • Announcements
    1. Announcements Blog
  • Exclusive Forum
    1. Elite Section
  • Forex Press
    1. Analytics
    2. Articles
    3. Success Stories
  • Discussion Areas
    1. General Discussion
    2. Metatrader 4
      1. Metatrader 3
    3. Metatrader 5
    4. Post and compare Trades
    5. Setup Questions
    6. Suggestions for Trading Systems
    7. Documentation
    8. Strategy Trader
  • Non Related Discussions
  • Downloads
    1. Indicators - Metatrader 4
      1. Indicators - Metatrader 3
      2. Indicators - Metatrader 5
    2. Manual trading systems
    3. Expert Advisors - Metatrader 4
      1. TSD Expert Advisors
      2. Expert Advisors - Metatrader 5
      3. Expert Advisors - Metatrader 3
    4. Tools and utilities
    5. Tools and utilities - Strategy Trader
  • Trading systems
    1. CatFx50
    2. Brain Systems
    3. Ema Cross
    4. Harmonic trading
    5. The "XO"-Method
    6. Phoenix
    7. Dolly
    8. Digital Filters
    9. Martingale/Average Cost and Hedging
  • Automated Trading
    1. News/Signal Trading
    2. FX Signals Marketplaces
  • Commercial systems
    1. Commercial Trading Systems and indicators
    2. Raw Commercial Ideas
    3. Evaluate Commercial Ideas
  • Brokers
    1. Metatrader brokers
  • Programming
    1. MetaTrader Programming
    2. TradeStation Programming
  • Training
    1. Metatrader
      1. Metatrader 4 mql 4 - Development course
        1. Questions
        2. Lessons
    2. TradeStation

» Popular Tags

analysis backtest best broker breakout broker charts close all orders script copier data window day trading decompile ex4 ea ma EUR/USD eurusd exit strategies expert expert adviser expert advisor financial

» Rankings

Hottest Threads

1  Eurusd/gbpusd
2  CatFx50
3  10points 3.mq4
4  FxOpen
5  Multi Timeframe Indicators

view more
 

Most Popular Threads

1  Multi Timeframe In...
2  CatFx50
3  Something interesting pl...
4  The Murrey Math Trading System
5  Show Us Your Best

view more

» FEATURED BROKERS



« Previous Thread | Next Thread »

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. magic number
    By forextrend in forum Metatrader 4
    Replies: 42
    Last Post: 06-12-2009, 09:25 AM
  2. Magic Number question
    By Yoda_Glenn in forum Expert Advisors - Metatrader 4
    Replies: 1
    Last Post: 02-22-2007, 08:26 AM
  3. MT4 Magic Number
    By DeadEye96 in forum Expert Advisors - Metatrader 4
    Replies: 2
    Last Post: 09-19-2006, 10:05 PM
  4. magic number
    By caldolegare in forum Metatrader 4
    Replies: 4
    Last Post: 03-19-2006, 05:28 AM

Bookmarks

Bookmarks
  • Submit to Digg Digg
  • Submit to del.icio.us del.icio.us
  • Submit to Technorati Technorati
  • Submit to Furl Furl
  • Submit to StumbleUpon StumbleUpon
  • Submit to Reddit Reddit
  • Submit to Facebook Facebook
  • Submit to Blink List Blink List
  • Submit to Google Google
  • Submit to Yahoo My Web Yahoo My Web
  • Submit to Twitter Twitter

Posting Permissions

  • 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
  • [VIDEO] code is On
  • HTML code is Off
  • Trackbacks are On
  • Pingbacks are On
  • Refbacks are On

Forum Rules

Forums
  • Free sections
  • Exclusive sections
  • All sections
Calendar Social Networking
Premium Forum
  • Elite Membership
  • Advanced Membership
All times are GMT. The time now is 08:20 PM.
Terms and Conditions
PRIVACY POLICY
Powered by vBulletin® Version 4.1.11
Copyright © 2012 vBulletin Solutions, Inc. All rights reserved.
Search Engine Friendly URLs by vBSEO 3.6.0 ©2011, Crawlability, Inc.
Digital Point modules: Sphinx-based search

Subscribe today and get...

Elite Membership
  • Elite Trading Systems
  • Propietary Indicators
  • Private Performance Reports
  • Elite EAs Source Code
  • 24/5 Trading Support!
Advanced Membership
  • Elite benefits, plus:
  • - MT4 & MT5 cutting-edge indicators.
  • - Advanced propieatry indicators and its source codes.
  • - Free conversion of MT4 indicatiors to MT5.
View our best offer!