Советник с применением ООП. Реальный пример |
//+------------------------------------------------------------------+
//| 2Mashki.mq4 |
//| Copyright 2016, AM2 |
//| http://www.forexsystems.biz |
//+------------------------------------------------------------------+
#property copyright "Copyright 2016, AM2"
#property link "http://www.forexsystems.biz"
#property version "1.00"
#property strict
//--- Inputs
extern double Lots = 0.1; // лот
extern double KLot = 1; // умножение лота
extern double MaxLot = 5; // максимальный лот
extern double Loss = 3000; // лось в рублях
extern double Profit = 20; // язь в рублях
extern int StopLoss = 0; // лось
extern int TakeProfit = 0; // язь
extern int TrailingStop = 0; // трал
extern int MA1Period = 12; // период МА1
extern int MA2Period = 40; // период МА2
extern int Slip = 30; // реквот
extern int Shift = 1; // на каком баре сигнал индикатора
extern int Magic = 123; // магик
datetime t=0;
//+------------------------------------------------------------------+
//| Expert initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Expert deinitialization function |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
Comment("");
}
//+------------------------------------------------------------------+
//| Установка ордера |
//+------------------------------------------------------------------+
void PutOrder(int type,double price)
{
int r=0,err=0;
color clr=Green;
double sl=0,tp=0;
if(type==1 || type==3 || type==5)
{
clr=Red;
if(StopLoss>0) sl=NormalizeDouble(price+StopLoss*Point,Digits);
if(TakeProfit>0) tp=NormalizeDouble(price-TakeProfit*Point,Digits);
}
if(type==0 || type==2 || type==4)
{
clr=Blue;
if(StopLoss>0) sl=NormalizeDouble(price-StopLoss*Point,Digits);
if(TakeProfit>0) tp=NormalizeDouble(price+TakeProfit*Point,Digits);
}
r=OrderSend(NULL,type,Lot(),NormalizeDouble(price,Digits),Slip,sl,tp,"",Magic,0,clr);
return;
}
//+------------------------------------------------------------------+
//| Лот для усреднителя |
//+------------------------------------------------------------------+
double Lot()
{
double lot=Lots;
double MinimumLot = MarketInfo(NULL,MODE_MINLOT);
double MaximumLot = MarketInfo(NULL,MODE_MAXLOT);
if(CountTrades()>0) lot=NormalizeDouble(Lots*MathPow(KLot,CountTrades()),2);
if(lot>MaxLot)lot=Lots;
if(lot<MinimumLot) lot=MinimumLot;
if(lot>MaximumLot) lot=MaximumLot;
return(lot);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
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()<2) count++;
}
}
}
return(count);
}
//+------------------------------------------------------------------+
//| Профит всех ордеров по типу ордера |
//+------------------------------------------------------------------+
double AllProfit(int ot=-1)
{
double pr=0;
for(int i=OrdersTotal()-1;i>=0;i--)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
{
if(OrderSymbol()==Symbol() && OrderMagicNumber()==Magic)
{
if(OrderType()==0 && (ot==0 || ot==-1))
{
pr+=OrderProfit()+OrderCommission()+OrderSwap();
}
if(OrderType()==1 && (ot==1 || ot==-1))
{
pr+=OrderProfit()+OrderCommission()+OrderSwap();
}
}
}
}
return(pr);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
void OpenOrd()
{
double MA11=iMA(NULL,0,MA1Period,0,0,0,Shift);
double MA12=iMA(NULL,0,MA1Period,0,0,0,Shift+1);
double MA21=iMA(NULL,0,MA2Period,0,0,0,Shift);
double MA22=iMA(NULL,0,MA2Period,0,0,0,Shift+1);
if(MA11<MA21 && MA12>MA22)
{
PutOrder(1,Bid);
}
if(MA11>MA21 && MA12<MA22)
{
PutOrder(0,Ask);
}
}
//+------------------------------------------------------------------+
//| Простой трал |
//+------------------------------------------------------------------+
void Trailing()
{
bool mod;
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)
{
if(Bid-OrderOpenPrice()>TrailingStop*Point)
{
if(OrderStopLoss()<Bid-TrailingStop*Point)
{
mod=OrderModify(OrderTicket(),OrderOpenPrice(),Bid-TrailingStop*Point,OrderTakeProfit(),0,Yellow);
return;
}
}
}
if(OrderType()==OP_SELL)
{
if((OrderOpenPrice()-Ask)>TrailingStop*Point)
{
if((OrderStopLoss()>(Ask+TrailingStop*Point)) || (OrderStopLoss()==0))
{
mod=OrderModify(OrderTicket(),OrderOpenPrice(),Ask+TrailingStop*Point,OrderTakeProfit(),0,Yellow);
return;
}
}
}
}
}
}
}
//+------------------------------------------------------------------+
//| Закрытие позиции по типу ордера |
//+------------------------------------------------------------------+
void CloseAll(int ot=-1)
{
bool cl;
for(int i=OrdersTotal()-1;i>=0;i--)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
{
if(OrderSymbol()==Symbol() && OrderMagicNumber()==Magic)
{
if(OrderType()==0 && (ot==0 || ot==-1))
{
RefreshRates();
cl=OrderClose(OrderTicket(),OrderLots(),NormalizeDouble(Bid,Digits),Slip,White);
}
if(OrderType()==1 && (ot==1 || ot==-1))
{
RefreshRates();
cl=OrderClose(OrderTicket(),OrderLots(),NormalizeDouble(Ask,Digits),Slip,White);
}
}
}
}
}
//+------------------------------------------------------------------+
//| Подсчет ордеров по типу |
//+------------------------------------------------------------------+
int CountOrders(int type)
{
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()==type) count++;
}
}
}
return(count);
}
//+------------------------------------------------------------------+
//| Expert tick function |
//+------------------------------------------------------------------+
void OnTick()
{
if(t!=Time[0])
{
if(TrailingStop>0) Trailing();
OpenOrd();
if(AllProfit(0)>Profit) CloseAll(0);
if(AllProfit(1)>Profit) CloseAll(1);
if(AllProfit()>Profit || AllProfit()<-Loss) CloseAll();
t=Time[0];
}
Comment("\n Открыто Buy ",CountOrders(0),
"\n Открыто Sell ",CountOrders(1),
"\n Общий профит: ",AllProfit(),
"\n Профит по Buy: ",AllProfit(0),
"\n Профит по Sell: ",AllProfit(1));
}
//+------------------------------------------------------------------+
class CMyClass
{
private:
int CountTrades();
public:
int CountOrders(int type);
double AllProfit(int ot=-1);
double Lot();
void PutOrder(int type,double price);
void CloseAll(int ot=-1);
void Trailing();
void OpenOrd();
};
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
int CMyClass::CountTrades(void)
{
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()<2) count++;
}
}
}
return(count);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
void CMyClass::CloseAll(int ot=-1)
{
bool cl;
for(int i=OrdersTotal()-1;i>=0;i--)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
{
if(OrderSymbol()==Symbol() && OrderMagicNumber()==Magic)
{
if(OrderType()==0 && (ot==0 || ot==-1))
{
RefreshRates();
cl=OrderClose(OrderTicket(),OrderLots(),NormalizeDouble(Bid,Digits),Slip,White);
}
if(OrderType()==1 && (ot==1 || ot==-1))
{
RefreshRates();
cl=OrderClose(OrderTicket(),OrderLots(),NormalizeDouble(Ask,Digits),Slip,White);
}
}
}
}
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
void CMyClass:<img src='http://opentraders.ru/templates/skin/g6h/images/smilies/008.gif' alt=' :P '> utOrder(int type,double price)
{
int r=0;
color clr=Green;
double sl=0,tp=0;
if(type==1 || type==3 || type==5)
{
clr=Red;
if(StopLoss>0) sl=NormalizeDouble(price+StopLoss*Point,Digits);
if(TakeProfit>0) tp=NormalizeDouble(price-TakeProfit*Point,Digits);
}
if(type==0 || type==2 || type==4)
{
clr=Blue;
if(StopLoss>0) sl=NormalizeDouble(price-StopLoss*Point,Digits);
if(TakeProfit>0) tp=NormalizeDouble(price+TakeProfit*Point,Digits);
}
r=OrderSend(NULL,type,Lot(),NormalizeDouble(price,Digits),Slip,sl,tp,"",Magic,0,clr);
return;
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
void CMyClass::OpenOrd()
{
double MA11=iMA(NULL,0,MA1Period,0,0,0,Shift);
double MA12=iMA(NULL,0,MA1Period,0,0,0,Shift+1);
double MA21=iMA(NULL,0,MA2Period,0,0,0,Shift);
double MA22=iMA(NULL,0,MA2Period,0,0,0,Shift+1);
if(MA11<MA21 && MA12>MA22)
{
PutOrder(1,Bid);
}
if(MA11>MA21 && MA12<MA22)
{
PutOrder(0,Ask);
}
}
//+------------------------------------------------------------------+
//| Простой трал |
//+------------------------------------------------------------------+
void CMyClass::Trailing()
{
bool mod;
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)
{
if(Bid-OrderOpenPrice()>TrailingStop*Point)
{
if(OrderStopLoss()<Bid-TrailingStop*Point)
{
mod=OrderModify(OrderTicket(),OrderOpenPrice(),Bid-TrailingStop*Point,OrderTakeProfit(),0,Yellow);
return;
}
}
}
if(OrderType()==OP_SELL)
{
if((OrderOpenPrice()-Ask)>TrailingStop*Point)
{
if((OrderStopLoss()>(Ask+TrailingStop*Point)) || (OrderStopLoss()==0))
{
mod=OrderModify(OrderTicket(),OrderOpenPrice(),Ask+TrailingStop*Point,OrderTakeProfit(),0,Yellow);
return;
}
}
}
}
}
}
}
//+------------------------------------------------------------------+
//| Профит всех ордеров по типу ордера |
//+------------------------------------------------------------------+
double CMyClass::AllProfit(int ot=-1)
{
double pr=0;
for(int i=OrdersTotal()-1;i>=0;i--)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
{
if(OrderSymbol()==Symbol() && OrderMagicNumber()==Magic)
{
if(OrderType()==0 && (ot==0 || ot==-1))
{
pr+=OrderProfit()+OrderCommission()+OrderSwap();
}
if(OrderType()==1 && (ot==1 || ot==-1))
{
pr+=OrderProfit()+OrderCommission()+OrderSwap();
}
}
}
}
return(pr);
}
//+------------------------------------------------------------------+
//| Подсчет ордеров по типу |
//+------------------------------------------------------------------+
int CMyClass::CountOrders(int type)
{
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()==type) count++;
}
}
}
return(count);
}
//+------------------------------------------------------------------+
//| Лот для усреднителя |
//+------------------------------------------------------------------+
double CMyClass::Lot()
{
double lot=Lots;
double MinimumLot = MarketInfo(NULL,MODE_MINLOT);
double MaximumLot = MarketInfo(NULL,MODE_MAXLOT);
if(mycl.CountTrades()>0) lot=NormalizeDouble(Lots*MathPow(KLot,mycl.CountTrades()),2);
if(lot>MaxLot)lot=Lots;
if(lot<MinimumLot) lot=MinimumLot;
if(lot>MaximumLot) lot=MaximumLot;
return(lot);
}
CMyClass mycl;
//+------------------------------------------------------------------+
//| Expert tick function |
//+------------------------------------------------------------------+
void OnTick()
{
if(t!=Time[0])
{
if(TrailingStop>0) mycl.Trailing();
mycl.OpenOrd();
if(mycl.AllProfit(0)>Profit) mycl.CloseAll(0);
if(mycl.AllProfit(1)>Profit) mycl.CloseAll(1);
if(mycl.AllProfit()>Profit || mycl.AllProfit()<-Loss) mycl.CloseAll();
t=Time[0];
}
Comment("\n Открыто Buy ",mycl.CountOrders(0),
"\n Открыто Sell ",mycl.CountOrders(1),
"\n Общий профит: ",mycl.AllProfit(),
"\n Профит по Buy: ",mycl.AllProfit(0),
"\n Профит по Sell: ",mycl.AllProfit(1));
}
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
//| 2MAOOP.mq4 |
//| Copyright 2016, AM2 |
//| http://www.forexsystems.biz |
//+------------------------------------------------------------------+
#property copyright "Copyright 2016, AM2"
#property link "http://www.forexsystems.biz"
#property version "1.00"
#property strict
//--- Inputs
extern double Lots = 0.1; // лот
extern double KLot = 1; // умножение лота
extern double MaxLot = 5; // максимальный лот
extern double Loss = 3000; // лось в рублях
extern double Profit = 20; // язь в рублях
extern int StopLoss = 0; // лось
extern int TakeProfit = 0; // язь
extern int TrailingStop = 0; // трал
extern int MA1Period = 12; // период МА1
extern int MA2Period = 40; // период МА2
extern int Slip = 30; // реквот
extern int Shift = 1; // на каком баре сигнал индикатора
extern int Magic = 123; // магик
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
class CMyClass
{
private:
int CountTrades();
public:
int CountOrders(int type);
double AllProfit(int ot=-1);
double Lot();
void PutOrder(int type,double price);
void CloseAll(int ot=-1);
void Trailing();
void OpenOrd();
};
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
int CMyClass::CountTrades(void)
{
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()<2) count++;
}
}
}
return(count);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
void CMyClass::CloseAll(int ot=-1)
{
bool cl;
for(int i=OrdersTotal()-1;i>=0;i--)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
{
if(OrderSymbol()==Symbol() && OrderMagicNumber()==Magic)
{
if(OrderType()==0 && (ot==0 || ot==-1))
{
RefreshRates();
cl=OrderClose(OrderTicket(),OrderLots(),NormalizeDouble(Bid,Digits),Slip,White);
}
if(OrderType()==1 && (ot==1 || ot==-1))
{
RefreshRates();
cl=OrderClose(OrderTicket(),OrderLots(),NormalizeDouble(Ask,Digits),Slip,White);
}
}
}
}
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
void CMyClass:<img src='http://opentraders.ru/templates/skin/g6h/images/smilies/008.gif' alt=' :P '> utOrder(int type,double price)
{
int r=0;
color clr=Green;
double sl=0,tp=0;
if(type==1 || type==3 || type==5)
{
clr=Red;
if(StopLoss>0) sl=NormalizeDouble(price+StopLoss*Point,Digits);
if(TakeProfit>0) tp=NormalizeDouble(price-TakeProfit*Point,Digits);
}
if(type==0 || type==2 || type==4)
{
clr=Blue;
if(StopLoss>0) sl=NormalizeDouble(price-StopLoss*Point,Digits);
if(TakeProfit>0) tp=NormalizeDouble(price+TakeProfit*Point,Digits);
}
r=OrderSend(NULL,type,Lot(),NormalizeDouble(price,Digits),Slip,sl,tp,"",Magic,0,clr);
return;
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
void CMyClass::OpenOrd()
{
double MA11=iMA(NULL,0,MA1Period,0,0,0,Shift);
double MA12=iMA(NULL,0,MA1Period,0,0,0,Shift+1);
double MA21=iMA(NULL,0,MA2Period,0,0,0,Shift);
double MA22=iMA(NULL,0,MA2Period,0,0,0,Shift+1);
if(MA11<MA21 && MA12>MA22)
{
PutOrder(1,Bid);
}
if(MA11>MA21 && MA12<MA22)
{
PutOrder(0,Ask);
}
}
//+------------------------------------------------------------------+
//| Простой трал |
//+------------------------------------------------------------------+
void CMyClass::Trailing()
{
bool mod;
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)
{
if(Bid-OrderOpenPrice()>TrailingStop*Point)
{
if(OrderStopLoss()<Bid-TrailingStop*Point)
{
mod=OrderModify(OrderTicket(),OrderOpenPrice(),Bid-TrailingStop*Point,OrderTakeProfit(),0,Yellow);
return;
}
}
}
if(OrderType()==OP_SELL)
{
if((OrderOpenPrice()-Ask)>TrailingStop*Point)
{
if((OrderStopLoss()>(Ask+TrailingStop*Point)) || (OrderStopLoss()==0))
{
mod=OrderModify(OrderTicket(),OrderOpenPrice(),Ask+TrailingStop*Point,OrderTakeProfit(),0,Yellow);
return;
}
}
}
}
}
}
}
//+------------------------------------------------------------------+
//| Профит всех ордеров по типу ордера |
//+------------------------------------------------------------------+
double CMyClass::AllProfit(int ot=-1)
{
double pr=0;
for(int i=OrdersTotal()-1;i>=0;i--)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
{
if(OrderSymbol()==Symbol() && OrderMagicNumber()==Magic)
{
if(OrderType()==0 && (ot==0 || ot==-1))
{
pr+=OrderProfit()+OrderCommission()+OrderSwap();
}
if(OrderType()==1 && (ot==1 || ot==-1))
{
pr+=OrderProfit()+OrderCommission()+OrderSwap();
}
}
}
}
return(pr);
}
//+------------------------------------------------------------------+
//| Подсчет ордеров по типу |
//+------------------------------------------------------------------+
int CMyClass::CountOrders(int type)
{
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()==type) count++;
}
}
}
return(count);
}
//+------------------------------------------------------------------+
//| Лот для усреднителя |
//+------------------------------------------------------------------+
double CMyClass::Lot()
{
double lot=Lots;
double MinimumLot = MarketInfo(NULL,MODE_MINLOT);
double MaximumLot = MarketInfo(NULL,MODE_MAXLOT);
if(mycl.CountTrades()>0) lot=NormalizeDouble(Lots*MathPow(KLot,mycl.CountTrades()),2);
if(lot>MaxLot)lot=Lots;
if(lot<MinimumLot) lot=MinimumLot;
if(lot>MaximumLot) lot=MaximumLot;
return(lot);
}
CMyClass mycl;
datetime t=0;
//+------------------------------------------------------------------+
//| Expert initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Expert deinitialization function |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
Comment("");
}
//+------------------------------------------------------------------+
//| Expert tick function |
//+------------------------------------------------------------------+
void OnTick()
{
if(t!=Time[0])
{
if(TrailingStop>0) mycl.Trailing();
mycl.OpenOrd();
if(mycl.AllProfit(0)>Profit) mycl.CloseAll(0);
if(mycl.AllProfit(1)>Profit) mycl.CloseAll(1);
if(mycl.AllProfit()>Profit || mycl.AllProfit()<-Loss) mycl.CloseAll();
t=Time[0];
}
Comment("\n Открыто Buy ",mycl.CountOrders(0),
"\n Открыто Sell ",mycl.CountOrders(1),
"\n Общий профит: ",mycl.AllProfit(),
"\n Профит по Buy: ",mycl.AllProfit(0),
"\n Профит по Sell: ",mycl.AllProfit(1));
}
//+------------------------------------------------------------------+
03 сентября 2016
|
08 сентября 2016
|
Комментарии (9)
20 Anatoly74 Сообщений: 3710 - Анатолий
34 AM2 Автор Сообщений: 15882 - Андрей
Я бы попытался(исходя из скрина):
1. Машки покрасить в разные цвета.
2. Добавить реверс.
3. Поменять принцип усреднения.
20 Anatoly74 Сообщений: 3710 - Анатолий
34 AM2 Автор Сообщений: 15882 - Андрей
20 Anatoly74 Сообщений: 3710 - Анатолий
Могу рассуждать чисто теоретически, что скорость исполнения mql скриптов довольно низкая, а если использовать ООП, то скорее всего это еще больше тормозов. Плюс ООП применяется, как раз для более глобальных и сложных систем(советников и т.д.), то есть грубо говоря умножаем тормоза на тормоза Редактирован: 5 сентября 2016, 02:00
17 vis Сообщений: 199 - ♫♪♫♪
Хм, а если взять mql5, то все будет лучше? ООП возможности вроде в нем отдельно анонсировались.
8 Miha Сообщений: 485 - Михаил
Но востребованность mql5 не особо высока.
Я к тому, что может ООП для mql4 использовать для построения приложения, а все расчеты и логику делать на C++?
17 vis Сообщений: 199 - ♫♪♫♪
34 AM2 Автор Сообщений: 15882 - Андрей
Зарегистрируйтесь или авторизуйтесь, чтобы оставить комментарий