#property copyright "Разработан 2022, AM2 и SSG"
#property link "ATS-TALLROCK"
#property version "1.04"
#property strict
//--- Inputs
extern double Lots = 0.1; // Фикс. размер позиции
extern double Risk = 10; // Размер в процентах от баланса
extern bool Trailing = true; // Tralling STOP
extern bool ProfitTrailing = false; // Авто STOPLOSS(рекомендуемое FALSE)
extern double H = 0.7; // Расчет автотрейлинга(с 0 до 1)
extern bool _CloseTime = true; // авторасчет времени закрытия позиции
extern int Magic = 123; //ID orders |показатели ниже не трогать|
extern int StopLoss = 0; // Stop Loss(ОБЯЗАТЕЛЬНО УСТАНОВИТЬ В СЛУЧАЕ АвтоSTOPLOSS TRUE!)
extern int TakeProfit = 0; // Take Profit
extern int TrailingStep = 1; // Шаг трала
extern int Slip = 30; // размер реквот
// Движок системы, не влезать!
extern int CountW1 = 50; // свечей для расчета W1
extern int CountH1 = 120; // свечей для расчета Н1
//+------------------------------------------------------------------+
//| 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(),NormalizeDouble(price,_Digits),Slip,sl,tp,"",Magic,0,clr);
return;
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
double Lot(){
double lot=Lots;
if(Lots==0) lot=AccountBalance()*Risk/100000;// 10000*Risk/100000=1
return(lot);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
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()<2) count++;
}
}
}
return(count);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
double Y(){
double y=0;
for(int i=0; i<CountW1; i++)
{
y+=(iHigh(NULL,PERIOD_W1,i)-iLow(NULL,PERIOD_W1,i))/_Point;
}
return(y/(CountW1*2));
}
//+------------------------------------------------------------------+
//| Сопровождение позиции простым тралом |
//+------------------------------------------------------------------+
void TrailingPositions() {
bool rez;
int TrailingStop = (int)NormalizeDouble((Y()*H),0);// Фиксированный размер трейлинг стопа
for (int i=0; i<OrdersTotal(); i++) {
if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) {
if (OrderMagicNumber()==Magic) {
if (OrderSymbol()==Symbol()) {
if (OrderType()==OP_BUY) {
if (!ProfitTrailing || (Bid-OrderOpenPrice())>TrailingStop*Point) {
if (OrderStopLoss()<Bid-(TrailingStop+TrailingStep-1)*Point) {
rez=OrderModify(OrderTicket(),OrderOpenPrice(),Bid-TrailingStop*_Point,OrderTakeProfit(),0,clrBlue);
}
}
}
if (OrderType()==OP_SELL) {
if (!ProfitTrailing || OrderOpenPrice()-Ask>TrailingStop*Point) {
if (OrderStopLoss()>Ask+(TrailingStop+TrailingStep-1)*Point || OrderStopLoss()==0) {
rez=OrderModify(OrderTicket(),OrderOpenPrice(),Ask+TrailingStop*_Point,OrderTakeProfit(),0,clrRed);
}
}
}
}
}
}
}
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
int CloseTime(){
int count=0;
double K=0, L=0, N=0;
double lo=Low[iLowest(NULL,0,MODE_LOW,CountH1,1)];
double hi=High[iHighest(NULL,0,MODE_HIGH,CountH1,1)];
double x=(hi-lo)/_Point;
K=NormalizeDouble(Y()*2,0);
L=x/120;
N=K/L;
count=(int)NormalizeDouble(N,0); // время закрытия в часах
return(count);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
void CloseTimeAll(int ot=-1){
bool cl=1;
int closeTime=CloseTime();
for(int i=OrdersTotal()-1; i>=0; i--){
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)){
if(OrderSymbol()==Symbol() && OrderMagicNumber()==Magic){
if(TimeCurrent()-OrderOpenTime()>closeTime*3600){
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);
}
}
}
}
}
}
//+------------------------------------------------------------------+
//| Expert tick function |
//+------------------------------------------------------------------+
void OnTick()
{
int closeTime=CloseTime();
double lo=Low[iLowest(NULL,0,MODE_LOW,CountH1,0)];
double hi=High[iHighest(NULL,0,MODE_HIGH,CountH1,0)];
double x=(hi-lo)/_Point;
if(_CloseTime)CloseTimeAll();
if(Trailing)TrailingPositions();
if(CountTrades()<1 && x>Y())
{
if(Bid<hi && Bid>(hi+lo)/2)
PutOrder(0,Ask);
if(Bid>lo && Bid<(hi+lo)/2)
PutOrder(1,Bid);
}
Comment( "\n ПОКАЗАТЕЛИ РАССЧИТАННЫЕ СИСТЕМОЙ ATS",
"\n Недельная норма движения : -- ",NormalizeDouble(Y(),0),
"\n Текущее движение на H1 : -- ",NormalizeDouble(x,0),
"\n Рассчитанный Трейлинг-стоп\Stop loss: -- ",NormalizeDouble(Y()*H,0),
"\n Рассчетное время закрытия позиции : -- ",NormalizeDouble(CloseTime(),0));
}
//+------------------------------------------------------------------+
Lerdon