на демо открывает также
Может в терминале проблема
//+------------------------------------------------------------------+
//| RSI.mq4 |
//| Copyright 2015, AM2 |
//| http://www.forexsystems.biz |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2015, AM2"
#property link "http://www.forexsystems.biz"
#property description "Simple expert advisor"
//--- Inputs
extern double Lots = 0.1; // лот
extern int StopLoss = 500; // лось
extern int TakeProfit = 500; // язь
extern int Count = 5; // позы
extern int Slip = 30; // реквот
extern int Magic = 123; // магик
extern string IndicatorProperties="--------------------";
extern int IndPeriod = 12; // период RSI
extern int IndLevel = 30; // уровень RSI
int t=0;
//+------------------------------------------------------------------+
//| Expert initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
//---
Comment("");
//---
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Expert deinitialization function |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
Comment("");
}
//+------------------------------------------------------------------+
//| Check for open order conditions |
//+------------------------------------------------------------------+
void OpenPos()
{
int r=0;
double sl=0,tp=0;
//--- get Ind
double rsi1=iRSI(Symbol(),0,IndPeriod,0,1);
double rsi2=iRSI(Symbol(),0,IndPeriod,0,2);
//--- sell conditions
if(rsi2>100-IndLevel && rsi1<100-IndLevel)
{
if(StopLoss>0) sl=NormalizeDouble(Bid+StopLoss*Point,Digits);
if(TakeProfit>0) tp=NormalizeDouble(Bid-TakeProfit*Point,Digits);
r=OrderSend(Symbol(),OP_SELL,Lots,NormalizeDouble(Bid,Digits),Slip,sl,tp,"",Magic,0,Red);
t=Time[0];
return;
}
//--- buy conditions
if(rsi2<IndLevel && rsi1>IndLevel)
{
if(StopLoss>0) sl=NormalizeDouble(Ask-StopLoss*Point,Digits);
if(TakeProfit>0) tp=NormalizeDouble(Ask+TakeProfit*Point,Digits);
r=OrderSend(Symbol(),OP_BUY,Lots,NormalizeDouble(Ask,Digits),Slip,sl,tp,"",Magic,0,Blue);
t=Time[0];
return;
}
//---
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
void CloseAll()
{
bool cl;
for(int i=OrdersTotal()-1;i>=0;i--)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
{
if(OrderMagicNumber()==Magic || OrderSymbol()==Symbol())
{
if(OrderType()==OP_BUY) cl=OrderClose(OrderTicket(),OrderLots(),Bid,Slip,Blue);
if(OrderType()==OP_SELL) cl=OrderClose(OrderTicket(),OrderLots(),Ask,Slip,Red);
}
}
}
}
//+------------------------------------------------------------------+
//| Check for close order conditions |
//+------------------------------------------------------------------+
void ClosePos()
{
//--- get Ind
double rsi1=iRSI(Symbol(),0,IndPeriod,0,1);
double rsi2=iRSI(Symbol(),0,IndPeriod,0,2);
for(int i=OrdersTotal()-1;i>=0;i--)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
{
if(OrderMagicNumber()==Magic || OrderSymbol()==Symbol())
{
if(OrderType()==OP_BUY)
{
if(rsi2>100-IndLevel && rsi1<100-IndLevel)
{
CloseAll();
}
}
if(OrderType()==OP_SELL)
{
if(rsi2<IndLevel && rsi1>IndLevel)
{
CloseAll();
}
}
}
}
}
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
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);
}
//+------------------------------------------------------------------+
//| OnTick function |
//+------------------------------------------------------------------+
void OnTick()
{
if(t!=Time[0] && CountTrades()<=Count)
{
OpenPos();
}
if(CountTrades()>0) ClosePos();
}
//+------------------------------------------------------------------+
if(OrderSymbol()==Symbol() && OrderMagicNumber()==Magic)
//+------------------------------------------------------------------+
//| StupTral.mq4 |
//| Copyright 2015, AM2 |
//| http://www.forexsystems.biz |
//+------------------------------------------------------------------+
#property copyright "Copyright 2015, AM2"
#property link "http://www.forexsystems.biz"
#property version "1.00"
#property strict
#property show_inputs
extern int TrailingDist = 50; //дистанция для включения трала
extern int TrailingStep = 20; //шаг трала
//+------------------------------------------------------------------+
void Trailing()
{
bool mod;
for(int i=OrdersTotal()-1;i>=0;i--)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
{
if(OrderSymbol()==Symbol())
{
if(OrderType()==OP_BUY)
{
// цена прошла дистанцию
if(Bid-OrderOpenPrice()>TrailingDist*Point)
{
// стоп ордера меньше чем на расстоянии шага от цены
if(OrderStopLoss()<Bid-TrailingStep*Point)
{
mod=OrderModify(OrderTicket(),OrderOpenPrice(),Bid-TrailingStep*Point,OrderTakeProfit(),0,Yellow);
}
}
}
if(OrderType()==OP_SELL)
{
// цена прошла дистанцию
if((OrderOpenPrice()-Ask)>TrailingDist*Point)
{
// стоп ордера меньше чем на расстоянии шага от цены
if((OrderStopLoss()>(Ask+TrailingStep*Point)) || (OrderStopLoss()==0))
{
mod=OrderModify(OrderTicket(),OrderOpenPrice(),Ask+TrailingStep*Point,OrderTakeProfit(),0,Yellow);
}
}
}
}
}
}
}
//+------------------------------------------------------------------+
//| Script program start function |
//+------------------------------------------------------------------+
void OnStart()
{
while(!IsStopped())
{
if(TrailingDist>0) Trailing();
}
}
//+------------------------------------------------------------------+
А можно сделать так? Если столб выше нулевой линии то бай если ниже то сэл, и чтоб также не на текущий столб смотрел а на предыдущие,
double ShiftFGreen=iCustom(Symbol(),0,"Fisher",IndPeriod,0,IndShift);
double ShiftFRed=iCustom(Symbol(),0,"Fisher",IndPeriod,1,IndShift);
//--- sell conditions
if(ShiftFGreen>0 && (LastDealType()==1 || LastDealType()==0))
//+------------------------------------------------------------------+
//| RSI.mq4 |
//| Copyright 2015, AM2 |
//| http://www.forexsystems.biz |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2015, AM2"
#property link "http://www.forexsystems.biz"
#property description "Simple expert advisor"
//--- Inputs
extern double Lots = 0.1; // лот
extern int StopLoss = 500; // лось
extern int TakeProfit = 500; // язь
extern int Slip = 30; // реквот
extern int Magic = 123; // магик
extern string IndicatorProperties="--------------------";
extern int IndPeriod = 12; // период RSI
extern int IndLevel = 30; // уровень RSI
//+------------------------------------------------------------------+
//| Expert initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
//---
Comment("");
//---
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Expert deinitialization function |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
Comment("");
}
//+------------------------------------------------------------------+
//| Check for open order conditions |
//+------------------------------------------------------------------+
void OpenPos()
{
int r=0;
double sl=0,tp=0;
//--- get Ind
double rsi1=iRSI(Symbol(),0,IndPeriod,0,1);
double rsi2=iRSI(Symbol(),0,IndPeriod,0,2);
//--- sell conditions
if(rsi2>100-IndLevel && rsi1<100-IndLevel)
{
if(StopLoss>0) sl=NormalizeDouble(Bid+StopLoss*Point,Digits);
if(TakeProfit>0) tp=NormalizeDouble(Bid-TakeProfit*Point,Digits);
r=OrderSend(Symbol(),OP_SELL,Lots,NormalizeDouble(Bid,Digits),Slip,sl,tp,"",Magic,0,Red);
return;
}
//--- buy conditions
if(rsi2<IndLevel && rsi1>IndLevel)
{
if(StopLoss>0) sl=NormalizeDouble(Ask-StopLoss*Point,Digits);
if(TakeProfit>0) tp=NormalizeDouble(Ask+TakeProfit*Point,Digits);
r=OrderSend(Symbol(),OP_BUY,Lots,NormalizeDouble(Ask,Digits),Slip,sl,tp,"",Magic,0,Blue);
return;
}
//---
}
//+------------------------------------------------------------------+
//| Check for close order conditions |
//+------------------------------------------------------------------+
void ClosePos()
{
//--- get Ind
double rsi1=iRSI(Symbol(),0,IndPeriod,0,1);
double rsi2=iRSI(Symbol(),0,IndPeriod,0,2);
for(int i=OrdersTotal()-1;i>=0;i--)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
{
if(OrderMagicNumber()==Magic || OrderSymbol()==Symbol())
{
if(OrderType()==OP_BUY)
{
if(rsi2>100-IndLevel && rsi1<100-IndLevel)
{
bool c=OrderClose(OrderTicket(),OrderLots(),Bid,Slip,White);
return;
}
}
if(OrderType()==OP_SELL)
{
if(rsi2<IndLevel && rsi1>IndLevel)
{
c=OrderClose(OrderTicket(),OrderLots(),Ask,Slip,White);
return;
}
}
}
}
}
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
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);
}
//+------------------------------------------------------------------+
//| OnTick function |
//+------------------------------------------------------------------+
void OnTick()
{
if(CountTrades()<1) OpenPos();
if(CountTrades()>0) ClosePos();
}
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
//| Shifer.mq4 |
//| Copyright 2015, AM2 |
//| http://www.forexsystems.biz |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2015, AM2"
#property link "http://www.forexsystems.biz"
#property description "Simple expert advisor"
//--- Inputs
extern double Lots = 0.1; // лот
extern int StopLoss = 500; // лось
extern int TakeProfit = 500; // язь
extern int BULevel = 300; // уровень бу
extern int BUPoint = 30; // пункты бу
extern int TrailingStop = 400; // трал
extern int Slip = 30; // реквот
extern int Magic = 123; // магик
extern string IndicatorProperties="--------------------";
extern int IndPeriod = 12; // период Fisher
extern int IndShift = 3; // сдвиг Fisher
//+------------------------------------------------------------------+
//| Expert initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
//---
Comment("");
//---
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Expert deinitialization function |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
Comment("");
}
//+------------------------------------------------------------------+
//| Check for open order conditions |
//+------------------------------------------------------------------+
void OpenPos()
{
int r=0;
double sl=0,tp=0;
//--- get Ind
double FGreen=iCustom(Symbol(),0,"Fisher",IndPeriod,0,1);
double FRed=iCustom(Symbol(),0,"Fisher",IndPeriod,1,1);
double ShiftFGreen=iCustom(Symbol(),0,"Fisher",IndPeriod,0,IndShift);
double ShiftFRed=iCustom(Symbol(),0,"Fisher",IndPeriod,1,IndShift);
//--- sell conditions
if(ShiftFGreen>0 && (LastDealType()==1 || LastDealType()==0))
{
if(StopLoss>0) sl=NormalizeDouble(Bid+StopLoss*Point,Digits);
if(TakeProfit>0) tp=NormalizeDouble(Bid-TakeProfit*Point,Digits);
r=OrderSend(Symbol(),OP_SELL,Lots,NormalizeDouble(Bid,Digits),Slip,sl,tp,"",Magic,0,Red);
return;
}
//--- buy conditions
if(ShiftFGreen<0 && (LastDealType()==2 || LastDealType()==0))
{
if(StopLoss>0) sl=NormalizeDouble(Ask-StopLoss*Point,Digits);
if(TakeProfit>0) tp=NormalizeDouble(Ask+TakeProfit*Point,Digits);
r=OrderSend(Symbol(),OP_BUY,Lots,NormalizeDouble(Ask,Digits),Slip,sl,tp,"",Magic,0,Blue);
return;
}
//---
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
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(TrailingStop>0)
{
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(TrailingStop>0)
{
if((OrderOpenPrice()-Ask)>TrailingStop*Point)
{
if((OrderStopLoss()>(Ask+TrailingStop*Point)) || (OrderStopLoss()==0))
{
mod=OrderModify(OrderTicket(),OrderOpenPrice(),Ask+TrailingStop*Point,OrderTakeProfit(),0,Yellow);
return;
}
}
}
}
}
}
}
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
void BU()
{
bool m;
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(OrderOpenPrice()<=(Bid-(BULevel+BUPoint)*Point) && OrderOpenPrice()>OrderStopLoss())
{
m=OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice()+BUPoint*Point,OrderTakeProfit(),0,Green);
return;
}
}
if(OrderType()==OP_SELL)
{
if(OrderOpenPrice()>=(Ask+(BULevel+BUPoint)*Point) && OrderOpenPrice()<OrderStopLoss())
{
m=OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice()-BUPoint*Point,OrderTakeProfit(),0,Green);
return;
}
}
}
}
}
}
//+------------------------------------------------------------------+
//| Check for close order conditions |
//+------------------------------------------------------------------+
void ClosePos()
{
//--- get Ind
double FGreen=iCustom(Symbol(),0,"Fisher",IndPeriod,0,1);
double FRed=iCustom(Symbol(),0,"Fisher",IndPeriod,1,1);
double ShiftFGreen=iCustom(Symbol(),0,"Fisher",IndPeriod,0,IndShift);
double ShiftFRed=iCustom(Symbol(),0,"Fisher",IndPeriod,1,IndShift);
for(int i=OrdersTotal()-1;i>=0;i--)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
{
if(OrderMagicNumber()==Magic || OrderSymbol()==Symbol())
{
if(OrderType()==OP_BUY)
{
if(ShiftFGreen>0)
{
bool c=OrderClose(OrderTicket(),OrderLots(),Bid,Slip,White);
return;
}
}
if(OrderType()==OP_SELL)
{
if(ShiftFGreen<0)
{
c=OrderClose(OrderTicket(),OrderLots(),Ask,Slip,White);
return;
}
}
}
}
}
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
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);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
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);
}
//+------------------------------------------------------------------+
//| OnTick function |
//+------------------------------------------------------------------+
void OnTick()
{
double FGreen=iCustom(Symbol(),0,"Fisher",IndPeriod,0,1);
double FRed=iCustom(Symbol(),0,"Fisher",IndPeriod,1,1);
if(CountTrades()<1) OpenPos();
if(CountTrades()>0) ClosePos();
if(BULevel!=0) BU();
if(TrailingStop!=0) Trailing();
Comment("\n FGreen: ",FGreen,
"\n FRed: ",FRed);
//---
}
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
//| StupTral.mq4 |
//| Copyright 2015, AM2 |
//| http://www.forexsystems.biz |
//+------------------------------------------------------------------+
#property copyright "Copyright 2015, AM2"
#property link "http://www.forexsystems.biz"
#property version "1.00"
#property strict
extern int TrailingDist = 300; //дистанция для включения трала
extern int TrailingStep = 200; //шаг трала
//+------------------------------------------------------------------+
//| ТРЕЙЛИНГ СТАНДАРТНЫЙ-СТУПЕНЧАСТЫЙ |
//| Функции передаётся тикет позиции, расстояние от курса открытия, |
//| на котором трейлинг запускается (пунктов) и "шаг", с которым он |
//| переносится (пунктов) |
//| Пример: при +30 стоп на +10, при +40 - стоп на +20 и т.д. |
//+------------------------------------------------------------------+
void TrailingStairs(int ticket,int trldistance,int trlstep)
{
double nextstair; // ближайшее значение курса, при котором будем менять стоплосс
// проверяем переданные значения
if((trldistance<MarketInfo(Symbol(),MODE_STOPLEVEL)) || (trlstep<1) || (trldistance<trlstep) || (ticket==0) || (!OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)))
{
Print("Трейлинг функцией TrailingStairs() невозможен из-за некорректности значений переданных ей аргументов.");
return;
}
// если длинная позиция (OP_BUY)
if(OrderType()==OP_BUY)
{
// расчитываем, при каком значении курса следует скорректировать стоплосс
// если стоплосс ниже открытия или равен 0 (не выставлен), то ближайший уровень = курс открытия + trldistance + спрэд
if((OrderStopLoss()==0) || (OrderStopLoss()<OrderOpenPrice()))
nextstair=OrderOpenPrice()+trldistance*Point;
// иначе ближайший уровень = текущий стоплосс + trldistance + trlstep + спрэд
else
nextstair=OrderStopLoss()+trldistance*Point;
// если текущий курс (Bid) >= nextstair и новый стоплосс точно лучше текущего, корректируем последний
if(Bid>=nextstair)
{
if(((OrderStopLoss()==0) || (OrderStopLoss()<OrderOpenPrice())) && (OrderOpenPrice()+trlstep*Point<Bid-MarketInfo(Symbol(),MODE_STOPLEVEL)*Point))
{
if(!OrderModify(ticket,OrderOpenPrice(),OrderOpenPrice()+trlstep*Point,OrderTakeProfit(),OrderExpiration()))
Print("Не удалось модифицировать стоплосс ордера №",OrderTicket(),". Ошибка: ",GetLastError());
}
}
else
{
if(!OrderModify(ticket,OrderOpenPrice(),OrderStopLoss()+trlstep*Point,OrderTakeProfit(),OrderExpiration()))
Print("Не удалось модифицировать стоплосс ордера №",OrderTicket(),". Ошибка: ",GetLastError());
}
}
// если короткая позиция (OP_SELL)
if(OrderType()==OP_SELL)
{
// расчитываем, при каком значении курса следует скорректировать стоплосс
// если стоплосс ниже открытия или равен 0 (не выставлен), то ближайший уровень = курс открытия + trldistance + спрэд
if((OrderStopLoss()==0) || (OrderStopLoss()>OrderOpenPrice()))
nextstair=OrderOpenPrice() -(trldistance+MarketInfo(Symbol(),MODE_SPREAD))*Point;
// иначе ближайший уровень = текущий стоплосс + trldistance + trlstep + спрэд
else
nextstair=OrderStopLoss() -(trldistance+MarketInfo(Symbol(),MODE_SPREAD))*Point;
// если текущий курс (Аск) >= nextstair и новый стоплосс точно лучше текущего, корректируем последний
if(Ask<=nextstair)
{
if(((OrderStopLoss()==0) || (OrderStopLoss()>OrderOpenPrice())) && (OrderOpenPrice() -(trlstep+MarketInfo(Symbol(),MODE_SPREAD))*Point>Ask+MarketInfo(Symbol(),MODE_STOPLEVEL)*Point))
{
if(!OrderModify(ticket,OrderOpenPrice(),OrderOpenPrice() -(trlstep+MarketInfo(Symbol(),MODE_SPREAD))*Point,OrderTakeProfit(),OrderExpiration()))
Print("Не удалось модифицировать стоплосс ордера №",OrderTicket(),". Ошибка: ",GetLastError());
}
}
else
{
if(!OrderModify(ticket,OrderOpenPrice(),OrderStopLoss()-(trlstep+MarketInfo(Symbol(),MODE_SPREAD))*Point,OrderTakeProfit(),OrderExpiration()))
Print("Не удалось модифицировать стоплосс ордера №",OrderTicket(),". Ошибка: ",GetLastError());
}
}
}
//+------------------------------------------------------------------+
void Trailing()
{
for(int i=OrdersTotal()-1; i>=0; i--)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
{
if(OrderSymbol()==Symbol())
{
TrailingStairs(OrderTicket(),TrailingDist,TrailingStep);
}
}
}
}
//+------------------------------------------------------------------+
//| Script program start function |
//+------------------------------------------------------------------+
void OnStart()
{
while(!IsStopped())
{
if(TrailingStep!=0) Trailing();
}
}
//+------------------------------------------------------------------+
просил автора добавить, но он игнорит и дает ссылки на другие траллы не скрипты с кучей ненужных настоек.
//+------------------------------------------------------------------+
//| Envel.mq4 |
//| Copyright 2015, AM2 |
//| http://www.forexsystems.biz |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2015, AM2"
#property link "http://www.forexsystems.biz"
#property description "Envelopes expert advisor"
//--- Inputs
extern double Lots = 0.1; // лот
extern int StopLoss = 500; // лось
extern int TakeProfit = 500; // язь
extern int BULevel = 300; // уровень бу
extern int BUPoint = 30; // пункты бу
extern int TrailingStop = 400; // трал
extern int Slip = 30; // реквот
extern int Magic = 123; // магик
//----
extern int Length = 20; // период Envelopes
extern double Deviation = 1; // отклонение Envelopes
//+------------------------------------------------------------------+
//| Expert initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
//---
//---
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Expert deinitialization function |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
//---
}
//+------------------------------------------------------------------+
//| Check for open order conditions |
//+------------------------------------------------------------------+
void OpenPos()
{
int res;
//--- get Ind
double UP=iEnvelopes(NULL,0,Length,0,0,0,Deviation,MODE_UPPER,1);
double DN=iEnvelopes(NULL,0,Length,0,0,0,Deviation,MODE_LOWER,1);
//--- sell conditions
if(Close[2]>UP && Close[1]<UP)
{
res=OrderSend(Symbol(),OP_SELL,Lots,NormalizeDouble(Bid,Digits),
Slip,NormalizeDouble(Bid+StopLoss*Point,Digits),
NormalizeDouble(Bid-TakeProfit*Point,Digits),"",Magic,0,Red);
return;
}
//--- buy conditions
if(Close[2]<DN && Close[1]>DN)
{
res=OrderSend(Symbol(),OP_BUY,Lots,NormalizeDouble(Ask,Digits),
Slip,NormalizeDouble(Ask-StopLoss*Point,Digits),
NormalizeDouble(Ask+TakeProfit*Point,Digits),"",Magic,0,Blue);
return;
}
//---
}
//+------------------------------------------------------------------+
//| Check for close order conditions |
//+------------------------------------------------------------------+
void ClosePos()
{
//--- get Ind
double UP=iEnvelopes(NULL,0,Length,0,0,0,Deviation,MODE_UPPER,1);
double DN=iEnvelopes(NULL,0,Length,0,0,0,Deviation,MODE_LOWER,1);
for(int i=OrdersTotal()-1;i>=0;i--)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
{
if(OrderMagicNumber()==Magic || OrderSymbol()==Symbol())
{
if(OrderType()==OP_BUY)
{
if(Close[2]>UP && Close[1]<UP)
{
bool c=OrderClose(OrderTicket(),OrderLots(),Bid,Slip,White);
return;
}
}
if(OrderType()==OP_SELL)
{
if(Close[2]<DN && Close[1]>DN)
{
c=OrderClose(OrderTicket(),OrderLots(),Ask,Slip,White);
return;
}
}
}
}
}
}
//+------------------------------------------------------------------+
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 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(TrailingStop>0)
{
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(TrailingStop>0)
{
if((OrderOpenPrice()-Ask)>TrailingStop*Point)
{
if((OrderStopLoss()>(Ask+TrailingStop*Point)) || (OrderStopLoss()==0))
{
mod=OrderModify(OrderTicket(),OrderOpenPrice(),Ask+TrailingStop*Point,OrderTakeProfit(),0,Yellow);
return;
}
}
}
}
}
}
}
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
void BU()
{
bool m;
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(OrderOpenPrice()<=(Bid-(BULevel+BUPoint)*Point) && OrderOpenPrice()>OrderStopLoss())
{
m=OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice()+BUPoint*Point,OrderTakeProfit(),0,Green);
return;
}
}
if(OrderType()==OP_SELL)
{
if(OrderOpenPrice()>=(Ask+(BULevel+BUPoint)*Point) && OrderOpenPrice()<OrderStopLoss())
{
m=OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice()-BUPoint*Point,OrderTakeProfit(),0,Green);
return;
}
}
}
}
}
}
//+------------------------------------------------------------------+
//| OnTick function |
//+------------------------------------------------------------------+
void OnTick()
{
if(CountTrades()<1) OpenPos();
else ClosePos();
if(BULevel!=0) BU();
if(TrailingStop!=0) Trailing();
}
//+------------------------------------------------------------------+
AM2