Программирование прибыли: от азов к секретам мастерства. Читайте, спрашивайте, делитесь опытом.
Бонус за сообщение 0.5$
Ответственный Модератор - Haos
Re: Разбор кода на MQL.
Рэндом » 01 окт 2013, 04:59
Индикатор FiboMA МТ5. Тема
viewtopic.php?f=9&t=207- Код: выделить все
//+------------------------------------------------------------------+
//| FiboMA.mq5 |
//| Copyright 2013, MetaQuotes Software Corp. |
//| http://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2013, MetaQuotes Software Corp."
#property link "http://www.mql5.com"
#property version "1.00"
#property indicator_chart_window
#property indicator_buffers 11
#property indicator_plots 11
//--- plot Main
#property indicator_label1 "Main"
#property indicator_type1 DRAW_LINE
#property indicator_color1 clrRed
#property indicator_style1 STYLE_SOLID
#property indicator_width1 1
//--- plot P1
#property indicator_label2 "P1"
#property indicator_type2 DRAW_LINE
#property indicator_color2 clrNavy
#property indicator_style2 STYLE_SOLID
#property indicator_width2 1
//--- plot P2
#property indicator_label3 "P2"
#property indicator_type3 DRAW_LINE
#property indicator_color3 clrNavy
#property indicator_style3 STYLE_SOLID
#property indicator_width3 1
//--- plot P3
#property indicator_label4 "P3"
#property indicator_type4 DRAW_LINE
#property indicator_color4 clrNavy
#property indicator_style4 STYLE_SOLID
#property indicator_width4 1
//--- plot P4
#property indicator_label5 "P4"
#property indicator_type5 DRAW_LINE
#property indicator_color5 clrNavy
#property indicator_style5 STYLE_SOLID
#property indicator_width5 1
//--- plot P5
#property indicator_label6 "P5"
#property indicator_type6 DRAW_LINE
#property indicator_color6 clrNavy
#property indicator_style6 STYLE_SOLID
#property indicator_width6 1
//--- plot S1
#property indicator_label7 "S1"
#property indicator_type7 DRAW_LINE
#property indicator_color7 clrDarkGreen
#property indicator_style7 STYLE_SOLID
#property indicator_width7 1
//--- plot S2
#property indicator_label8 "S2"
#property indicator_type8 DRAW_LINE
#property indicator_color8 clrDarkGreen
#property indicator_style8 STYLE_SOLID
#property indicator_width8 1
//--- plot S3
#property indicator_label9 "S3"
#property indicator_type9 DRAW_LINE
#property indicator_color9 clrDarkGreen
#property indicator_style9 STYLE_SOLID
#property indicator_width9 1
//--- plot S4
#property indicator_label10 "S4"
#property indicator_type10 DRAW_LINE
#property indicator_color10 clrDarkGreen
#property indicator_style10 STYLE_SOLID
#property indicator_width10 1
//--- plot S5
#property indicator_label11 "S5"
#property indicator_type11 DRAW_LINE
#property indicator_color11 clrDarkGreen
#property indicator_style11 STYLE_SOLID
#property indicator_width11 1
//--- input parameters
input int Diapozon=100;
input int MA_Period=55;
//--- indicator buffers
double MainBuffer[];
double P1Buffer[];
double P2Buffer[];
double P3Buffer[];
double P4Buffer[];
double P5Buffer[];
double S1Buffer[];
double S2Buffer[];
double S3Buffer[];
double S4Buffer[];
double S5Buffer[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int ma;
int OnInit()
{
//--- indicator buffers mapping
SetIndexBuffer(0,MainBuffer,INDICATOR_DATA);
SetIndexBuffer(1,P1Buffer,INDICATOR_DATA);
SetIndexBuffer(2,P2Buffer,INDICATOR_DATA);
SetIndexBuffer(3,P3Buffer,INDICATOR_DATA);
SetIndexBuffer(4,P4Buffer,INDICATOR_DATA);
SetIndexBuffer(5,P5Buffer,INDICATOR_DATA);
SetIndexBuffer(6,S1Buffer,INDICATOR_DATA);
SetIndexBuffer(7,S2Buffer,INDICATOR_DATA);
SetIndexBuffer(8,S3Buffer,INDICATOR_DATA);
SetIndexBuffer(9,S4Buffer,INDICATOR_DATA);
SetIndexBuffer(10,S5Buffer,INDICATOR_DATA);
ma=iMA(_Symbol,_Period,MA_Period,0,MODE_EMA,PRICE_MEDIAN);
//---
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function |
//+------------------------------------------------------------------+
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 to_copy;
if(prev_calculated>rates_total || prev_calculated<0) to_copy=rates_total;
else
{
to_copy=rates_total-prev_calculated;
if(prev_calculated>0) to_copy++;
}
if(IsStopped()) return(0);
if(CopyBuffer(ma,0,0,to_copy,MainBuffer)<=0)
{
Print("Getting EMA is failed! Error",GetLastError());
return(0);
}
int limit;
if(prev_calculated==0)
limit=0;
else limit=prev_calculated-1;
for(int i=limit;i<rates_total && !IsStopped();i++)
{
S1Buffer[i]=MainBuffer[i]+Diapozon*0.24*_Point;
S2Buffer[i]=MainBuffer[i]+Diapozon*0.38*_Point;
S3Buffer[i]=MainBuffer[i]+Diapozon*0.5*_Point;
S4Buffer[i]=MainBuffer[i]+Diapozon*0.68*_Point;
S5Buffer[i]=MainBuffer[i]+Diapozon*_Point;
P1Buffer[i]=MainBuffer[i]-Diapozon*0.24*_Point;
P2Buffer[i]=MainBuffer[i]-Diapozon*0.38*_Point;
P3Buffer[i]=MainBuffer[i]-Diapozon*0.5*_Point;
P4Buffer[i]=MainBuffer[i]-Diapozon*0.68*_Point;
P5Buffer[i]=MainBuffer[i]-Diapozon*_Point;
}
//--- return value of prev_calculated for next call
return(rates_total);
}
//+------------------------------------------------------------------+
Здесь интересна эта строка кода:
- Код: выделить все
CopyBuffer(ma,0,0,to_copy,MainBuffer)
Данные встроенного индикатора копируются напрямую в буфер индикатора. Этот прием хорош если вам надо без изменений отобразить встроенный индикатор и вывести дополнительную информацию.
-

Рэндом
- Специалист MQL
-
- Сообщений: 13700
- Зарегистрирован: 18 июл 2013, 08:05
- Средств на руках: 31.45

- Группа: Администраторы
- Благодарил (а): 1131 раз.
- Поблагодарили: 3174 раз.
Каждый заблуждается в меру своих возможностей.
Re: Разбор кода на MQL.
Рэндом » 28 окт 2013, 03:44
Советник LazyTrader. Тема с советником
viewtopic.php?f=9&t=228Код
- Код: выделить все
//+------------------------------------------------------------------+
//| LazyTrader.mq4 |
//| Рэндом |
//| http://mt4.maxiforex.ru/ |
//+------------------------------------------------------------------+
#property copyright "Рэндом"
#property link "http://mt4.maxiforex.ru/"
//--- input parameters
extern int Step=200;
extern int Slipage=30;
extern double Lot=0.1;
//+------------------------------------------------------------------+
//| expert initialization function |
//+------------------------------------------------------------------+
double sl;
int init()
{
//----
//----
return(0);
}
//+------------------------------------------------------------------+
//| expert deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
//----
//----
return(0);
}
//+------------------------------------------------------------------+
//| expert start function |
//+------------------------------------------------------------------+
int start()
{
//----
if(Period()!=240)
{
Print("Установите советник на H4");
return(0);
}
if(OrdersTotal()==0)
{
datetime d=Time[0];
if(TimeDayOfWeek(d)==1)
if(TimeHour(d)==4)
{
double h=High[1]+Step*Point;
double l=Low[1]-Step*Point;
sl=(h-l)/Point;
OrderSend(Symbol(),OP_BUYSTOP,Lot,h,Slipage,h-sl*Point,h+3*sl*Point);
OrderSend(Symbol(),OP_SELLSTOP,Lot,l,Slipage,l+sl*Point,l-3*sl*Point);
}
}
else
{
int limit=OrdersTotal()-1;
for(int i=limit;i>=0;i--)
{
OrderSelect(i,SELECT_BY_POS);
if(OrderType()==OP_BUY && OrderStopLoss()!=OrderOpenPrice())
if(Ask>OrderOpenPrice()+sl*Point) OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice(),OrderTakeProfit(),0);
if(OrderType()==OP_SELL && OrderStopLoss()!=OrderOpenPrice())
if(Bid<OrderOpenPrice()-sl*Point)OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice(),OrderTakeProfit(),0);
}
d=Time[0];
if(TimeDayOfWeek(d)==5)
if(TimeHour(d)==20)
{
for(i=limit;i>=0;i--)
{
OrderSelect(i,SELECT_BY_POS);
if(OrderType()==OP_BUY) OrderClose(OrderTicket(),OrderLots(),Bid,Slipage);
if(OrderType()==OP_SELL) OrderClose(OrderTicket(),OrderLots(),Ask,Slipage);
if(OrderType()==OP_BUYSTOP || OrderType()==OP_SELLSTOP) OrderDelete(OrderTicket());
}
}
}
//----
return(0);
}
//+------------------------------------------------------------------+
Здесь нас интересует следующая строчка:
- Код: выделить все
double sl;
Это глобальная переменная. Все дело в том что нам надо сохранить значение стоп лосса между вызовами функции start. Вот для этого и используется глобальная переменная. И это тот случай когда ее нужно использовать.
-

Рэндом
- Специалист MQL
-
- Сообщений: 13700
- Зарегистрирован: 18 июл 2013, 08:05
- Средств на руках: 31.45

- Группа: Администраторы
- Благодарил (а): 1131 раз.
- Поблагодарили: 3174 раз.
Каждый заблуждается в меру своих возможностей.
Re: Разбор кода на MQL.
ahnenerbe » 10 фев 2014, 10:58
Пробуем, Спасибо!
Последний раз редактировалось
Рэндом 11 фев 2014, 06:16, всего редактировалось 1 раз.
Причина: Бонус снят.
-

ahnenerbe
-
- Сообщений: 6
- Зарегистрирован: 10 фев 2014, 10:19
- Средств на руках: 0.00

- Группа: Новые пользователи
- Благодарил (а): 0 раз.
- Поблагодарили: 0 раз.
Re: Разбор кода на MQL.
Рэндом » 05 июн 2014, 02:06
- Код: выделить все
//+------------------------------------------------------------------+
//| Hobby.mq4 |
//| Рэндом |
//| http://investforum.ru/ |
//+------------------------------------------------------------------+
#property copyright "Рэндом"
#property link "http://investforum.ru/"
#property version "1.00"
#property strict
//--- input parameters
input int EMAHigh=5;
input int EMALow=5;
input double Lot=0.1;
input int TP1=0;
input int TP2=300;
input int TP3=500;
input int Slippage=50;
input int BezU=200;
input int TrallingStop=200;
//+------------------------------------------------------------------+
//| Expert initialization function |
//+------------------------------------------------------------------+
double sl;
int OnInit()
{
//---
//---
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Expert deinitialization function |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
//---
}
//+------------------------------------------------------------------+
//| Expert tick function |
//+------------------------------------------------------------------+
void OnTick()
{
//---
if(OrdersTotal()==0)
{
if(Volume[0]>1) return;
if(Buy())
{
double tp;
sl=Low[1];
if(TP1>0) tp=Ask+TP1*Point;
else tp=iMA(Symbol(),Period(),EMAHigh,0,MODE_EMA,PRICE_HIGH,1);
int rez=OrderSend(Symbol(),OP_BUY,Lot,Ask,Slippage,sl,tp);
rez=OrderSend(Symbol(),OP_BUY,Lot,Ask,Slippage,sl,tp+TP2*Point);
rez=OrderSend(Symbol(),OP_BUY,Lot,Ask,Slippage,sl,tp+TP3*Point);
return;
}
if(Sell())
{
double tp;
sl=High[1];
if(TP1>0) tp=Bid-TP1*Point;
else tp=iMA(Symbol(),Period(),EMALow,0,MODE_EMA,PRICE_LOW,1);
int rez=OrderSend(Symbol(),OP_SELL,Lot,Bid,Slippage,sl,tp);
rez=OrderSend(Symbol(),OP_SELL,Lot,Bid,Slippage,sl,tp-TP2*Point);
rez=OrderSend(Symbol(),OP_SELL,Lot,Bid,Slippage,sl,tp-TP3*Point);
return;
}
}
if(OrdersTotal()>0)
{
int limit=OrdersTotal();
for(int i=0;i<limit;i++)
{
int r=OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
if(OrderType()==OP_BUY)
{
if(Bid>OrderOpenPrice()+BezU*Point && OrderStopLoss()<OrderOpenPrice()) int rez=OrderModify(OrderTicket(),Ask,OrderOpenPrice(),OrderTakeProfit(),0);
}
if(OrderType()==OP_SELL)
{
if(Bid<OrderOpenPrice()-BezU*Point && OrderStopLoss()>OrderOpenPrice()) int rez=OrderModify(OrderTicket(),Bid,OrderOpenPrice(),OrderTakeProfit(),0);
}
}
}
if(OrdersTotal()>0 && OrdersTotal()<3)
{
int limit=OrdersTotal();
for(int i=0;i<limit;i++)
{
int r=OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
if(OrderType()==OP_BUY)
{
if(Bid>OrderStopLoss()+sl*Point+TrallingStop*Point) int rez=OrderModify(OrderTicket(),Ask,Bid-sl,OrderTakeProfit(),0);
}
if(OrderType()==OP_SELL)
{
if(Bid<OrderStopLoss()-sl*Point-TrallingStop*Point) int rez=OrderModify(OrderTicket(),Bid,Ask+sl,OrderTakeProfit(),0);
}
}
}
}
//+------------------------------------------------------------------+
bool Sell()
{
double eh=iMA(Symbol(),Period(),EMAHigh,0,MODE_EMA,PRICE_HIGH,1);
if(High[1]>eh && Close[1]<eh) return true;
return false;
}
bool Buy()
{
double el=iMA(Symbol(),Period(),EMALow,0,MODE_EMA,PRICE_LOW,1);
if(Low[1]<el && Close[1]>el) return true;
return false;
}
С новыми билдами произошли изменения в языке. Тут нам интересно input int TP1=0; Это аналог оператора extern. И еще интересно это void OnTick(). Эта функция заменила функцию Start и так же вызывается при каждом тике.
-

Рэндом
- Специалист MQL
-
- Сообщений: 13700
- Зарегистрирован: 18 июл 2013, 08:05
- Средств на руках: 31.45

- Группа: Администраторы
- Благодарил (а): 1131 раз.
- Поблагодарили: 3174 раз.
Каждый заблуждается в меру своих возможностей.
Кто сейчас на форуме?
Сейчас этот форум просматривают: нет зарегистрированных пользователей и гости: 28
Права доступа к форуму
Вы не можете начинать темы
Вы не можете отвечать на сообщения
Вы не можете редактировать свои сообщения
Вы не можете удалять свои сообщения
Вы не можете добавлять вложения