EA to allow opening of opposite trade type

 

Hey guys,

My EA basically states, if Total Open Orders <= 1, buy/sell conditions. What I want now is for the EA to see that if it creates a buy order, it can still allow a sell order to be made but NOT another buy order. My EA states that up to 2 orders can be open at one time, but I want it to only allow the second order to be of the opposite type of what's currently open.

Adding this is going to assist with my drawdown issue, I'm just not sure how to code it up. I'll attach my EA. Not looking for criticisms about my decision to use ForexEAdvisor, just looking to add the above part. Thanks a lot guys. 

//+------------------------------------------------------------------+
//                        DO NOT DELETE THIS HEADER
//             DELETING THIS HEADER IS COPYRIGHT INFRIGMENT 
//
//                   Copyright ©2011, ForexEAdvisor.com
//                 ForexEAdvisor Strategy Builder version 0.2
//                        http://www.ForexEAdvisor.com 
//
// THIS EA CODE HAS BEEN GENERATED USING FOREXEADVISOR STRATEGY BUILDER 0.2 
// on: 2/18/2013 6:39:22 AM
// Disclaimer: This EA is provided to you "AS-IS", and ForexEAdvisor disclaims any warranty
// or liability obligations to you of any kind. 
// UNDER NO CIRCUMSTANCES WILL FOREXEADVISOR BE LIABLE TO YOU, OR ANY OTHER PERSON OR ENTITY,
// FOR ANY LOSS OF USE, REVENUE OR PROFIT, LOST OR DAMAGED DATA, OR OTHER COMMERCIAL OR
// ECONOMIC LOSS OR FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, STATUTORY, PUNITIVE,
// EXEMPLARY OR CONSEQUENTIAL DAMAGES WHATSOEVER RELATED TO YOUR USE OF THIS EA OR 
// FOREXEADVISOR STRATEGY BUILDER     
// Because software is inherently complex and may not be completely free of errors, you are 
// advised to verify this EA. Before using this EA, please read the ForexEAdvisor Strategy Builder
// license for a complete understanding of ForexEAdvisor' disclaimers.  
// USE THIS EA AT YOUR OWN RISK. 
//  
// Before adding this expert advisor to a chart, make sure there are NO
// open positions.
//                      DO NOT DELETE THIS HEADER
//             DELETING THIS HEADER IS COPYRIGHT INFRIGMENT 
//+------------------------------------------------------------------+


extern int MagicNumber=17801;
extern double Lots =0.1;
extern double StopLoss=20;
extern double TakeProfit=1;
extern int TrailingStop=15;
extern int Slippage=3;

//+------------------------------------------------------------------+
//    expert start function
//+------------------------------------------------------------------+
int BarsCount = 0;

int start()
{
if (Bars > BarsCount)
  {

  double MyPoint=Point;
  if(Digits==3 || Digits==5) MyPoint=Point*10;
  
  double TheStopLoss=0;
  double TheTakeProfit=0;
  if( TotalOrdersCount()<=0) 
  {
     int result=0;
     if((Low[3]>Low[2])&&(Low[2]<Low[1])&&(iMA(NULL,0,10,0,MODE_EMA,PRICE_CLOSE,1)>iMA(NULL,0,10,0,MODE_EMA,PRICE_CLOSE,2))) // Here is your open buy rule
     {
        result=OrderSend(Symbol(),OP_BUY,Lots,Ask,Slippage,0,0,"EA Generator www.ForexEAdvisor.com",MagicNumber,0,Blue);
        SendNotification("Opened BUY - SWINGER");

        if(result>0)
        {
         TheStopLoss=0;
         TheTakeProfit=0;
         if(TakeProfit>0) TheTakeProfit=Ask+TakeProfit*MyPoint;
         if(StopLoss>0) TheStopLoss=Ask-StopLoss*MyPoint;
         OrderSelect(result,SELECT_BY_TICKET);
         OrderModify(OrderTicket(),OrderOpenPrice(),NormalizeDouble(TheStopLoss,Digits),NormalizeDouble(TheTakeProfit,Digits),0,Green);
        
        }
        BarsCount = Bars;
  
        return(0);
        
     }
     if((High[3]<High[2])&&(High[2]>High[1])&&(iMA(NULL,0,10,0,MODE_EMA,PRICE_CLOSE,1)<iMA(NULL,0,10,0,MODE_EMA,PRICE_CLOSE,2))) // Here is your open Sell rule
     {
        result=OrderSend(Symbol(),OP_SELL,Lots,Bid,Slippage,0,0,"EA Generator www.ForexEAdvisor.com",MagicNumber,0,Red);
        SendNotification("Opened SELL - SWINGER");
        if(result>0)
        {
         TheStopLoss=0;
         TheTakeProfit=0;
         if(TakeProfit>0) TheTakeProfit=Bid-TakeProfit*MyPoint;
         if(StopLoss>0) TheStopLoss=Bid+StopLoss*MyPoint;
         OrderSelect(result,SELECT_BY_TICKET);
         OrderModify(OrderTicket(),OrderOpenPrice(),NormalizeDouble(TheStopLoss,Digits),NormalizeDouble(TheTakeProfit,Digits),0,Green);
        }
        BarsCount = Bars;
  }

        return(0);
     }


  }
 
  for(int cnt=0;cnt<OrdersTotal();cnt++)
     {
      OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
      if(OrderType()<=OP_SELL &&   
         OrderSymbol()==Symbol() &&
         OrderMagicNumber()==MagicNumber 
         )  
        {
         if(OrderType()==OP_BUY)  
           {
            if(TrailingStop>0)  
              {                 
               if(Bid-OrderOpenPrice()>MyPoint*TrailingStop)
                 {
                  if(OrderStopLoss()<Bid-MyPoint*TrailingStop)
                    {
                     OrderModify(OrderTicket(),OrderOpenPrice(),Bid-TrailingStop*MyPoint,OrderTakeProfit(),0,Green);
                     return(0);
                    }
                 }
              }
           }
         else 
           {
            if(TrailingStop>0)  
              {                 
               if((OrderOpenPrice()-Ask)>(MyPoint*TrailingStop))
                 {
                  if((OrderStopLoss()>(Ask+MyPoint*TrailingStop)) || (OrderStopLoss()==0))
                    {
                     OrderModify(OrderTicket(),OrderOpenPrice(),Ask+MyPoint*TrailingStop,OrderTakeProfit(),0,Red);
                     return(0);
                    }
                 }
              }
           }
        }
     }
         if(OrderProfit()>=TakeProfit)
    {
    OrderClose(OrderTicket(),Lots,Ask,Slippage);
    }
   return(0);
   
}

int TotalOrdersCount()
{
  int result=0;
  for(int i=0;i<OrdersTotal();i++)
  {
     OrderSelect(i,SELECT_BY_POS ,MODE_TRADES);
     if (OrderMagicNumber()==MagicNumber) result++;

   }
  return (result);
}
 
mattmoneywilson:
My EA basically states, if Total Open Orders <= 1, buy/sell conditions. What I want now is for the EA to see that if it creates a buy order, it can still allow a sell order to be made but NOT another buy order. My EA states that up to 2 orders can be open at one time, but I want it to only allow the second order to be of the opposite type of what's currently open.

Adding this is going to assist with my drawdown issue, I'm just not sure how to code it up.
  1. if( TotalOrdersCount()<=0)
    Your posted code says zero not one
  2. EA builder has been discussed before. It generates terrible code. Do a search.
  3. Like: Not checking return codes
  4. Like: Not adjusting for 4/5 digit brokers (slippage)
  5. Like: using Bars (max bars on chart) instead of time, and using ">" instead of "!=".
  6. Of course you don't know how to code it, because you used EAB. Why aren't you asking on their forum?
  7. Since there are no slaves here, you have only three choices: Search for it, learn to code it, or pay someone. We're not going to code it FOR you. We are willing to HELP you when you post your attempt and the nature of your problem.
  8. Hedging is not allowed in the US
  9. Hedging is not going to help with your drawdown issue, it will double it, imo.
  10. Instead of counting the total open orders, count each type.
 
mattmoneywilson:

Hey guys,

My EA basically states, if Total Open Orders <= 1, buy/sell conditions. What I want now is for the EA to see that if it creates a buy order, it can still allow a sell order to be made but NOT another buy order. My EA states that up to 2 orders can be open at one time, but I want it to only allow the second order to be of the opposite type of what's currently open.

Adding this is going to assist with my drawdown issue, I'm just not sure how to code it up. I'll attach my EA. Not looking for criticisms about my decision to use ForexEAdvisor, just looking to add the above part. Thanks a lot guys. 

If you don't want to learn to code then you are going to have problems adding code to this EA.  If you want to learn to code then learn to code :  Book  Documentation    if you want someone else to write you code for you then go here:  Code my EA for me - MT4
 

LOL yikes.

Let me ask it another way. What would the logic look like if I wanted to achieve my above inquiry? I can do the coding/research myself once I know how it should look. Sorry for the code saying 0. I had made a change after I quoted the code.

I do want to get better at coding, hence why I'm asking on the forum. I don't want to pay someone else to do it for me if it's something I can do myself, however I am stuck and am requesting help. I have done a search for what I'm looking to do and I have been unsuccessful finding an example. If you know how to help me I would appreciate the expertise. The reason why I am using ForexEAdvisor is because it's basic enough for me to grasp the concepts of coding. I don't need the advanced features outlined by WHRoeder because quite frankly I have never found a need for any of those. Also, I'm not living in America so your comments on hedging in that context do not apply to me. Forgive my ignorance in assuming a forum is used to ask a question.

I'd appreciate any specific help you can offer me because I have posted my question here as my last attempt to figure it out. If anyone knows how to help me out, or has a better idea, I'm all ears. Thanks again, 

 
mattmoneywilson: because quite frankly I have never found a need for any of those.
  1. Checking error return code - don't need those. Don't come back here and ask why it's not working!
  2. Not learning the language - don't need that to add more code. This forum is about coding mq4 - go to the EA builder forum then.
  3. "What would the logic look like" - asked and answered: "instead of counting the total open orders, count each type." You already have code that counts total open orders. If you can't code the difference, then there is no common language for us to communicate with.
  4. If you don't want to learn to code in mq4 - stop wasting our time asking such simple questions and hire someone.
  5. Since there are no slaves here, you have only three choices: Search for it, learn to code it, or pay someone. We're not going to code it FOR you. We are willing to HELP you when you post your attempt and the nature of your problem.
You are asking how to drive from A to B when you don't know the difference between a stop sign and a traffic light. It's not going to happen.
 
mattmoneywilson:

LOL yikes.

Let me ask it another way. What would the logic look like if I wanted to achieve my above inquiry? I can do the coding/research myself once I know how it should look. Sorry for the code saying 0. I had made a change after I quoted the code.

Which ticket is this code applicable to ?  what are the units of OrderProfit() ?  what are the units of TakeProfit ?  will this code close an OP_BUY ?

         if(OrderProfit()>=TakeProfit)
    {
    OrderClose(OrderTicket(),Lots,Ask,Slippage);
    }
 

I figured it out. Attached screenshot of most recent backtest. Thanks,

OppOrder 

Reason: