///+-----------------------------------------------------------------+ //| Original idea from: BUY-SELL STOP.mq4 | //| Copyright © 2008, Guillermo Arango | //| williama@mail2airforce.com | //+------------------------------------------------------------------+ #property copyright "Copyright © 2008, Rob Turcotte" #property link "acceleratorfx@gmail.com" #property show_inputs extern int Level = 10; extern double Lots = 0.1; extern int TakeProfit=140; extern bool Place_buylimit = true; extern bool Place_selllimit = true; extern int GridSpacing=35; extern double GridUpperLimit=0; extern double GridLowerLimit=0; extern double StartPoint= 202.45; extern double MagicNum = 1001; int totalOpenOrders = 0; bool OpenOrders = false; int total, cnt, totalBuyStop, totalSellStop, totalBuy, totalSell, TSS, TBS; double spread; //+------------------------------------------------------------------+ //| expert start function | //+------------------------------------------------------------------+ int start() { //----Establish if Stop Order filled total = OrdersTotal(); totalBuyStop = 0; totalSellStop = 0; totalSell = 0; totalBuy = 0; for(cnt = 0; cnt < total; cnt++) { OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES); if(OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNum) { if(OrderType() == OP_BUYSTOP) totalBuyStop++; if(OrderType() == OP_BUY) totalBuy++; } } for(cnt = 0; cnt < total; cnt++) { OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES); if(OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNum) { if(OrderType() == OP_SELLSTOP) totalSellStop++; if(OrderType() == OP_SELL) totalSell++; } } TSS = totalSellStop; TBS = totalBuyStop; Comment("TSS=", TSS, " TBS=", TBS); if(totalOpenOrders < (totalBuy + totalSell)) // Order just opened, open more pending orders { totalOpenOrders = totalBuy + totalSell; OpenPendingOrders(); } if(totalOpenOrders > 0) OpenOrders = true; if(OpenOrders == true && (totalBuy + totalSell) == 0) // all orders closed, close pending orders { ClosePendingOrders(); totalOpenOrders = 0; } if(totalBuyStop == 0 && totalSellStop == 0) //no open orders or pending orders, start cycle again { StartPoint = Ask; OpenPendingOrders(); } return(0); } //+------------------------------------------------------------------+ //+------------------------------------------------------------------+ //| script "send pending order with expiration data" | //+------------------------------------------------------------------+ int OpenPendingOrders() { int ticket; double CurrentLevel=0.00; double point; int TakeProfit1=TakeProfit; double StartPoint1=StartPoint; double GridUpperLimit= StartPoint1 + ((TakeProfit1+8)*Point); double GridLowerLimit= StartPoint1 - (TakeProfit1*Point); //---- spread = MarketInfo(Symbol(), MODE_SPREAD); point=MarketInfo(Symbol(),MODE_POINT); int total; total=OrdersTotal(); //---- CurrentLevel = GridLowerLimit; while(CurrentLevel < GridUpperLimit) while(true) { if (Place_selllimit) if(CurrentLevel < (Bid - (Point * Level))) ticket=OrderSend(Symbol(),OP_SELLSTOP,Lots,CurrentLevel,0,GridUpperLimit,GridLowerLimit+(spread*Point),"SELLSTOP",MagicNum,0,Red); if (Place_buylimit) if(CurrentLevel > (Ask + (Point * Level)) && CurrentLevel < (Ask + (Point * TakeProfit))) //ONLY BUY ABOVE ASK PRICE ticket=OrderSend(Symbol(),OP_BUYSTOP,Lots,CurrentLevel,0,GridLowerLimit,GridUpperLimit-(spread*Point),"BUYSTOP",MagicNum,0,Green); CurrentLevel = CurrentLevel + (GridSpacing * Point); if(CurrentLevel > GridUpperLimit) break; } //---- return(0); } //+------------------------------------------------------------------+ //+------------------------------------------------------------------+ // Close Pending Orders int ClosePendingOrders() { int total = OrdersTotal(); for(int i=total-1;i>=0;i--) { OrderSelect(i, SELECT_BY_POS); int type = OrderType(); bool result = false; if(OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNum) { switch(type) { //Close pending orders case OP_BUYLIMIT : case OP_BUYSTOP : case OP_SELLLIMIT : case OP_SELLSTOP : result = OrderDelete( OrderTicket() ); } OpenOrders = false; if(result == false) { Alert("Order " , OrderTicket() , " failed to close. Error:" , GetLastError() ); Sleep(3000); } } } return(0); }