//+-----------------------------------------------------------------------------+
//|                              Place pending on last swing high or low v3.mq4 |
//|                                                               Edward Munroe |
//|                                                  Hugh Briss @ Forex Factory |
//+-----------------------------------------------------------------------------+
#property copyright "Edward Munroe"

#property show_inputs

extern double manuallotsize = 0;
extern double risk = 50.0;
extern int buyorsell = 1;
extern int demarktrendsetting = 1;
extern bool deletepending = false;
extern double atrmult = 2.0;

double atrstop, atr, atrcalc, lotcalc, lotsize, sellstop, buystop, sellstoploss, buystoploss;
int lowestshift, highestshift;

int start()
{
if(deletepending == true)
{
// Delete pending orders on this pair only
for(int counter1 = 0; counter1 <= OrdersTotal()-1; counter1++)
{
OrderSelect(counter1, SELECT_BY_POS, MODE_TRADES);
if((OrderSymbol() == Symbol() && OrderType() == OP_BUYSTOP) || (OrderSymbol() == Symbol() && OrderType() == OP_SELLSTOP))
{
OrderDelete(OrderTicket());
}
}
}

atr = iATR(NULL,0,20,1); atrcalc = atr * atrmult; atrstop = NormalizeDouble(atrcalc,0);
lotcalc = risk/atrstop; lotsize = NormalizeDouble(lotcalc,1);
if(manuallotsize > 0) lotsize = manuallotsize;

if(buyorsell == 0)
{
lowestshift = iLowest(NULL,0,MODE_LOW,demarktrendsetting,0); 
sellstop = Low[lowestshift] - 1; sellstoploss = sellstop + atrstop;
OrderSend(Symbol(),OP_SELLSTOP,lotsize,sellstop,100,sellstoploss,0,0,0,0,CLR_NONE);
}

if(buyorsell == 1)
{
highestshift = iHighest(NULL,0,MODE_HIGH,demarktrendsetting,0);
buystop = High[highestshift] + 1 + (MarketInfo(Symbol(),MODE_SPREAD)/100); buystoploss = buystop - atrstop;
OrderSend(Symbol(),OP_BUYSTOP,lotsize,buystop,100,buystoploss,0,0,0,0,CLR_NONE);
}

return(0);
}





