Need to stop this looping just want it to open 2 pending orders, one one is filled delete the other one and then not open any more until dropped on chart again.
Code here.
//+------------------------------------------------------------------+//| Open Two Pend Orders.mq4 |//+------------------------------------------------------------------+#property copyright""#property link""#property version""#include <stdlib.mqh>
//---inputbool DynamicLotSize =true; //Use Money Managementinputdouble EquityPercent = 2; //Risk Percentinputdouble FixedLotSize = 0.1; //Fixed LotSizeinputdouble StopLoss = 100; //StopLoss( in Points)inputdouble TakeProfit = 300; //TakeProfit( in Points)inputdouble TrailingStop = 50; //TrailingStop(in Points)inputdouble PipsAway = 50; //points away from the current Bid & Askinputdouble Slippage = 30; //Slippageinputdouble Magic = 1111; //Magic Numberdouble LotSize;
int ticket1;
int ticket2;
int t=0;
//---//+------------------------------------------------------------------+//| expert initialization function |//+------------------------------------------------------------------+int init()
{
//----//----return(0);
}
//+------------------------------------------------------------------+//| expert deinitialization function |//+------------------------------------------------------------------+int deinit()
{
//----//----return(0);
}
//+------------------------------------------------------------------+//| expert start function |//+------------------------------------------------------------------+int start()
{
//---Detect Open or Pending Ordersint total=OrdersTotal();
t=0;
for(int i=total-1;i>=0;i--)
{
OrderSelect(i,SELECT_BY_POS);
int type=OrderType();
if(( OrderSymbol()==Symbol()) && (OrderMagicNumber()==Magic))
{
switch(type)
{
caseOP_BUY : t=1;
caseOP_SELL : t=1;
caseOP_BUYLIMIT : t=1;
caseOP_BUYSTOP : t=1;
caseOP_SELLLIMIT : t=1;
caseOP_SELLSTOP : t=1;
}
}
}
if(t<1)
{
//Lot Size Calculationif(DynamicLotSize==true)
{
double RiskAmount= AccountEquity() *(EquityPercent/100);
double TickValue = MarketInfo(Symbol(),MODE_TICKVALUE);
if(Point==0.001 || Point==0.00001) TickValue*=10;
double CalcLots=(RiskAmount/StopLoss)/TickValue;
LotSize=CalcLots;
}
else LotSize=FixedLotSize;
// Lot size verificationif(LotSize<MarketInfo(Symbol(),MODE_MINLOT))
{
LotSize=MarketInfo(Symbol(),MODE_MINLOT);
}
elseif(LotSize>MarketInfo(Symbol(),MODE_MAXLOT))
{
LotSize=MarketInfo(Symbol(),MODE_MAXLOT);
}
if(MarketInfo(Symbol(),MODE_LOTSTEP)==0.1)
{
LotSize=NormalizeDouble(LotSize,1);
}
else LotSize=NormalizeDouble(LotSize,2);
//Open two Pending Orders simultaneouslydouble t1 = Ask + PipsAway * Point; //BuyStop Entry Pointdouble t2 = Bid - PipsAway * Point; //SellStop Entry Pointdouble sl1 = t1 - StopLoss * Point; //BuyStop Stoplossdouble sl2 = t2 + StopLoss * Point; //SellStop Stoplossdouble tp1 = t1 + TakeProfit * Point; //BuyStop TakeProfitdouble tp2 = t2 - TakeProfit * Point; //SellStop TakeProfit
ticket1 = OrderSend( Symbol(),OP_BUYSTOP,LotSize,t1, Slippage,sl1,tp1,"ZAY",Magic,0,White);
ticket2 = OrderSend( Symbol(),OP_SELLSTOP,LotSize,t2, Slippage, sl2,tp2,"ZAY",Magic,0,White);
}
for(int j=0; j<OrdersTotal(); j++)
{
//---if opened order is "Buy", close another pending order "Sellstop" and use trailing stop for opened "Buy" orderOrderSelect(ticket1,SELECT_BY_TICKET);
if(OrderType()==OP_BUY)
{
// delete pending orderOrderDelete(ticket2);
// use Trailling Stopif(Bid-OrderOpenPrice()>Point*TrailingStop)
{
if(OrderStopLoss()<Bid-Point*TrailingStop)
{
OrderModify(OrderTicket(),OrderOpenPrice(),Bid-Point*TrailingStop,OrderTakeProfit(),0,Green);
}
}
}
//---if opened order is "Sell", close another pending order "Buystop" and use trailing stop for opened "Sell" orderOrderSelect(ticket2,SELECT_BY_TICKET);
if(OrderType()==OP_SELL)
{
// delete pending orderOrderDelete(ticket1);
// use Trailling Stopif((OrderOpenPrice()-Ask)>(Point*TrailingStop))
{
if(OrderStopLoss()>(Ask+Point*TrailingStop))
{
OrderModify(OrderTicket(),OrderOpenPrice(),Ask+Point*TrailingStop,OrderTakeProfit(),0,Red);
}
}
}
}
return(0);
}
//+------------------------------------------------------------------+
Allow the use of cookies to log in to the MQL5.com website.
Please enable the necessary setting in your browser, otherwise you will not be able to log in.
You are signed in. Two-factor authentication has been disabled. Set it up again and scan a new QR code, or continue with two-factor authentication disabled.
Need to stop this looping just want it to open 2 pending orders, one one is filled delete the other one and then not open any more until dropped on chart again.
Code here.
Thanks in advance, novice programmer!