Buy and Sell Stop Orders

 

I've managed to open Buy and Sell Limit orders but not Buy and Sell Stop orders.  Can anyone tell what I'm doing wrong?

Thanks!

 //+------------------------------------------------------------------+
//| Custom_Test.mq4 |
//| me |
//| http://www.metatrader.info |
//+------------------------------------------------------------------+

#property copyright "me"
#property link "http://"  


extern string Settings_n_1 = "--------------------------";
extern int Side = 1;
extern int MP_Y = 0; 
extern int MP_X = 0;


int total, Ticket, Total, i, T, Buy = 0, Sell = 0, ZBLa, ZSLa, ZBLb, ZSLb, ZBLc, ZSLc, ZBLd, ZSLd, ZBLe, ZSLe, ZBLf, ZSLf, ZBLg, ZSLg, ZBLh, ZSLh, ZBLi, ZSLi, ZBLj, ZSLj, DNew, DOld, XHLB, XHLS, HLB, HLS, PB, PS, TB, TS, Tt, Bx = 0, Sx = 0, BLx = 0, SLx = 0, oBLx, oSLx, Ttx, Tx = 0, Bnx = 0, Snx = 0, Ttnx, PCNT =0, Cnt = 0, N = 0, BZ = 0, SZ = 0, BL = 0, SL = 0, TL = 0, CountZ, Count, oCount, CountX = 0, S, VH, VL, TVL, BFirstTicket, BFirstTicketI, BSecondTicket, SFirstTicket, SFirstTicketI, SSecondTicket, HighPoint = 0, LowPoint = 0, AU, AD, YU, YD, YUO, YDO, Tick = 0, LTM = 0, SLTM = 0, SAU = 0, SAD = 0, TAU, TAD, CNG, LCNG, XCNG, FXx, Xx, ZCNT = 0, ncenter, ocenter, CAy = 0, CBy = 0, CCy = 0, CDy = 0, CEy = 0, CFy = 0, CAz = 0, CBz = 0, CCz = 0, CDz = 0, CEz = 0, CFz = 0;
double vO, vH, vL, dB, dS, VX, VXb, SPDz, hSPDz, lSPDz, Lot, LotI, bid, ask, obid, oask, Mbx, Msx, Mmx, BStart, SStart, BFin, SFin, BTotal, STotal, TTotal = 0, ZTotal = 0, XTotal, YTotal = 0, oopen, Oldopen, TakePB, TakePS, TakeTPB, TakeTPS, OTakePB, OTakePS, TakePBX, TakePSX, TakeHBX, TakeHSX, Spread, PnvH, PnvL, oPnvH, oPnvL, FirstHL, HDiff, LDiff, BDiff, SDiff, LMkr, HMkr, Vol, BFirstPrice, BFirstPriceI, BSecondPrice, BFirstProfit, BFirstProfitI, BSecondProfit, BOldProfit, SFirstPrice, SFirstPriceI, SSecondPrice, SFirstProfit, SFirstProfitI, SSecondProfit, SOldProfit, CurBProfit, CurtBProfit, CurSProfit, CurtSProfit, NewProfit, DayHighI, DayLowI, WeekHighI, WeekLowI, MonthHighI, MonthLowI, BY, SY, HL;
string text, ArrowUpA, ArrowDownA, ArrowUpB, ArrowDownB, ArrowUpC, ArrowDownC, ArrowUpD, ArrowDownD, ArrowUpE, ArrowDownE, OHday, UpSymbol="ñ", DnSymbol="ò", NtSymbol="«";




//+------------------------------------------------------------------+
//| expert initialization function  |
//+------------------------------------------------------------------+

int init() 
{ObjectsDeleteAll();
//----
  
//---- 
return(0);  
}

//+------------------------------------------------------------------+
//| expert deinitialization function  |
//+------------------------------------------------------------------+
int deinit() 
{
//----
    
//---- 
return(0); 
} 



//+------------------------------------------------------------------+
//| expert start function |
//+------------------------------------------------------------------+
int start() 
{
//----    

      
      bool Order, Modify;
      Lot = 0.01;
      Ticket = OrderTicket(); 
      Total = OrdersTotal();
      Spread = ((Ask - Bid)*100000);
      double SpreadA = (Ask - Bid);      
      Tick = Tick + 1;

        double vC=iClose(NULL,5,0);    // 5 Minute Close


       double M=(Bid+0.00001);   //  BUY Limit Price
       double Nx=(Ask-0.00001);  //  SELL Limit Price

       
       double MS=(Ask-0.00001);  //  SELL Stop Price
       double NS=(Bid+0.00001);   //  BUY Stop Price
       


// ------------------------------------------------------------------------------ OPEN BUY -------------
       
        
       Alert("OPEN BUY STOP"); 
       Alert("Attempt to Open BUY Stop. Waiting for Response..."); 
       //Alert(" Spread =  " + DoubleToStr(NormalizeDouble(Spread,5),0));
     //  Alert(" SpreadA =   " + DoubleToStr(SpreadA,Digits));
  
           // Open Bracket 2
       RefreshRates(); 
       //T = T-1;
       
       double TakeTPBS = (Bid+0.00002);
       
       Alert("Take Profit = ",DoubleToStr(TakeTPBS,Digits));
       
       Order = OrderSend(Symbol(),OP_BUYSTOP,Lot,MS,2,0,TakeTPBS);       
       Tt = GetLastError();
     
       if(Cnt > 2)
       {Cnt = 0; return(0);}      
       
       if (Tt > 0)
       {Order = false; Alert("Break Buy Here!            ",Tt,"   Bid=   ",DoubleToStr(Bid,Digits),"   Ask=   ",DoubleToStr(Ask,Digits),"   MS =  ",DoubleToStr(MS,Digits),"   Nx =  ",DoubleToStr(Nx,Digits),"   T =  ",T); Sleep (5999); RefreshRates(); Cnt = Cnt + 1;
        Alert (GetLastError());}
       
       
       
       if (OrderSelect(0, SELECT_BY_POS)==True)
       {Buy = 1;       // Open Bracket 3
        Alert ("Opened - Buy Order  ",OrderTicket(),"    TP =  ",(DoubleToStr(TakeTPBS, Digits)),"    Ask =  ",DoubleToStr(Ask,Digits),"   MS =  ",DoubleToStr(MS,Digits),"   Nx =  ",DoubleToStr(Nx,Digits),"   T =  ",T);
        }    
      
 
       Alert("OPEN BUY LIMIT"); 
       Alert("Attempt to Open BUY Limit. Waiting for Response..."); 
      // Alert(" Spread =  " + DoubleToStr(NormalizeDouble(Spread,5),0));
      // Alert(" SpreadA =   " + DoubleToStr(SpreadA,Digits));
  
           // Open Bracket 2
       RefreshRates(); 
       //T = T-1;

       
       TakeTPB = (Bid+0.00002);
       
       Alert("Take Profit = ",DoubleToStr(TakeTPB,Digits));
       
       Order = OrderSend(Symbol(),OP_BUYLIMIT,Lot,M,2,0,TakeTPB);       
       Tt = GetLastError();
     
       if(Cnt > 2)
       {Cnt = 0; return(0);}      
       
       if (Tt > 0)
       {Order = false; Alert("Break Buy Here!            ",Tt,"   Bid=   ",DoubleToStr(Bid,Digits),"   Ask=   ",DoubleToStr(Ask,Digits),"   M =  ",DoubleToStr(M,Digits),"   Nx =  ",DoubleToStr(Nx,Digits),"   T =  ",T); Sleep (5999); RefreshRates(); Cnt = Cnt + 1;
        Alert (GetLastError());}
       
       
       
       if (OrderSelect(0, SELECT_BY_POS)==True)
       {Buy = 1;       // Open Bracket 3
        Alert ("Opened - Buy Order  ",OrderTicket(),"    TP =  ",(DoubleToStr(TakeTPB, Digits)),"    Ask =  ",DoubleToStr(Ask,Digits),"   M =  ",DoubleToStr(M,Digits),"   Nx =  ",DoubleToStr(Nx,Digits),"   T =  ",T);
        }










       Alert("OPEN SELL STOP"); 
       Alert("Attempt to Open SELL Stop. Waiting for Response..."); 
      // Alert(" Spread =  " + DoubleToStr(NormalizeDouble(Spread,5),0));
     //  Alert(" SpreadA =   " + DoubleToStr(SpreadA,Digits));
 
       {       
       RefreshRates(); 

       double TakeTPSS = (Ask-0.00002);
       
       Order = OrderSend(Symbol(),OP_SELLLIMIT,Lot,NS,2,0,TakeTPS);       
       Tt = GetLastError();
     
       if(Cnt > 2)
       {Cnt = 0; return(0);}      
       
       if (Tt > 0)
       {Order = false; Alert("Break Sell Here!            ",Tt,"   Bid=   ",DoubleToStr(Bid,Digits),"   Ask=   ",DoubleToStr(Ask,Digits),"   M =  ",DoubleToStr(M,Digits),"   NS =  ",DoubleToStr(NS,Digits),"   T =  ",T); Sleep (5999); RefreshRates(); Cnt = Cnt + 1;
        Alert (GetLastError());}
       
       
       
       if (OrderSelect(0, SELECT_BY_POS)==True)
       
       {Sell = 1;       // Open Bracket 3
       Alert ("Opened - Sell Order  ",OrderTicket(),"    TP =  ",(DoubleToStr(TakeTPSS, Digits)),"    Bid =  ",DoubleToStr(Bid,Digits),"   M =  ",DoubleToStr(M,Digits),"   NS =  ",DoubleToStr(NS,Digits),"   T =  ",T);
       }             
       }             




       Alert("OPEN SELL LIMIT"); 
       Alert("Attempt to Open SELL Limit. Waiting for Response..."); 
       //Alert(" Spread =  " + DoubleToStr(NormalizeDouble(Spread,5),0));
       //Alert(" SpreadA =   " + DoubleToStr(SpreadA,Digits));
 
       {       
       RefreshRates(); 

       TakeTPS = (Ask-0.00002);
       
       Order = OrderSend(Symbol(),OP_SELLLIMIT,Lot,Nx,2,0,TakeTPS);       
       Tt = GetLastError();
     
       if(Cnt > 2)
       {Cnt = 0; return(0);}      
       
       if (Tt > 0)
       {Order = false; Alert("Break Sell Here!            ",Tt,"   Bid=   ",DoubleToStr(Bid,Digits),"   Ask=   ",DoubleToStr(Ask,Digits),"   M =  ",DoubleToStr(M,Digits),"   Nx =  ",DoubleToStr(Nx,Digits),"   T =  ",T); Sleep (5999); RefreshRates(); Cnt = Cnt + 1;
        Alert (GetLastError());}
       
       
       
       if (OrderSelect(0, SELECT_BY_POS)==True)
       
       {Sell = 1;       // Open Bracket 3
       Alert ("Opened - Sell Order  ",OrderTicket(),"    TP =  ",(DoubleToStr(TakeTPS, Digits)),"    Bid =  ",DoubleToStr(Bid,Digits),"   M =  ",DoubleToStr(M,Digits),"   Nx =  ",DoubleToStr(Nx,Digits),"   T =  ",T);
       }             
       }             






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


//+------------------------------------------------------------------+


 // Write Procedure


 
Yellowbeard:

Can anyone tell what I'm doing wrong?


nothing
 
When I execute, I get error 130 ( invalid stops ) on Buy and Sell stop orders, only.
 

if you manage to open a limit order,

there is no way to open a stop orders

and vice versa

and of course you gonna get error 130


 

If I can manually place Buy and / or Sell Limit orders above and below the Ask and Bid respectively, at the same time; how is it that, I can't place them the same way, with an EA?


Thanks!

 
ye, but you can't place manually at the very same place (where you placed the limit order) a "stop order"
 
I actually don't need to place stop orders.  I just wanted to place a series of limit orders below the Bid ( Buys ) and above the Ask  ( Sells ), spaced at a certain pips distance apart.  I just took it that, a stop order was actually a type of limit order, which is placed above the Ask for Buy and below the Bid for Sell.
 

Yellowbeard:

a stop order was actually a type of limit order,


absolutely not the same


A buy–limit order can only be executed at the limit price or lower. For example, if an investor wants to buy a stock, but doesn't want to pay more than $20 for it, the investor can place a limit order to buy the stock at $20. By entering a limit order rather than a market order, the investor will not buy the stock at a higher price, but, may get fewer shares than he wants or not get the stock at all.

A sell–limit order is analogous; it can only be executed at the limit price or higher.

Both buy and sell orders can be additionally constrained. Two of the most common additional constraints are fill or kill (FOK) and all or none (AON). FOK orders are either filled completely on the first attempt or canceled outright, while AON orders stipulate that the order must be filled with the entire number of shares specified, or not filled at all. If it is not filled, it is still held on the order book for later execution.

A sell–stop order is an instruction to sell at the best available price after the price goes below the stop price. A sell–stop price is always below the current market price. For example, if an investor holds a stock currently valued at $50 and is worried that the value may drop, he/she can place a sell–stop order at $40. If the share price drops to $40, the broker sells the stock at the next available price. This can limit the investor's losses (if the stop price is at or above the purchase price) or lock in some of the investor's profits.

A buy–stop order is typically used to limit a loss (or to protect an existing profit) on a short sale. A buy-stop price is always above the current market price. For example, if an investor sells a stock short—hoping for the stock price to go down so they can return the borrowed shares at a lower price (i.e., covering)—the investor may use a buy stop order to protect against losses if the price goes too high. It can also be used to advantage in a declining market when you want to enter a long position close to the bottom after turnaround.


read more

 
Thanks!  For the info and clarity!
 
Yellowbeard: I've managed to open Buy and Sell Limit orders but not Buy and Sell Stop orders.  Can anyone tell what I'm doing wrong?
double M=(Bid+0.00001);   //  BUY Limit Price
double Nx=(Ask-0.00001);  //  SELL Limit Price

double MS=(Ask-0.00001);  //  SELL Stop Price
double NS=(Bid+0.00001);   //  BUY Stop Price
  1. Trying to open a Buy Limit above market.
    Trying to open a Sell Limit below market.
    Trying to open a Buy Stop below market.
    Trying to open a Sell Stop below market.
  2. Not checking that your pending price differs from market by at least MarketInfo(market_pair, MODE_STOPLEVEL) * Point
Reason: