
//+------------------------------------------------------------------+
//| ZigZagAlert.mq4 |
//| Copyright 2006-2014, MetaQuotes Software Corp. |
//| http://www.mql4.com |
//+------------------------------------------------------------------+
#property copyright "2006-2014, MetaQuotes Software Corp."
#property link "http://www.mql4.com"
#property strict
#property indicator_chart_window
#property indicator_buffers 1
#property indicator_color1 Red
//---- indicator parameters
input int InpDepth=12; // Depth
input int InpDeviation=5; // Deviation
input int InpBackstep=3; // Backstep
//---- indicator buffers
double ExtZigzagBuffer[];
double ExtHighBuffer[];
double ExtLowBuffer[];
//--- globals
int ExtLevel=3; // recounting's depth of extremums
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
if(InpBackstep>=InpDepth)
{
Print("Backstep cannot be greater or equal to Depth");
return(INIT_FAILED);
}
//--- 2 additional buffers
IndicatorBuffers(3);
//---- drawing settings
SetIndexStyle(0,DRAW_SECTION);
//---- indicator buffers
SetIndexBuffer(0,ExtZigzagBuffer);
SetIndexBuffer(1,ExtHighBuffer);
SetIndexBuffer(2,ExtLowBuffer);
SetIndexEmptyValue(0,0.0);
//---- indicator short name
IndicatorShortName("ZigZag("+string(InpDepth)+","+string(InpDeviation)+","+string(InpBackstep)+")");
//---- initialization done
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
const int prev_calculated,
const datetime &time[],
const double &open[],
const double &high[],
const double &low[],
const double &close[],
const long &tick_volume[],
const long &volume[],
const int &spread[])
{
int i,limit,counterZ,whatlookfor=0;
int back,pos,lasthighpos=0,lastlowpos=0;
double extremum;
double curlow=0.0,curhigh=0.0,lasthigh=0.0,lastlow=0.0;
//--- check for history and inputs
if(rates_total<InpDepth || InpBackstep>=InpDepth)
return(0);
//--- first calculations
if(prev_calculated==0)
limit=InitializeAll();
else
{
//--- find first extremum in the depth ExtLevel or 100 last bars
i=counterZ=0;
while(counterZ<ExtLevel && i<100)
{
if(ExtZigzagBuffer[i]!=0.0)
counterZ++;
i++;
}
//--- no extremum found - recounting all from begin
if(counterZ==0)
limit=InitializeAll();
else
{
//--- set start position to found extremum position
limit=i-1;
//--- what kind of extremum?
if(ExtLowBuffer[i]!=0.0)
{
//--- low extremum
curlow=ExtLowBuffer[i];
//--- will look for the next high extremum
whatlookfor=1;
}
else
{
//--- high extremum
curhigh=ExtHighBuffer[i];
//--- will look for the next low extremum
whatlookfor=-1;
}
//--- clear the rest data
for(i=limit-1; i>=0; i--)
{
ExtZigzagBuffer[i]=0.0;
ExtLowBuffer[i]=0.0;
ExtHighBuffer[i]=0.0;
}
}
}
//--- main loop
for(i=limit; i>=0; i--)
{
//--- find lowest low in depth of bars
extremum=low[iLowest(NULL,0,MODE_LOW,InpDepth,i)];
//--- this lowest has been found previously
if(extremum==lastlow)
extremum=0.0;
else
{
//--- new last low
lastlow=extremum;
//--- discard extremum if current low is too high
if(low[i]-extremum>InpDeviation*Point)
extremum=0.0;
else
{
//--- clear previous extremums in backstep bars
for(back=1; back<=InpBackstep; back++)
{
pos=i+back;
if(ExtLowBuffer[pos]!=0 && ExtLowBuffer[pos]>extremum)
ExtLowBuffer[pos]=0.0;
}
}
}
//--- found extremum is current low
if(low[i]==extremum)
ExtLowBuffer[i]=extremum;
else
ExtLowBuffer[i]=0.0;
//--- find highest high in depth of bars
extremum=high[iHighest(NULL,0,MODE_HIGH,InpDepth,i)];
//--- this highest has been found previously
if(extremum==lasthigh)
extremum=0.0;
else
{
//--- new last high
lasthigh=extremum;
//--- discard extremum if current high is too low
if(extremum-high[i]>InpDeviation*Point)
extremum=0.0;
else
{
//--- clear previous extremums in backstep bars
for(back=1; back<=InpBackstep; back++)
{
pos=i+back;
if(ExtHighBuffer[pos]!=0 && ExtHighBuffer[pos]<extremum)
ExtHighBuffer[pos]=0.0;
}
}
}
//--- found extremum is current high
if(high[i]==extremum)
ExtHighBuffer[i]=extremum;
else
ExtHighBuffer[i]=0.0;
}
//--- final cutting
if(whatlookfor==0)
{
lastlow=0.0;
lasthigh=0.0;
}
else
{
lastlow=curlow;
lasthigh=curhigh;
}
for(i=limit; i>=0; i--)
{
switch(whatlookfor)
{
case 0: // look for peak or lawn
if(lastlow==0.0 && lasthigh==0.0)
{
if(ExtHighBuffer[i]!=0.0)
{
lasthigh=High[i];
lasthighpos=i;
whatlookfor=-1;
ExtZigzagBuffer[i]=lasthigh;
}
if(ExtLowBuffer[i]!=0.0)
{
lastlow=Low[i];
lastlowpos=i;
whatlookfor=1;
ExtZigzagBuffer[i]=lastlow;
}
}
break;
case 1: // look for peak
if(ExtLowBuffer[i]!=0.0 && ExtLowBuffer[i]<lastlow && ExtHighBuffer[i]==0.0)
{
ExtZigzagBuffer[lastlowpos]=0.0;
lastlowpos=i;
lastlow=ExtLowBuffer[i];
ExtZigzagBuffer[i]=lastlow;
}
if(ExtHighBuffer[i]!=0.0 && ExtLowBuffer[i]==0.0)
{
lasthigh=ExtHighBuffer[i];
lasthighpos=i;
ExtZigzagBuffer[i]=lasthigh;
whatlookfor=-1;
}
break;
case -1: // look for lawn
if(ExtHighBuffer[i]!=0.0 && ExtHighBuffer[i]>lasthigh && ExtLowBuffer[i]==0.0)
{
ExtZigzagBuffer[lasthighpos]=0.0;
lasthighpos=i;
lasthigh=ExtHighBuffer[i];
ExtZigzagBuffer[i]=lasthigh;
}
if(ExtLowBuffer[i]!=0.0 && ExtHighBuffer[i]==0.0)
{
lastlow=ExtLowBuffer[i];
lastlowpos=i;
ExtZigzagBuffer[i]=lastlow;
whatlookfor=1;
}
break;
}
}
//--- done
if(ExtZigzagBuffer[1]>0) Alert("Окончание луча: " + DoubleToString(ExtZigzagBuffer[1],Digits) + " " +Symbol() + " " + IntegerToString(Period()));
Comment("\n ZZ0: ",ExtZigzagBuffer[0],
"\n Окончание луча: ",ExtZigzagBuffer[1],
"\n Верхний излом: ",ExtHighBuffer[0],
"\n Нижний излом: ",ExtLowBuffer[0]);
return(rates_total);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
int InitializeAll()
{
ArrayInitialize(ExtZigzagBuffer,0.0);
ArrayInitialize(ExtHighBuffer,0.0);
ArrayInitialize(ExtLowBuffer,0.0);
//--- first counting position
return(Bars-InpDepth);
}
//+------------------------------------------------------------------+
Буду очень признателен если и на МТ4 сделаете.)Cмотрели код постом выше?
Можете подсказать, как добавить мартингейл в советник?
//+------------------------------------------------------------------+
//| TimeOpen.mq4 |
//| Copyright 2015, AM2 |
//| http://www.forexsystems.biz |
//+------------------------------------------------------------------+
#property copyright "Copyright 2015, AM2"
#property link "http://www.forexsystems.biz"
#property version "1.00"
#property description "Time Open expert advisor"
#include <stdlib.mqh>
//--- Inputs
extern int StopLoss = 555; // лось
extern int TakeProfit = 555; // язь
extern int BuySell = 2; // 0-бай. 1-селл. 2-бай/селл
extern int Slip = 100; // проскальзывание
extern int Count = 1; // количество открываемых ордеров
extern int MAGIC = 123; // магик
extern double Lots = 0.1; // лот
extern double KLot = 2; // увеличение лота
extern double MaxLot = 5.0; // максимальный лот
extern string t = "Время";
extern int t1 = 1; // время 1
extern int t2 = 5; // время 2
extern int t3 = 15; // время 3
extern int t4 = 30; // время 4
extern int t5 = 60; // время 5
extern int t6 = 240; // время 6
extern int t7 = 1440; // время 7
extern int t8 = 10080; // время 8
extern int t9 = 43200; // время 9
int tf[9],k=0;
datetime StartTime;
//+------------------------------------------------------------------+
//| Expert initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
//---
StartTime=TimeCurrent();
tf[0]=t1;
tf[1]=t2;
tf[2]=t3;
tf[3]=t4;
tf[4]=t5;
tf[5]=t6;
tf[6]=t7;
tf[7]=t8;
tf[8]=t9;
//---
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Expert deinitialization function |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
//---
}
//+------------------------------------------------------------------+
int CountTrades()
{
int count=0;
for(int i=OrdersTotal()-1;i>=0;i--)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
{
if(OrderSymbol()==Symbol() && OrderMagicNumber()==MAGIC)
{
if(OrderType()==OP_BUY || OrderType()==OP_SELL) count++;
}
}
}
return(count);
}
//+------------------------------------------------------------------+
void OpenOrd()
{
int res,err;
double sl,tp;
double ask=MarketInfo(OrderSymbol(),MODE_ASK);
double bid=MarketInfo(OrderSymbol(),MODE_BID);
//--- sell conditions
if(BuySell==1)
{
if(StopLoss>0) sl=NormalizeDouble(bid+StopLoss*Point,Digits); else sl=0;
if(TakeProfit>0) tp=NormalizeDouble(bid-TakeProfit*Point,Digits); else tp=0;
res=OrderSend(Symbol(),OP_SELL,Lot(),NormalizeDouble(bid,Digits),Slip,sl,tp,"",MAGIC,0,Red);
if(res<0)
{
err=GetLastError();
Print("ОШИБКА ВЫСТАВЛЕНИЯ ОРДЕРА SELL: ",err,"(",ErrorDescription(err),")");
} else {
RefreshRates();
}
return;
}
Sleep(1000);
//--- buy conditions
if(BuySell==0)
{
if(StopLoss>0) sl=NormalizeDouble(ask-StopLoss*Point,Digits); else sl=0;
if(TakeProfit>0) tp=NormalizeDouble(ask+TakeProfit*Point,Digits); else tp=0;
res=OrderSend(Symbol(),OP_BUY,Lot(),NormalizeDouble(ask,Digits),Slip,sl,tp,"",MAGIC,0,Blue);
if(res<0)
{
err=GetLastError();
Print("ОШИБКА ВЫСТАВЛЕНИЯ ОРДЕРА BUY: ",err,"(",ErrorDescription(err),")");
} else {
RefreshRates();
}
return;
}
Sleep(1000);
//--- buy sell conditions
if(BuySell==2)
{
//--- buy conditions
if(LastDealType()==2 || LastDealType()==0)
{
if(StopLoss>0) sl=NormalizeDouble(ask-StopLoss*Point,Digits); else sl=0;
if(TakeProfit>0) tp=NormalizeDouble(ask+TakeProfit*Point,Digits); else tp=0;
res=OrderSend(Symbol(),OP_BUY,Lot(),NormalizeDouble(ask,Digits),Slip,sl,tp,"",MAGIC,0,Blue);
if(res<0)
{
err=GetLastError();
Print("ОШИБКА ВЫСТАВЛЕНИЯ ОРДЕРА BUY: ",err,"(",ErrorDescription(err),")");
} else {
RefreshRates();
}
return;
}
Sleep(1000);
//--- sell conditions
if(LastDealType()==1 || LastDealType()==0)
{
if(StopLoss>0) sl=NormalizeDouble(bid+StopLoss*Point,Digits); else sl=0;
if(TakeProfit>0) tp=NormalizeDouble(bid-TakeProfit*Point,Digits); else tp=0;
res=OrderSend(Symbol(),OP_SELL,Lot(),NormalizeDouble(bid,Digits),Slip,sl,tp,"",MAGIC,0,Red);
if(res<0)
{
err=GetLastError();
Print("ОШИБКА ВЫСТАВЛЕНИЯ ОРДЕРА SELL: ",err,"(",ErrorDescription(err),")");
} else {
RefreshRates();
}
return;
}
Sleep(1000);
}
//---
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
int LastDealType()
{
int type=0;
if(OrdersHistoryTotal()==0)
{
type=0;
}
if(OrderSelect(OrdersHistoryTotal()-1,SELECT_BY_POS,MODE_HISTORY))
{
if(OrderType()==OP_BUY)
{
type=1;//buy
}
if(OrderType()==OP_SELL)
{
type=2;//sell
}
}
return(type);
}
//+------------------------------------------------------------------+
void CloseAll()
{
bool cl,sel;
for(int i=0; i<OrdersTotal(); i++)
{
sel=OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
if(OrderSymbol()==Symbol() && OrderMagicNumber()==MAGIC)
{
if(OrderType()==OP_BUY) cl=OrderClose(OrderTicket(),OrderLots(),Bid,Slip,Blue);
if(OrderType()==OP_SELL) cl=OrderClose(OrderTicket(),OrderLots(),Ask,Slip,Red);
}
Sleep(1000);
}
}
//+------------------------------------------------------------------+
//| Calculate optimal lot size |
//+------------------------------------------------------------------+
double Lot()
{
double lot;
if(OrdersHistoryTotal()==0)
{
lot=Lots;
}
if(OrderSelect(OrdersHistoryTotal()-1,SELECT_BY_POS,MODE_HISTORY))
{
if(OrderProfit()>0)
{
lot=Lots;
k=0;
}
if(OrderProfit()<0)
{
lot=OrderLots()*KLot;
}
}
if(lot>MaxLot || lot==0) lot=Lots;
return(lot);
}
//+------------------------------------------------------------------+
//| OnTick function |
//+------------------------------------------------------------------+
void OnTick()
{
if(k>9) k=0;
if(CountTrades()<Count) OpenOrd();
if((TimeCurrent()-StartTime)>tf[k]*60)
{
k++;
CloseAll();
StartTime=TimeCurrent();
}
Comment("\n Current Time: ",TimeToString(TimeCurrent(),TIME_SECONDS),
"\n Start Time: ",TimeToString(StartTime,TIME_SECONDS),
"\n Time Frame: ",tf[k],
"\n K: ",k,
"\n Lot: ",Lot(),
"\n Last Deal Type: ",LastDealType());
}
//+------------------------------------------------------------------+
AM2