int BuyCnt, SellCnt;
void RecountOrders()
{
BuyCnt = 0;
SellCnt = 0;
int cnt = OrdersTotal();
for (int i=0; i < cnt; i++)
{
if (!OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) continue;
if (OrderSymbol() != Symbol()) continue;
int type = OrderType();
if (type == OP_BUY) BuyCnt++;
if (type == OP_SELL) SellCnt++;
}
}
RecountOrders();
if (BuyCnt+SellCnt > 0) return;
int Slippage = 3;
CloseOrders();
void CloseOrders()
{
int cnt = OrdersTotal();
for (int i=cnt-1; i>=0; i--)
{
if (!OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) continue;
if (OrderSymbol() != Symbol()) continue;
int type = OrderType();
if (type == OP_BUY)
{
OrderClose(OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_BID), Slippage);
}
if (type == OP_SELL)
{
OrderClose(OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_ASK), Slippage);
}
}
}
void NextDay(int& day, int& month, int& year)
{
datetime Time0 = TimeCurrent();
datetime Tomorrow = Time0 + 24*60*60;
day = TimeDayOfYear(Tomorrow);
month = TimeMonth(Tomorrow);
year = TimeYear(Tomorrow);
}
int day, month, year;
NextDay(day, month, year);
bool LastOrderSelect(int pool, int type1, int type2 = -1)
{
datetime tm = -1;
int ticket = -1;
if (pool == MODE_TRADES)
{
int cnt = OrdersTotal();
for (int i=0; i < cnt; i++)
{
if (!OrderSelect(i, SELECT_BY_POS, pool)) continue;
if (OrderSymbol() != Symbol()) continue;
if (OrderMagicNumber() != Magic) continue;
int type = OrderType();
if (type == type1 || type == type2)
{
if (OrderOpenTime() > tm)
{
tm = OrderOpenTime();
ticket = OrderTicket();
}
}
}
return (OrderSelect(ticket, SELECT_BY_TICKET));
}
if (pool == MODE_HISTORY)
{
cnt = OrdersHistoryTotal();
for (i=0; i < cnt; i++)
{
if (!OrderSelect(i, SELECT_BY_POS, pool)) continue;
if (OrderSymbol() != Symbol()) continue;
if (OrderMagicNumber() != Magic) continue;
type = OrderType();
if (type == type1 || type == type2)
{
if (OrderCloseTime() > tm)
{
tm = OrderCloseTime();
ticket = OrderTicket();
}
}
}
return (OrderSelect(ticket, SELECT_BY_TICKET));
}
return (false);
}
if (LastOrderSelect(MODE_HISTORY, OP_BUY, OP_SELL))
{
Print("OrderType: ", OrderType());
}
extern bool TrailingOn = true;
extern int TrailingStart = 30;
extern int TrailingSize = 30;
void TrailPositions()
{
int cnt = OrdersTotal();
for (int i=0; i<cnt; i++)
{
if (!OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) continue;
if (OrderSymbol() != Symbol()) continue;
if (OrderMagicNumber() != Magic) continue;
int type = OrderType();
if (type == OP_BUY)
{
if (Bid-OrderOpenPrice() > TrailingStart*Point)
{
if (OrderStopLoss() < Bid - (TrailingSize+1)*Point)
{
OrderModify(OrderTicket(), OrderOpenPrice(), Bid-TrailingSize*Point, OrderTakeProfit(), 0);
}
}
}
if (type == OP_SELL)
{
if (OrderOpenPrice()-Ask > TrailingStart*Point)
{
if (OrderStopLoss() > Ask + (TrailingSize+1)*Point || OrderStopLoss() == 0)
{
OrderModify(OrderTicket(), OrderOpenPrice(), Ask+TrailingSize*Point, OrderTakeProfit(), 0);
}
}
}
}
}
if (TrailingOn) TrailPositions();
FinGeR wrote:@RickD
What is your opinion about the upcoming new MQL5?
FinGeR wrote:How to program in MQL4 Pin formation ?
RickD wrote:Standard trailing:
- Code: Select all
extern bool TrailingOn = true;
extern int TrailingStart = 30;
extern int TrailingSize = 30;
void TrailPositions()
{
int cnt = OrdersTotal();
for (int i=0; i<cnt; i++)
{
if (!OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) continue;
if (OrderSymbol() != Symbol()) continue;
if (OrderMagicNumber() != Magic) continue;
int type = OrderType();
if (type == OP_BUY)
{
if (Bid-OrderOpenPrice() > TrailingStart*Point)
{
if (OrderStopLoss() < Bid - (TrailingSize+1)*Point)
{
OrderModify(OrderTicket(), OrderOpenPrice(), Bid-TrailingSize*Point, OrderTakeProfit(), 0);
}
}
}
if (type == OP_SELL)
{
if (OrderOpenPrice()-Ask > TrailingStart*Point)
{
if (OrderStopLoss() > Ask + (TrailingSize+1)*Point || OrderStopLoss() == 0)
{
OrderModify(OrderTicket(), OrderOpenPrice(), Ask+TrailingSize*Point, OrderTakeProfit(), 0);
}
}
}
}
}
Usage:
- Code: Select all
if (TrailingOn) TrailPositions();
Users browsing this forum: No registered users and 1 guest