Going MAD!!! OrderSend won't work once i use variable to define StopLoss

 

This EA is designed to put a limit back on a drawn line after the relevant timeframe closes below it. I am just using a sell at bid for now so i can test it on the 1minute chart. I have added a chime so you know once the program has run past the order send function.

Whenever I try to add a stop loss to this order simply by replacing the "0.0" in the stop loss area of the order send line with my variable "StopLossLevel")..... the order doesn't open.. I know my stop loss is valid because the risk gets worked out correctly. Please someone shed some light on this for me, i am going mad.

I dont know how to get the error messages up so i can see where the problem is..... :-(
//+------------------------------------------------------------------+
//|                                               CDF Close Sell.mq4 |
//|                              Marty Asher - asher.marty@gmail.com |
//|                                        http://www.metaquotes.net |
//+------------------------------------------------------------------+
#property copyright "M.Asher 2011"
#property link      "http://www.metaquotes.net"
#define SIGNAL_NONE 0
#define SIGNAL_BUY   1
#define SIGNAL_SELL  2

extern string TLineName="Line";
extern color LineColor=Black; extern int LineStyle=STYLE_SOLID;
extern int CloseExtra=50;
extern string AlertWav="alert.wav";
extern int StopLoss = 300;
extern double TakeProfitLevel = 0.0;
extern double RiskPercentage = 0.02;
extern bool SignalMail = False;


bool Alerted = False;

double Lots = 0;
double StopLossLevel = 0;
double StopLossLevel2;
double StopSize;

int Slippage = 3;
int MagicNumber = 0;
int BarCount;

//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
  {
//----
   BarCount = Bars;
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
{

double High1 = iHigh(NULL, 0, 1 + 0);
double Low1 = iLow(NULL, 0, 1 + 0);
double Close1 = iClose(NULL, 0, 1 + 0);
double Val = ObjectGetValueByShift(TLineName, 0);

int Total;
int Ticket;
int Order = SIGNAL_NONE; 
int counted_bars=IndicatorCounted();

ObjectCreate(TLineName, OBJ_TREND, 0, Time[25], Bid, Time[0], Ask);
ObjectSet(TLineName, OBJPROP_STYLE, LineStyle);
ObjectSet(TLineName, OBJPROP_COLOR, LineColor);


if (BarCount != Bars && Alerted == false) //Checking that we havnt already put on a trade
 { 
      if (Close1+CloseExtra* Point <= Val) //Initial Sell Procedure
      {    
            //Check free margin
            if (AccountFreeMargin() < 500) 
            {
               Print("We have no money. Free Margin = ", AccountFreeMargin());
               return(0);
            }
            //Check free margin finshed    
       StopLossLevel = Val + StopLoss * Point;
       StopSize = StopLossLevel - Bid;       
       Lots = (AccountFreeMargin() * RiskPercentage)/(StopSize*100000);
       Ticket = OrderSend(Symbol(), OP_SELL, Lots, Bid, Slippage, 0.0 , TakeProfitLevel, "Sell(#" + MagicNumber + ")", MagicNumber, 0, DeepPink);
       PlaySound(AlertWav);     
            //Check for multiple tickets
            if(Ticket > 0) 
            {
            if (OrderSelect(Ticket, SELECT_BY_TICKET, MODE_TRADES)) 
            {
                    Print("BUY order opened : ", OrderOpenPrice());
               if (SignalMail) SendMail("[Signal Alert]", "[" + Symbol() + "] " + DoubleToStr(Ask, Digits) + " Open Buy");
                 } 
                 else 
                     {
                Print("Error opening BUY order : ", GetLastError());
                      }
            }
            //Check for multiple tickets finished
      
      }
 BarCount = Bars;
 Alerted = True; 
 }

//----
//----
   return(0);
}
//+------------------------------------------------------------------+
Files:
 

marty087:

I dont know how to get the error messages up so i can see where the problem is..... :-(

Your code looks like it already prints the Error, check the Expert tab (or Journal if you are using the Strategy Tester).
 
  1. OrderSend won't work once i use variable to define StopLoss
    Ticket = OrderSend(Symbol(), OP_SELL, Lots, Bid, Slippage
    Always test return codes to find out WHY
    int ticket = OrderSend(...);
    if (ticket < 0) Alert("OrderSend Failed: ", GetLastError());

  2. if (Close1+CloseExtra* Point <= Val) 
    :
    StopLossLevel = Val + StopLoss * Point; 
    :
    Ticket = OrderSend(Symbol(), OP_SELL, Lots, Bid, Slippage, 0.0 , ...
    EAs must adjust for 4/5 digit brokers (TP, SL, AND slippage) On ECN brokers you must open first and THEN set stops
    //++++ These are adjusted for 5 digit brokers.
    int     pips2points;    // slippage  3 pips    3=points    30=points
    double  pips2dbl;       // Stoploss 15 pips    0.015      0.0150
    int     Digits.pips;    // DoubleToStr(dbl/pips2dbl, Digits.pips)
    int     init(){
         if (Digits % 2 == 1){      // DE30=1/JPY=3/EURUSD=5 forum.mql4.com/43064#515262
                    pips2dbl    = Point*10; pips2points = 10;   Digits.pips = 1;
        } else {    pips2dbl    = Point;    pips2points =  1;   Digits.pips = 0; }
        // OrderSend(... Slippage.Pips * pips2points, Bid - StopLossPips * pips2dbl
    //---- These are adjusted for 5 digit brokers.
        /* On ECN brokers you must open first and THEN set stops
        int ticket = OrderSend(..., 0,0,...)
        if (ticket < 0)
           Alert("OrderSend failed: ", GetLastError());
        else if (!OrderSelect(ticket, SELECT_BY_TICKET))
           Alert("OrderSelect failed: ", GetLastError());
        else if (!OrderModify(OrderTicket()...)
           Alert("OrderModify failed: ", GetLastError());
         */
    

 

I think your logic is wrong, take this line out and try it to see if it executes a trade.

if (Close1+CloseExtra* Point <= Val) //Initial Sell Procedure
reason being, your drawing a trendline that ends on the current Ask price and your comparing that to be more than close + close extra which will give you a value 50 points above the current Bid price so unless you have a spread of 50+ pips you wont get a trade, I havent tested all that but thats my guess to the cause of no trades
 
SDC:

I think your logic is wrong, take this line out and try it to see if it executes a trade.

If that happened there would be a message in the log/Experts tab . . .
 

why would there be a message if the logic is wrong ? if the code itself is correct it wouldnt generate any errors

 

The prb could be this:

if (BarCount != Bars 
Bars return max values in your charts. I use Time[0], instead.
 
WHRoeder:
  1. OrderSend won't work once i use variable to define StopLoss
    Always test return codes to find out WHY
  2. EAs must adjust for 4/5 digit brokers (TP, SL, AND slippage) On ECN brokers you must open first and THEN set stops
Thank you for your help WHRoeder: You hit the nail on the head with adding the order modify feature after the ticket was opened. Much appreciated.
 
Thanks everyone else for your suggestions, much appreciated.
Reason: