Hi Mladen and Mrtools ,
So my learning EA (a simple MA crossover EA) is able to set SL and TP to buy orders but ...its getting errors on sell orders... what could be the problem?
See attached picture and code...
Kindly advise. Thank you in advance.
thank you
Mwamba
extern int TP = 1000;
extern int SL = 200;
double realTP(bool buy_sell){
double auxTP;
if (TP == 0){
auxTP = 0;
}
else{
if (buy_sell == true){ //buy
auxTP = Ask+TP*Point;
}
else{
auxTP = Bid-TP*Point;
}
}
return(auxTP);
}
double realSL(bool buy_sell){
double auxSL;
if (SL == 0){
auxSL = 0;
}
else{
if (buy_sell == true){ //buy
auxSL = Ask-SL*Point;
}
else{
auxSL = Bid+SL*Point;
}
}
return(auxSL);
}
void BuyCross()
{
if(AlertOnly)
{
Alert(AlertMessageBuy);
return;
}
if(lastTime != iTime(Symbol(), timeFrame, 1))
{
int tickBuy = OrderSend(Symbol(), OP_BUY, Lotsize, Ask, 3,realSL(true),realTP(true),TradeComment, Magic, 0, Blue);
if(tickBuy < 0)
{
Print("Order Buy Failed");
}
upperComment = upperComment + "\r\n" + "Buy" ;
lastTime = iTime(Symbol(), timeFrame, 1);
}
}
void SellCross()
{
if(AlertOnly)
{
Alert(AlertMessageSell);
return;
}
if(lastTime != iTime(Symbol(), timeFrame, 1))
{
int tickSell = OrderSend(Symbol(), OP_SELL,Lotsize, Bid, 3, realSL(true),realTP(true),TradeComment, Magic, 0, Red);
if(tickSell < 0)
{
Print("Order Sell Failed");
}
upperComment = upperComment + "\r\n" + "Sell";
lastTime = iTime(Symbol(), timeFrame, 1);
}
}mwambaFX
Error 130 means that your stop loss and/or take profit is to close to the current price. Adjust those and it should be OK.
Also, as I see, for a sell order you should modify this :
realSL(true)
to this :
realSL(false)
Thank you for the response Mladen.
However the suggestion is not working.
Could the error be here...
double auxTP;
if (TP == 0){
auxTP = 0;
}
else{
if (buy_sell == true){ //buy
auxTP = Ask+TP*Point;
}
else{
auxTP = Bid-TP*Point;
}
}
return(auxTP);
}
double realSL(bool buy_sell){
double auxSL;
if (SL == 0){
auxSL = 0;
}
else{
if (buy_sell == true){ //buy
auxSL = Ask-SL*Point;
}
else{
auxSL = Bid+SL*Point;
}Thank you for the response Mladen.
However the suggestion is not working.
Could the error be here...
double auxTP;
if (TP == 0){
auxTP = 0;
}
else{
if (buy_sell == true){ //buy
auxTP = Ask+TP*Point;
}
else{
auxTP = Bid-TP*Point;
}
}
return(auxTP);
}
double realSL(bool buy_sell){
double auxSL;
if (SL == 0){
auxSL = 0;
}
else{
if (buy_sell == true){ //buy
auxSL = Ask-SL*Point;
}
else{
auxSL = Bid+SL*Point;
}According to that code, if you call realSL() function with true in the argument, it returns the stop loss for buy order (not sell order). You must call it as realSL(false) for sell order stop loss
Thank you for the response Mladen.
However the suggestion is not working.
Could the error be here...
double auxTP;
if (TP == 0){
auxTP = 0;
}
else{
if (buy_sell == true){ //buy
auxTP = Ask+TP*Point;
}
else{
auxTP = Bid-TP*Point;
}
}
return(auxTP);
}
double realSL(bool buy_sell){
double auxSL;
if (SL == 0){
auxSL = 0;
}
else{
if (buy_sell == true){ //buy
auxSL = Ask-SL*Point;
}
else{
auxSL = Bid+SL*Point;
}What is not working?
Hi mladen ... got it work ... made a little adjustment together with your suggestion.
apprentice coder i was getting the error on the screenshot
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
Hi Mladen and Mrtools ,
So my learning EA (a simple MA crossover EA) is able to set SL and TP to buy orders but ...its getting errors on sell orders... what could be the problem?
See attached picture and code...
Kindly advise. Thank you in advance.
thank you
Mwamba
extern int TP = 1000;
extern int SL = 200;
double realTP(bool buy_sell){
double auxTP;
if (TP == 0){
auxTP = 0;
}
else{
if (buy_sell == true){ //buy
auxTP = Ask+TP*Point;
}
else{
auxTP = Bid-TP*Point;
}
}
return(auxTP);
}
double realSL(bool buy_sell){
double auxSL;
if (SL == 0){
auxSL = 0;
}
else{
if (buy_sell == true){ //buy
auxSL = Ask-SL*Point;
}
else{
auxSL = Bid+SL*Point;
}
}
return(auxSL);
}
void BuyCross()
{
if(AlertOnly)
{
Alert(AlertMessageBuy);
return;
}
if(lastTime != iTime(Symbol(), timeFrame, 1))
{
int tickBuy = OrderSend(Symbol(), OP_BUY, Lotsize, Ask, 3,realSL(true),realTP(true),TradeComment, Magic, 0, Blue);
if(tickBuy < 0)
{
Print("Order Buy Failed");
}
upperComment = upperComment + "\r\n" + "Buy" ;
lastTime = iTime(Symbol(), timeFrame, 1);
}
}
void SellCross()
{
if(AlertOnly)
{
Alert(AlertMessageSell);
return;
}
if(lastTime != iTime(Symbol(), timeFrame, 1))
{
int tickSell = OrderSend(Symbol(), OP_SELL,Lotsize, Bid, 3, realSL(true),realTP(true),TradeComment, Magic, 0, Red);
if(tickSell < 0)
{
Print("Order Sell Failed");
}
upperComment = upperComment + "\r\n" + "Sell";
lastTime = iTime(Symbol(), timeFrame, 1);
}
}