Forex
Google
New signals service!

Go Back   Forex Trading > Programming > Metatrader Programming


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 (2) Thread Tools Display Modes
  2 links from elsewhere to this Post. Click to view. #1 (permalink)  
Old 02-22-2008, 07:29 PM
matrixebiz's Avatar
Senior Member
 
Join Date: Oct 2006
Posts: 1,159
matrixebiz is on a distinguished road
Little question about EA's

Hello All, I have this simple EA that just trades on the VQ indicator and exits with an Exit indicator which mostly works but......

Two questions;
1) The EA only trades/opens ONE order at a time for the currency it's on until the order is closed. What do I need to change so that the EA will open another trade if VQ signals another trade even if a trade on the same currency is already open? So to allow two, three, ... same currency trades open at the same time.

2) The EA doesn't seem to be checking the MagicNumber before closing a trade. It will close the trade according to the correct currency but if I'm running the EA on two charts using the same currency and different settings and MagicNumbers the EA will close a trade that was opened by the other chart also from the same currency even though it does have a different MagicNumber. How do I make the EA also obey the MagicNumber when going to close a trade if it's running on two charts of the same currency?

Thank you

Code:
//+------------------------------------------------------------------+
//|                                                   #VQExit_EA.mq4 |
//+------------------------------------------------------------------+

#define SIGNAL_NONE 0
#define SIGNAL_BUY   1
#define SIGNAL_SELL  2
#define SIGNAL_CLOSEBUY 3
#define SIGNAL_CLOSESELL 4

extern int MagicNumber = 121212;
extern bool SignalMail = False;
extern bool EachTickMode = False;
extern double Lots = 0.1;
extern int Slippage = 3;
extern bool UseStopLoss = True;
extern int StopLoss = 40;
extern bool UseTakeProfit = True;
extern int TakeProfit = 40;
extern bool UseTrailingStop = False;
extern int TrailingStop = 25;

int BarCount;
int Current;
bool TickCheck = False;
//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init() {
   BarCount = Bars;

   if (EachTickMode) Current = 0; else Current = 1;

   return(0);
}
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit() {
   return(0);
}
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start() {
   int Order = SIGNAL_NONE;
   int Total, Ticket;
   double StopLossLevel, TakeProfitLevel;



   if (EachTickMode && Bars != BarCount) TickCheck = False;
   Total = OrdersTotal();
   Order = SIGNAL_NONE;

   //+------------------------------------------------------------------+
   //| Variable Begin                                                   |
   //+------------------------------------------------------------------+


double Entry1 = iCustom(NULL, 0, "VQ", false,0,5,3,1,5,true,false,true,true,false,true,1485, 0, 1);
double Entry2 = iCustom(NULL, 0, "VQ", false,0,5,3,1,5,true,false,true,true,false,true,1485, 0, 2);
double Up2 = iCustom(NULL, 0, "VQ", false,0,5,3,1,5,true,false,true,true,false,true,1485, 1, 2);
double Down2 = iCustom(NULL, 0, "VQ", false,0,5,3,1,5,true,false,true,true,false,true,1485, 2, 2);

double Entry3 = iCustom(NULL, 0, "Exit", 5,3,5, 0, 1);
double Entry4 = iCustom(NULL, 0, "Exit", 5,3,5, 0, 2);


   
   //+------------------------------------------------------------------+
   //| Variable End                                                     |
   //+------------------------------------------------------------------+

   //Check position
   bool IsTrade = False;

   for (int i = 0; i < Total; i ++) {
      OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
      if(OrderType() <= OP_SELL &&  OrderSymbol() == Symbol()) {
         IsTrade = True;
         if(OrderType() == OP_BUY) {
            //Close

            //+------------------------------------------------------------------+
            //| Signal Exit Buy                                                  |
            //+------------------------------------------------------------------+

            if (Entry3 > Entry4 && ((EachTickMode && !TickCheck) || (!EachTickMode && (Bars != BarCount)))) {
               OrderClose(OrderTicket(), OrderLots(), Bid, Slippage, MediumSeaGreen);
               if (SignalMail) SendMail("[Signal Alert]", "[" + Symbol() + "] " + DoubleToStr(Bid, Digits) + " Close Buy");
               if (!EachTickMode) BarCount = Bars;
               IsTrade = False;
               continue;
            }
            //Trailing stop
            if(UseTrailingStop && TrailingStop > 0) {                 
               if(Bid - OrderOpenPrice() > Point * TrailingStop) {
                  if(OrderStopLoss() < Bid - Point * TrailingStop) {
                     OrderModify(OrderTicket(), OrderOpenPrice(), Bid - Point * TrailingStop, OrderTakeProfit(), 0, MediumSeaGreen);
                     if (!EachTickMode) BarCount = Bars;
                     continue;
                  }
               }
            }
         } else {
            //Close

            //+------------------------------------------------------------------+
            //| Signal Exit Sell                                                 |
            //+------------------------------------------------------------------+

            if (Entry3 < Entry4 && ((EachTickMode && !TickCheck) || (!EachTickMode && (Bars != BarCount)))) {
               OrderClose(OrderTicket(), OrderLots(), Ask, Slippage, DarkOrange);
               if (SignalMail) SendMail("[Signal Alert]", "[" + Symbol() + "] " + DoubleToStr(Ask, Digits) + " Close Sell");
               if (!EachTickMode) BarCount = Bars;
               IsTrade = False;
               continue;
            }
            //Trailing stop
            if(UseTrailingStop && TrailingStop > 0) {                 
               if((OrderOpenPrice() - Ask) > (Point * TrailingStop)) {
                  if((OrderStopLoss() > (Ask + Point * TrailingStop)) || (OrderStopLoss() == 0)) {
                     OrderModify(OrderTicket(), OrderOpenPrice(), Ask + Point * TrailingStop, OrderTakeProfit(), 0, DarkOrange);
                     if (!EachTickMode) BarCount = Bars;
                     continue;
                  }
               }
            }
         }
      }
   }

   //+------------------------------------------------------------------+
   //| Signal Entry                                                     |
   //+------------------------------------------------------------------+

   //Buy
   if (Up2==Down2 && Entry1>Entry2 && ((EachTickMode && !TickCheck) || (!EachTickMode && (Bars != BarCount)))) {
      if(!IsTrade) {
         //Check free margin
         if (AccountFreeMargin() < (1000 * Lots)) {
            Print("We have no money. Free Margin = ", AccountFreeMargin());
            return(0);
         }

         if (UseStopLoss) StopLossLevel = Ask - StopLoss * Point; else StopLossLevel = 0.0;
         if (UseTakeProfit) TakeProfitLevel = Ask + TakeProfit * Point; else TakeProfitLevel = 0.0;

         Ticket = OrderSend(Symbol(), OP_BUY, Lots, Ask, Slippage, StopLossLevel, TakeProfitLevel, "Buy(#" + MagicNumber + ")", MagicNumber, 0, DodgerBlue);
         if(Ticket > 0) {
            if (OrderSelect(Ticket, SELECT_BY_TICKET, MODE_TRADES)) {
				Print("BUY order opened : ", OrderOpenPrice());
                if (SignalMail) SendMail("[Signal Alert]", "[" + Symbol() + "] " + DoubleToStr(Ask, Digits) + " Open Buy");
			} else {
				Print("Error opening BUY order : ", GetLastError());
			}
         }
         if (EachTickMode) TickCheck = True;
         if (!EachTickMode) BarCount = Bars;
         return(0);
      }
   }

   //Sell
   if (Up2==Down2 && Entry1<Entry2 && ((EachTickMode && !TickCheck) || (!EachTickMode && (Bars != BarCount)))) {
      if(!IsTrade) {
         //Check free margin
         if (AccountFreeMargin() < (1000 * Lots)) {
            Print("We have no money. Free Margin = ", AccountFreeMargin());
            return(0);
         }

         if (UseStopLoss) StopLossLevel = Bid + StopLoss * Point; else StopLossLevel = 0.0;
         if (UseTakeProfit) TakeProfitLevel = Bid - TakeProfit * Point; else TakeProfitLevel = 0.0;

         Ticket = OrderSend(Symbol(), OP_SELL, Lots, Bid, Slippage, StopLossLevel, TakeProfitLevel, "Sell(#" + MagicNumber + ")", MagicNumber, 0, DeepPink);
         if(Ticket > 0) {
            if (OrderSelect(Ticket, SELECT_BY_TICKET, MODE_TRADES)) {
				Print("SELL order opened : ", OrderOpenPrice());
                if (SignalMail) SendMail("[Signal Alert]", "[" + Symbol() + "] " + DoubleToStr(Bid, Digits) + " Open Sell");
			} else {
				Print("Error opening SELL order : ", GetLastError());
			}
         }
         if (EachTickMode) TickCheck = True;
         if (!EachTickMode) BarCount = Bars;
         return(0);
      }
   }

   if (!EachTickMode) BarCount = Bars;

   return(0);
}
//+------------------------------------------------------------------+
Attached Files
File Type: mq4 #VQExit_EA.mq4 (8.1 KB, 22 views)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 02-22-2008, 08:42 PM
Junior Member
 
Join Date: Jan 2008
Posts: 24
SimonF is on a distinguished road
Do you know any code at all? I don't know how much I need to tell you to make you understand.

PHP Code:
      if(!IsTrade) { 
is on some lines, this makes so it doesn't open more trades if one is open alreadty.

The line that checks open orders if they are correct currency:
PHP Code:
      if(OrderType() <= OP_SELL &&  OrderSymbol() == Symbol()) { 
you need to add
PHP Code:
 && OrderMagicNumber()==MagicNumber 
__________________
boo
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 02-22-2008, 09:17 PM
matrixebiz's Avatar
Senior Member
 
Join Date: Oct 2006
Posts: 1,159
matrixebiz is on a distinguished road
Ok, thank you for responding. Don't really code at all. Just learning some basic stuff. So to kill two birds with one stone, could I do?;

Code:
//Check position
   bool IsTrade = False;

   for (int i = 0; i < Total; i ++) {
      OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
      if(OrderType() <= OP_SELL && OrderMagicNumber()==MagicNumber &&  OrderSymbol() == Symbol()) {
         // IsTrade = True;
         IsTrade = False;
         if(OrderType() == OP_BUY) {
            //Close
or just delete this line if(!IsTrade) { from the Buy and Sell entry commands
but still add && OrderMagicNumber()==MagicNumber


EDIT: if I remove the if(!IsTrade) { and the EA trades more times that will screw up my close command because I wont have away of tracking what trade I want to close If I have two E/J trades open how can I decipher between the two ?? Ok, maybe I just leave that part alone and have one order at a time

Last edited by matrixebiz; 02-22-2008 at 09:30 PM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply

Bookmarks

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

LinkBacks (?)
LinkBack to this Thread: http://www.forex-tsd.com/metatrader-programming/12486-little-question-about-eas.html
Posted By For Type Date
Little question about EA's - Forex Trading This thread Refback 03-01-2008 07:43 AM
Expert Advisor | Forex MetaTrader Expert Advisors | Over 40 of the Best EA's for MT4 on Squidoo This thread Refback 02-24-2008 04:58 AM

Similar Threads
Thread Thread Starter Forum Replies Last Post
EURGBP - EA's Superion Expert Advisors - Metatrader 4 0 09-25-2007 07:30 AM
Help EA's not loading jatotek Metatrader 4 0 03-31-2007 12:32 AM
How do you trade with EA's? cubesteak General Discussion 2 07-12-2006 06:21 PM
Help! Where/how do I download the EA's carlfritch Metatrader 4 4 06-15-2006 03:22 PM
MT3 EA's forexpipmaster Metatrader 3 1 11-23-2005 07:15 AM


All times are GMT. The time now is 10:52 AM.



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