//==========================================================================================================================
// 顶级趋势V1.0 坐石观云清修改
//==========================================================================================================================
#include <stdlib.mqh>
extern string Main = "==== Main SETTINGS ====";
extern bool EA_Goiler = true;
extern bool Line_on_Chart = true;
extern bool EA_Diver = true;
extern double MaxRiskPercent = 0.7;
extern double MaxLotSize = 5;
extern double DecreaseFactor = 135;
extern double Lots = 0.01;
//extern int StopLoss = 500;
extern int TakeProfit = 800;
extern int Trall = 700;
extern double TrallKoef = 2.2;
int NumberOfTry = 5;
int PauseAfterError = 2;
int Slippage = 10;
color clOpenBuy = Aqua;
color clOpenSell = Red;
//==========================================================================================================================
extern string Goiler = "==== Goiler SETTINGS ====";
extern int Start_Goiler = 00;
//extern int Start_Trade_Buy = 00;
//extern int Start_Trade_Sell = 00;
extern int MAGICGO1 = 11;
extern int MAGICGO2 = 22;
extern int koef1Buy_up = 251;
extern int koef2Buy_up = 541;
extern int koef3Buy_up = 365;
extern int koef1Sell_up = 441;
extern int koef2Sell_up = 389;
extern int koef3Sell_up = 369;
//==========================================================================================================================
//----variables for indicator Diver
extern string Diver = "==== Diver SETTINGS ====";
extern int fastEMAbuy = 10;
extern int slowEMAbuy = 53;
extern int signalSMAbuy = 5;
extern int fastEMAsell = 46;
extern int slowEMAsell = 83;
extern int signalSMAsell = 17;
extern int TF = 15;
extern int MAGICDI1 = 33;
extern int MAGICDI2 = 44;
double DiverBuy = 0;
double DiverSell = 0;
//----variables from Goiler
static double EntryBuy;
static double TP1Buy;
static double TP2Buy;
static double SLBuy;
static double TPBuy;
static double EntrySell;
static double TP1Sell;
static double TP2Sell;
static double SLSell;
static double TPSell;
int koef1Orig = 180;
int koef2Orig = 225;
int koef3Orig = 360;
double NewEntryOrig ;
double NewEntry2Orig ;
double NewTP1Orig ;
double NewTP2Orig ;
double NewSLOrig ;
double NewEntryBuy ;
double NewEntry2Buy ;
double NewTP1Buy ;
double NewTP2Buy ;
double NewSLBuy ;
double NewEntrySell ;
double NewEntry2Sell ;
double NewTP1Sell ;
double NewTP2Sell ;
double NewSLSell ;
string SymbolString;
static datetime prevtime_GO ;
static datetime prevtimebuy_Di ;
static datetime prevtimesell_Di ;
static datetime prevtimetrallbuy ;
static datetime prevtimetrallsell ;
double min = 0.01;
double max = 5;
double OrdLot = 0.01;
int width=2;
int style=1;
//+------------------------------------------------------------------+
//| Check for indicator |
//+------------------------------------------------------------------+
void Goiler()
{
if (Time[0]<=prevtime_GO+60*60) return;
prevtime_GO=Time[0];
NewEntryOrig = iCustom(NULL,0,"顶级趋势V3",koef1Orig,koef2Orig,koef3Orig,0,0);
//NewEntry2Orig = iCustom(NULL,0,"顶级趋势V3",koef1Orig,koef2Orig,koef3Orig,1,0);
NewTP1Orig = iCustom(NULL,0,"顶级趋势V3",koef1Orig,koef2Orig,koef3Orig,2,0);
NewTP2Orig = iCustom(NULL,0,"顶级趋势V3",koef1Orig,koef2Orig,koef3Orig,3,0);
NewSLOrig = iCustom(NULL,0,"顶级趋势V3",koef1Orig,koef2Orig,koef3Orig,4,0);
NewEntryBuy = iCustom(NULL,0,"顶级趋势V3",koef1Buy_up,koef2Buy_up,koef3Buy_up,0,0);
//NewEntry2Buy = iCustom(NULL,0,"顶级趋势V3",koef1Buy_up,koef2Buy_up,koef3Buy_up,1,0);
NewTP1Buy = iCustom(NULL,0,"顶级趋势V3",koef1Buy_up,koef2Buy_up,koef3Buy_up,2,0);
NewTP2Buy = iCustom(NULL,0,"顶级趋势V3",koef1Buy_up,koef2Buy_up,koef3Buy_up,3,0);
NewSLBuy = iCustom(NULL,0,"顶级趋势V3",koef1Buy_up,koef2Buy_up,koef3Buy_up,4,0);
NewEntrySell = iCustom(NULL,0,"顶级趋势V3",koef1Sell_up,koef2Sell_up,koef3Sell_up,0,0);
//NewEntry2Sell = iCustom(NULL,0,"顶级趋势V3",koef1Sell_up,koef2Sell_up,koef3Sell_up,1,0);
NewTP1Sell = iCustom(NULL,0,"顶级趋势V3",koef1Sell_up,koef2Sell_up,koef3Sell_up,2,0);
NewTP2Sell = iCustom(NULL,0,"顶级趋势V3",koef1Sell_up,koef2Sell_up,koef3Sell_up,3,0);
NewSLSell = iCustom(NULL,0,"顶级趋势V3",koef1Sell_up,koef2Sell_up,koef3Sell_up,4,0);
if (NewEntryBuy>0.0 && (NewTP1Buy>NewEntryBuy && NewTP1Orig>NewEntryOrig))
{
EntryBuy=NewEntryBuy;
TP1Buy=NewTP1Buy;
TP2Buy=NewTP2Buy;
SLBuy=NewSLBuy;
if (Line_on_Chart)
{
PlotLine("怩钿拎閇"+NewEntryBuy+"]",NewEntryBuy,Gold, TimeCurrent(), TimeCurrent()+20*3600) ;
//PlotLine("漕徉怅郲"+shift+"]",Entry2,Orange);
PlotLine("蝈殛拎閇"+NewTP1Buy+"]",NewTP1Buy,Lime, TimeCurrent(), TimeCurrent()+20*3600);
PlotLine("蝈殛_2拎閇"+NewTP2Buy+"]",NewTP2Buy,DodgerBlue, TimeCurrent(), TimeCurrent()+20*3600);
PlotLine("耱铒拎閇"+NewSLBuy+"]",NewSLBuy,Red, TimeCurrent(), TimeCurrent()+20*3600);
}
CheckForOpenGoilerBuy();
}
if (NewEntrySell>0.0 && (NewTP1Sell<NewEntrySell && NewTP1Orig<NewEntryOrig))
{
EntrySell=NewEntrySell;
TP1Sell=NewTP1Sell;
TP2Sell=NewTP2Sell;
SLSell=NewSLSell;
if (Line_on_Chart)
{
PlotLine("怩钿彦腚["+NewEntrySell+"]",NewEntrySell,Gold, TimeCurrent(), TimeCurrent()+20*3600) ;
//PlotLine("漕徉怅郲"+shift+"]",Entry2,Orange);
PlotLine("蝈殛彦腚["+NewTP1Sell+"]",NewTP1Sell,Lime, TimeCurrent(), TimeCurrent()+20*3600);
PlotLine("蝈殛_2彦腚["+NewTP2Sell+"]",NewTP2Sell,DodgerBlue, TimeCurrent(), TimeCurrent()+20*3600);
PlotLine("耱铒彦腚["+NewSLSell+"]",NewSLSell,Red, TimeCurrent(), TimeCurrent()+20*3600);
}
CheckForOpenGoilerSell();
}
}
//+------------------------------------------------------------------+
//| Check for open Buy order conditions |
//+------------------------------------------------------------------+
void CheckForOpenGoilerBuy()
{
int i, k=OrdersTotal()-1;
for(i=k; i>=0; i--)
{
if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
{
if(OrderSymbol()==Symbol()&& OrderType()==OP_BUY && OrderMagicNumber()==MAGICGO1 && TimeCurrent()-OrderOpenTime()<3600) return(0);
}
}
TPBuy = NormalizeDouble(Ask+TakeProfit*Point, Digits);
OpenPosition(OP_BUY, SLBuy, TPBuy, MAGICGO1, LotsOptimizedBuy()*3, " Goiler Buy"+MAGICGO1);
}
//+------------------------------------------------------------------+
//| Check for open Sell order conditions |
//+------------------------------------------------------------------+
void CheckForOpenGoilerSell()
{
int i, k=OrdersTotal()-1;
for(i=k; i>=0; i--)
{
if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
{
if(OrderSymbol()==Symbol()&& OrderType()==OP_SELL && OrderMagicNumber()==MAGICGO2 && TimeCurrent()-OrderOpenTime()<3600) return(0);
}
}
TPSell = NormalizeDouble(Bid-TakeProfit*Point, Digits);
OpenPosition(OP_SELL, SLSell, TPSell, MAGICGO2, LotsOptimizedSell()*3, " Goiler Sell"+MAGICGO2);
}
//+------------------------------------------------------------------+
//| Function for open order |
//+------------------------------------------------------------------+
void OpenPosition(int op, double sl, double tp, int mn, double ll, string lsComm)
{
color clOpen;
double pp;
int err, it, ticket, ticketmod;
if (op==OP_BUY) clOpen=clOpenBuy; else clOpen=clOpenSell;
//ll=GetSizeLot();
for (it=1; it<=NumberOfTry; it++)
{
//if (!IsTesting() && (!IsExpertEnabled() || IsStopped())) break;
while (!IsTradeAllowed()) Sleep(5000);
RefreshRates();
if (op==OP_BUY) pp=Ask; else pp=Bid;
pp=NormalizeDouble(pp, Digits);
ticket=OrderSend(Symbol(),op,ll,pp,Slippage,0,0,lsComm,mn,0,clOpen);
if (ticket>0)
{
ticketmod=OrderModify(ticket,OrderOpenPrice(),sl,tp,0,clOpen);
if (ticketmod>0) break;
}
else
{
err=GetLastError();
if (err==128 || err==142 || err==143 || err==144)
{
Sleep(1000*PauseAfterError);
if (ExistPositions(Symbol(), op, mn)) break;
Print("Error(",err,") opening position: ",ErrorDescription(err),", try ",it);
continue;
}
if (err==146) while (IsTradeContextBusy()) Sleep(1000*PauseAfterError);
Print("Error(",err,") opening position: ",ErrorDescription(err),", try ",it);
//Print("Ask=",Ask," Bid=",Bid," ll=",ll," op=",GetNameOP(op),
// " pp=",pp," sl=",sl," tp=",tp," mn=",mn);
if (err==2 || err==64 || err==65 || err==4110 || err==4111) break;
Sleep(1000*PauseAfterError);
}
}
}
//+----------------------------------------------------------------------------+
//+----------------------------------------------------------------------------+
//| |
//+----------------------------------------------------------------------------+
bool ExistPositions(string sym, int op, int mn)
{
int i, k=OrdersTotal();
if (sym=="") sym=Symbol();
for (i=0; i<k; i++)
{
if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
{
if (OrderSymbol()==sym)
{
if (OrderType()==OP_BUY || OrderType()==OP_SELL)
{
if (OrderType()==op)
{
if (OrderMagicNumber()==mn)
{
if (TimeCurrent()-OrderOpenTime()<600)return(True);
}
}
}
}
}
}
return(False);
}
//+------------------------------------------------------------------+
//| Calculate optimal lot size Buy |
//+------------------------------------------------------------------+
double LotsOptimizedBuy()
{
double lotBuy;
if (Lots>0)
{
lotBuy=Lots;
return(lotBuy);
}
int lossesBuy=0; // number of losses orders without a break
//---- select lot size
lotBuy=NormalizeDouble(AccountFreeMargin()*MaxRiskPercent/100000.0, 2);
//---- calculate number of losses orders without a break
if(DecreaseFactor>0)
{
int i, k=OrdersHistoryTotal()-1;
for(i=k; i>=0; i--)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)==false) { Print("Error in history!"); break; }
if(OrderSymbol()!=Symbol()) continue;
//----
if(OrderMagicNumber()==MAGICGO1)
{
if(OrderProfit()>=0) break;
if(OrderProfit()<0) lossesBuy++;
}
}
}
if(lossesBuy>0)
{
lotBuy=NormalizeDouble(lotBuy-lotBuy*lossesBuy/DecreaseFactor,2);
}
//---- return lot size
if(lotBuy< min) lotBuy=min;
if(MaxLotSize>max) MaxLotSize=max;
if(lotBuy>MaxLotSize) lotBuy=MaxLotSize;
return(lotBuy);
}
//+------------------------------------------------------------------+
//| Calculate optimal lot size Sell |
//+------------------------------------------------------------------+
double LotsOptimizedSell()
{
double lotSell;
if (Lots>0)
{
lotSell=Lots;
return(lotSell);
}
int lossesSell=0; // number of losses orders without a break
//---- select lot size
lotSell=NormalizeDouble(AccountFreeMargin()*MaxRiskPercent/100000.0, 2);
//---- calculate number of losses orders without a break
if(DecreaseFactor>0)
{
int i, k=OrdersHistoryTotal()-1;
for(i=k; i>=0; i--)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)==false) { Print("Error in history!"); break; }
if(OrderSymbol()!=Symbol()) continue;
//----
if(OrderMagicNumber()==MAGICGO2)
{
if(OrderProfit()>=0) break;
if(OrderProfit()<0) lossesSell++;
}
}
}
if(lossesSell>0)
{
lotSell=NormalizeDouble(lotSell-lotSell*lossesSell/DecreaseFactor,2);
}
//---- return lot size
if(lotSell< min) lotSell=min;
if(MaxLotSize>max) MaxLotSize=max;
if(lotSell>MaxLotSize) lotSell=MaxLotSize;
return(lotSell);
}
//+------------------------------------------------------------------+
//| Check Trall Buy |
//+------------------------------------------------------------------+
void Check_TR_Buy()
{
if (Time[0]<=prevtimetrallbuy) return;
prevtimetrallbuy=Time[0];
if (Bid < TP1Buy) return;
double newSL=0.0;
int i, k=OrdersTotal()-1;
for(i=k; i>=0; i--)
if (OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
{
if (OrderProfit() <= 0.0) continue;
{
if (OrderSymbol()==Symbol() && (OrderMagicNumber()==MAGICGO1 || OrderMagicNumber()==MAGICDI1))
{
if (Bid >= TP1Buy) // && OrderMagicNumber()==MAGICGO1)
{
newSL= NormalizeDouble(EntryBuy,Digits); // OrderOpenPrice();
if (newSL<=OrderStopLoss()) newSL=OrderStopLoss();
}
if (Bid >= TP2Buy) // && OrderMagicNumber()==MAGICGO1)
{
newSL=NormalizeDouble(TP1Buy,Digits);
if (newSL<=OrderStopLoss()) newSL=OrderStopLoss();
}
if (Bid >= TP2Buy && Bid>=OrderStopLoss()+Trall*TrallKoef*Point)
{
newSL=NormalizeDouble(Bid-Trall*Point,Digits);
if (newSL<=OrderStopLoss()) newSL=OrderStopLoss();
}
if (newSL<=0.0) return;
if (newSL<=OrderStopLoss()) continue;
if (OrderModify(OrderTicket(),OrderOpenPrice(),newSL,OrderTakeProfit(),0,Yellow))
{
OrdLot = NormalizeDouble(OrderLots()/3.0,3);
if (OrdLot<min) OrdLot=min;
OrderClose(OrderTicket(),OrdLot,Bid,Slippage,Aqua);
continue;
}
}
}
continue;
}
return;
}
//+------------------------------------------------------------------+
//| Check Trall Sell |
//+------------------------------------------------------------------+
void Check_TR_Sell()
{
if (Time[0]<=prevtimetrallsell) return;
prevtimetrallsell=Time[0];
if (Ask > TP1Sell) return;
double newSL=0;
int i, k=OrdersTotal()-1;
for(i=k; i>=0; i--)
if (OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
{
if (OrderProfit() <= 0.0) continue;
{
if (OrderSymbol()==Symbol() && (OrderMagicNumber()==MAGICGO2 || OrderMagicNumber()==MAGICDI2))
{
if (Ask <= TP1Sell) // && OrderMagicNumber()==MAGICGO2)
{
newSL= NormalizeDouble(EntrySell,Digits); // OrderOpenPrice();
if (newSL>=OrderStopLoss()) newSL=OrderStopLoss();
}
if (Ask <= TP2Sell) // && OrderMagicNumber()==MAGICGO2)
{
newSL=NormalizeDouble(TP1Sell,Digits);
if (newSL>=OrderStopLoss()) newSL=OrderStopLoss();
}
if (Ask <= TP2Sell && Ask<=OrderStopLoss()-Trall*TrallKoef*Point)
{
newSL=NormalizeDouble(Ask+Trall*Point,Digits);
if (newSL>=OrderStopLoss()) newSL=OrderStopLoss();
}
if (newSL<=0.0) return;
if (newSL>=OrderStopLoss()) continue;
if (OrderModify(OrderTicket(),OrderOpenPrice(),newSL,OrderTakeProfit(),0,Yellow))
{
OrdLot = NormalizeDouble(OrderLots()/3.0,3);
if (OrdLot<min) OrdLot=min;
OrderClose(OrderTicket(),OrdLot,Ask,Slippage,Red);
continue;
}
}
}
continue;
}
return;
}
//===================================================================================================================
//+------------------------------------------------------------------+
//| Check for open Buy order conditions Diver |
//+------------------------------------------------------------------+
void CheckForOpenDiverBuy()
{
if (Time[0]==prevtimebuy_Di) return;
prevtimebuy_Di=Time[0];
double DiverTPBuy = 0;
double DiverSLBuy = 0;
bool GolierBuy = false;
int i, k=OrdersTotal()-1;
for(i=k; i>=0; i--)
if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
{
{
if(OrderSymbol()==Symbol()&& OrderType()==OP_BUY && OrderMagicNumber()==MAGICGO1) // && OrderOpenTime()<TimeCurrent()-260000)
{
SLBuy=OrderStopLoss();
TPBuy=OrderTakeProfit();
GolierBuy = true;
break;
}
else continue;
}
}
double DiverBuy = iCustom(NULL,TF,"顶级趋势V5",fastEMAbuy,slowEMAbuy,signalSMAbuy,0,2);
if (DiverBuy != 2147483647 && GolierBuy == true)
{
OpenPosition(OP_BUY, SLBuy, TPBuy, MAGICDI1, LotsOptimizedBuy()*3, " Diver Buy "+MAGICDI1);
}
}
//+------------------------------------------------------------------+
//| Check for open Sell order conditions Diver |
//+------------------------------------------------------------------+
void CheckForOpenDiverSell()
{
if (Time[0]==prevtimesell_Di) return;
prevtimesell_Di=Time[0];
double DiverTPSell = 0;
double DiverSLSell = 0;
bool GolierSell = false;
int i, k=OrdersTotal()-1;
for(i=k; i>=0; i--)
if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
{
{
if(OrderSymbol()==Symbol()&& OrderType()==OP_SELL && OrderMagicNumber()==MAGICGO2) // && OrderOpenTime()<TimeCurrent()-260000)
{
SLBuy=OrderStopLoss();
TPBuy=OrderTakeProfit();
GolierSell = true;
break;
}
else continue;
}
}
DiverSell = iCustom(NULL,TF,"顶级趋势V5",fastEMAsell,slowEMAsell,signalSMAsell,1,2);
if(DiverSell != 2147483647 && GolierSell == true)
{
OpenPosition(OP_SELL, SLSell, TPSell, MAGICDI2, LotsOptimizedSell()*3, " Diver Sell"+MAGICDI2);
}
}
//===================================================================================================================
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
void PlotLine(string name,double value,color line_color, datetime time1, datetime time2)
{
double valueN=NormalizeDouble(value,Digits);
bool res = ObjectCreate(name,OBJ_TREND,0,time1,valueN,time2,valueN);
ObjectSet(name, OBJPROP_WIDTH, width);
ObjectSet(name, OBJPROP_STYLE, style);
ObjectSet(name, OBJPROP_RAY, false);
ObjectSet(name, OBJPROP_BACK, true);
ObjectSet(name, OBJPROP_COLOR, line_color);
}
//+------------------------------------------------------------------+
//| Init function |
//+------------------------------------------------------------------+
void init ()
{
SymbolString = Symbol();
min=MarketInfo(Symbol(),MODE_MINLOT);
max=MarketInfo(Symbol(),MODE_MAXLOT);
Comment ("The Expert is loaded and waiting data.");
}
//+------------------------------------------------------------------+
//| Start function |
//+------------------------------------------------------------------+
void start()
{
if(Bars<100 || IsTradeAllowed()==false) return;
Comment (SymbolString,": The Expert is working..."+"\n Time: ", TimeToStr( TimeCurrent(), TIME_DATE|TIME_SECONDS));
if(OrdersTotal()>0)
{
Check_TR_Buy();
Check_TR_Sell();
}
if (EA_Goiler==true)
{
if(TimeHour(TimeCurrent()) == Start_Goiler) Goiler();
}
if (EA_Diver==true)
{
CheckForOpenDiverBuy();
CheckForOpenDiverSell();
}
}
// =================================================================================================
|