Error Code "0"?

 

I am struggling to understand why the long positions work and the short trades dont at all?

I have a feeling that it has something to do with my init function to determine the number of decimal places with the given broker at hand. (as this seems to be the culprit for why phantom orders are thrown out right at the beginning of when I first initiate the tester...)

Anyone have any ideas why the short side is telling me this error?


Thanks in advance (the code is a little messy at the moment, I intend on cleaning it up of course!) 

//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
  {

   double ticksize = MarketInfo(Symbol(), MODE_TICKSIZE);
   if (ticksize == 0.00001 || ticksize == 0.001) // if you set this to 0 it WONT throw an order out straight away.
   pips = ticksize*10;
   else pips = ticksize;  
   

   return(0);
  }
//+----

//+------------------------------------------------------------------+
//| Order Enter Function                                             |
//+------------------------------------------------------------------+

void OrderEntry (int direction)
{    
   
  int PositionIndex;    //  <-- this variable is the index used for the loop
  int TotalNumberOfOrders;   //  <-- this variable will hold the number of orders currently in the Trade pool
  TotalNumberOfOrders = OrdersTotal();    // <-- we store the number of Orders in the variable
   
   
   double s12 = iMA(NULL,60,60,0,1,0,1); 
   double i121 = iMA(NULL,60,60,0,1,0,1);
   
   double LotSize=0;
   double Equity=AccountEquity();
   double RiskedAmount=Equity*RiskPercent*0.01;

   int iTBT= iBarShift(NULL,60, triggerBarTime, true),
        iHH= iHighest(NULL,60, MODE_HIGH, iTBT + 1, 0); 
            double buyPrice = High[iHH]+PadAmount*pips;
   
   
    int iTBT_1= iBarShift(NULL,60, triggerBarTime1, true),
        iLL= iLowest(NULL,60, MODE_LOW, iTBT_1 + 1, 0); 
            double sellPrice = Low[iLL]-PadAmount*pips;
            
   
   double buy_stop_price = iMA(NULL,60,60,0,1,0,1)-PadAmount*pips; 
   double pips_to_bsl = buyPrice-buy_stop_price;         
   double buy_takeprofit_price = (pips_to_bsl*RewardRatio)+buyPrice;   
   double sell_stop_price = iMA(NULL,60,60,0,1,0,1)+PadAmount*pips; 
   double pips_to_ssl = sell_stop_price-sellPrice;     
   double sell_takeprofit_price = sellPrice-(pips_to_ssl*RewardRatio);
   
//+----------------------------------------------------------------------------------------------------------------------------------------+  
//+----------------------------------------------------------------------------------------------------------------------------------------+   
   
  
if (direction==0) //--Buy--//
   {
      
      {
      double bsl=buy_stop_price;
      double btp=buy_takeprofit_price;
      LotSize = (RiskedAmount/(pips_to_bsl/pips))/10;
      if(OpenOrdersThisPair(Symbol())==0) 
      int BuyTicketOrder = OrderSend(Symbol(),OP_BUYSTOP,LotSize,buyPrice,3,bsl,btp,NULL,MagicNumber,0,Green); 
      
      if(BuyTicketOrder > 0)
         {
         Print("Order Placed #", BuyTicketOrder);
         }
      else
         {
         Print("Order Send Failed, error # ", GetLastError());   
         }
  
      }
   }

            
if (direction==1)//--Sell--//
   {
      {
      double ssl=sell_stop_price;
      double stp=sell_takeprofit_price;
      LotSize = (RiskedAmount/(pips_to_ssl/pips))/10;    
      if(OpenOrdersThisPair(Symbol())==0)  
      int SellTicketOrder = OrderSend(Symbol(),OP_SELLSTOP,LotSize,sellPrice,3,ssl,stp,NULL,MagicNumber,0,Red); 
  
      if(SellTicketOrder > 0)
         {
         Print("Order Placed #", SellTicketOrder);
         }
      else
         {
         Print("Order Send Failed, error # ", GetLastError());   
         }
 
      }
   }
} 
 

does it enter trades when you comment

//if(OpenOrdersThisPair(Symbol())==0
does it enter when
LotSize = 0.1;
etc. work your way back till it starts entering trades, once there you will probably know what the problem is. hth
 
Yea it enters on the long side even with lot size "0"...

Changing the OpenOrderThisPair doesnt work either.... Hmmm. It's very strange...
 
DomGilberto:
Yea it enters on the long side even with lot size "0"...

Changing the OpenOrderThisPair doesnt work either.... Hmmm. It's very strange...

If this test returns non zero . . .

if(OpenOrdersThisPair(Symbol()) == 0)  

  . . .  the OrderSend() is not called, but you still check if SellTicketOrder is greater than 0 . . .

Do this . . .

if (direction==1)//--Sell--//
   {
      {
      double ssl=sell_stop_price;
      double stp=sell_takeprofit_price;
      LotSize = (RiskedAmount/(pips_to_ssl/pips))/10;    
      if(OpenOrdersThisPair(Symbol())==0)
         {
         int SellTicketOrder = OrderSend(Symbol(),OP_SELLSTOP,LotSize,sellPrice,3,ssl,stp,NULL,MagicNumber,0,Red); 
  
         if(SellTicketOrder > 0)
            {
            Print("Order Placed #", SellTicketOrder);
            }
         else
            {
            Print("Order Send Failed, error # ", GetLastError());   
            }
         }
      }
 
Nope - doesn't seem to want to send the sell order...

It must be an error in the way I wrote the code regarding the "IF" statements within the for loop that sends the OrderEntry function to trigger an order...
 
DomGilberto:
Nope - doesn't seem to want to send the sell order...

It must be an error in the way I wrote the code regarding the "IF" statements within the for loop that sends the OrderEntry function to trigger an order...

So what error do you get now ?  if you don't get one and you don't get "Order placed # 12345" then the OrderSend() isn't being called and you have an issue with your OpenOrdersThisPair() function . . .  post your code.
 
When this runs, it works fine for the long side(I have just been playing around with this for the past 2 hours now). Thing is, I will most likely wind you and a certain somebody up, as I know where I am going wrong (where I call "CheckForMaTrade" function) with regards to the IF statements relative to the moving average parameters, and how if they are "true" it will send OrderEntry to OrderSend... Problem is, I am still learning how to write that part properly...
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+

int start()
  {
//----
   
   if(OpenOrdersThisPair(Symbol())>=1)
      {
      if(UseMoveToBreakEven)MoveToBreakEven();
      if(UseTrailingStop)AdjustTrail();
      if(Use_MA_Trail)MA_Trail();
      }
      if(IsNewCandle())CheckForMaTradeLong();
      if(IsNewCandle())CheckForMaTradeShort();
//----
   return(0);
  }
 
DomGilberto:
When this runs, it works fine for the long side(I have just been playing around with this for the past 2 hours now). Thing is, I will most likely wind you and a certain somebody up, as I know where I am going wrong (where I call "CheckForMaTrade" function) with regards to the IF statements relative to the moving average parameters, and how if they are "true" it will send OrderEntry to OrderSend... Problem is, I am still learning how to write that part properly...

RaptorUK:
So what error do you get now ?  if you don't get one and you don't get "Order placed # 12345" then the OrderSend() isn't being called and you have an issue with your OpenOrdersThisPair() function . . .  post your code.


So .. . .  what error do you get, etc . . .
 
Oh sorry - I am no longer getting any error what so ever. I am getting just buy orders going through and no sell orders...

It's in relation to my previous thread regarding MA cross overs - I still don't get it! But I don't want to wind anyone up and beat a dead horse whilst people are giving me the answer (although I dont understand the answer lol)! Cheers RaptorUK for your help!
 
     if(IsNewCandle())CheckForMaTradeLong();
     if(IsNewCandle())CheckForMaTradeShort();
While I support factoring operations into functions, I don't for most variations of New Candle/New Bar because that function can only be called ONCE [per start()]
int start(){
   static datetime time0; bool isNewCandle = time0 != Time[0]; time0 = Time[0]; // Once per start only.
   :
      if(isNewCandle)CheckForMaTradeLong();
      if(isNewCandle)CheckForMaTradeShort();
 
Thanks WHRoeder - good tip!