int start()
{
if (CountTrades() == 0)
{
TP = NormalizeDouble(Ask + TakeProfit * Point, Digits);
SL = NormalizeDouble(Ask - StopLoss * Point, Digits);
OrderSend(Symbol(), OP_BUY, Lots, Ask, Slippage, SL, TP, NULL, Magic, 0, CLR_NONE);
{
for (int i = OrdersHistoryTotal()-1; i>=0; i--)
{
if (OrderSelect(i, SELECT_BY_POS, MODE_HISTORY))
{
if (OrderSymbol() == Symbol() && OrderMagicNumber() == Magic)
{
if ( OrderType()==OP_BUY)
{
if (StringFind(OrderComment(),"sl")>=0)
{
lastlot = FindLastLots(OP_BUY);
lastlot = NormalizeDouble(lastlot * Multiplier, 2);
TP = NormalizeDouble(Bid - TakeProfit * Point, Digits);
SL = NormalizeDouble(Bid + StopLoss * Point, Digits);
OrderSend(Symbol(), OP_SELL, lastlot, Bid, Slippage, SL, TP, NULL, Magic, 0, CLR_NONE);
{
if (StringFind(OrderComment(),"tp")>=0)
{
TP = NormalizeDouble(Ask + TakeProfit * Point, Digits);
SL = NormalizeDouble(Ask - StopLoss * Point, Digits);
OrderSend(Symbol(), OP_BUY, Lots, Ask, Slippage, SL, TP, NULL, Magic, 0, CLR_NONE);
}
}
}
}
if(OrderType()==OP_SELL)
{
if(StringFind(OrderComment(),"sl")>=0)
{
lastlot = FindLastLots(OP_SELL);
lastlot = NormalizeDouble(lastlot * Multiplier, 2);
TP = NormalizeDouble(Ask + TakeProfit * Point, Digits);
SL = NormalizeDouble(Ask - StopLoss * Point, Digits);
OrderSend(Symbol(), OP_BUY, lastlot, Ask, Slippage, SL, TP, NULL, Magic, 0, CLR_NONE);
{
if (StringFind(OrderComment(),"tp")>=0)
{
TP = NormalizeDouble(Bid - TakeProfit * Point, Digits);
SL = NormalizeDouble(Bid + StopLoss * Point, Digits);
OrderSend(Symbol(), OP_SELL, Lots, Bid, Slippage, SL, TP, NULL, Magic, 0, CLR_NONE);
}
}
}
}
}
}
}
}
}
return(0);
}
//+------------------------------------------------------------------+
int CountTrades()
{
int count = 0;
for (int i=OrdersTotal()-1; i>=0; i--)
{
if(OrderSelect(i,SELECT_BY_POS, MODE_TRADES))
{
if(OrderSymbol() == Symbol() && OrderMagicNumber() == Magic)
count++;
}
}
return(count);
}
//+------------------------------------------------------------------+
double FindLastLots(int otype)
{
double oldlots;
int oldticket;
ticket = 0;
for(int i=OrdersHistoryTotal()-1; i>=0; i--)
{
if(OrderSelect(i, SELECT_BY_POS, MODE_HISTORY))
{
if (OrderSymbol() == Symbol() && OrderMagicNumber() == Magic)
{
oldticket = OrderTicket();
if(oldticket > ticket)
{
oldlots = OrderLots();
ticket = oldticket;
}
}
}
}
return(oldlots);
}