//+------------------------------------------------------------------+
//| vldim@front.ru |
//| |
//| |
//+------------------------------------------------------------------+
extern string TimeFrame = "H4"; // ðàáî÷èé ÒàéìÔðåéì
extern string str1 = "----- TrendStrength_Í1 -----"; // ïàðàìåòðû èíäèêàòîðà TrendStrength_Í1
extern int Bar = 1; // íîìåð áàðà, íà êîòîðîì ïðîâåðÿåòñÿ ïåðåñå÷åíèå (0 èëè 1)
extern double Level_Up = 0.0016; // Âåðõíèé óðîâåíü èíäèêàòîðà TrendStrength_Í1
extern double Level_Dn = -0.0016; // Íèæíèé óðîâåíü èíäèêàòîðà TrendStrength_Í1
extern string str2 = "---------------";
extern int StopLoss = 50; // ÑòîïËîññ (åñëè íå èñïîëüçóåòñÿ, òî = 0)
extern int TakeProfit = 50; // ÒåéêÏðîôèò (åñëè íå èñïîëüçóåòñÿ, òî = 0)
extern string str3 = "----- Ëîò -----";
extern bool Use_MM = false; // èñïîëüçîâàíèå ÌÌ
extern bool Calñ_Lot = true; // âû÷èñëåíèå ðàçìåðà ëîòà îò Äåïîçèòà
extern double Lot_Start = 0.1; // íà÷àëüíàÿ âåëè÷èíà ëîòà ïðè Calñ_Lot=false, ëîò ïðè Use_MM=false
extern double Depo_Perc = 10; // ïðîöåíò îò äåïîçèòà äëÿ âû÷èñëåíèÿ ðàçìåðà ëîòà ïðè Calñ_Lot=true
extern double LotExponent = 2; // êîýôôèöèåíò óâåëè÷åíèÿ ëîòà
extern int LossSeries = 3; // ìàêñèìàëüíîå êîëè÷åñòâî óâåëè÷åíèé ëîòà
extern string str4 = "---------------";
extern int MaxAttempts = 5; // êîëè÷åñòâî ïîïûòîê îòêðûòèÿ îðäåðà, ïîñëå êîòîðûõ îòêðûòèå ïðîèñõîäèò Ñòàðòîâûì ëîòîì
extern int TrailingStop = 20; // ðàçìåð ÒðåéëèíãÑòîïà (åñëè íå èñïîëüçóåòñÿ = 0)
extern int TrailingStep = 2; // øàã ÒðåéëèíãÑòîïà
extern int Slippage = 3; // ïðîñêàëüçûâàíèå
extern int Magic_Number = 777; // ìàãè÷åñêèé íîìåð
//+------------------------------------------------------------------+
//| expert initialization function |
//+------------------------------------------------------------------+
int init()
{
//----
TFdefin();
//----
return(0);
}
//+------------------------------------------------------------------+
//| expert deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
//----
//----
return(0);
}
//+------------------------------------------------------------------+
//| expert start function |
//+------------------------------------------------------------------+
string Ñomment="";
int TF;
int StopLevel, Spred;
int Set_Num;
bool OpenBuy_1=false;
bool OpenSell_1=false;
#include <TradeContext.mq4>
int start()
{
//----
int cnt;
double pr, sl, tp, Lot;
OpenBuy_1=OpenBuy_1();
OpenSell_1=OpenSell_1();
StopLevel=MarketInfo(Symbol(), MODE_STOPLEVEL);
Spred=MarketInfo(Symbol(), MODE_SPREAD);
if(OpenSell_1==true) CloseOrders(Symbol(), OP_BUY);
if(OpenBuy_1==true) CloseOrders(Symbol(), OP_SELL);
if(OrdersQuan(Symbol(), OP_BUY)==0 && (OpenBuy_1==true || OpenBuy_2()==true) &&
iBarShift(NULL, TF, LastOrderOpenTime(OP_BUY))>0)
{
RefreshRates();
pr=Ask;
if(StopLoss>(StopLevel+Spred)) sl=pr-StopLoss*Point;
if(TakeProfit>StopLevel) tp=pr+TakeProfit*Point;
Lot=Lot(OP_BUY);
// if(OpenBuy_1==true) Lot=Lot_Start;
Set_Num++;
if(Set_Num>MaxAttempts) Lot=Lot_Start;
SetOrder(Symbol(), OP_BUY, Lot, pr, sl, tp, Ñomment, 0);
}
if(OrdersQuan(Symbol(), OP_SELL)==0 && (OpenSell_1==true || OpenSell_2()==true) &&
iBarShift(NULL, TF, LastOrderOpenTime(OP_SELL))>0)
{
RefreshRates();
pr=Bid;
if(StopLoss>(StopLevel+Spred)) sl=pr+StopLoss*Point;
if(TakeProfit>StopLevel) tp=pr-TakeProfit*Point;
Lot=Lot(OP_SELL);
// if(OpenSell_1==true) Lot=Lot_Start;
Set_Num++;
if(Set_Num>MaxAttempts) Lot=Lot_Start;
SetOrder(Symbol(), OP_SELL, Lot, pr, sl, tp, Ñomment, 0);
}
if(TrailingStop>StopLevel)
{
for(cnt=0;cnt<OrdersTotal();cnt++)
{
OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
if(OrderMagicNumber()==Magic_Number) TrailingPositions();
}
}
Print("error = ",GetLastError());
}
//+------------------------------------------------------------------+
//| Óñòàíîâêà îðäåðà |
//| Ïàðàìåòðû: |
//| sym - èíñòðóìåíò |
//| type - òèï îðäåðà |
//| lt - ëîò |
//| pr - öåíà |
//| sl - ÑòîïËîññ |
//| tp - ÒåéêÏðîôèò |
//| com - êîììåíòàðèé |
//| exp - ñðîê èñòå÷åíèÿ îòëîæåííîãî îðäåðà |
//+------------------------------------------------------------------+
bool SetOrder(string sym, int type, double lt, double pr, double sl, double tp, string com, datetime exp)
{
int ticket; // íîìåð îòêðûòîãî îðäåðà
color col; // öâåò ñòðåëêè îòêðûòèÿ
if(type==OP_BUY || type==OP_BUYSTOP) col=Blue;
else col=Red;
if(TradeIsBusy()==1)
{
ticket=OrderSend(sym,type,lt,NormalizeDouble(pr,Digits),Slippage,NormalizeDouble(sl,Digits),
NormalizeDouble(tp,Digits),com,Magic_Number,exp,col);
}
Sleep(1000);
TradeIsNotBusy();
if(ticket>0)
{
Set_Num=0;
return(true);
}
else return(false);
}
//+------------------------------------------------------------------+
//| Âû÷èñëåíèå êîëè÷åñòâà îðäåðîâ |
//| Ïàðàìåòðû: |
//| sym - èíñòðóìåíò |
//| type - òèï îðäåðà |
//+------------------------------------------------------------------+
int OrdersQuan(string sym, int type)
{
int cnt,total;
for(cnt=0;cnt<OrdersTotal();cnt++)
{
OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
if(OrderSymbol()==sym && OrderMagicNumber()==Magic_Number)
{
if(OrderType()==type) total++;
}
}
return(total);
}
//+-------------------------------------------------------------------------+
//| Ïðåîáðàçîâàíèå ðàáî÷åãî ÒàéìÔðåéìà èç ñòðîêîâîãî â öåëî÷èñëåííûé ôîðìàò |
//+-------------------------------------------------------------------------+
void TFdefin()
{
if(TimeFrame=="M1" || TimeFrame=="Ì1") TF=1;
if(TimeFrame=="M5" || TimeFrame=="Ì5") TF=5;
if(TimeFrame=="M15" || TimeFrame=="Ì15") TF=15;
if(TimeFrame=="M30" || TimeFrame=="Ì30") TF=30;
if(TimeFrame=="H1" || TimeFrame=="Í1") TF=60;
if(TimeFrame=="H4" || TimeFrame=="Í4") TF=240;
if(TimeFrame=="D1") TF=1440;
if(TimeFrame=="W1") TF=10080;
if(TimeFrame=="MN1") TF=43200;
}
//+------------------------------------------------------------------+
//| Çàêðûòèå/óäàëåíèå îðäåðîâ çàäàííîãî òèïà |
//| Ïàðàìåòðû: |
//| sym - èíñòðóìåíò |
//| type - òèï îðäåðà |
//+------------------------------------------------------------------+
void CloseOrders(string sym, int type)
{
int cnt,total_com;
while(OrdersQuan(Symbol(),type)>0)
{
total_com=OrdersTotal();
for(cnt=0;cnt<total_com;cnt++)
{
OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
if(OrderMagicNumber()==Magic_Number)
{
if(OrderType()==type)
{
if(type==OP_BUY)
{
if(TradeIsBusy()==1)
{
RefreshRates();
if(OrderClose(OrderTicket(),OrderLots(),NormalizeDouble(Bid,Digits),3,Violet))
{
if (cnt<(total_com-1))
{
cnt--;
total_com--;
}
}
}
Sleep(1000);
TradeIsNotBusy();
}
if(type==OP_SELL)
{
if(TradeIsBusy()==1)
{
RefreshRates();
if(OrderClose(OrderTicket(),OrderLots(),NormalizeDouble(Ask,Digits),3,Violet))
{
if (cnt<(total_com-1))
{
cnt--;
total_com--;
}
}
}
Sleep(1000);
TradeIsNotBusy();
}
if(type==OP_BUYSTOP || type==OP_SELLSTOP || type==OP_BUYLIMIT || type==OP_SELLLIMIT)
{
if(TradeIsBusy()==1)
{
if(OrderDelete(OrderTicket()))
{
if (cnt<(total_com-1))
{
cnt--;
total_com--;
}
}
}
Sleep(1000);
TradeIsNotBusy();
}
}
}
}
}
}
bool OpenBuy_1()
{
bool res=false;
if(Cross_TS_Up()==true)
{
res=true;
}
return(res);
}
bool OpenSell_1()
{
bool res=false;
if(Cross_TS_Dn()==true)
{
res=true;
}
return(res);
}
bool OpenBuy_2()
{
bool res=false;
if(LastOrder_StopLoss(OP_BUY)==true &&
TS(0)>NormalizeDouble(Level_Up, Digits))
{
res=true;
}
return(res);
}
bool OpenSell_2()
{
bool res=false;
if(LastOrder_StopLoss(OP_SELL)==true &&
TS(0)<NormalizeDouble(Level_Dn, Digits))
{
res=true;
}
return(res);
}
bool LastOrder_StopLoss(int type)
{
bool res=false;
int cnt, num;
for(cnt=OrdersHistoryTotal()-1; cnt>=0; cnt--)
{
OrderSelect(cnt, SELECT_BY_POS, MODE_HISTORY);
if(OrderMagicNumber()==Magic_Number)
{
if(OrderType()==type)
{
if(NormalizeDouble(OrderClosePrice(), Digits)==NormalizeDouble(OrderStopLoss(), Digits))
{
res=true;
}
}
break;
}
}
return(res);
}
bool Cross_TS_Up()
{
bool res=false;
int i;
if(TS(Bar)>NormalizeDouble(Level_Dn, Digits))
{
if(TS(Bar+1)<NormalizeDouble(Level_Dn, Digits))
{
res=true;
}
if(TS(Bar+1)==NormalizeDouble(Level_Dn, Digits))
{
i=Bar+2;
while(TS(i)==NormalizeDouble(Level_Dn, Digits))
{
i++;
}
if(TS(i)<NormalizeDouble(Level_Dn, Digits))
{
res=true;
}
}
}
return(res);
}
bool Cross_TS_Dn()
{
bool res=false;
int i;
if(TS(Bar)<NormalizeDouble(Level_Up, Digits))
{
if(TS(Bar+1)>NormalizeDouble(Level_Up, Digits))
{
res=true;
}
if(TS(Bar+1)==NormalizeDouble(Level_Up, Digits))
{
i=Bar+2;
while(TS(i)==NormalizeDouble(Level_Up, Digits))
{
i++;
}
if(TS(i)>NormalizeDouble(Level_Up, Digits))
{
res=true;
}
}
}
return(res);
}
double TS(int bar)
{
double ts=iCustom(NULL, TF, "TrendStrength_Í1_mod", 0, bar);
ts=NormalizeDouble(ts, Digits);
return(ts);
}
datetime LastOrderOpenTime(int type)
{
int cnt;
datetime ot;
for(cnt=OrdersHistoryTotal()-1; cnt>=0; cnt--)
{
OrderSelect(cnt, SELECT_BY_POS, MODE_HISTORY);
if(OrderMagicNumber()==Magic_Number)
{
if(OrderType()==type)
{
ot=OrderOpenTime();
break;
}
}
}
return(ot);
}
double Lot(int type)
{
double lt;
int cnt, LotExact;
double Lot_Min=MarketInfo(Symbol(), MODE_MINLOT);
double Lot_Max=MarketInfo(Symbol(), MODE_MAXLOT);
double last_lot;
if(MarketInfo(Symbol(), MODE_MINLOT)==0.1) LotExact=1;
if(MarketInfo(Symbol(), MODE_MINLOT)==0.01) LotExact=2;
if(Use_MM==false) return(Lot_Start);
if(Calñ_Lot==true /*&& (OpenBuy_1==true || OpenSell_1==true)*/)
{
Lot_Start=AccountEquity()*Depo_Perc/MarketInfo(Symbol(), MODE_LOTSIZE);
Lot_Start=NormalizeDouble(Lot_Start, LotExact);
}
last_lot=Last_Lot();
if(last_lot==0)
{
return(Lot_Start);
}
else
{
if(LossNum(type)<LossSeries)
{
lt=last_lot*LotExponent;
}
else
{
lt=last_lot;
}
}
/*
lt=NormalizeDouble(MathFloor(Lot_Start*MathPow(10, LotExact))/MathPow(10, LotExact), LotExact);
lt=NormalizeDouble(MathCeil(lt*MathPow(10, LotExact))/MathPow(10, LotExact), LotExact);
*/
if(lt<Lot_Min) lt=Lot_Min;
if(lt>Lot_Max) lt=Lot_Max;
lt=NormalizeDouble(lt, LotExact);
return(lt);
}
double Last_Lot()
{
double lt;
int cnt;
for(cnt=OrdersHistoryTotal()-1; cnt>=0; cnt--)
{
OrderSelect(cnt, SELECT_BY_POS, MODE_HISTORY);
if(OrderMagicNumber()==Magic_Number)
{
if(OrderType()==OP_BUY || OrderType()==OP_SELL)
{
if(OrderProfit()>=0) break;
lt=OrderLots();
break;
}
}
}
return(lt);
}
int LossNum(int type)
{
int cnt, num;
for(cnt=OrdersHistoryTotal()-1; cnt>=0; cnt--)
{
OrderSelect(cnt, SELECT_BY_POS, MODE_HISTORY);
if(OrderMagicNumber()==Magic_Number)
{
if(OrderType()!=type) break;
if(OrderLots()==Lot_Start) break;
num++;
}
}
return(num);
}
//+------------------------------------------------------------------+
//| Ñîïðîâîæäåíèå ïîçèöèè ïðîñòûì òðàëîì |
//+------------------------------------------------------------------+
void TrailingPositions()
{
double pBid, pAsk, pp;
pp = MarketInfo(OrderSymbol(), MODE_POINT);
if(OrderType()==OP_BUY)
{
pBid = MarketInfo(OrderSymbol(), MODE_BID);
if((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((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);
}
Comments