Can I open long, and then short if long is still open with EA?

 

Hi everyone

I used an online code genetor to generate some code with a long and short entry signal.  Here's the scenario, it triggers long and a trade is opened with stops and targets set.  Then the market reverses, stop is not hit but a short is triggered.  I would like it to open a short (keeping long open) with stops and target set and then just wait for the direction to confirm.  I want no more than one long and one short at any time.  I do not want multiple entries in the same direction. 

I know I could just make an EA for short and another for long and run on 2 charts but when I create a 2min chart, only one of them updates, the other stays static, so I need one EA to handle both directions on the 2 min chart (I think).

Is there some way to modify this bit of the code to make this happen. 

Thanks 

 

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);
                    }
                 }
              }
           }
        }
     }
   return(0);
 
metasteve:

Hi everyone

I used an online code genetor to generate some code with a long and short entry signal.  

Thats a bad idea,  what did you learn about mql4 in the process of doing that ? nothing that enables you to make some simple modifications,  if you want to learn to code then do so:  Book   if you want someone else to write code for you then you can do that to:  write my MT4 EA for me
 

Lots, I have learnt that its very similar to C and since I can program in C# and VB.net it was a quick way to get a start, and there are many mods to the entry logic that I have made myself, I am just trying to get my head around how the orders are checked and managed, and was hoping someone would point me in the right direction for how its done.  I am also learning that MT4 might be the wrong platform for auto trading 2 min charts.  If that is the case then thanks to the online code generator I have only wasted a few hours.

 
metasteve:

Lots, I have learnt that its very similar to C and since I can program in C# and VB.net it was a quick way to get a start, and there are many mods to the entry logic that I have made myself, I am just trying to get my head around how the orders are checked and managed, and was hoping someone would point me in the right direction for how its done.  I am also learning that MT4 might be the wrong platform for auto trading 2 min charts.  If that is the case then thanks to the online code generator I have only wasted a few hours.

OK,  to do what you describe in your first post you simply need to let the long and short be opened but only open them once you have first checked how many long and short trades your EA has already opened,  if there is less than one long trade you can open the long, if there is less than one short trade you can open a short.

So use a loop to loop through the open orders,  OrderSelect() them in the loop,  check their OrderSymbol() to make sure it matches the symbol your EA is running on, check their OrderMagicNumber() if you are using one . . .  count up the longs,  count up the sells . . .  now you can decided if you are placing new trades or not. 

If you read this and understand it you will get some coding suggestions:  Loops and Closing or Deleting Orders

 

You should also read this and check your trade function return values and report your errors:  What are Function return values ? How do I use them ? 

 
metasteve:

Can I open long, and then short if long is still open with EA?

I used an online code genetor to generate

I create a 2min chart, only one of them updates, the other stays static, so I need one EA to handle both directions on the 2 min chart (I think).
  1. Hedging is not allowed in the US
  2. Bad, already explained
  3. The generator can only signal one chart so of course only one updates. Why would you want to duplicate all the code just to change direction?
 

Raptor  Thanks.

 

WH

1. I dont live in the US.

2. Efficient or perhaps lazy, but not bad, the code works great and took 5 mins to create.

3. For my 5 min strategy I have two 5min charts open, one with a long EA and one with a short EA, each with its own magic number.  Works fine.  If I could get two 2min charts running at the same time I wouldnt need to do it in one EA.

 
metasteve: 2. Efficient or perhaps lazy, but not bad, the code works great and took 5 mins to create.
Then you can spend 10 minutes to create a bidirectional one and don't have to bother us when you have coding problems.
 

Your EABuilder is making code

trying modifying every Point

doing it this way

will fail often on an account.....

 

For those that may be interesed in something similar here is the code that I added.  Not as complicated as suggested above because I only trade 1 pair so no need to check symbols, and there will only  be 1 open trade when the type test is done.  Tested on a 1min chart, seems to work ok.

 

//if a trade is open determine its type
if( TotalOrdersCount()<2 ) // no trades or one trade open
 
    int tradetype=3; //any number other than 1 or 2 to to declare it as something
    if ( TotalOrdersCount()>0 )  //then a trade is open so get its type
        tradetype=TradeTypeTest();
 

//entry conditions for long
if(tradetype!=1//plus rest of conditions for long entry

//entry conditions for short
if(tradetype!=2//plus rest of conditions for short entry
   MagicNumber=MagicNumber*2 //to give it a different number for short position



// need to allow for new magic number in stop loss control
if(OrderType()<=OP_SELL &&   
         OrderSymbol()==Symbol() &&
         OrderMagicNumber()==MagicNumber ||OrderMagicNumber()==MagicNumber*2 //add or statement to deal with both mag nums
         //rest of stop loss control here



//called to determine if open trade is long or short

int TradeTypeTest()  //1 =long, 2 = short
{
  int tradetype=2; //set default to 2 for case where there is open short trade
     if(OrderTakeProfit()>OrderStopLoss())  //change to 1 as open trade is long
     tradetype=1; 
   return (tradetype);
}

 

For those that consider this thread to be a waste of their precious time, feel free to move on to the next post without replying.

 
metasteve:

For those that consider this thread to be a waste of their precious time, feel free to move on to the next post without replying.

And with that kind of attitude I'm sure many will take your advice . . .
 
metasteve:

Raptor  Thanks.

 

WH

1. I dont live in the US.

2. Efficient or perhaps lazy, but not bad, the code works great and took 5 mins to create.

3. For my 5 min strategy I have two 5min charts open, one with a long EA and one with a short EA, each with its own magic number.  Works fine.  If I could get two 2min charts running at the same time I wouldnt need to do it in one EA.

In the US by law you are not allowed to hedge. For the rest of the world hedging is broker dependent. I am not in the US either, but my broker insists on using a "net" based trading system (going long, then short results in no position). It obviously makes them more money as you pay the spread twice, and they also do not need to cover your margin twice (even though your net margin is zero).

 I am not going to go into the merits or demerits of hedging as there are some strong opinions for and against. My stance is that I am an adult and I should be able to loose (or gain) money as quickly as I like. And some strategies like grid systems cannot work without hedging (unless you have two accounts and alot of complication). 

 Look for a broker that uses a "position" based system. 

Reason: