//+------------------------------------------------------------------+
//| Expert tick function |
//+------------------------------------------------------------------+
void OnTick()
{
if (last_open>=iTime(NULL, TF, 0))
return;
else
{
TradeMap();
if (pr>ProfitClose)
CloseAll();
//---
if(b>MaxOrdersLimit)
if(OrderClose (BuyPriceMaxTic, BuyPriceMaxLot, Bid , Slippage, clrBlue))
if(s>MaxOrdersLimit)
if(OrderClose (SelPriceMinTic, SelPriceMinLot, Ask , Slippage, clrRed))
//---
open = iOpen (symb,TF,1);
close = iClose(symb,TF,1);
high = iHigh (symb,TF,1);
low = iLow (symb,TF,1);
if (close>open)
PutOrder(OP_BUY);
if (close<open)
PutOrder(OP_SELL);
if (close==open)
{
if (high-open > open-low)
PutOrder(OP_SELL);
if (high-open < open-low)
PutOrder(OP_BUY);
}
last_open=iTime(NULL, TF, 0);
}
}
//+------------------------------------------------------------------+
//| Function |
//+------------------------------------------------------------------+
bool PutOrder(int dir)
{
op = NP((dir==OP_BUY)?Ask:Bid); // цена открытия
lot = AccountFreeMargin()*Risk/100/MarketInfo(Symbol(), MODE_MARGINREQUIRED);
//-------------------------------------------------------------//
if(lot>MaximalLots)
lot=MaximalLots;
lot=NL(lot);
if(!ValidLot(lot))
return(false);
//-------------------------------------------------------------//
int ticket = OrderSend(Symbol(), dir, lot, op, int(Slippage), 0, 0, NULL, MagicNumber);
if (ticket<=0)
{
Print("OrderSend "+Symbol()+" lot="+DoubleToString(lot, 2)+" error "+string(GetLastError()));
return(false); // выходим при ошибке
}
return(true);
}
//+------------------------------------------------------------------+
bool ValidLot(double volume)
{
//--- минимально допустимый объем для торговых операций
double min_volume=SymbolInfoDouble(Symbol(),SYMBOL_VOLUME_MIN);
if(volume<min_volume)
return(false);
//--- максимально допустимый объем для торговых операций
double max_volume=SymbolInfoDouble(Symbol(),SYMBOL_VOLUME_MAX);
if(volume>max_volume)
return(false);
//--- получим минимальную градацию объема
double volume_step=SymbolInfoDouble(Symbol(),SYMBOL_VOLUME_STEP);
int ratio=(int)MathRound(volume/volume_step);
if(MathAbs(ratio*volume_step-volume)>0.0000001)
return(false);
return(true);
}
//+------------------------------------------------------------------+
bool CloseAll()
{
for(int i=OrdersTotal()-1;i>=0;i--)
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
if(OrderMagicNumber()==MagicNumber)
if(OrderSymbol()==Symbol())
if(OrderProfit()>0)
{
if(OrderType()==OP_BUY)
if(!OrderClose(OrderTicket(),OrderLots(),Bid,Slippage))
Sleep(500);
if(OrderType()==OP_SELL)
if(!OrderClose(OrderTicket(),OrderLots(),Ask,Slippage))
Sleep(500);
}
for(int i=OrdersTotal()-1;i>=0;i--)
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
if(OrderMagicNumber()==MagicNumber)
if(OrderSymbol()==Symbol())
// if(OrderProfit()<=0)
{
if(OrderType()==OP_BUY)
if(!OrderClose(OrderTicket(),OrderLots(),Bid,Slippage))
Sleep(500);
if(OrderType()==OP_SELL)
if(!OrderClose(OrderTicket(),OrderLots(),Ask,Slippage))
Sleep(500);
}
return true;
}
//+------------------------------------------------------------------+
double NP(double d) { double TickSize=MarketInfo(Symbol(), MODE_TICKSIZE); if (TickSize==0) TickSize=1; return ND(MathRound(d/TickSize)*TickSize); }
//+------------------------------------------------------------------+
double ND(double d, int n=-1)
{
if (n<0)
return NormalizeDouble(d, Digits);
return NormalizeDouble(d, n);
}
//+------------------------------------------------------------------+
double NL(double _lot) // нормализация лота
{
double LotStep=MarketInfo(Symbol(), MODE_LOTSTEP);
int k=0;
if (LotStep<=0.001) k=3; else if (LotStep<=0.01) k=2; else if (LotStep<=0.1) k=1; // шаг лота
double MinLot=MarketInfo(Symbol(), MODE_MINLOT);
double MaxLot=MarketInfo(Symbol(), MODE_MAXLOT);
return ND(MathMin(MaxLot, MathMax(MinLot, _lot)), k); // венули нормализованный лот
}
//+------------------------------------------------------------------+
void TradeMap()
{
BuyPriceMax=0;BuyPriceMin=0;BuyPriceMaxLot=0;BuyPriceMinLot=0;
SelPriceMin=0;SelPriceMax=0;SelPriceMinLot=0;SelPriceMaxLot=0;
BuyPriceMaxTic=0;BuyPriceMinTic=0;SelPriceMaxTic=0;SelPriceMinTic=0;
BuyLotsSum=0;SelLotsSum=0;WeighBuy=0;WeighSell=0;
op=0;lt=0;tp=0;pr=0;
BuyBU=0;SelBU=0;FullBU=0;
tk=0;b=0;s=0;
for(int i=OrdersTotal()-1;i>=0;i--)
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
if(OrderMagicNumber()==MagicNumber)
if(OrderSymbol()==Symbol())
{
op=NormalizeDouble(OrderOpenPrice(),Digits());
lt=NormalizeDouble(OrderLots(),2);
tk=OrderTicket();
pr += OrderProfit() + OrderCommission() + OrderSwap();
if(OrderType()==OP_BUY)
{
b++;
BuyLotsSum += lt;
WeighBuy += lt*op;
if(op>BuyPriceMax || BuyPriceMax==0)
{
BuyPriceMax = op;
BuyPriceMaxLot = lt;
BuyPriceMaxTic = tk;
}
if(op<BuyPriceMin || BuyPriceMin==0)
{
BuyPriceMin = op;
BuyPriceMinLot = lt;
BuyPriceMinTic = tk;
}
}
// ===
if(OrderType()==OP_SELL)
{
s++;
SelLotsSum += lt;
WeighSell += lt*op;
if(op>SelPriceMax || SelPriceMax==0)
{
SelPriceMax = op;
SelPriceMaxLot = lt;
SelPriceMaxTic = tk;
}
if(op<SelPriceMin || SelPriceMin==0)
{
SelPriceMin = op;
SelPriceMinLot = lt;
SelPriceMinTic = tk;
}
}
}
//---
if (BuyLotsSum - SelLotsSum != 0)
FullBU = (WeighBuy - WeighSell)/(BuyLotsSum - SelLotsSum);
}
//+------------------------------------------------------------------+
kvashnin007