EA giving invalid stops error 130 , please help

 
#property show_inputs
#include <WinUser32.mqh>
#include <stdlib.mqh>
#define NL "\n"
#define highline "Next high round number"
#define highlinetradetrigger "Next buy trade trigger"
#define lowline "Next low round number"
#define lowlinetradetrigger "Next sell trade trigger"


/*
void DisplayUserFeedback()
start()

----Trading functions----
void LookForTradingOpportunity()
bool SendSingleTrade(int type, string comment, double lotsize, double price, double stop, double TP, int magic)
bool DoesTradeExist()

----Matt's Order Reliable library code
bool O_R_CheckForHistory(int ticket) Cheers Matt, You are a star.
void O_R_Sleep(double mean_time, double max_time)

*/




extern string bs="----Basic stuff----";
extern double Lot=0.01;
extern int MagicNumber=615046;
extern string TradeComment="Round numbers";
extern bool CriminalIsECN=false;
extern int TradeTriggerPips=15;
extern bool ReverseTradeDirection=false;
extern bool TakeFirstTrade=true;
extern string tpsl="----Stop loss----";
extern int StopLoss=100;
extern string TSL="----Trailing stop loss----";
extern int TrailingStopPips=15;
extern int BreakEvenProfitPips=1;
extern string trs="----Odds and ends----";
extern int DisplayGapSize=30;
extern bool DeleteLinesOnExit=true;
extern double bprice=1.3480;
extern double sprice=1.3460;
double sl = 20;
double TP = 20;




//Matt's O-R stuff
int O_R_Setting_max_retries = 10;
double O_R_Setting_sleep_time = 4.0; /* seconds */
double O_R_Setting_sleep_max = 15.0; /* seconds */


//Round numbers
double RoundNumberHigh, RoundNumberHighTrigger;
double RoundNumberLow, RoundNumberLowTrigger;

//Misc stuff
string ScreenMessage, Gap;
bool RobotDisabled;
string DisabledMessage;
int Spread;


void DisplayUserFeedback()
{
ScreenMessage = "";

ScreenMessage = StringConcatenate(ScreenMessage, Gap, NL);
ScreenMessage = StringConcatenate(ScreenMessage, Gap, "Lot size = ", Lot, NL);
ScreenMessage = StringConcatenate(ScreenMessage, Gap, "Stop loss = ", StopLoss, NL);
ScreenMessage = StringConcatenate(ScreenMessage,Gap, "Magic number = ", MagicNumber, NL);
double spread = MarketInfo(Symbol(), MODE_SPREAD);
ScreenMessage = StringConcatenate(ScreenMessage, Gap, "Spread = ", spread, NL);

ScreenMessage= StringConcatenate(ScreenMessage, NL);
Comment(ScreenMessage);


}//void DisplayUserFeedback()


//+------------------------------------------------------------------+
//| expert Initialization function |
//+------------------------------------------------------------------+

int init()
{

int multiplier;
if(Digits == 2 || Digits == 4) multiplier = 1;
if(Digits == 3 || Digits == 5) multiplier = 10;
if(Digits == 6) multiplier = 100;
if(Digits == 7) multiplier = 1000;

TradeTriggerPips*= multiplier;
StopLoss*= multiplier;
BreakEvenProfitPips*= multiplier;



Gap="";
if (DisplayGapSize >0)
{
for (int cc=0; cc< DisplayGapSize; cc++)
{
Gap = StringConcatenate(Gap, " ");
}
}



Comment(".........Waiting for a tick");
start();

}

//+------------------------------------------------------------------+
//| expert Deinitialization function |
//+------------------------------------------------------------------+

int deinit()
{


Comment("");

}



bool DoesTradeExist()
{
//Searches for open trades


if (OrdersTotal() == 0) return(false); //nothing to do
bool found = false;

for (int cc = OrdersTotal() - 1; cc >= 0; cc--)
{
if (!OrderSelect(cc, SELECT_BY_POS) ) continue;
if (OrderMagicNumber() == MagicNumber && OrderSymbol() == Symbol() )
{
found = true;


if (OrderProfit() > 0) TrailingStopLoss();
}
}


return(found);

}//End bool DoesTradeExist()



bool SendSingleTrade(int type, string comment, double lotsize, double price, double sl, double TP, int magic)
{



int slippage = 10;
if (Digits == 3 || Digits == 5) slippage = 100;

color col = Red;
if (type == OP_BUY || type == OP_BUYSTOP) col = Green;

int expiry = 0;
//if (SendPendingTrades) expiry = TimeCurrent() + (PendingExpiryMinutes * 60);


if (!CriminalIsECN) int ticket = OrderSend(Symbol(),type, lotsize, price, slippage, sl, TP, comment, magic, expiry, col);


//Is a 2 stage criminal
if (CriminalIsECN)
{
ticket = OrderSend(Symbol(),type, lotsize, price, slippage, 0, 0, comment, magic, expiry, col);
if (sl != 0)
{
if (ticket > 0)
bool result = OrderModify(ticket, OrderOpenPrice(), sl, TP, 0, CLR_NONE);
if (!result)
{
int err=GetLastError();
Print(Symbol(), " ", type," SL order modify failed with error(",err,"): ",ErrorDescription(err));
}//if (!result)
}//if (Sl != 0)


}//if (CriminalIsECN)

//Error trapping for both
if (ticket < 0)
{
string stype;
if (type == OP_BUY) stype = "OP_BUY";
if (type == OP_BUYSTOP) stype = "OP_BUYSTOP";
if (type == OP_SELL) stype = "OP_SELL";
if (type == OP_SELLSTOP) stype = "OP_SELLSTOP";
err=GetLastError();
Alert(Symbol(), " ", stype," Manual EA order send failed with error(",err,"): ",ErrorDescription(err));
Print(Symbol(), " ", stype," Manual EA order send failed with error(",err,"): ",ErrorDescription(err));
return(false);
}//if (ticket < 0)

//Make sure the trade has appeared in the platform's history to avoid duplicate trades
O_R_CheckForHistory(ticket);

//Got this far, so trade send succeeded
return(true);


}//End bool SendSingleTrade(int type, string comment, double lotsize, double price, double stop, double TP)



//START TRADE LOGIC//

void LookForTradingOpportunity()
{

if (IsTradeContextBusy() ) return;

double sl, TP;
RefreshRates();
bool result;


double MinStop = MarketInfo(Symbol(), MODE_STOPLEVEL) * Point;


if (!ReverseTradeDirection)
{
//Long

if(Bid >= bprice)
{


if (TakeFirstTrade)
{
//First trade with tp
result = false;
while (result == false)
{
if (StopLoss > 0) sl = NormalizeDouble(Ask - (StopLoss * Point), Digits);

result = SendSingleTrade(OP_BUY, TradeComment, Lot, Ask, sl, TP, MagicNumber);
}//while (result = false)
}//if (TakeFirstTrade)


}//



//Short
// if (Bid <= lo && Open[0] > lo && lo > 0)
if(Bid <= sprice)
{


if (TakeFirstTrade)
{
//First trade with tp
result = false;
while (!result)
{
if (StopLoss > 0) sl = NormalizeDouble(Bid + (StopLoss * Point), Digits);
result = SendSingleTrade(OP_SELL, TradeComment, Lot, Bid, sl, TP, MagicNumber);
}//While (!result)
}//if (TakeFirstTrade)


}//if (Bid <= lo && Open[1] > lo && lo > 0)
}//if (!ReverseTradeDirection)


}//void LookForTradingOpportunity()
//END TRADE LOGIC //


I am getting error 130 Invalid stops, where could i have gone wrong ??

when i set extern int StopLoss=0; all is fine in terms of orders getting fired except that no tp or sl values are put in along with the order .
 

  1. if(Digits == 7) multiplier = 1000;
    TradeTriggerPips*= multiplier;
    StopLoss*= multiplier;
    Each time you change pair, timeframe, etc, the EA does a deinit/init call so your stops become 10x 100x 1000x. Always treat externals as constants.
  2. ticket = OrderSend(Symbol(),type, lotsize, price, slippage, 0, 0, comment, magic, expiry, col);
    if (sl != 0)
    {
    if (ticket > 0)
    bool result = OrderModify(ticket, OrderOpenPrice(), sl, TP, 0, CLR_NONE);
    What is OrderOpenPrice here? You did NOT do a orderSelect first
        int ticket = OrderSend(...)
        if (ticket < 0)
           Alert("OrderSend failed: ", GetLastError());
        else if (!OrderSelect(ticket, SELECT_BY_POS))
           Alert("OrderSelect failed: ", GetLastError());
        else if (!OrderModify(OrderTicket()...)
           Alert("OrderModify failed: ", GetLastError());
    

  3. extern double sprice=1.3460;
    double sl = 20;
    double TP = 20;
    bool SendSingleTrade(int type, string comment, double lotsize, double price, double sl, double TP, int magic)
    { global sl, TP are hidden here - bad form but ok}
    
    //START TRADE LOGIC//
    void LookForTradingOpportunity()
    {
    if (IsTradeContextBusy() ) return;
    
    double sl, TP;  // global sl, TP also hidden here 
    Delete the global sl, TP

  4. if (StopLoss > 0) sl = NormalizeDouble(Ask - (StopLoss * Point), Digits);
    
    result = SendSingleTrade(OP_BUY, TradeComment, Lot, Ask, sl, TP, MagicNumber);
    You buy at the Ask, stops are relative to the Bid.
    if (StopLoss > 0) sl = NormalizeDouble(Bid + (StopLoss * Point), Digits);
    result = SendSingleTrade(OP_SELL, TradeComment, Lot, Bid, sl, TP, MagicNumber); 
    Sell at the bid, stops are relative to the Ask.
 

Hi


I have implemented the changes but the error remains the same, I have attached the entire code. All that i am expecting the program to do is

take the specified value for stop loss and Take Profit and the buy price and sell price and enter buy or sell order when the bid goes above buy price specified and bid price goes below the sell price specified and take the trade in that direction, at any point of time only one trade will be take which meets the logic, now when it fires the order buy or sell as the case may be, it will calculated the stoploss and takeprofit values and fire it along with the order . ( the problem is here as the TP and SL values simply wont get fired along with the order, however buy and sell order based on logic gets fired ) . Now as the price moves it will use the trail stop loss feature to cover the buy or sell price and on meeting the specificed target will just book the order .

The code meets the objective only to the extent of order getting fired as of now .

All that is expected is


Regards


Unimak
Files:
manualuea.mq4  16 kb
Reason: