
Собственно надо заставить его закрывать правильно ордера, что меня лично смущает — отсутствие параметра «проскальзование».
так он просто не закрывает ордера.
//+------------------------------------------------------------------+
//| virtual_trailing_stop.mq4 |
//| Copyright 2015, cmillion |
//| http://cmillion.ru |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2015, cmillion@narod.ru"
#property link "http://cmillion.ru"
#property strict
//---
input int Stoploss = 0; // Стоп-лосс
input int Takeprofit = 0; // Тейк-профит
input int TrailingStop = 5; // Длина трала
input int TrailingStart = 5; // Минимальная прибыль для старта
input int TrailingStep = 1; // Шаг трала
input int Slippage = 100; // Проскальзывание
input int Magic = 31115; // Магик
double TrallB = 0;
double TrallS = 0;
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
int start()
{
bool c;
double OOP,SL;
int b=0,s=0,tip,TicketB=0,TicketS=0;
for(int i=OrdersTotal()-1;i>=0;i--)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
{
if(OrderSymbol()==Symbol() && OrderMagicNumber()==Magic)
{
tip = OrderType();
OOP = NormalizeDouble(OrderOpenPrice(),Digits);
if(tip==OP_BUY)
{
b++;
TicketB=OrderTicket();
if(Stoploss!=0 && Bid<=OOP - Stoploss * Point) c=OrderClose(OrderTicket(),OrderLots(),NormalizeDouble(Bid,Digits),Slippage,clrNONE);
if(Takeprofit!=0 && Bid>=OOP + Takeprofit * Point) c=OrderClose(OrderTicket(),OrderLots(),NormalizeDouble(Bid,Digits),Slippage,clrNONE);
if(TrailingStop>0)
{
SL=NormalizeDouble(Bid-TrailingStop*Point,Digits);
if(SL>=OOP+TrailingStart*Point && (TrallB==0 || TrallB+TrailingStep*Point<SL)) TrallB=SL;
}
}
if(tip==OP_SELL)
{
s++;
if(Stoploss!=0 && Ask>=OOP + Stoploss * Point) {if(OrderClose(OrderTicket(),OrderLots(),NormalizeDouble(Ask,Digits),Slippage,clrNONE)) continue;}
if(Takeprofit!=0 && Ask<=OOP - Takeprofit * Point) {if(OrderClose(OrderTicket(),OrderLots(),NormalizeDouble(Ask,Digits),Slippage,clrNONE)) continue;}
TicketS=OrderTicket();
if(TrailingStop>0)
{
SL=NormalizeDouble(Ask+TrailingStop*Point,Digits);
if(SL<=OOP-TrailingStart*Point && (TrallS==0 || TrallS-TrailingStep*Point>SL)) TrallS=SL;
}
}
}
}
}
if(b!=0)
{
if(b>1) Comment("Трал корректно работает только с 1 ордером");
else
if(TrallB!=0)
{
Comment("Тралим ордер ",TicketB);
DrawHline("SL Buy",TrallB,clrBlue,1);
if(Bid<=TrallB)
{
if(OrderSelect(TicketB,SELECT_BY_TICKET))
if(OrderProfit()>0)
if(!OrderClose(TicketB,OrderLots(),NormalizeDouble(Ask,Digits),Slippage,clrRed))
Comment("Ошибка закрытия ордера ",GetLastError());
}
}
}
else {TrallB=0;ObjectDelete("SL Buy");}
//---
if(s!=0)
{
if(s>1) Comment("Трал корректно работает только с 1 ордером");
else
if(TrallS!=0)
{
Comment("Тралим ордер ",TicketS);
DrawHline("SL Sell",TrallS,clrRed,1);
if(Ask>=TrallS)
{
if(OrderSelect(TicketS,SELECT_BY_TICKET))
if(OrderProfit()>0)
if(!OrderClose(TicketS,OrderLots(),NormalizeDouble(Ask,Digits),Slippage,clrRed))
Comment("Ошибка закрытия ордера ",GetLastError());
}
}
}
else {TrallS=0;ObjectDelete("SL Sell");}
//---
int err;
if(IsTesting() && OrdersTotal()==0)
{
double Lot=0.1;
err=OrderSend(Symbol(),OP_BUY,Lot,NormalizeDouble(Ask,Digits),Slippage,0,0,"тест",0);
//err=OrderSend(Symbol(),OP_SELL,Lot,NormalizeDouble(Bid,Digits),Slippage,0,0,"тест",0);
return(0);
}
return(0);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
void DrawHline(string name,double P,color clr,int WIDTH)
{
if(ObjectFind(name)!=-1) ObjectDelete(name);
ObjectCreate(name,OBJ_HLINE,0,0,P,0,0,0,0);
ObjectSet(name,OBJPROP_COLOR,clr);
ObjectSet(name,OBJPROP_STYLE,2);
ObjectSet(name,OBJPROP_WIDTH,WIDTH);
}
//+------------------------------------------------------------------+
спасибо, а как сделать чтобы он работал на автономном графике? Именно сдвиг линии по времени там не работает
input string TimeShift = "18:05"; //сдвиг линий по времени
//+------------------------------------------------------------------+
//| Bars.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
#property indicator_chart_window
input int bar = 4; //расстояние между линиями
input int bars = 150; //баров для расчета
input int width = 1; //толщина линии
input string TimeShift = "18:05"; //сдвиг линий по времени
input string Name = "VLine";
input color clr = clrAliceBlue;
datetime tm=0;
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
//--- indicator buffers mapping
ObjectsDeleteAll(0,OBJ_VLINE);
//---
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
ObjectsDeleteAll(0,OBJ_VLINE);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
void PutVLine(string name,datetime t)
{
ObjectCreate(name,OBJ_VLINE,0,t,0);
ObjectSet(name,OBJPROP_COLOR,clr);
ObjectSet(name,OBJPROP_STYLE,STYLE_SOLID);
ObjectSet(name,OBJPROP_WIDTH,width);
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
const int prev_calculated,
const datetime &time[],
const double &open[],
const double &high[],
const double &low[],
const double &close[],
const long &tick_volume[],
const long &volume[],
const int &spread[])
{
//---
int shift=iBarShift(NULL,0,StringToTime(TimeShift));
if(tm!=time[0])
{
ObjectsDeleteAll(0,OBJ_VLINE);
for(int i=shift;i<=bars;i++)
{
PutVLine(Name+TimeToStr(time[i]),time[i]);
i=i+bar;
}
tm=time[0];
}
Comment("\n Shift:",shift);
//--- return value of prev_calculated for next call
return(rates_total);
}
//+------------------------------------------------------------------+
Вопрос: ставлю отметку например 25.01.2016 09:08, при смене ТФ М5 отметка сдвигается на 25.01.2016 8:35, при смене ТФ H1 отметка стоит на 25.01.2016 01:00. Расчёт каждый раз идет согласно тф? Просто хотелось бы чтобы при смене ТФ она не менялась и оставалась на 25.01.2016 09:08 как на м1 к примеру
но я тут подшаманил этот эксперт
не могли бы исправить или написать новый
Тейк так и не выставил.
Вы со мной согласны?!.. Если да, то тогда вопрос исчерпан.
lot=(int)AccountBalance()/1000;
lot=lot/10;
//+------------------------------------------------------------------+
//| MidTake.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
input int StopLoss = 3000; // стоп лосс ордера
input int TakeProfit = 10; // тейк профит ордера
input int Param = 0; //
input int Count = 10; // количество ордеров которое считает советник
//+------------------------------------------------------------------+
//| Expert initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
//---
//---
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Expert deinitialization function |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
//---
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
void Mode(int from,int to)
{
bool m;
double all=0;
double count=0,tp=0,sl=0;
for(int i=to-1;i>=from;i--)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
{
if(OrderSymbol()==Symbol())
{
if(OrderType()==OP_BUY || OrderType()==OP_SELL)
{
all+=OrderOpenPrice();
count++;
}
}
}
}
if(count>0) all=NormalizeDouble(all/count,Digits);
for(int i=to-1;i>=from;i--)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
{
if(OrderSymbol()==Symbol())
{
if(OrderType()==OP_BUY)
{
tp=NormalizeDouble(all+TakeProfit*Point,Digits);
sl=NormalizeDouble(all-StopLoss*Point,Digits);
if(OrderStopLoss()!=sl || OrderTakeProfit()!=tp)
m=OrderModify(OrderTicket(),OrderOpenPrice(),sl,tp,0,Yellow);
}
else
if(OrderType()==OP_SELL)
{
tp=NormalizeDouble(all-TakeProfit*Point,Digits);
sl=NormalizeDouble(all+StopLoss*Point,Digits);
if(OrderStopLoss()!=sl || OrderTakeProfit()!=tp)
m=OrderModify(OrderTicket(),OrderOpenPrice(),sl,tp,0,Yellow);
}
}
}
}
return;
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
int CountTrades()
{
int count=0;
for(int i=OrdersTotal()-1;i>=0;i--)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
{
if(OrderSymbol()==Symbol())
{
if(OrderType()==OP_BUY || OrderType()==OP_SELL) count++;
}
}
}
return(count);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
void PutOrder(int type,double pr)
{
int r=0;
color clr=clrNONE;
if(type==1 || type==3 || type==5)
{
clr=Red;
}
if(type==0 || type==2 || type==4)
{
clr=Blue;
}
r=OrderSend(NULL,type,0.1,NormalizeDouble(pr,Digits),0,0,0,"",0,0,clr);
return;
}
//+------------------------------------------------------------------+
//| Expert tick function |
//+------------------------------------------------------------------+
void OnTick()
{
if(CountTrades()<=Count)
{
if(Param==0)
{
Mode(0,CountTrades());
}
if(Param==1)
{
if(CountTrades()==1) Mode(0,1);
if(CountTrades()>1 && MathMod(CountTrades(),2)==0) Mode(0,CountTrades()/2);
}
if(Param==2)
{
if(CountTrades()>2 && MathMod(CountTrades(),3)==0) Mode(0,CountTrades()/3);
}
}
if(IsTesting())
{
if(OrdersTotal()<1)
{
for(int i=1;i<=10;i++)
{
PutOrder(2,Ask-200*Point*i);
}
}
}
Comment("\n Остаток: ",MathMod(CountTrades(),3),
"\n Позиций: ",CountTrades());
return;
}
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
//| Lot.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 double MaxLot = 10; // максимальный лот
extern double Risk = 5; // риск
extern int StopLoss = 500; // лось
extern int TakeProfit = 500; // язь
extern int Slip = 30; // реквот
extern int Magic = 123; // магик
int Lock=0;
double PrevBalance=0;
//+------------------------------------------------------------------+
//| Expert initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
//---
//---
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Expert deinitialization function |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
//---
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
void PutOrder(int type,double price)
{
int r=0;
color clr=clrNONE;
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;
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
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);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
double Lot()
{
double LotSize=MarketInfo(Symbol(),MODE_LOTSIZE);
double MinLot=MarketInfo(Symbol(),MODE_MINLOT);
double lot=Lots;
lot=(int)AccountBalance()/1000;
lot=lot/10;
//баланс меньше 1000
if(AccountBalance()<1000)
{
lot=NormalizeDouble(AccountBalance()*Risk/LotSize,2);
if(lot<=MinLot) lot=MinLot;
if(lot>=MaxLot) lot=MaxLot;
}
// баланс уменьшился с 3100 до 2900
if(AccountBalance()< PrevBalance && (int)AccountBalance()/1000<(int)PrevBalance/1000)lot=lot-0.1;
return(lot);
}
//+------------------------------------------------------------------+
//| Expert tick function |
//+------------------------------------------------------------------+
void OnTick()
{
double rsi=iRSI(NULL,0,14,PRICE_CLOSE,1);
if(CountTrades()<1 && rsi<30)
{
PrevBalance=AccountBalance();
PutOrder(0,Ask);
//PutOrder(1,Bid);
}
Comment("\n Lot: ",Lot(),
"\n Balance: ",NormalizeDouble(AccountBalance(),2),
"\n PrevBalance: ",PrevBalance,
"\n LotSize: ",MarketInfo(Symbol(),MODE_LOTSIZE),
"\n Balance/1000: ",NormalizeDouble(AccountBalance()/1000,0),
"\n int Balance/1000: ",(int)AccountBalance()/1000, //
"\n int PrevBalance/1000: ",(int)PrevBalance/1000,
"\n MathMod(AccountBalance(),1000: ",MathMod(AccountBalance(),1000));
}
//+------------------------------------------------------------------+
lot=(int)AccountBalance()/1000;
lot=lot/10;
double Lot()
{
double LotSize=MarketInfo(Symbol(),MODE_LOTSIZE);
double MinLot=MarketInfo(Symbol(),MODE_MINLOT);
double lot=Lots;
lot=(int)AccountBalance()/1000;
lot=lot/10;
//баланс меньше 1000
if(AccountBalance()<1000)
{
lot=NormalizeDouble(AccountBalance()*Risk/LotSize,2);
if(lot<=MinLot) lot=MinLot;
if(lot>=MaxLot) lot=MaxLot;
}
// баланс уменьшился с 3100 до 2900
if(AccountBalance()< PrevBalance && (int)AccountBalance()/1000<(int)PrevBalance/1000)lot=lot-0.1;
return(lot);
}
//+------------------------------------------------------------------+
//| MidTake.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
input int StopLoss = 3000; // стоп лосс ордера
input int TakeProfit = 10; // тейк профит ордера
input int Param = 0; //
input int Count = 10; // количество ордеров которое считает советник
//+------------------------------------------------------------------+
//| Expert initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
//---
//---
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Expert deinitialization function |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
//---
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
void Mode(int from,int to)
{
double all=0;
double count=0,tp=0,sl=0;
for(int i=to-1;i>=from;i--)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
{
if(OrderSymbol()==Symbol())
{
if(OrderType()==OP_BUY || OrderType()==OP_SELL)
{
all+=OrderOpenPrice();
count++;
}
}
}
}
if(count>0) all=NormalizeDouble(all/count,Digits);
for(int i=to-1;i>=from;i--)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
{
if(OrderSymbol()==Symbol())
{
if(OrderType()==OP_BUY)
{
tp=NormalizeDouble(all+TakeProfit*Point,Digits);
sl=NormalizeDouble(all-StopLoss*Point,Digits);
bool mod=OrderModify(OrderTicket(),OrderOpenPrice(),sl,tp,0,Yellow);
}
else
if(OrderType()==OP_SELL)
{
tp=NormalizeDouble(all-TakeProfit*Point,Digits);
sl=NormalizeDouble(all+StopLoss*Point,Digits);
bool mod=OrderModify(OrderTicket(),OrderOpenPrice(),sl,tp,0,Yellow);
}
}
}
}
return;
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
int CountTrades()
{
int count=0;
for(int i=OrdersTotal()-1;i>=0;i--)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
{
if(OrderSymbol()==Symbol())
{
if(OrderType()==OP_BUY || OrderType()==OP_SELL) count++;
}
}
}
return(count);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
void PutOrder(int type,double pr)
{
int r=0;
color clr=clrNONE;
if(type==1 || type==3 || type==5)
{
clr=Red;
}
if(type==0 || type==2 || type==4)
{
clr=Blue;
}
r=OrderSend(NULL,type,0.1,NormalizeDouble(pr,Digits),0,0,0,"",0,0,clr);
return;
}
//+------------------------------------------------------------------+
//| Expert tick function |
//+------------------------------------------------------------------+
void OnTick()
{
if(CountTrades()<=Count)
{
if(Param==0) Mode(0,CountTrades());
if(Param==1)
{
if(CountTrades()==1) Mode(0,1);
if(CountTrades()>1 && MathMod(CountTrades(),2)==0) Mode(0,CountTrades()/2);
}
if(Param==2)
{
if(CountTrades()>2 && MathMod(CountTrades(),3)==0) Mode(0,CountTrades()/3);
}
}
if(IsTesting())
{
if(OrdersTotal()<1)
{
for(int i=1;i<=10;i++)
{
PutOrder(2,Ask-200*Point*i);
}
}
}
Comment("\n Остаток: ",MathMod(CountTrades(),3),
"\n Позиций: ",CountTrades());
}
//+------------------------------------------------------------------+
AM2