Download MetaTrader 5
To add comments, please log in or register
Add the thread to Favorites. Track the changes even faster!
Keith Watford
Moderator
9505
Keith Watford 2016.05.17 13:36  

You need to re-think your code.

buy_hedge==0 && sell_hedge==0 && OrderOpenPrice()>Bid+Hedge*Pip)

These will always be true as you set them both to zero and don't change them. You don't enclose the following block of code in curly braces {}

You don't use the GV to check anything and you name it with an integer. The check does nothing as you don't assign the returned bool to any variable.

Check whether the OrderSend fails, and if it does, print the error code.

trader3000
227
trader3000 2016.05.18 10:27  
GumRai:

You need to re-think your code.

These will always be true as you set them both to zero and don't change them. You don't enclose the following block of code in curly braces {}

You don't use the GV to check anything and you name it with an integer. The check does nothing as you don't assign the returned bool to any variable.

Check whether the OrderSend fails, and if it does, print the error code.

Thank you.  I think I am on the right track, but now it will open multiple hedge trades even though it states total<=1.  My code now looks like this:

if(total<=1 && OrderOpenPrice()>Bid+Hedge*Pip){
               GlobalVariableCheck(ticket);
               sell_hedge=OrderSend(Symbol(),OP_SELL,HedgeLots,Bid,3,Bid+StopLossHedge*Pip,0,"Hedge",0,0,Blue);
               sell_hedge=GlobalVariableGet(ticket);
               sell_hedge=sell_hedge+1;
               GlobalVariableSet(ticket,sell_hedge);
               int New_SellHedge=GlobalVariableGet(ticket);
               if(sell_hedge!=New_SellHedge) {sell_hedge=New_SellHedge;}
              }
            }
trader3000
227
trader3000 2016.05.19 16:10  

Thank you for this advice.  I finally figured it out and it now works.  The bit of code looks like this:

if(GlobalVariableCheck("InTrade")){
      }
      else if(OrderOpenPrice()>Bid+Hedge*point){
         sell_ticket=OrderSend(Symbol(),OP_SELL,HedgeLots,Bid,3,Bid+StopLossHedge*point,0,"Hedge",magic,0,Blue);{
         GlobalVariableSet("InTrade",1);
         GlobalVariableDel("InTrade");
         return(0);
        }
     }
   }
Keith Watford
Moderator
9505
Keith Watford 2016.05.19 21:52  
         GlobalVariableSet("InTrade",1);
         GlobalVariableDel("InTrade");

What is the point of setting the GV then immediately deleting it?

trader3000
227
trader3000 2016.05.20 11:57  
GumRai:

What is the point of setting the GV then immediately deleting it?

Makes sense thank you.  So is it better like this:

if(GlobalVariableCheck("InTrade")){
  }
   else if(OrderOpenPrice()>Bid+Hedge*Pip){
      sell_ticket=OrderSend(Symbol(),OP_SELL,HedgeLots,Bid,3,Bid+StopLossHedge*Pip,0,"Hedge",magic,0,Blue);{
         GlobalVariableSet("InTrade",1);
         return(0);
        }
      }
    }
GlobalVariableDel("InTrade");
Keith Watford
Moderator
9505
Keith Watford 2016.05.20 12:14  
Trader3000:

Makes sense thank you.  So is it better like this:

 

No, it still makes no sense at all.

There should be a reason to delete the GV

trader3000
227
trader3000 2016.05.24 08:53  
GumRai:

No, it still makes no sense at all.

There should be a reason to delete the GV

Thank you again.  How about this?

int total=0;
   for(int i=OrdersTotal()-1; i>=0; i--)
      if(OrderSelect(i,SELECT_BY_POS) && OrderMagicNumber()==0 && OrderSymbol()==
         total++;
        }
if(GlobalVariableCheck("InTrade")){
  }
   else if(total==1 && OrderOpenPrice()>Bid+Hedge*Pip){
      sell_ticket=OrderSend(Symbol(),OP_SELL,HedgeLots,Bid,3,Bid+StopLossHedge*Pip,0,"Hedge",magic,0,Blue);{
         GlobalVariableSet("InTrade",1);
            if(total<1) GlobalVariableDel("InTrade");
            return(0);
           }
         }
       }
Keith Watford
Moderator
9505
Keith Watford 2016.05.24 10:47  
Trader3000:

Thank you again.  How about this?

 

It still doesn't make sense

   else if(total==1 && OrderOpenPrice()>Bid+Hedge*Pip){
      sell_ticket=OrderSend(Symbol(),OP_SELL,HedgeLots,Bid,3,Bid+StopLossHedge*Pip,0,"Hedge",magic,0,Blue);{
         GlobalVariableSet("InTrade",1);
            if(total<1) GlobalVariableDel("InTrade");
            return(0);
           }
         }

The code block is only executed if total==1, so the check if(total<1) will always be false.

/ /12345
To add comments, please log in or register