Осталось только сделать так, чтоб закрывались все ордера, когда есть замок между ордерами.
Объясните этот момент максимально подробно со скринами.
Осталось только сделать так, чтоб закрывались все ордера, когда есть замок между ордерами.
//+------------------------------------------------------------------+
//| Grider.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 Lot = 0.1; // Trade volume
extern double KLot = 1.3; // Lot increase
extern double MaxLot = 5; // Maximum lot
extern int StopLoss = 0; // Order sroploss
extern int TakeProfit = 0; // Order takeprofit
extern int Profit = 0; // Querency Profit
extern int TrailingStop = 0; // Trailing stop
extern int TrailingStep = 0; // Trailing step
extern int Count = 5; // Orders count
extern int Expiration = 55; // The expiry of the order in hours
extern int Step = 100; // Order step
extern int Delta = 100; // Distance from the price
extern int Slip = 3; // Slippage
extern int StopLimit = 0; // 0-Stop 1-Limit
extern int ClosePos = 0; // 1-Close Positions
extern int Magic = 123; // Magic number
datetime t=0;
//+------------------------------------------------------------------+
//| Expert initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
if(!IsTradeAllowed())Alert("Allow the EA to trade!");
if(!IsLibrariesAllowed())Alert("Allow DLL import!");
if(!IsExpertEnabled()) Alert("Allow startup advisors!");
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Expert deinitialization function |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
Comment("");
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
void PutOrder(int type,double price)
{
int r=0,err=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(type),NormalizeDouble(price,Digits),Slip,sl,tp,"",Magic,TimeCurrent()+Expiration*3600,clr);
return;
}
//+------------------------------------------------------------------+
//| Ступенчатый трал if(TrailingStop>0) Trailing(); |
//+------------------------------------------------------------------+
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+TrailingStep-1)*Point)
{
mod=OrderModify(OrderTicket(),OrderOpenPrice(),NormalizeDouble(Bid-TrailingStop*Point,Digits),OrderTakeProfit(),0,Yellow);
}
}
}
if(OrderType()==OP_SELL)
{
if((OrderOpenPrice()-Ask)>TrailingStop*Point)
{
if(OrderStopLoss()>Ask+(TrailingStop+TrailingStep-1)*Point || OrderStopLoss()==0)
{
mod=OrderModify(OrderTicket(),OrderOpenPrice(),NormalizeDouble(Ask+TrailingStop*Point,Digits),OrderTakeProfit(),0,Yellow);
}
}
}
}
}
}
}
//+------------------------------------------------------------------+
//| Закрытие позиции по типу ордера |
//+------------------------------------------------------------------+
void CloseAll(int ot=-1)
{
bool cl;
int err=0;
for(int i=OrdersTotal()-1;i>=0;i--)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
{
if(OrderSymbol()==Symbol() && OrderMagicNumber()==Magic)
{
if(OrderType()==0 && (ot==0 || ot==-1))
{
RefreshRates();
cl=OrderClose(OrderTicket(),OrderLots(),NormalizeDouble(Bid,Digits),Slip,White);
}
if(OrderType()==1 && (ot==1 || ot==-1))
{
RefreshRates();
cl=OrderClose(OrderTicket(),OrderLots(),NormalizeDouble(Ask,Digits),Slip,White);
}
}
}
}
}
//+------------------------------------------------------------------+
//| Подсчет ордеров по типу |
//+------------------------------------------------------------------+
int CountOrders(int type=-1)
{
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 || (OrderType()>=0 && type==-1)) count++;
}
}
}
return(count);
}
//+------------------------------------------------------------------+
//| Профит всех ордеров по типу ордера |
//+------------------------------------------------------------------+
double AllProfit(int ot=-1)
{
double pr=0;
for(int i=OrdersTotal()-1;i>=0;i--)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
{
if(OrderSymbol()==Symbol() && OrderMagicNumber()==Magic)
{
if(OrderType()==0 && (ot==0 || ot==-1))
{
pr+=OrderProfit()+OrderCommission()+OrderSwap();
}
if(OrderType()==1 && (ot==1 || ot==-1))
{
pr+=OrderProfit()+OrderCommission()+OrderSwap();
}
}
}
}
return(pr);
}
//+------------------------------------------------------------------+
//| Лот для усреднителя |
//+------------------------------------------------------------------+
double Lots(int type)
{
double lots=Lot;
lots=NormalizeDouble(Lot*MathPow(KLot,CountOrders(type)),2);
if(lots>MaxLot)lots=Lot;
return(lots);
}
//+------------------------------------------------------------------+
//| Удаление отложенных ордеров |
//+------------------------------------------------------------------+
void DelOrder(int type)
{
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()==type) del=OrderDelete(OrderTicket());
}
}
}
}
//+------------------------------------------------------------------+
//| Expert tick function |
//+------------------------------------------------------------------+
void OnTick()
{
if(TrailingStop>0) Trailing();
if(t!=Time[0])
{
if(Open[1]<Close[1])
{
DelOrder(5);
DelOrder(2);
if(ClosePos>0) CloseAll();
for(int i=1; i<=Count;i++)
{
if(StopLimit==0)PutOrder(4,Bid+Delta*Point+Step*Point*i);//buystop
if(StopLimit==1)PutOrder(3,Bid+Delta*Point+Step*Point*i);//selllimit
}
}
if(Open[1]>Close[1])
{
DelOrder(4);
DelOrder(3);
if(ClosePos>0) CloseAll();
for(int i=1; i<=Count;i++)
{
if(StopLimit==0)PutOrder(5,Bid-Delta*Point-Step*Point*i);//sellstop
if(StopLimit==1)PutOrder(2,Bid-Delta*Point-Step*Point*i);//buylimit
}
}
t=Time[0];
}
if(AllProfit()>Profit && Profit>0) CloseAll();
Comment("\n All Profit: ",AllProfit(-1),
"\n Buy Profit: ",AllProfit(0),
"\n Sell Profit: ",AllProfit(1),
"\n Buy Positions: ",CountOrders(0),
"\n Sell Positions: ",CountOrders(1),
"\n All Positions: ",CountOrders(0)+CountOrders(1));
}
//+------------------------------------------------------------------+
if(lot>MaxLot)lot=Lots;
if(lot>MaxLot)lot=MaxLot;
Buy, Sell, другое — напр. флет, «средний уровень-зона», ожидание разворота (зона приближения экстремума) и т.п.
ну почему отсутствие системы? ты же по market profile торговал — а это и есть система
AM2