Trade when Bid/Ask less or Equal to X pips

 

Can someone Please Help me on what I am doing wrong on my code. I want to open a trade once price goes X number of pips above or below Ask/Bid.

My Code Below is not opening trades and returns no error

sellprice=iOpen(NULL, 0, 0)-Distance*MyPoint*Point; //Distance is the number of pips away from price

sellprice=NormalizeDouble(sellprice,Digits);
  bid=NormalizeDouble(Bid,Digits);
  
  if (bid<=sellprice)
  {
  ticket=OrderSend (Symbol(),OP_SELL,Lots,Bid,1,Bid+StopLoss*MyPoint*Point,Bid-TakeProfit*MyPoint*Point,"Sell Order",MagicNumber,0,Red);
   if(ticket<0)
     {
      Print("OrderSend failed with error #",GetLastError());
     }
   else
      Print("OrderSend placed successfully");     
  }
 

I have tried

double PreviousTick=0.0;
double Sellprice=Bid;

if(Sellprice < PreviousTick -Distance*MyPoint*Point)
 {
  ticket=OrderSend (Symbol(),OP_SELL,Lots,Bid,1,Bid+StopLoss*MyPoint*Point,Bid-TakeProfit*MyPoint*Point,"Sell Order",MagicNumber,0,Red);
   if(ticket<0)
     {
      Print("OrderSend failed with error #",GetLastError());
     }
   else
      Print("OrderSend placed successfully");     
  }

But still not working

 
Ndumiso Mavuso:

I have tried

But still not working

Try this:

static double PreviousTick=0.0;
double Sellprice=Bid;

if(Sellprice < PreviousTick -Distance*MyPoint*Point && PreviousTick>0)
 {
  ticket=OrderSend (Symbol(),OP_SELL,Lots,Bid,1,Bid+StopLoss*MyPoint*Point,Bid-TakeProfit*MyPoint*Point,"Sell Order",MagicNumber,0,Red);
   if(ticket<0)
     {
      Print("OrderSend failed with error #",GetLastError());
     }
   else
      Print("OrderSend placed successfully");     
  }
PreviousTicke = Sellprice;
 
Still Not Working But Thank You for the reply
 

My head Hurts Now

sellprice=iOpen(NULL, 0, 0)-Distance*MyPoint*Point;
double ask=NormalizeDouble((Ask),5);
double bid=NormalizeDouble((Bid),5);
double sellprice=NormalizeDouble((sellprice),5);
double over=((sellprice)/1);
GlobalVariableSet("over",over);


if(GlobalVariableGet("over")>=(Ask))
{
  ticket=OrderSend (Symbol(),OP_SELL,Lots,Bid,1,Bid+StopLoss*MyPoint*Point,Bid-TakeProfit*MyPoint*Point,"Sell Order",MagicNumber,0,Red);
   if(ticket<0)
     {
      Print("OrderSend failed with error #",GetLastError());
     }
   else
      Print("OrderSend placed successfully");     
  }

Still Not working, no response no nonthing

 
Ndumiso Mavuso:

Can someone Please Help me on what I am doing wrong on my code. I want to open a trade once price goes X number of pips above or below Ask/Bid.

My Code Below is not opening trades and returns no error

The code can only do open order with SELLLIMIT/SELLSTOP or BUYLIMIT/BUYSTOP (Pending Order)

Variable ticket should boolean variable.

 
3rjfx:

The code can only do open order with SELLLIMIT/SELLSTOP or BUYLIMIT/BUYSTOP (Pending Order)

Variable ticket should boolean variable.

But I do not want to use pending Orders, I could have just use them and saved myself from this headache, So you are saying this can no be done?

The reason I want to code this is that I currently using pending orders with spread filter so I use this to filter spread

//+------------------------------------------------------------------+
//|If Spread increase Delete Orders                                  |
//+-----------------------------------------------------------Ndumiso+
  if (Spread>MaxSpread)  
  {
  for (a=total-1; a >=0; a--)
  {
  OrderSelect(a,SELECT_BY_POS,MODE_TRADES);
  coment=OrderComment();
  par=StringSubstr(coment,0,6);
  if(OrderType()==OP_SELLSTOP)
  {
  OrderDelete(OrderTicket());
  Print("Deleting SELL_STOP"," Ordertype:",OrderType());
  return;
  }
  if(OrderType()==OP_BUYSTOP)
  {
  OrderDelete(OrderTicket());
  Print("Deleting BUY_STOP"," Ordertype:",OrderType());
  return;
  }
  }

But I find it not reliable

 

Is there a reason why Bid can not be < than my sellprice?

sellprice=iOpen(NULL, 0, 0)-Distance*MyPoint*Point;
 sellprice=NormalizeDouble(sellprice,Digits);

if(sellprice>Bid)
{
  ticket=OrderSend (Symbol(),OP_SELL,Lots,Bid,1,Bid+StopLoss*MyPoint*Point,Bid-TakeProfit*MyPoint*Point,"Sell Order",MagicNumber,0,Red);
   if(ticket<0)
     {
      Print("OrderSend failed with error #",GetLastError());
     }
   else
      Print("OrderSend placed successfully");     
  }

Because if i say

if(sellprice<Bid)

Orders are opened but not the other way around. Is there a logic reason behind this?

 
Ndumiso Mavuso:

Is there a reason why Bid can not be < than my sellprice?

Because if i say

Orders are opened but not the other way around. Is there a logic reason behind this?

What is the value of Distance ? MyPoint ? and sellprice ? Print them and you will see why your Bid is can not be lower.
 
Alain Verleyen:
What is the value of Distance ? MyPoint ? and sellprice ? Print them and you will see why your Bid is can not be lower.

The value of sellprice look accurate on backtest as it suppose to look, candle Open - 2.5 pips


 
Ndumiso Mavuso:

The value of sellprice look accurate on backtest as it suppose to look, candle Open - 2.5 pips


You printed your sell price every new candle, is your open condition is also checked every new candle ?
Reason: