Results 1 to 5 of 5
 0 Attachment(s)    

Thread: Looking for help with writing a no hedge piece of code

  1. #1

    Default Looking for help with writing a no hedge piece of code

    Hi,

    I'm hoping to get some help with a slice of coding. I have a support and resistance system and I'm trying to edit the ea to include the option of hedging. (Since I live in the US and am with a no-hedge broker).

    I know that when my EA is activated and I have an open order, the opposite pending order will automatically be deleted. The problem is, I can't properly backtest and tweak the system because the Strategy Tester doesn't have hedge/no hedge options.

    So, I'm trying to edit the code to include that option. Obviously I need to put in a bool and set it to false (which Ive done), but I'm not sure where to go from there.

    The EA is a typical support/resistance EA. It places pending orders x distance from either the support or resistance. Using an example, if the market is in a down trend and hits my pending sell and turns it into a market order, and then the trend reverses, I need to delete pending buy orders (or disallow pending buy orders) until the sell order is closed. The EA also deletes pending orders on an expiration timer and at the end of the inputted trading hours.

    I think I'm sort of on the right track. I've written this piece of code, but what happens is pending sell orders (sell stop orders) are deleted as soon as they are placed, whether there is an active buy order or not.

    Hoping someone can assist me.

    PHP Code:
             if (hedge==false
                {
                    
    int total OrdersTotal();
                    for(
    m=total-1;m>=0;m--) 
                    {
                        
    OrderSelect(mSELECT_BY_POS);
                        
    int type=OrderType();
                        
    int type1=OP_BUY;
                        
    int type2=OP_BUYSTOP;
                        
    int type3=OP_SELL;
                        
    int type4=OP_SELLSTOP;
                        
                        if(
    type==type2 && type1>0
                            {
                                
    OrderDelete(OrderTicket());
                            } 
                        if(
    type==type4 && type3>0)
                            {
                                
    OrderDelete(OrderTicket());
                            }
                    }
                } 

  2. #2

    Default

    Ok! I think I've got it. It seems to be allowing an active order and deleting the opposite pending order. As well as switching between the two.

    PHP Code:
        if (hedge==false
            {
                
    int total OrdersTotal();
                for(
    m=total-1;m>=0;m--)
                    {
                        
    OrderSelect(mSELECT_BY_POS);
                        if(
    OrderType()==OP_BUY)
                            {
                                
    deletesellpending(magic);
                            } 
                        if(
    OrderType()==OP_SELL)
                            {
                                
    deletebuypending(magic);
                            }
                        
                    }
            } 
    Now, it sort of brings up another question. The EA places objects and connecting lines to show where an order was opened and closed. With setting up the deletepending this way, My chart actually gets filled with continuous Sell/Buy Stop indicators immediately followed by closing/deleted indicators. This continues until there is no longer an open position.

    So, what I'd like to try now is to figure out if I can put in a check at the ordering function to stop a pending order even being placed. This will create a cleaner chart screen for me.

    I know I figured that one out on my own, but I'm still looking for help since I am not very good with programming languages.

    This is the Order Function:

    PHP Code:
    if((count(OP_BUYSTOP,magic)+count(OP_BUY,magic))<maxtrades && buy && tpb<tradesperbar && IsTradeAllowed())
         {
              
    // -- Taken out coding to shorten post
                        
              
    ticket=OrderSend(Symbol(),OP_BUYSTOP,ilots,resistance+distance*pt,slippage*mt,sl,tp,comment+"Bid:  "+DoubleToStr(Bid,5),magic,expire,Blue);
              if(
    ticket<=0)
              {
                Print(
    "Error Occured : "+errordescription(GetLastError()));
              }
              else
                   {
                
    tps++;
                Print(
    "Order opened : "+Symbol()+" Buy @ "+Ask+" SL @ "+sl+" TP @"+tp+" ticket ="+ticket);
               }
         }
                    
    if((
    count(OP_SELLSTOP,magic)+count(OP_SELL,magic))<maxtrades && sell && tpb<tradesperbar && IsTradeAllowed())
         {
              
    // -- Taken out coding to shorten post
                        
              
    ticket=OrderSend(Symbol(),OP_SELLSTOP,ilots,support-distance*pt,slippage*mt,sl,tp,comment+"Ask:  "+DoubleToStr(Ask,5),magic,expire,Red); 
          if(
    ticket<=0)
               {
                Print(
    "Error Occured : "+errordescription(GetLastError()));
               }
               else
               {
                    
    tpb++;
                Print(
    "Order opened : "+Symbol()+" Sell @ "+Bid+" SL @ "+sl+" TP @"+tp+" ticket ="+ticket);
               }
         } 
    I was thinking if I put in something like && OP_SELL==false (or OP_BUY==false in the sell order section). Is that the right idea?

  3. #3

    Default

    I've programmed things like you are trying to do.... I found that keeping it simple has worked better for me. I loop through Buy conditions and Sell conditions separately... and I also after placing an Order write a simple int xx to +1 and make a simple if (xx<1) or if (xx>0)....to exclude or include additional code. I then reset xx to 0 as condition warrants.
    It is easier to test this way for me as I can verify Buys are working by taking out sell code with /* */ and the same with the opposite side also..... before allowing both to run at the same time.
    It does look like you may be going in that direction with tps and tpb but can't see all your code so don't know...
    Last edited by BillR; 02-26-2012 at 17:11.

  4. #4

    Default

    Yeah.. Programming has it's functions, variables, and directives, but they follow a simple language principle. You can talk/think it out. Doesn't necessarily mean that I can write it properly, but I have a clear understanding of what I am trying to do. And when I read code, I have an understanding of what it is saying.

    Btw, I sent you a private message with the full code. I think simple is better too, which is why I'm trying to rewrite things to be more simplistic.

    I still need to write that piece to stop pending orders being placed if the opposite position is open, but the hedge option that I was writing is working perfectly. I just want to "clean it up" now by making sure the pending orders are never placed to begin with.

    Edit: Update

    I was looking at the code and realized that the order types were already counting. It was right there in the ordering process, so I just added the additional condition that an opposite position didn't exist.

    PHP Code:
    I changed this:
    if((
    count(OP_BUYSTOP,magic)+count(OP_BUY,magic))<maxtrades && buy && tpb<tradesperbar && IsTradeAllowed())

    To this:
    if((
    count(OP_BUYSTOP,magic)+count(OP_BUY,magic))<maxtrades && buy && tpb<tradesperbar && count(OP_SELL,magic)==&& IsTradeAllowed())

    And 
    the opposite on the Sell function 
    Now the no hedge option is working perfectly. I've come up with a couple more ideas that I might try:
    1. An option to close all trades at the end of day.
    2. The EA uses an indicator call. I wrote an indicator for 5 minute highs and lows to be used on a 1 minute chart. Thinking that I might (at some point) add in a user selected option of using 15 minute High/Low on a 5 minute chart, 30 Hi/Lo on a 15 minute chart, 1 Hour on a 30 Minute chart..etc..etc..
    Last edited by gregwendland; 02-26-2012 at 22:43.

  5. #5

    Default

    Update:

    Well, I've got something wrong. While the above modifications worked perfectly on the Strategy Tester, the second one (to prevent a pending order being made if an opposite position is active) doesn't seem to be working on demo forward testing. Does anyone have an idea on what I might try?

    Edit:

    What would be the effect if I went back to the hedge portion that I put in earlier and set buy or sell to false?

    PHP Code:

    if (hedge==false)  
            { 
                
    int total OrdersTotal(); 
                for(
    m=total-1;m>=0;m--) 
                    { 
                        
    OrderSelect(mSELECT_BY_POS); 
                        if(
    OrderType()==OP_BUY
                            { 
                                
    deletesellpending(magic);
                                
    buy=false
                            }  
                        if(
    OrderType()==OP_SELL
                            { 
                                
    deletebuypending(magic); '
                                sell=false;
                            } 
                         
                    } 
            } 
    Last edited by gregwendland; 02-27-2012 at 10:22. Reason: Additional Thoughts

Similar Threads

  1. Need help in writing a code
    By micoul81 in forum Ideas for expert advisors
    Replies: 0
    Last Post: 10-06-2014, 17:14
  2. A7 Hedge
    By a7med in forum Ideas for expert advisors
    Replies: 3
    Last Post: 11-03-2011, 10:27
  3. Code needed to track ea in order to hedge
    By snkpool in forum MQL programming
    Replies: 0
    Last Post: 09-28-2010, 03:57
  4. HEDGE WIN EA
    By civfan in forum Ideas for expert advisors
    Replies: 3
    Last Post: 09-20-2010, 20:48

Tags for this Thread

backtest, broker, closing, coding, demo, hedge, hedging, hours, indicator, indicators, live, magic, programming, resistance, sells, simple, slippage, stop, strategy, support, system, test, time, timer, trading, trend

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •