//+------------------------------------------------------------------+
//| Simple.mq4 |
//| Copyright 2024, AM2 |
//|
www.forexsystems.biz |
//+------------------------------------------------------------------+
#property copyright «Copyright 2024, AM2»
#property link «
www.forexsystems.biz»
#property version «1.00»
#property strict
//--- Inputs
extern double Lots = 0.1; // лот
extern double KLot = 1.2; // умножение лота
extern double MaxLot = 5; // максимальный лот
extern int StopLoss = 0; // лось
extern int TakeProfit = 111; // язь
extern int StartHour = 0; // час начала торговли
extern int StartMin = 30; // минута начала торговли
extern int EndHour = 23; // час окончания торговли
extern int EndMin = 30; // минута окончания торговли
extern int Step = 333; // шаг
extern int Count = 20; // число поз
extern int Slip = 30; // реквот
extern int CloseSig = 0; // 1-закрытие по сигналу
extern int Spread = 33; // спред
extern int Magic = 123; // магик
extern string Comm = «Scalp»;
datetime t=0;
//+------------------------------------------------------------------+
//| Expert initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
//---
Comment("");
//---
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Expert deinitialization function |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
Comment("");
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
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,Lot(type),NormalizeDouble(price,_Digits),Slip,sl,tp,Comm,Magic,0,clr);
return;
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
bool TimeSession(int aStartHour,int aStartMinute,int aStopHour,int aStopMinute,datetime aTimeCur)
{
//--- время начала сессии
int StartTime=3600*aStartHour+60*aStartMinute;
//--- время окончания сессии
int StopTime=3600*aStopHour+60*aStopMinute;
//--- текущее время в секундах от начала дня
aTimeCur=aTimeCur%86400;
if(StopTime<StartTime)
{
//--- переход через полночь
if(aTimeCur>=StartTime || aTimeCur<StopTime)
{
return(true);
}
}
else
{
//--- внутри одного дня
if(aTimeCur>=StartTime && aTimeCur<StopTime)
{
return(true);
}
}
return(false);
}
//+------------------------------------------------------------------+
//| Подсчет позиций |
//+------------------------------------------------------------------+
int CountTrades(int ot=-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()==0 && (ot==0 || ot==-1))
count++;
if(OrderType()==1 && (ot==1 || ot==-1))
count++;
}
}
}
return(count);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
double Lot(int type)
{
double lot=Lots;
if(CountTrades(type)>0)
{
lot=NormalizeDouble(Lots*MathPow(KLot,CountTrades(type)),2);
}
if(lot>MaxLot)
lot=Lots;
return(lot);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
void CloseAll(int ot=-1)
{
bool cl=1;
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 FindOrderType()
{
int type=0;
for(int i=OrdersTotal()-1; i>=0; i--)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
{
if(OrderSymbol()==Symbol() && OrderMagicNumber()==Magic)
{
type=OrderType();
break;
}
}
}
return(type);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
double FindLastBuyPrice()
{
double p=0;
for(int i=OrdersTotal()-1; i>=0; i--)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
{
if(OrderSymbol()==Symbol() && OrderType()==OP_BUY && OrderMagicNumber()==Magic)
{
p=OrderOpenPrice();
break;
}
}
}
return(p);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
double FindLastSellPrice()
{
double p=0;
for(int i=OrdersTotal()-1; i>=0; i--)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
{
if(OrderSymbol()==Symbol() && OrderType()==OP_SELL && OrderMagicNumber()==Magic)
{
p=OrderOpenPrice();
break;
}
}
}
return(p);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
void ModifyOrders()
{
bool mod=1;
double allb=0,alls=0;
double countb=0,counts=0,tp=0,sl=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)
{
allb+=OrderOpenPrice()*OrderLots();
countb+=OrderLots();
}
if(OrderType()==1)
{
alls+=OrderOpenPrice()*OrderLots();
counts+=OrderLots();
}
}
}
}
if(countb>0)
allb=NormalizeDouble(allb/countb,_Digits);
if(counts>0)
alls=NormalizeDouble(alls/counts,_Digits);
for(int i=OrdersTotal()-1; i>=0; i--)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
{
if(OrderSymbol()==Symbol() && OrderMagicNumber()==Magic)
{
if(OrderType()==0)
{
if(TakeProfit>0)
tp=NormalizeDouble(allb+TakeProfit*_Point,_Digits);
if(StopLoss>0)
sl=NormalizeDouble(allb-StopLoss*_Point,_Digits);
if(OrderTakeProfit()!=tp || OrderStopLoss()!=sl)
mod=OrderModify(OrderTicket(),OrderOpenPrice(),sl,tp,0,Yellow);
}
else
if(OrderType()==1)
{
if(TakeProfit>0)
tp=NormalizeDouble(alls-TakeProfit*_Point,_Digits);
if(StopLoss>0)
sl=NormalizeDouble(alls+StopLoss*_Point,_Digits);
if(OrderTakeProfit()!=tp || OrderStopLoss()!=sl)
mod=OrderModify(OrderTicket(),OrderOpenPrice(),sl,tp,0,Yellow);
}
}
}
}
}
//+------------------------------------------------------------------+
//| Expert tick function |
//+------------------------------------------------------------------+
void OnTick()
{
int spread = (int)SymbolInfoInteger(NULL,SYMBOL_SPREAD);
bool buy = iClose(NULL,PERIOD_H4,0)>iOpen(NULL,PERIOD_H4,0) && iClose(NULL,PERIOD_W1,0)>iOpen(NULL,PERIOD_W1,0) && iClose(NULL,PERIOD_MN1,0)>iOpen(NULL,PERIOD_MN1,0);
bool sell = iClose(NULL,PERIOD_H4,0)<iOpen(NULL,PERIOD_H4,0) && iClose(NULL,PERIOD_W1,0)<iOpen(NULL,PERIOD_W1,0) && iClose(NULL,PERIOD_MN1,0)<iOpen(NULL,PERIOD_MN1,0);
if(spread<=Spread && t!=Time[0])
{
if(CountTrades()<1 && TimeSession(StartHour,StartMin,EndHour,EndMin,TimeCurrent()))
{
if(buy)
{
PutOrder(0,Ask);
}
if(sell)
{
PutOrder(1,Bid);
}
}
if(CountTrades()<Count)
{
if(FindOrderType()==0 && FindLastBuyPrice()-Ask>Step*_Point)
{
PutOrder(0,Ask);
ModifyOrders();
}
if(FindOrderType()==1 && Bid-FindLastSellPrice()>Step*_Point)
{
PutOrder(1,Bid);
ModifyOrders();
}
}
if(CloseSig>0)
{
if(sell)
{
CloseAll(0);
}
if(buy)
{
CloseAll(1);
}
}
t=Time[0];
}
Comment("\n Spread: ",spread);
}
//+------------------------------------------------------------------+
Комментарии (1)
14 verta81 Сообщений: 390
Зарегистрируйтесь или авторизуйтесь, чтобы оставить комментарий