//************************ Ilan buy
void IlanBuy(double Multipler, double MaxLot, int StartLotExp, int AverageTP, double StepExp, int StartStepExp,int Step, int MaxOrder, int Magik )
{
double biggestlot=0;
double near_ord_price=999999;
double step_=0;
int ordersum_=0;
int Repeat=10;
double minlot=10000;
// calculate summe lots and last order open price
for (int i=OrdersTotal()-1; i >= 0; i--)
{
if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
{
if (OrderType() == OP_BUY && OrderSymbol() == Symbol() && OrderMagicNumber() == Magik)
{
if ( near_ord_price > OrderOpenPrice()) near_ord_price = OrderOpenPrice();
if ( biggestlot < OrderLots()) biggestlot = OrderLots();
if ( minlot > OrderLots()) minlot = OrderLots();
ordersum_ +=1;
}}
else
{
Print("OrderSelect() ERROR Buy.code 15 ", ErrorDescription(GetLastError()));
}}
// *************************calculate and open buy order *****************************
step_=Step;
//------------------------------------------------------------------------------------------------------------------------
if ( ordersum_ >= StartStepExp ) { step_ = NormalizeDouble(step_ * MathPow(StepExp ,((ordersum_+1) - StartStepExp)),0); }
//------------------------------------------------------------------------------------------------------------------------
if (near_ord_price - Ask > step_*Point && near_ord_price != 999999 && ordersum_ < MaxOrder && ordersum_ > 0 )
{
//-------------------------------------------------------------------------
double new_lot = NormalizeDouble((biggestlot*Multipler) ,2);
if ( new_lot==biggestlot ) new_lot=minlot + biggestlot;
if (ordersum_ < StartLotExp ) new_lot=minlot;
if (new_lot > MaxLot ) new_lot=MaxLot;
//-------------------------------------------------------------------------
//--------------------------------------------------------------------------------------------
int ticket = OrderSend(Symbol(), OP_BUY, new_lot, Ask, 2, 0, 0, "t", Magik, 0, Blue);
if ( ticket < 0 ) { Print("OrderSend() ERROR Buy.code 15 ", ErrorDescription(GetLastError()));}
//--------------------------------------------------------------------------------------------
// *******************************calculate TP ******************
double Lots_al =0;
double Average_lp =0;
double Average_tp =0;
for ( int i=OrdersTotal()-1; i >= 0; i--)
{
if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
{
if (OrderType() == OP_BUY && OrderSymbol() == Symbol() && OrderMagicNumber() == Magik)
{
Lots_al += OrderLots();
Average_lp +=(OrderLots()*OrderOpenPrice());
}
}
else
{
Print("OrderSelect() ERROR Buy.code 15.1 ", ErrorDescription(GetLastError()));
}}
//************************************************
Average_tp= NormalizeDouble(Average_lp/Lots_al,Digits);
Average_tp=Average_tp+(AverageTP*Point);
// ********************** modify order *************************
double modify_sl=0;
double modify_tp=0;
for ( int i=OrdersTotal()-1; i >= 0; i--)
{
if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
{ if (OrderType() == OP_BUY && OrderSymbol() == Symbol() && OrderMagicNumber() == Magik)
{
for (int ii=Repeat; ii >= 0; ii--)
{
//------------------------------------------------------------
if ( Average_tp < Ask ) {modify_sl=Average_tp; modify_tp=0; }
else {modify_sl=0; modify_tp=Average_tp;}
//------------------------------------------------------------
bool ret = OrderModify(OrderTicket(), OrderOpenPrice(), modify_sl, modify_tp, 0, Yellow);
if (ret == true ) break;
Print("OrderModify() ERROR Buy.code 15", ErrorDescription(GetLastError()));
Sleep(500);
while( !IsTradeAllowed() ) Sleep(100);
RefreshRates();
}
}}
else
{
Print("OrderSelect()ERROR Buy.code 15.2 ", ErrorDescription(GetLastError()));
}}}
}
//######## END Ilan buy
//********************** Ilan sell
void IlanSell(double Multipler, double MaxLot, int StartLotExp, int AverageTP, double StepExp, int StartStepExp,int Step, int MaxOrder, int Magik )
{
double biggestlot=0;
double near_ord_price=0;
double step_=0;
int ordersum_=0;
int Repeat=100;
double minlot=10000;
// calculate summe lots and last order open price
for (int i=OrdersTotal()-1; i >= 0; i--)
{
if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
{
if (OrderType() == OP_SELL && OrderSymbol() == Symbol() && OrderMagicNumber() == Magik)
{
if ( near_ord_price < OrderOpenPrice()) near_ord_price = OrderOpenPrice();
if ( biggestlot < OrderLots()) biggestlot = OrderLots();
if ( minlot > OrderLots()) minlot = OrderLots();
ordersum_ +=1;
}
}
else
{
Print("OrderSelect() ERROR Sell.code 16 ", ErrorDescription(GetLastError()));
}}
// *************************calculate and open buy order *****************************
//------------------------------------------------------------------------------------------------------------------------
step_=Step;
if ( ordersum_ >= StartStepExp ) { step_ = NormalizeDouble(step_ * MathPow(StepExp ,((ordersum_+1) - StartStepExp)),0); }
//------------------------------------------------------------------------------------------------------------------------
if ( Bid - near_ord_price > step_*Point && near_ord_price != 0 && ordersum_ < MaxOrder && ordersum_ > 0 )
{
//------------------------------------------------------------
double new_lot = NormalizeDouble((biggestlot*Multipler) ,2);
if ( new_lot==biggestlot ) new_lot=minlot + biggestlot;
if (ordersum_ < StartLotExp ) new_lot=minlot;
if (new_lot > MaxLot ) new_lot=MaxLot;
//------------------------------------------------------------
//-------------------------------------------------------------------------------------------
int ticket = OrderSend(Symbol(), OP_SELL, new_lot, Bid,2 , 0, 0, "t", Magik, 0, Red);
if ( ticket < 0 ) { Print("OrderSend() ERROR Sell.code 16 ", ErrorDescription(GetLastError())); }
//-------------------------------------------------------------------------------------------
// *******************************calculate TP ******************
//------------------
double Lots_al=0;
double Average_lp=0;
double Average_tp=0;
//------------------
//-------------------------------------------------------------------------------------------
for ( int i=OrdersTotal()-1; i >= 0; i--)
{
if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
{
if (OrderType() == OP_SELL && OrderSymbol() == Symbol() && OrderMagicNumber() == Magik)
{
Lots_al += OrderLots();
Average_lp +=OrderLots()*OrderOpenPrice();
}
}
else Print("OrderSelect() ERROR Sell.code 16.1", ErrorDescription(GetLastError()));
}
//---------------------------------------------------------------------------------------------
// ********************** modify order *************************
//-----------------------------------------------------
Average_tp= NormalizeDouble(Average_lp/Lots_al,Digits);
Average_tp=Average_tp-AverageTP*Point;
double modify_sl=0;
double modify_tp=0;
//-----------------------------------------------------
for ( int i=OrdersTotal()-1; i >= 0; i--)
{
if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
{
if (OrderType() == OP_SELL && OrderSymbol() == Symbol() && OrderMagicNumber() == Magik)
{
for (int ii=Repeat; ii >= 0; ii--)
{
//-----------------------------------------------------------
if ( Average_tp > Bid ) {modify_sl=Average_tp; modify_tp=0; }
else {modify_sl=0; modify_tp=Average_tp; }
//-----------------------------------------------------------
bool ret = OrderModify(OrderTicket(), OrderOpenPrice(), modify_sl, modify_tp, 0, Yellow);
if (ret == true ) break;
Print("OrderModify() ERROR Sell.code 16 ", ErrorDescription(GetLastError()));
Sleep(500);
while( !IsTradeAllowed() ) Sleep(100);
}
}
}
else Print("OrderSelect() ERROR Sell.code 16.2 ", ErrorDescription(GetLastError()));
}
}
}
//########## END Ilan sell