Download MetaTrader 5

Trade when Bid/Ask less or Equal to X pips

To add comments, please log in or register
Ndumiso Mavuso
4383
Ndumiso Mavuso 2015.09.21 01:28 

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");     
  }
Ndumiso Mavuso
4383
Ndumiso Mavuso 2015.09.21 01:56  

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

Enrico Lambino
4356
Enrico Lambino 2015.09.21 09:17  
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;
Ndumiso Mavuso
4383
Ndumiso Mavuso 2015.09.21 11:52  
Still Not Working But Thank You for the reply
Ndumiso Mavuso
4383
Ndumiso Mavuso 2015.09.21 12:04  

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

Roberto Jacobs
47131
Roberto Jacobs 2015.09.21 12:25  
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.

Ndumiso Mavuso
4383
Ndumiso Mavuso 2015.09.21 12:39  
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

Ndumiso Mavuso
4383
Ndumiso Mavuso 2015.09.21 13:12  

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?

Alain Verleyen
Moderator
31134
Alain Verleyen 2015.09.21 13:28  
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.
Ndumiso Mavuso
4383
Ndumiso Mavuso 2015.09.21 14:22  
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


Alain Verleyen
Moderator
31134
Alain Verleyen 2015.09.21 14:32  
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 ?
12
To add comments, please log in or register