//+------------------------------------------------------------------+
//| Drug_2MA_Exp2.mq4 |
//| Oxy |
//| http://oxy.opentraders.ru/bio/ |
//+------------------------------------------------------------------+
#property copyright "Oxy"
#property link "http://oxy.opentraders.ru/bio/"
#property version "1.00"
#property strict
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
enum name_MA_method
{
Simple,
Exponential,
Smoothed,
Linear_Weighted,
};
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
enum name_MA_price
{
CLOSE,
OPEN,
HIGH,
LOW,
MEDIAN,
TYPICAL,
WEIGHTED,
};
//------- Внешние параметры -----------------------------------------+
input string name_MA1="______________MA_1________________"; // параметры MA 1
extern int ma1_period = 6; // Период
extern int ma1_shift = 0; // Сдвиг
extern name_MA_method ma1_method = Exponential; // Метод MA
extern name_MA_price ma1_applied_price = CLOSE; // Применить к
input string name_MA2="______________MA_2________________"; // параметры MA 2
extern int ma2_period = 24; // Период
extern int ma2_shift = 0; // Сдвиг
extern name_MA_method ma2_method = Exponential; // Метод MA
extern name_MA_price ma2_applied_price = CLOSE; // Применить к
input string expert="________Переменные_эксперта_______"; // переменные
extern double Lot = 0.1; // Лот
extern int StopLoss = 400; // Stop Loss, 0 - не ставим
extern int TakeProfit = 800; // Take Profit, 0 - не ставим
extern int Slippage = 30; // Проскальзывание цены
extern int MagicNumber = 577575; // Идентификатор ордера
//------- Глобальные переменные советника -------------------------------------+
string Symb;
//+------------------------------------------------------------------+
//| Expert initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
if(!IsTradeAllowed()) { Print("Необходимо разрешить советнику торговать!"); return(INIT_FAILED);}
Symb=Symbol();
if(Lot<MarketInfo(Symb,MODE_MINLOT) || Lot>MarketInfo(Symb,MODE_MAXLOT)) { Print("Неправильно выставлен лот!"); return(INIT_FAILED);}
if(StopLoss !=0 && StopLoss <MarketInfo(Symb, MODE_STOPLEVEL)) {Print("Неправильно выставлен Stop Loss!"); return(INIT_FAILED);}
if(TakeProfit!=0 && TakeProfit<MarketInfo(Symb, MODE_STOPLEVEL)) {Print("Неправильно выставлен Take Profit!"); return(INIT_FAILED);}
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Expert deinitialization function |
//+------------------------------------------------------------------+
void OnDeinit(const int reason) { }
//+------------------------------------------------------------------+
//| Expert tick function |
//+------------------------------------------------------------------+
void OnTick()
{
double iMA1_0 = iMA(Symb, 0, ma1_period, ma1_shift, method_MA(ma1_method), price_MA(ma1_applied_price), 0);
double iMA1_1 = iMA(Symb, 0, ma1_period, ma1_shift, method_MA(ma1_method), price_MA(ma1_applied_price), 1);
double iMA1_2 = iMA(Symb, 0, ma1_period, ma1_shift, method_MA(ma1_method), price_MA(ma1_applied_price), 2);
double iMA2_0 = iMA(Symb, 0, ma2_period, ma2_shift, method_MA(ma2_method), price_MA(ma2_applied_price), 0);
double iMA2_1 = iMA(Symb, 0, ma2_period, ma2_shift, method_MA(ma2_method), price_MA(ma2_applied_price), 1);
double iMA2_2 = iMA(Symb, 0, ma2_period, ma2_shift, method_MA(ma2_method), price_MA(ma2_applied_price), 2);
if(NewBar())
{
if(iMA1_0<iMA2_0 && iMA1_1<iMA2_1 && iMA1_2>iMA2_2)
{ // sell
closeOpenPos(0);
if(HaveOpenPos()==false) SellPos_stepPoint(Lot,StopLoss,TakeProfit);
}
if(iMA1_0>iMA2_0 && iMA1_1>iMA2_1 && iMA1_2<iMA2_2)
{ // buy
closeOpenPos(1);
if(HaveOpenPos()==false) BuyPos_stepPoint(Lot,StopLoss,TakeProfit);
}
}
}
//+------------------------------------------------------------------+
bool HaveOpenPos(int ot=-1)
{
int i,k=OrdersTotal();
if(ot<0 || ot>1) ot=-1;
for(i=k-1; i>=0; i--)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
{
if(OrderSymbol()==Symb && OrderMagicNumber()==MagicNumber)
{
if(ot==-1)
{
if(OrderType()==0 || OrderType()==1) return (true);
} else {
if(ot==OrderType()) return (true);
}
}
}
}
return (false);
}
//+------------------------------------------------------------------+
void closeOpenPos(int oCt=-1)
{
int i,ot,k=OrdersTotal();
for(i=k-1; i>=0; i--)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
{
if(OrderSymbol()==Symb && OrderMagicNumber()==MagicNumber)
{
ot=OrderType();
if(ot==0 && (oCt==0 || oCt==-1))
{
RefreshRates();
if(!OrderClose(OrderTicket(),OrderLots(),Bid,Slippage)) Print("Не удалось закрыть ордер ",OrderTicket(),"!");
}
if(ot==1 && (oCt==1 || oCt==-1))
{
RefreshRates();
if(!OrderClose(OrderTicket(),OrderLots(),Ask,Slippage)) Print("Не удалось закрыть ордер ",OrderTicket(),"!");
}
}
}
}
}
//+------------------------------------------------------------------+
bool BuyPos_stepPoint(double _lot,int _sl=0,int _tp=0,string _comment=NULL)
{
double sl=0,tp=0;
RefreshRates();
if(_sl>0) sl=Ask-(double)_sl*Point;
if(_tp>0) tp=Ask+(double)_tp*Point;
if(!OrderSend(Symb,OP_BUY,NormalizeDouble(_lot,2),NormalizeDouble(Ask,Digits),Slippage,NormalizeDouble(sl,Digits),NormalizeDouble(tp,Digits),_comment,MagicNumber))
{
Print("Не удалось OP_BUY!");
return (false);
}
return (true);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
bool SellPos_stepPoint(double _lot,int _sl=0,int _tp=0,string _comment=NULL)
{
double sl=0,tp=0;
RefreshRates();
if(_sl>0) sl=Bid+(double)_sl*Point;
if(_tp>0) tp=Bid-(double)_tp*Point;
if(!OrderSend(Symb,OP_SELL,NormalizeDouble(_lot,2),NormalizeDouble(Bid,Digits),Slippage,NormalizeDouble(sl,Digits),NormalizeDouble(tp,Digits),_comment,MagicNumber))
{
Print("Не удалось OP_SELL!");
return (false);
}
return (true);
}
//+------------------------------------------------------------------+
int method_MA(name_MA_method method)
{
switch(method)
{
case Simple: return(0);
case Exponential: return(1);
case Smoothed: return(2);
case Linear_Weighted: return(3);
}
return(0);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
int price_MA(name_MA_price applied_price)
{
switch(applied_price)
{
case CLOSE: return(0);
case OPEN: return(1);
case HIGH: return(2);
case LOW: return(3);
case MEDIAN: return(4);
case TYPICAL: return(5);
case WEIGHTED: return(6);
}
return(0);
}
//+------------------------------------------------------------------+
bool NewBar()
{
static datetime lastbar=0;
datetime curbar=Time[0];
if(lastbar!=curbar)
{
lastbar=curbar;
return (true);
}
else
{
return(false);
}
}
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
//| MAExp.mq4 |
//| Copyright 2014, AM2 |
//| http://www.forexsystems.biz |
//+------------------------------------------------------------------+
#property copyright "Copyright 2014, AM2"
#property link "http://www.forexsystems.biz"
#property version "1.00"
#property description "Moving Average sample expert advisor"
#define MAGICMA 20131111
//--- Inputs
input double StopLoss = 500;
input double TakeProfit = 500;
input double Lots = 0.1;
input int MovingPeriod = 12;
input int MovingShift = 6;
input int Delta = 6;
input int Slip = 30;
input int Count = 100;
input int EquityRisk = 20; // если значение больше 0 включается закрытие по эквити. вычисляется в процентах от баланса.
input int EquityProfit = 20; // если значение больше 0 включается закрытие по профиту эквити. вычисляется в процентах от баланса.
double Prosadka;
//+------------------------------------------------------------------+
//| Calculate open positions |
//+------------------------------------------------------------------+
int Orders(string symbol)
{
int buys=0,sells=0;
//---
for(int i=0;i<OrdersTotal();i++)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) break;
if(OrderSymbol()==Symbol() && OrderMagicNumber()==MAGICMA)
{
if(OrderType()==OP_BUY) buys++;
if(OrderType()==OP_SELL) sells++;
}
}
//--- return orders volume
if(buys>0) return(buys);
else return(-sells);
}
//+------------------------------------------------------------------+
//| Check for open order conditions |
//+------------------------------------------------------------------+
void OpenPos()
{
double ma;
int res;
//--- get Moving Average
ma=iMA(NULL,0,MovingPeriod,MovingShift,MODE_SMA,PRICE_CLOSE,0);
//--- sell conditions
if(Bid<ma && ma-Bid<Delta*Point)
{
res=OrderSend(Symbol(),OP_SELL,Lots,Bid,Slip,Bid+StopLoss*Point,Bid-TakeProfit*Point,"",MAGICMA,0,Red);
return;
}
//--- buy conditions
if(Ask>ma && Ask-ma<Delta*Point)
{
res=OrderSend(Symbol(),OP_BUY,Lots,Ask,Slip,Ask-StopLoss*Point,Ask+TakeProfit*Point,"",MAGICMA,0,Blue);
return;
}
//---
}
//+------------------------------------------------------------------+
//| Check for close order conditions |
//+------------------------------------------------------------------+
void ClosePos()
{
double ma;
//--- get Moving Average
ma=iMA(NULL,0,MovingPeriod,MovingShift,MODE_SMA,PRICE_CLOSE,0);
//---
for(int i=0;i<OrdersTotal();i++)
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
{
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) break;
if(OrderMagicNumber()!=MAGICMA || OrderSymbol()!=Symbol()) continue;
//--- check order type
if(OrderType()==OP_BUY)
{
if(Ask<ma)
{
CloseAll();
}
break;
}
if(OrderType()==OP_SELL)
{
if(Bid>ma)
{
CloseAll();
}
break;
}
}
//---
}
//+------------------------------------------------------------------+
void CloseAll()
{
bool cl,sel;
for(int i=OrdersTotal()-1;i>=0;i--)
{
sel=OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
if(OrderSymbol()==Symbol())
{
if(OrderSymbol()==Symbol() && OrderMagicNumber()==MAGICMA)
{
if(OrderType()==OP_BUY) cl=OrderClose(OrderTicket(),OrderLots(),Bid,Slip,Blue);
if(OrderType()==OP_SELL) cl=OrderClose(OrderTicket(),OrderLots(),Ask,Slip,Red);
}
Sleep(1000);
}
}
}
//+------------------------------------------------------------------+
bool NewBar()
{
static datetime lastbar=0;
datetime curbar=Time[0];
if(lastbar!=curbar)
{
lastbar=curbar;
return (true);
}
else
{
return(false);
}
}
//+------------------------------------------------------------------+
//| OnTick function |
//+------------------------------------------------------------------+
void OnTick()
{
if(NewBar())
{
if(Orders(Symbol())<=Count) OpenPos();
//ClosePos();
}
if(EquityRisk>0)
{
double BALANCE=AccountInfoDouble(ACCOUNT_BALANCE);
double EQUITY=AccountInfoDouble(ACCOUNT_EQUITY);
Prosadka=100*(BALANCE-EQUITY)/BALANCE;
{
if(Prosadka>=EquityRisk)
{
CloseAll();
Print("Closed All due to Stop Out");
}
}
}
if(EquityProfit>0)
{
BALANCE=AccountInfoDouble(ACCOUNT_BALANCE);
EQUITY=AccountInfoDouble(ACCOUNT_EQUITY);
Prosadka=100*(EQUITY-BALANCE)/BALANCE;
{
if(Prosadka>=EquityProfit)
{
CloseAll();
Print("Closed All due to All Profit");
}
}
}
//--------------
Comment("\n Баланс: ", DoubleToString(AccountInfoDouble(ACCOUNT_BALANCE),2),
"\n Средства: ", DoubleToString(AccountInfoDouble(ACCOUNT_EQUITY),2),
"\n Прибыль: ", DoubleToString(AccountInfoDouble(ACCOUNT_PROFIT)),2);
//---
}
//+------------------------------------------------------------------+
/////////////////////////////////////////////////////////////////////////вешаеи стоп на убыточный ордер
for(i=0; i<OrdersTotal(); i++)
{
s=OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
{
if(OrderSymbol()==Symbol1 && OrderMagicNumber()==Magic)
{
Comment("\n Order Profit ", OrderProfit());
if(OrderProfit()<<img src='http://opentraders.ru/templates/skin/g6h/images/smilies/010.gif' alt=' *shock* '> rderLoss)
{
if(OrderType()==OP_BUY)
{
ModifyOrder(OrderTicket(),OrderOpenPrice(),OrderOpenPrice()-StopLoss*Point,OrderTakeProfit());
}
if(OrderType()==OP_SELL)
{
ModifyOrder(OrderTicket(),OrderOpenPrice(),OrderOpenPrice()+StopLoss*Point,OrderTakeProfit());
}
}
}
if(OrderSymbol()==Symbol2 && OrderMagicNumber()==Magic)
{
Comment("\n Order Profit ", OrderProfit());
if(OrderProfit()<<img src='http://opentraders.ru/templates/skin/g6h/images/smilies/010.gif' alt=' *shock* '> rderLoss)
{
if(OrderType()==OP_BUY)
{
ModifyOrder(OrderTicket(),OrderOpenPrice(),OrderOpenPrice()-StopLoss*Point,OrderTakeProfit());
}
if(OrderType()==OP_SELL)
{
ModifyOrder(OrderTicket(),OrderOpenPrice(),OrderOpenPrice()+StopLoss*Point,OrderTakeProfit());
}
}
}
}
}
/////////////////////////////////////////////////////////////////////////
//+------------------------------------------------------------------+
//| MAExp.mq4 |
//| Copyright 2014, AM2 |
//| http://www.forexsystems.biz |
//+------------------------------------------------------------------+
#property copyright "Copyright 2014, AM2"
#property link "http://www.forexsystems.biz"
#property version "1.00"
#property description "Moving Average sample expert advisor"
#define MAGICMA 20131111
//--- Inputs
input double StopLoss = 500;
input double TakeProfit = 500;
input double Lots = 0.1;
input int MovingPeriod = 12;
input int MovingShift = 6;
input int Delta = 6;
input int Slip = 30;
input int Count = 100;
//+------------------------------------------------------------------+
//| Calculate open positions |
//+------------------------------------------------------------------+
int Orders(string symbol)
{
int buys=0,sells=0;
//---
for(int i=0;i<OrdersTotal();i++)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) break;
if(OrderSymbol()==Symbol() && OrderMagicNumber()==MAGICMA)
{
if(OrderType()==OP_BUY) buys++;
if(OrderType()==OP_SELL) sells++;
}
}
//--- return orders volume
if(buys>0) return(buys);
else return(-sells);
}
//+------------------------------------------------------------------+
//| Check for open order conditions |
//+------------------------------------------------------------------+
void OpenPos()
{
double ma;
int res;
//--- get Moving Average
ma=iMA(NULL,0,MovingPeriod,MovingShift,MODE_SMA,PRICE_CLOSE,0);
//--- sell conditions
if(Bid<ma && ma-Bid<Delta*Point)
{
res=OrderSend(Symbol(),OP_SELL,Lots,Bid,Slip,Bid+StopLoss*Point,Bid-TakeProfit*Point,"",MAGICMA,0,Red);
return;
}
//--- buy conditions
if(Ask>ma && Ask-ma<Delta*Point)
{
res=OrderSend(Symbol(),OP_BUY,Lots,Ask,Slip,Ask-StopLoss*Point,Ask+TakeProfit*Point,"",MAGICMA,0,Blue);
return;
}
//---
}
//+------------------------------------------------------------------+
//| Check for close order conditions |
//+------------------------------------------------------------------+
void ClosePos()
{
double ma;
//--- get Moving Average
ma=iMA(NULL,0,MovingPeriod,MovingShift,MODE_SMA,PRICE_CLOSE,0);
//---
for(int i=0;i<OrdersTotal();i++)
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
{
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) break;
if(OrderMagicNumber()!=MAGICMA || OrderSymbol()!=Symbol()) continue;
//--- check order type
if(OrderType()==OP_BUY)
{
if(Ask<ma)
{
CloseAll();
}
break;
}
if(OrderType()==OP_SELL)
{
if(Bid>ma)
{
CloseAll();
}
break;
}
}
//---
}
//+------------------------------------------------------------------+
void CloseAll()
{
bool cl,sel;
for(int i=OrdersTotal()-1;i>=0;i--)
{
sel=OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
if(OrderSymbol()==Symbol())
{
if(OrderSymbol()==Symbol() && OrderMagicNumber()==MAGICMA)
{
if(OrderType()==OP_BUY) cl=OrderClose(OrderTicket(),OrderLots(),Bid,Slip,Blue);
if(OrderType()==OP_SELL) cl=OrderClose(OrderTicket(),OrderLots(),Ask,Slip,Red);
}
Sleep(1000);
}
}
}
//+------------------------------------------------------------------+
bool NewBar()
{
static datetime lastbar=0;
datetime curbar=Time[0];
if(lastbar!=curbar)
{
lastbar=curbar;
return (true);
}
else
{
return(false);
}
}
//+------------------------------------------------------------------+
//| OnTick function |
//+------------------------------------------------------------------+
void OnTick()
{
if(NewBar())
{
if(Orders(Symbol())<=Count) OpenPos();
ClosePos();
}
//---
}
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
//| Expert tick function |
//+------------------------------------------------------------------+
void OnTick()
{
//---
if(OrdersTotal()<1){PutBuyLimitCountOrders();}
if(CountTrades()==10 && BuOrdersProfit()>0) {CloseBuOrders();}
Comment("\nBu Orders Profit: ",BuOrdersProfit(),
"\nOpen Position: ",CountTrades());
}
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
void PutBuyLimitCountOrders()
{
for(int i=1;i<=10;i++)
{
int ticket=OrderSend(Symbol(),OP_BUYLIMIT,0.2,Ask-(i*100*Point),3,
Ask-(i*100*Point)-2000*Point,
Ask-(i*100*Point)+2000*Point,"",111,0,Blue);
}
}
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
double BuOrdersProfit()
{
double all=0;
for(int i=OrdersTotal()-1;i>=7;i--)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
{
if(OrderSymbol()==Symbol() && OrderMagicNumber()==111)
{
if(OrderType()==OP_BUY)
{
all+=OrderProfit();
}
}
}
}
all=NormalizeDouble(all,Digits);
return(all);
}
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
double CloseBuOrders()
{
double all=0;
bool cl;
for(int i=OrdersTotal()-1;i>=7;i--)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
{
if(OrderSymbol()==Symbol() && OrderMagicNumber()==111)
{
if(OrderType()==OP_BUY)
{
cl=OrderClose(OrderTicket(),OrderLots(),Bid,3,White);
}
}
}
}
all=NormalizeDouble(all,Digits);
return(all);
}
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
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()==111)
{
if(OrderType()==OP_BUY || OrderType()==OP_SELL)
count++;
}
}
}
return(count);
}
//+------------------------------------------------------------------+
AM2