Еще нужно добавить 4-в противоположную сторону.
//+------------------------------------------------------------------+
//| Random.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 int StopLoss = 500; // лось
extern int TakeProfit = 500; // язь
extern int BULevel = 0; // уровень БУ
extern int BUPoint = 30; // пункты БУ
extern int TrailingStop = 0; // трал
extern int BarsCount = 3; // через сколько баров открывать
extern int BuySell = 0; // 0-Candle 1-buy 2-sell 3-random
extern int Slip = 30; // реквот
extern int Magic = 123; // магик
datetime t=0;
//+------------------------------------------------------------------+
//| Expert initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
//---
MathSrand(GetTickCount());
//---
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Expert deinitialization function |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
//---
}
//+------------------------------------------------------------------+
//| Установка ордера |
//+------------------------------------------------------------------+
void PutOrder(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,Lots,NormalizeDouble(price,Digits),Slip,sl,tp,"",Magic,0,clr);
return;
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
int CountTrades()
{
int count=0;
for(int i=OrdersTotal()-1;i>=0;i--)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
{
if(OrderType()<2) count++;
}
}
return(count);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
datetime OOT()
{
datetime oot=0;
for(int i=OrdersTotal()-1;i>=0;i--)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
{
if(OrderType()<2) oot=OrderOpenTime();
break;
}
}
return(oot);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
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 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,Yellow);
return;
}
}
if(OrderType()==OP_SELL)
{
if(OrderOpenPrice()>=(Ask+(BULevel+BUPoint)*Point) && (OrderOpenPrice()<OrderStopLoss() || OrderStopLoss()==0))
{
m=OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice()-BUPoint*Point,OrderTakeProfit(),0,Yellow);
return;
}
}
}
}
}
}
//+------------------------------------------------------------------+
//| Expert tick function |
//+------------------------------------------------------------------+
void OnTick()
{
MathSrand(GetTickCount());
bool buy=true,sell=true;
if(BuySell==0){buy=Close[1]>Open[1]; sell=Close[1]<Open[1];}
if(BuySell==1){buy=true; sell=false;}
if(BuySell==2){buy=false; sell=true;}
if(BuySell==3)
{
int res=MathRand();
if(res>16383) {buy=true; sell=false;}
if(res<16383) {buy=false; sell=true;}
}
if(CountTrades()<1) PutOrder(1,Bid);
if(t!=Time[0] && TimeCurrent()-OOT()>BarsCount*PeriodSeconds())
{
if(buy) PutOrder(0,Ask);
if(sell) PutOrder(1,Bid);
t=Time[0];
}
if(BULevel>0) BU();
if(TrailingStop>0) Trailing();
Comment(MathRand());
}
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
//| Random.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 int StopLoss = 500; // лось
extern int TakeProfit = 500; // язь
extern int BULevel = 0; // уровень БУ
extern int BUPoint = 30; // пункты БУ
extern int TrailingStop = 0; // трал
extern int BarsCount = 3; // через сколько баров открывать
extern int BuySell = 0; // 0-Candle 1-buy 2-sell 3-random
extern int Slip = 30; // реквот
extern int Magic = 123; // магик
datetime t=0;
//+------------------------------------------------------------------+
//| Expert initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
//---
//---
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Expert deinitialization function |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
//---
}
//+------------------------------------------------------------------+
//| Установка ордера |
//+------------------------------------------------------------------+
void PutOrder(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,Lots,NormalizeDouble(price,Digits),Slip,sl,tp,"",Magic,0,clr);
return;
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
int CountTrades()
{
int count=0;
for(int i=OrdersTotal()-1;i>=0;i--)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
{
if(OrderType()<2) count++;
}
}
return(count);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
datetime OOT()
{
datetime oot=0;
for(int i=OrdersTotal()-1;i>=0;i--)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
{
if(OrderType()<2) oot=OrderOpenTime();
break;
}
}
return(oot);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
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 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,Yellow);
return;
}
}
if(OrderType()==OP_SELL)
{
if(OrderOpenPrice()>=(Ask+(BULevel+BUPoint)*Point) && (OrderOpenPrice()<OrderStopLoss() || OrderStopLoss()==0))
{
m=OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice()-BUPoint*Point,OrderTakeProfit(),0,Yellow);
return;
}
}
}
}
}
}
//+------------------------------------------------------------------+
//| Expert tick function |
//+------------------------------------------------------------------+
void OnTick()
{
bool buy=true,sell=true;
if(BuySell==0){buy=Close[1]>Open[1]; sell=Close[1]<Open[1];}
if(BuySell==1){buy=true; sell=false;}
if(BuySell==2){buy=false; sell=true;}
if(BuySell==3)
{
if(MathRand()/32768==0) {buy=true; sell=false;}
else {buy=false; sell=true;}
}
if(CountTrades()<1) PutOrder(1,Bid);
if(t!=Time[0] && TimeCurrent()-OOT()>BarsCount*PeriodSeconds())
{
if(buy) PutOrder(0,Ask);
if(sell) PutOrder(1,Bid);
t=Time[0];
}
if(BULevel>0) BU();
if(TrailingStop>0) Trailing();
}
//+------------------------------------------------------------------+
Главное, чтобы присутствовал, данный пункт т.з.: "- объём ордера, если 0, то такой же, как рыночный".
//+------------------------------------------------------------------+
//| Poshnik.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 int StopLoss = 500; // лось
extern int TakeProfit = 500; // язь
extern int Delta = 100; //
extern int BuySell = 0; // 1-buy 2-sell 0-off
extern int Slip = 30; // реквот
extern int Magic = 123; // магик
//+------------------------------------------------------------------+
//| Expert initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
//---
//---
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Expert deinitialization function |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
//---
}
//+------------------------------------------------------------------+
//| Установка ордера |
//+------------------------------------------------------------------+
void PutOrder(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,Lots,NormalizeDouble(price,Digits),Slip,sl,tp,"",Magic,0,clr);
return;
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
int CountTrades()
{
int count=0;
for(int i=OrdersTotal()-1;i>=0;i--)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
{
if(OrderType()<2) count++;
}
}
return(count);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
double MarketOOP()
{
double oop=0;
if(OrderSelect(OrdersTotal()-1,SELECT_BY_POS,MODE_TRADES))
{
if(OrderType()<2) oop=OrderOpenPrice();
}
return(oop);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
int LastType()
{
int type=8;
if(OrderSelect(OrdersTotal()-1,SELECT_BY_POS,MODE_TRADES))
{
if(OrderType()<2) type=OrderType();
}
return(type);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
double StopOOP()
{
double oop=0;
if(OrderSelect(OrdersTotal()-1,SELECT_BY_POS,MODE_TRADES))
{
if(OrderType()>1) oop=OrderOpenPrice();
}
return(oop);
}
//+------------------------------------------------------------------+
//| Удаление отложенных ордеров |
//+------------------------------------------------------------------+
void DelOrder()
{
bool del;
for(int i=OrdersTotal()-1;i>=0;i--)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
{
if(OrderType()>1) del=OrderDelete(OrderTicket());
}
}
}
//+------------------------------------------------------------------+
//| Expert tick function |
//+------------------------------------------------------------------+
void OnTick()
{
if(CountTrades()<1)
{
DelOrder();
if(BuySell==1) PutOrder(0,Ask);
if(BuySell==2) PutOrder(1,Bid);
}
if(MarketOOP()>0 && StopOOP()!=NormalizeDouble(MarketOOP()-Delta*Point,Digits) && LastType()==0) PutOrder(5,MarketOOP()-Delta*Point);
if(MarketOOP()>0 && StopOOP()!=NormalizeDouble(MarketOOP()+Delta*Point,Digits) && LastType()==1) PutOrder(4,MarketOOP()+Delta*Point);
}
//+------------------------------------------------------------------+
Проблема в том, что если открылись и закрылись в рамках одной свечи, то открывается новая серия?
AM2