//+------------------------------------------------------------------+
//| OpenOrders.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 = 300; // расстояние от цены
extern int Slip = 30; // реквот
extern int Magic = 123; // магик
datetime t=0;
//+------------------------------------------------------------------+
//| Expert initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
//---
Comment("");
//---
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
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 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);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
void DelOrder()
{
bool del;
for(int i=OrdersTotal()-1;i>=0;i--)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
{
if(OrderSymbol()==Symbol() && OrderMagicNumber()==Magic)
{
if(OrderType()>1) del=OrderDelete(OrderTicket());
}
}
}
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
int Last()
{
int result=0;
if(OrderSelect(OrdersHistoryTotal()-1,SELECT_BY_POS,MODE_HISTORY))
{
if(OrderSymbol()==Symbol() && OrderMagicNumber()==Magic)
{
if(OrderProfit()>0)
{
result=1;//tp
}
if(OrderProfit()<0)
{
result=2;//sl
}
}
}
return(result);
}
//+------------------------------------------------------------------+
//| OnTick function |
//+------------------------------------------------------------------+
void OnTick()
{
if(Last()==1) DelOrder();
if(CountOrders(0)<1 && CountOrders(1)<1)
{
if(CountOrders(4)<1)PutOrder(4,Bid+Delta*Point);
if(CountOrders(5)<1)PutOrder(5,Bid-Delta*Point);
}
}
//+------------------------------------------------------------------+
Пожалуста посмотрите почему не выставляются ордера.
сигналы
Open[1]<Close[1] && Open[0]>Close[1] покупка
Open[1]>Close[1] && Open[0]<Close[1] продажа
//+------------------------------------------------------------------+
//| HedgeMartin.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
//--- Inputs
extern int Delta = 100; // дельта
extern double Loss = 9000; // убыток в валюте
extern double Profit = 10; // профит в валюте
extern double Lots = 0.01; // лот
extern double KLot = 2; // увеличение лота
extern double MaxLot = 10; // максимальный лот
extern int CloseRevers = 0; // 1-закрывать ордера при встречном сигнале
extern int Slip = 30; // проскальзывание
extern int Magic = 123; // магик
double BuyStopPrice=0,SellStopPrice=0;
//+------------------------------------------------------------------+
//| Expert initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Expert deinitialization function |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
}
//+------------------------------------------------------------------+
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 LastOrderType()
{
int type=0;
if(OrderSelect(OrdersTotal()-1,SELECT_BY_POS,MODE_TRADES))
{
if(OrderSymbol()==Symbol() && OrderMagicNumber()==Magic)
{
if(OrderType()==OP_BUY) type=1; //sell
if(OrderType()==OP_SELL) type=2; //buy
}
}
return(type);
}
//+------------------------------------------------------------------+
void CloseAll()
{
bool cl=true;
double pBid,pAsk;
for(int i=OrdersTotal()-1;i>=0;i--)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
{
if(OrderSymbol()==Symbol() && OrderMagicNumber()==Magic)
{
pBid=MarketInfo(OrderSymbol(),MODE_BID);
pAsk=MarketInfo(OrderSymbol(),MODE_ASK);
if(OrderType()==OP_BUY) cl=OrderClose(OrderTicket(),OrderLots(),fND(pBid),Slip,Blue);
if(OrderType()==OP_SELL) cl=OrderClose(OrderTicket(),OrderLots(),fND(pAsk),Slip,Red);
}
}
}
}
//+------------------------------------------------------------------+
double fND(double d,int n=-1)
{
if(n<0) return(NormalizeDouble(d, Digits));
return(NormalizeDouble(d, n));
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
double AllProfit()
{
double profit=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) profit+=OrderProfit()+OrderSwap()+OrderCommission();
}
}
}
return(profit);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
void DelOrder()
{
bool del;
for(int i=OrdersTotal()-1;i>=0;i--)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
{
if(OrderSymbol()==Symbol() && OrderMagicNumber()==Magic)
{
if(OrderType()>1) del=OrderDelete(OrderTicket());
}
}
}
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
int LastDealResult()
{
int result=0;
if(OrdersHistoryTotal()==0)
{
result=0;
}
if(OrderSelect(OrdersHistoryTotal()-1,SELECT_BY_POS,MODE_HISTORY))
{
if(OrderSymbol()==Symbol() && OrderMagicNumber()==Magic)
{
if(OrderProfit()>0)
{
result=1;//tp
}
if(OrderProfit()<0)
{
result=2;//sl
}
}
}
return(result);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
double Lot()
{
double lot=Lots;
if(CountTrades()>0) lot=NormalizeDouble(Lots*MathPow(KLot,CountTrades()),2);
if(lot>MaxLot)lot=Lots;
return(lot);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
void CloseOrder(int ord)
{
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()==ord && ord==0) cl=OrderClose(OrderTicket(),OrderLots(),NormalizeDouble(Bid,Digits),Slip,CLR_NONE);
if(OrderType()==ord && ord==1) cl=OrderClose(OrderTicket(),OrderLots(),NormalizeDouble(Ask,Digits),Slip,CLR_NONE);
}
}
}
return;
}
//+------------------------------------------------------------------+
//| Expert tick function |
//+------------------------------------------------------------------+
void OnTick()
{
// торгуем
int res=0,i=0;
double tp=0,bid=0,ask=0,pr=0;
bid=MarketInfo(Symbol(),MODE_BID);
ask=MarketInfo(Symbol(),MODE_ASK);
// открытие первого ордера по условию
if(CountTrades()<1)
{
DelOrder();
//---- buy
if(Open[1]<Close[1] && Open[0]>Close[1])
{
res=OrderSend(Symbol(),OP_BUY,Lot(),NormalizeDouble(ask,Digits),Slip,0,0,"",Magic,0,Blue);
BuyStopPrice=ask;
SellStopPrice=ask-Delta*Point;
return;
}
//---- sell
if(Open[1]>Close[1] && Open[0]<Close[1])
{
res=OrderSend(Symbol(),OP_SELL,Lot(),NormalizeDouble(bid,Digits),Slip,0,0,"",Magic,0,Red);
SellStopPrice=bid;
BuyStopPrice=pr=bid+Delta*Point;
return;
}
}
// открытие последующих ордеров
if(CountTrades()>0)
{
if(LastOrderType()==1)
{
pr=SellStopPrice;
res=OrderSend(Symbol(),OP_SELLSTOP,Lot(),NormalizeDouble(pr,Digits),Slip,0,0,"",Magic,0,Red);
return;
}
if(LastOrderType()==2)
{
pr=BuyStopPrice;
res=OrderSend(Symbol(),OP_BUYSTOP,Lot(),NormalizeDouble(pr,Digits),Slip,0,0,"",Magic,0,Blue);
return;
}
}
if(LastDealResult()==1)DelOrder();
if(AllProfit()>=Profit || AllProfit()<=-Loss)
{
CloseAll();
DelOrder();
}
if(CloseRevers>0)
{
// sell кроем баи
if(Open[1]>Close[1] && Open[0]<Close[1]) CloseOrder(0);
// buy кроем селлы
if(Open[1]<Close[1] && Open[0]>Close[1]) CloseOrder(1);
}
Comment("\n Profit: ",DoubleToString(AllProfit(),2),
"\n Last Order Type: ",LastOrderType(),
"\n Count Trades: ",CountTrades());
}
//+------------------------------------------------------------------+
extern bool CloseRevers = true; // закрывать ордера при встречном сигнале
Этот момент поясните подробнее?
AM2