
//+------------------------------------------------------------------+
//| OTL.mq4 |
//| Copyright © 2013, AM2 |
//| http://www.forexsystems.biz |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2011, AM2"
#property link "http://www.forexsystems.biz"
// 1. В зависимости от цвета предыдущей свечи выставляем отложку на расстоянии Delta от цены в StartHour часов на пробой.
// 2. Время истечения ордера Expiration часов.
// 3. Ордер выставляется с установленными значениями стоплосс и тейкпрофит.
// 4. Торговля фиксированным лотом.
// 5. Торговля на открытии свечи на часовом графике.
extern int StopLoss = 1000; //Стоплосс ордера
extern int TakeProfit = 1000; //Тейкпрофит ордера
extern int StartHour = 7; //Час начала торговли(терминальное время)
extern int Delta = 100; //Расстояние от цены для установки ордера
extern int Expiration = 4; //Время истечения ордера
extern double Lot = 1; //Объём позиции
extern double KLot = 2; //Увеличение лота
extern double MaxLot = 5; //Максимальный oбъём позиции
extern int Magic = 130228; //Магик
double LastOpen=0;
//+------------------------------------------------------------------+
//| Calculate optimal lot size |
//+------------------------------------------------------------------+
double Lots()
{
double lot;
if(OrdersHistoryTotal()==0)
{
lot=Lot;
}
if(OrderSelect(OrdersHistoryTotal()-1,SELECT_BY_POS,MODE_HISTORY))
{
if(OrderProfit()>0)
{
lot=Lot;
}
if(OrderProfit()<=0)
{
lot=OrderLots()*KLot;
}
}
if(lot>MaxLot)lot=Lot;
return(lot);
}
//+------------------------------------------------------------------+
//| OnTick function |
//+------------------------------------------------------------------+
void OnTick()
{
//----
if(LastOpen==Open[0])return;
int b,s,p,res;
datetime expiration=TimeCurrent()+3600*Expiration;
double BuyPrice=Ask+Delta*Point;
double SellPrice=Bid-Delta*Point;
for(int i=0; i<OrdersTotal(); i++)
{
if(OrderSelect(i,SELECT_BY_POS)==true)
{
if(OrderSymbol()!=Symbol() || OrderMagicNumber()!=Magic) continue;
if(OrderType()==OP_BUY || OrderType()==OP_SELL) p++;
if(OrderType()==OP_BUYSTOP) b++;
if(OrderType()==OP_SELLSTOP) s++;
}
}
//---- buy stop
if(Close[1]-Open[1]>0 && Hour()==StartHour && p<1 && b<1)
{
res=OrderSend(Symbol(),OP_BUYSTOP,Lots(),
NormalizeDouble(BuyPrice,Digits),0,
NormalizeDouble(BuyPrice-StopLoss*Point,Digits),
NormalizeDouble(BuyPrice+TakeProfit*Point,Digits),
"",Magic,expiration,Blue);
return;
}
//---- sell stop
if(Open[1]-Close[1]>0 && Hour()==StartHour && p<1 && s<1)
{
res=OrderSend(Symbol(),OP_SELLSTOP,Lots(),
NormalizeDouble(SellPrice,Digits),0,
NormalizeDouble(SellPrice+StopLoss*Point,Digits),
NormalizeDouble(SellPrice-TakeProfit*Point,Digits),
"",Magic,expiration,Red);
return;
}
LastOpen=Open[0];
//----
}
//--------------------------------------------------------------------
//+------------------------------------------------------------------+
//| CloseLot.mq4 |
//| Copyright 2015, AM2 |
//| http://www.forexsystems.biz |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2015, AM2"
#property link "http://www.forexsystems.biz"
#property description "Simple expert advisor"
#include <stdlib.mqh>
//--- Inputs
extern int StopLoss = 500;
extern int StopLoss1 = 0;
extern int TakeProfit = 20;
extern int TakeProfit1 = 5;
extern int TakeProfit2 = 10;
extern int TakeProfit3 = 15;
extern int OpenOrder=2; //0-off. 1-buy. 2-sell.
extern int Slip = 30;
extern double Lot = 1;
extern double Lot1 = 0.1;
extern double Lot2 = 0.2;
extern double Lot3 = 0.3;
extern double StopLot = 0.4;
extern int Magic = 0;
bool CloseOrd1=true,CloseOrd2=true,CloseOrd3=true,CloseOrd4=true;
//+------------------------------------------------------------------+
//| 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=0,err=0;
double sl;
//--- sell
if(OpenOrder==2)
{
double bid=MarketInfo(NULL,MODE_BID);
if(StopLoss==0) sl=0; else sl=bid+StopLoss*Point;
res=OrderSend(Symbol(),OP_SELL,Lot,bid,Slip,sl,bid-TakeProfit*Point,"",Magic,0,Red);
if(res<0)
{
err=GetLastError();
Print("ОШИБКА ВЫСТАВЛЕНИЯ ОРДЕРА SELL: ",err,"(",ErrorDescription(err),")");
} else {
RefreshRates();
}
}
//--- buy
if(OpenOrder==1)
{
double ask=MarketInfo(NULL,MODE_ASK);
if(StopLoss==0) sl=0; else sl=ask-StopLoss*Point;
res=OrderSend(Symbol(),OP_BUY,Lot,ask,Slip,sl,ask+TakeProfit*Point,"",Magic,0,Blue);
if(res<0)
{
err=GetLastError();
Print("ОШИБКА ВЫСТАВЛЕНИЯ ОРДЕРА BUY: ",err,"(",ErrorDescription(err),")");
} else {
RefreshRates();
}
}
//---
}
//+------------------------------------------------------------------+
void CloseOrders()
{
bool cl;
for(int i=OrdersTotal()-1;i>=0;i--)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
{
if(OrderSymbol()==Symbol())
{
if(OrderMagicNumber()==Magic)
{
if(OrderType()==OP_BUY)
{
if(Bid-OrderOpenPrice()>TakeProfit1*Point)
{
if(CloseOrd1) cl=OrderClose(OrderTicket(),Lot1,Bid,Slip,Blue);
CloseOrd1=false;
}
if(Bid-OrderOpenPrice()>TakeProfit2*Point)
{
if(CloseOrd2) cl=OrderClose(OrderTicket(),Lot2,Bid,Slip,Blue);
CloseOrd2=false;
}
if(Bid-OrderOpenPrice()>TakeProfit3*Point)
{
if(CloseOrd3) cl=OrderClose(OrderTicket(),Lot3,Bid,Slip,Blue);
CloseOrd3=false;
}
if(Bid-OrderOpenPrice()>TakeProfit*Point)
{
cl=OrderClose(OrderTicket(),OrderLots(),Bid,Slip,Blue);
}
if(Bid-OrderOpenPrice()<StopLoss1*Point && StopLoss1>0)
{
if(CloseOrd4) cl=OrderClose(OrderTicket(),StopLot,Bid,Slip,Blue);
CloseOrd4=false;
}
}
if(OrderType()==OP_SELL)
{
if(OrderOpenPrice()-Ask>TakeProfit1*Point)
{
if(CloseOrd1) cl=OrderClose(OrderTicket(),Lot1,Ask,Slip,Red);
CloseOrd1=false;
}
if(OrderOpenPrice()-Ask>TakeProfit2*Point)
{
if(CloseOrd2) cl=OrderClose(OrderTicket(),Lot2,Ask,Slip,Red);
CloseOrd2=false;
}
if(OrderOpenPrice()-Ask>TakeProfit3*Point)
{
if(CloseOrd3) cl=OrderClose(OrderTicket(),Lot3,Ask,Slip,Red);
CloseOrd3=false;
}
if(OrderOpenPrice()-Ask>TakeProfit*Point)
{
cl=OrderClose(OrderTicket(),OrderLots(),Ask,Slip,Red);
}
if(OrderOpenPrice()-Ask<StopLoss1*Point && StopLoss1>0)
{
if(CloseOrd4) cl=OrderClose(OrderTicket(),StopLot,Ask,Slip,Blue);
CloseOrd4=false;
}
}
}
}
}
}
return;
}
//+------------------------------------------------------------------+
//| OnTick function |
//+------------------------------------------------------------------+
void OnTick()
{
int p=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) p++;
}
}
}
if(p<1)
{
CloseOrd1=true;
CloseOrd2=true;
CloseOrd3=true;
CloseOrd4=true;
OpenPos();
}
if(p>0) CloseOrders();
Comment("\n Positions: ",p,
"\n CloseOrd1: ",CloseOrd1,
"\n CloseOrd2: ",CloseOrd2,
"\n CloseOrd3: ",CloseOrd3,
"\n CloseOrd4: ",CloseOrd4);
//---
}
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
int ticket()
{
int tik;
if(OrderSelect(OrdersHistoryTotal()-1,SELECT_BY_POS,MODE_HISTORY))tik=OrderTicket();
return(tik);
}
//+------------------------------------------------------------------+
//| Last lot size |
//+------------------------------------------------------------------+
double LastLot()
{
double lot=0;
if(OrderSelect(OrdersHistoryTotal()-1,SELECT_BY_POS,MODE_HISTORY))lot=OrderLots();
return(lot);
}
extern int StopLoss1 = 0;
extern double StopLot = 0.4;
//+------------------------------------------------------------------+
//| CloseLot.mq4 |
//| Copyright 2015, AM2 |
//| http://www.forexsystems.biz |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2015, AM2"
#property link "http://www.forexsystems.biz"
#property description "Simple expert advisor"
#include <stdlib.mqh>
//--- Inputs
extern int StopLoss = 500;
extern int StopLoss1 = 0;
extern int TakeProfit = 20;
extern int TakeProfit1 = 5;
extern int TakeProfit2 = 10;
extern int TakeProfit3 = 15;
extern int OpenOrder=2; //0-off. 1-buy. 2-sell.
extern int Slip = 30;
extern double Lot = 1;
extern double Lot1 = 0.1;
extern double Lot2 = 0.2;
extern double Lot3 = 0.3;
extern double StopLot = 0.4;
extern int Magic = 0;
bool CloseOrd1=true,CloseOrd2=true,CloseOrd3=true,CloseOrd4=true;
//+------------------------------------------------------------------+
//| 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=0,err=0;
double sl;
//--- sell
if(OpenOrder==2)
{
double bid=MarketInfo(NULL,MODE_BID);
if(StopLoss==0) sl=0; else sl=bid+StopLoss*Point;
res=OrderSend(Symbol(),OP_SELL,Lot,bid,Slip,sl,bid-TakeProfit*Point,"",Magic,0,Red);
if(res<0)
{
err=GetLastError();
Print("ОШИБКА ВЫСТАВЛЕНИЯ ОРДЕРА SELL: ",err,"(",ErrorDescription(err),")");
} else {
RefreshRates();
}
}
//--- buy
if(OpenOrder==1)
{
double ask=MarketInfo(NULL,MODE_ASK);
if(StopLoss==0) sl=0; else sl=ask-StopLoss*Point;
res=OrderSend(Symbol(),OP_BUY,Lot,ask,Slip,sl,ask+TakeProfit*Point,"",Magic,0,Blue);
if(res<0)
{
err=GetLastError();
Print("ОШИБКА ВЫСТАВЛЕНИЯ ОРДЕРА BUY: ",err,"(",ErrorDescription(err),")");
} else {
RefreshRates();
}
}
//---
}
//+------------------------------------------------------------------+
void CloseOrders()
{
bool cl;
for(int i=OrdersTotal()-1;i>=0;i--)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
{
if(OrderSymbol()==Symbol())
{
if(OrderMagicNumber()==Magic)
{
if(OrderType()==OP_BUY)
{
if(Bid-OrderOpenPrice()>TakeProfit1*Point)
{
if(CloseOrd1) cl=OrderClose(OrderTicket(),Lot1,Bid,Slip,Blue);
CloseOrd1=false;
}
if(Bid-OrderOpenPrice()>TakeProfit2*Point)
{
if(CloseOrd2) cl=OrderClose(OrderTicket(),Lot2,Bid,Slip,Blue);
CloseOrd2=false;
}
if(Bid-OrderOpenPrice()>TakeProfit3*Point)
{
if(CloseOrd3) cl=OrderClose(OrderTicket(),Lot3,Bid,Slip,Blue);
CloseOrd3=false;
}
if(Bid-OrderOpenPrice()>TakeProfit*Point)
{
cl=OrderClose(OrderTicket(),OrderLots(),Bid,Slip,Blue);
}
if(Bid-OrderOpenPrice()<StopLoss1*Point && StopLoss1>0)
{
cl=OrderClose(OrderTicket(),StopLot,Bid,Slip,Blue);
CloseOrd4=false;
}
}
if(OrderType()==OP_SELL)
{
if(OrderOpenPrice()-Ask>TakeProfit1*Point)
{
if(CloseOrd1) cl=OrderClose(OrderTicket(),Lot1,Ask,Slip,Red);
CloseOrd1=false;
}
if(OrderOpenPrice()-Ask>TakeProfit2*Point)
{
if(CloseOrd2) cl=OrderClose(OrderTicket(),Lot2,Ask,Slip,Red);
CloseOrd2=false;
}
if(OrderOpenPrice()-Ask>TakeProfit3*Point)
{
if(CloseOrd3) cl=OrderClose(OrderTicket(),Lot3,Ask,Slip,Red);
CloseOrd3=false;
}
if(OrderOpenPrice()-Ask>TakeProfit*Point)
{
cl=OrderClose(OrderTicket(),OrderLots(),Ask,Slip,Red);
}
if(OrderOpenPrice()-Ask<StopLoss1*Point && StopLoss1>0)
{
cl=OrderClose(OrderTicket(),StopLot,Ask,Slip,Blue);
CloseOrd4=false;
}
}
}
}
}
}
return;
}
//+------------------------------------------------------------------+
//| OnTick function |
//+------------------------------------------------------------------+
void OnTick()
{
int p=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) p++;
}
}
}
if(p<1)
{
CloseOrd1=true;
CloseOrd2=true;
CloseOrd3=true;
OpenPos();
}
if(p>0) CloseOrders();
Comment("\n Positions: ",p,
"\n CloseOrd1: ",CloseOrd1,
"\n CloseOrd2: ",CloseOrd2,
"\n CloseOrd3: ",CloseOrd3);
//---
}
//+------------------------------------------------------------------+
Проверил ваш код, ордера открывает и закрывает как надо, только пока не увеличивает ставки.
AM2