int OnInit()
{
return(INIT_SUCCEEDED);
}
double MA_1, MA_2;
string GetNameOP = 0;
extern bool AllPositions =True; // Управлять всеми позициями
extern bool ProfitTrailing=True; // Тралить только профит
extern int TrailingStop =10; // Фиксированный размер трала
extern int TrailingStep =1; // Шаг трала
extern bool UseSound =False; // Использовать звуковой сигнал
extern string NameFileSound ="expert.wav"; // Наименование звукового файла
void OnTick()
{
for(int i=0; i<OrdersTotal(); i++)
{
if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
{
if (AllPositions || OrderSymbol()==Symbol())
{
TrailingPositions();
}
}
}
Comment(CountBuyPositions()+" Number of Buy Positions:\n",
CountSellPositions()+" Number of Sell Positions:\n");
MA_1=iMA(NULL,0,7,0,MODE_SMMA,PRICE_MEDIAN,0);
MA_2=iMA(NULL,0,17,0,MODE_SMMA,PRICE_MEDIAN,0);
// first buy
if((MA_1>MA_2) && (CountBuyPositions()<1))
{
int _FirstBuy = OrderSend
(
Symbol(), // currency
OP_BUY, // operation
0.03, // lot size
Ask, // price
0, // slippage
Ask-300*_Point,// stop
Ask+20*_Point, // take
NULL, //comment
123, // id
0, //exp.date
clrGreen // color
);
}
else
{
if((MA_1<MA_2) && (CountSellPositions()<1))
// sell
{
int _FirstSell = OrderSend
(
Symbol(), // currency
OP_SELL, // operation
0.03, // lot size
Bid, // price
0, // slippage
Bid+300*_Point,// stop
Bid-20*_Point, // take
NULL, //comment
123, // id
0, //exp.date
clrRed // color
);
}
}
if((MA_1>MA_2) && (CountBuyPositions()<50) && isOrderInProfit()==1)
// buy
{
int _Buy = OrderSend
(
Symbol(), // currency
OP_BUY, // operation
0.03, // lot size
Ask, // price
0, // slippage
Ask-300*_Point,// stop
Ask+20*_Point, // take
NULL, //comment
123, // id
0, //exp.date
clrGreen // color
);
}
else
{
if((MA_1<MA_2) && (CountSellPositions()<50) && isOrderInProfit()==1)
// sell
{
int _Sell = OrderSend
(
Symbol(), // currency
OP_SELL, // operation
0.03, // lot size
Bid, // price
0, // slippage
Bid+300*_Point,// stop
Bid-20*_Point, // take
NULL, //comment
123, // id
0, //exp.date
clrRed // color
);
}
}
}
int CountBuyPositions()
{
int NumberOfBuyPositions=0;
for(int i=OrdersTotal()-1; i>=0; i--)
{
OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
string CurrencyPair=OrderSymbol();
if(_Symbol==CurrencyPair)
if(OrderType()==OP_BUY)
{
NumberOfBuyPositions=NumberOfBuyPositions+1;
}
}
return NumberOfBuyPositions;
}
int CountSellPositions()
{
int NumberOfSellPositions=0;
for(int i=OrdersTotal()-1; i>=0; i--)
{
OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
string CurrencyPair=OrderSymbol();
if(_Symbol==CurrencyPair)
if(OrderType()==OP_SELL)
{
NumberOfSellPositions=NumberOfSellPositions+1;
}
}
return NumberOfSellPositions;
}
int isOrderInProfit()
{
int OrderInProfit=0;
{
OrderSelect(OrdersTotal()-1,SELECT_BY_POS);
if (OrderProfit()>1)
{OrderInProfit=1;}
}
return OrderInProfit;
}
//+------------------------------------------------------------------+
//| Сопровождение позиции простым тралом |
//+------------------------------------------------------------------+
void TrailingPositions()
{
double pBid, pAsk, pp;
//----
pp=MarketInfo(OrderSymbol(), MODE_POINT);
if (OrderType()==OP_BUY)
{
pBid=MarketInfo(OrderSymbol(), MODE_BID);
if (!ProfitTrailing || (pBid-OrderOpenPrice())>TrailingStop*pp)
{
if (OrderStopLoss()<pBid-(TrailingStop+TrailingStep-1)*pp)
{
ModifyStopLoss(pBid-TrailingStop*pp);
return;
}
}
}
if (OrderType()==OP_SELL)
{
pAsk=MarketInfo(OrderSymbol(), MODE_ASK);
if (!ProfitTrailing || OrderOpenPrice()-pAsk>TrailingStop*pp)
{
if (OrderStopLoss()>pAsk+(TrailingStop+TrailingStep-1)*pp || OrderStopLoss()==0)
{
ModifyStopLoss(pAsk+TrailingStop*pp);
return;
}
}
}
}
//+------------------------------------------------------------------+
//| Перенос уровня StopLoss |
//| Параметры: |
//| ldStopLoss - уровень StopLoss |
//+------------------------------------------------------------------+
void ModifyStopLoss(double ldStopLoss)
{
bool fm;
fm=OrderModify(OrderTicket(),OrderOpenPrice(),ldStopLoss,OrderTakeProfit(),0,CLR_NONE);
if (fm && UseSound) PlaySound(NameFileSound);
}
//+------------------------------------------------------------------+