//+------------------------------------------------------------------+ //| Stub.mq4 | //| | //| set SS and BS base on actual price.. //| but when the prices change (either down or up), //| the BS and SS also follow the price.. //| //| ex: //| //| actual price EU : 1.3000 //| set 10pips SS and BS : ss 1.2990 and BS 1.3010 //| set TP 20pip : SS 1.2970 BS 1.3030 //| set SL 10pip : SS 1.3000 and BS 1.3000 //| //| but when the price change to 1.3025, the SS,BS,TP and SL also auto change to : //| SS 1.3015 (SL 1.3025 TP 1.2995)and BS 1.3035 (SL 1.3025 TP 1.3055) //+------------------------------------------------------------------+ #include #include extern int BS_SS_Gap = 10; extern int StopLoss = 20; extern int TakeProfit = 10; extern double Lots = 0.1; double myPoint; int buy.1,sell.1,ticket; string TradeSymbol; double BuyStopPrice, SellStopPrice; double STprice, TPprice; double spread; //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ int init() { myPoint = SetPoint(Symbol()); return(0); } int start() { TradeSymbol=Symbol(); if(TotalTradesThisEA()==0) { buy.1 = OpenBuyStop(); sell.1 = OpenSellStop(); } RefreshRates(); UpdateOrders(); return(0); } int OpenBuyStop() { int err; spread = Ask - Bid; BuyStopPrice = Bid + BS_SS_Gap * Point; TPprice = 0; if (TakeProfit > 0) TPprice = BuyStopPrice + spread + TakeProfit * myPoint; STprice = 0; if (StopLoss > 0) STprice = BuyStopPrice + spread - StopLoss * myPoint; ticket=OrderSend(Symbol(), OP_BUYSTOP, Lots, BuyStopPrice + spread, 0, STprice, TPprice, "", 101, 0, Green); if(ticket>0) { if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) { Print(ticket); return(ticket); } } else { err = GetLastError(); Print("Error Opening BuyStop Order: ",err, ErrorDescription(err)); return(0); } } int OpenSellStop() { int err; spread = Ask - Bid; SellStopPrice = Bid - BS_SS_Gap * Point; TPprice = 0; if (TakeProfit > 0) TPprice = SellStopPrice -spread - TakeProfit * myPoint; STprice = 0; if (StopLoss > 0) STprice = SellStopPrice - spread + StopLoss * myPoint; ticket=OrderSend(Symbol(), OP_SELLSTOP, Lots, SellStopPrice - spread, 0, STprice, TPprice, "", 102, 0, Red); if(ticket>0) { if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) { Print(ticket); return(ticket); } } else { err = GetLastError(); Print("Error Opening BuyStop Order: ",err, ErrorDescription(err)); return(0); } } void UpdateOrders() { spread = Ask - Bid; BuyStopPrice = Bid + spread + BS_SS_Gap * Point; SellStopPrice = Bid - spread - BS_SS_Gap * Point; for(int c=0;c 0) TPprice = BuyStopPrice + TakeProfit * myPoint; STprice = 0; if (StopLoss > 0) STprice = BuyStopPrice - StopLoss * myPoint; OrderModify(OrderTicket(), BuyStopPrice, STprice, TPprice, 0, Aqua); } } if(OrderType()==OP_SELLSTOP) { if (OrderOpenPrice() != SellStopPrice) { TPprice = 0; if (TakeProfit > 0) TPprice = SellStopPrice - TakeProfit * myPoint; STprice = 0; if (StopLoss > 0) STprice = SellStopPrice + StopLoss * myPoint; OrderModify(OrderTicket(), SellStopPrice, STprice, TPprice, 0, HotPink); } } } } } //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ int TotalTradesThisEA() { int i, TradesThisSymbol=0; for(i=0;i