Download MetaTrader 5
To add comments, please log in or register
Do you want to deposit to your account? Do this in any convenient way!
Adam
22
Adam 2015.06.29 01:45 

Here is the code

int start()
{
   double BuyTP=Ask+(TakeProfit*Point);
   double SellTP=Bid-(TakeProfit*Point);
   double lots=(AccountBalance()/10000)*Lot;
   ArrowUp=ObjectFind("SignalUp");
   ArrowDown=ObjectFind("SignalDown");
   Strength=MathAbs(ObjectDescription("WPRValue"));
   
   if(!OpenTrade&&ArrowUp==0&&Strength<=BuySignalStrength)  //Buy Logic
   {
      Ticket=OrderSend(NULL,OP_BUY,lots,Ask,10,NULL,BuyTP,NULL,Magic,NULL,clrBlue);
      OpenTrade=true;
      Position="Buy";    //Sets Position to Buy
   }
   
   if(!OpenTrade&&ArrowDown==0&&Strength>=SellSignalStrength)   //Sell Logic
   {
      Ticket=OrderSend(NULL,OP_SELL,lots,Bid,10,NULL,SellTP,NULL,Magic,NULL,clrRed);
      OpenTrade=true;
      Position="Sell";    //Sets Position to Sell
      
   }   
   
   if(OpenTrade&&Position=="Buy"&&(Strength>=ExitBuySignalStrength||(BuyArrowExit&&ArrowUp!=0))) //Buy close Logic
   {
      OrderClose(Ticket,Lot,Bid,10000,clrGreen);
      OpenTrade=false;
      Position="";
   }
   
   if(OpenTrade&&Position=="Sell"&&(Strength<=ExitSellSignalStrength||(SellArrowExit&&ArrowDown!=0)))//Sell close Logic
   {
      OrderClose(Ticket,Lot,Ask,10000,clrGreen);
      OpenTrade=false;
      Position="";
   }
   if(!OrderSelect(0,SELECT_BY_POS))//Set OpenTrade to false after TP
   {
      OpenTrade=false;  
      Position="";
   }   
   
   
      ObjectCreate("ObjName", OBJ_LABEL, 0, 0, 0);
      ObjectSetText("ObjName","Open Trade: "+OpenTrade+" Position: "+Position+" Arrow: "+ArrowUp+ArrowDown+" Strength: "+Strength+" Error:"+GetLastError(),25, "Verdana", Red);
      ObjectSet("ObjName", OBJPROP_CORNER, 0);
      ObjectSet("ObjName", OBJPROP_XDISTANCE, 20);
      ObjectSet("ObjName", OBJPROP_YDISTANCE, 20);
   
  
   
   
return(0);
}

 and here is a back test. All of the conditions are met but it won't open a trade. SellSignalStrength is set to 95.

 

whroeder1
13610
whroeder1 2015.06.29 11:52  
      Ticket=OrderSend(NULL,OP_BUY,lots,Ask,10,NULL,BuyTP,NULL,Magic,NULL,clrBlue);
  1. Check your return codes (OrderSend) and find out why. What are Function return values ? How do I use them ? - MQL4 forum and Common Errors in MQL4 Programs and How to Avoid Them - MQL4 Articles
  2. NULL is not a valid SL, NULL is not a valid expiration. Use zero.
Adam
22
Adam 2015.06.29 14:21  
So, I found out that if i take out the "Ticket=" then it will make multiple trades, but won't close them unless it hits the TP, obviously. I copied the code you posted from another EA I have and had no problems with it in that EA, so there must be something where Ticket is keeping it from opening more trades but I can't find anything.
Adam
22
Adam 2015.06.29 14:47  

Another update. I used 

  if(OrderSelect(0,SELECT_BY_POS))
   {
      Ticket=OrderTicket();
   }  

 to set the ticket value instead of assigning it at the order placement. It appears that if the order is closed by 

   if(OpenTrade&&Position=="Buy"&&(Strength>=ExitBuySignalStrength||(BuyArrowExit&&ArrowUp!=0))) //Buy close Logic
   {
      OrderClose(Ticket,Lot,Bid,10000,clrGreen);
      OpenTrade=false;
      Position="";
   }
   
   if(OpenTrade&&Position=="Sell"&&(Strength<=ExitSellSignalStrength||(SellArrowExit&&ArrowDown!=0)))//Sell close Logic
   {
      OrderClose(Ticket,Lot,Ask,10000,clrGreen);
      OpenTrade=false;
      Position="";
   }

 then it won't open any other orders for some reason.

Adam
22
Adam 2015.06.29 15:09  
More experiments done, even if it hits the TP line it won't open any other trades. So once the first trade is closed, the program is incapable of opening any other trades.
Adam
22
Adam 2015.06.29 17:19  
Okay I figured it out. So I didn't NormalizeDouble my lots variable so it was trying to pass an impossible parameter ex.999999925172 lots.
whroeder1
13610
whroeder1 2015.06.29 18:44  
Do NOT use NormalizeDouble, EVER. For ANY Reason. It's a kludge, don't use it. It's use is always wrong
/
To add comments, please log in or register