Open Trade on New Bar

 

I want to open a buy trade if on a new bar current price is higher than open price and a sell if the opposite happens

this is my code

my new candle code works but i get order error 130 trying to check and open trade

 

void checkBar(){
   slip = slippage*pips;
   if(StopLoss>0){stop_loss=StopLoss*pips;}else{stop_loss=0;}
   if(TakeProfit>0){take_profit=TakeProfit*pips;}else{take_profit=0;}
   if (Bid>PRICE_OPEN){
      //open buy;
      OrderSend(Symbol(),OP_BUY,lotsize,Bid,slip,(Bid-stop_loss),(Bid+take_profit),NULL,Magic,0,clrNONE);
   }
   if (Bid<PRICE_OPEN){
      //open sell;
      OrderSend(Symbol(),OP_SELL,lotsize,Ask,slip,(Ask+stop_loss),(Ask-take_profit),NULL,Magic,0,clrNONE);
   }
   
}
 
Nurudeen Amedu:

I want to open a buy trade if on a new bar current price is higher than open price and a sell if the opposite happens

this is my code

my new candle code works but i get order error 130 trying to check and open trade

 

I think your code opening the first order with no error 130 (invalid stops)
But the errror is starting from the second positiin and so on....
Right?
----
I think that you need to reset the variables (stop_loss) and (take_profit) to again be equal to 0
So, you sould add another code lines that initiate them again in the top of your code:
---
void checkBar(){
RefreshRates();
stop_loss =0;
take_profit =0;
.
.
.
.
}
//That's All
 
Mohammad Soubra:
I think your code opening the first order with no error 130 (invalid stops)
But the errror is starting from the second positiin and so on....
Right?
----
I think that you need to reset the variables (stop_loss) and (take_profit) to again be equal to 0
So, you sould add another code lines that initiate them again in the top of your code:
---
void checkBar(){
RefreshRates();
stop_loss =0;
take_profit =0;
.
.
.
.
}
//That's All

thanks i fixed it using 

   if (Ask<iOpen(Symbol(),0,1)){
      //open sell;
   OrderSend(Symbol(),OP_SELL,lotsize,Bid,slip,Bid+stop_loss,(Bid-take_profit),NULL,Magic,0,clrRed);
   }
      
   
   if (Ask>iOpen(Symbol(),0,1)){
      //open buy;
      OrderSend(Symbol(),OP_BUY,lotsize,Ask,slip,Ask-stop_loss,(Ask+take_profit),NULL,Magic,0,clrBlue);
   }
 

To make things easier for both of us, I think the following is clear ... 

Please refer to the following code to assign SL and TP for Buy and Sell orders ...

double GetStopLossBuy() {return (Bid-SL*Point);} 
double GetStopLossSell() {return(Ask+SL*Point);} 
double GetTakeProfitBuy() {return(Ask+TP*Point);} 
double GetTakeProfitSell() {return(Bid-TP*Point);} 

 So, you need to use Ask in the Stop loss for the Sell order (You used Bid !!!).

 you need to use Bid in the Stop loss for the Buy order (You used Ask !!!).

 

I assume, the code will be as follows ...

   if (Ask<iOpen(Symbol(),0,1)){
      //open sell;
   OrderSend(Symbol(),OP_SELL,lotsize,Bid,slip,Ask+stop_loss,(Bid-take_profit),NULL,Magic,0,clrRed);
   }
      
   
   if (Ask>iOpen(Symbol(),0,1)){
      //open buy;
      OrderSend(Symbol(),OP_BUY,lotsize,Ask,slip,Bid-stop_loss,(Ask+take_profit),NULL,Magic,0,clrBlue);
   }

  

Considering there are no other mistake(s) ...

Please let me know if it works for you ...