А как задавать на скрипте ту часть, которую следует закрыть?
В настройках Lot1 лот ближайшей к цене метки и т.д.
extern double Lot1 = 0.01;
extern double Lot2 = 0.02;
extern double Lot3 = 0.03;
А как задавать на скрипте ту часть, которую следует закрыть?
extern double Lot1 = 0.01;
extern double Lot2 = 0.02;
extern double Lot3 = 0.03;
//+------------------------------------------------------------------+
//| CloseExp.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
extern int TakeProfit1 = 100;
extern int TakeProfit2 = 200;
extern int TakeProfit3 = 300;
extern int Slip=30;
extern double Lot1 = 0.01;
extern double Lot2 = 0.02;
extern double Lot3 = 0.03;
double tp1=0,tp2=0,tp3=0;
bool CloseTP1=true,CloseTP2=true,CloseTP3=true;
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
int OnInit()
{
//---
Comment("");
//---
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Expert deinitialization function |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
Comment("");
ObjectsDeleteAll();
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
void PutLable(string name,datetime time,double price)
{
ObjectCreate(0,name,OBJ_ARROW_LEFT_PRICE,0,time,price);
//--- установим цвет метки
ObjectSetInteger(0,name,OBJPROP_COLOR,Red);
//--- установим стиль окаймляющей линии
ObjectSetInteger(0,name,OBJPROP_STYLE,STYLE_SOLID);
//--- установим размер метки
ObjectSetInteger(0,name,OBJPROP_WIDTH,1);
//--- включим (true) или отключим (false) режим перемещения метки мышью
//--- при создании графического объекта функцией ObjectCreate, по умолчанию объект
//--- нельзя выделить и перемещать. Внутри же этого метода параметр selection
//--- по умолчанию равен true, что позволяет выделять и перемещать этот объект
ObjectSetInteger(0,name,OBJPROP_SELECTABLE,true);
ObjectSetInteger(0,name,OBJPROP_SELECTED,true);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
int OrdersType()
{
int t=0;
for(int i=OrdersTotal()-1;i>=0;i--)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
{
if(OrderSymbol()==Symbol())
{
if(OrderType()==OP_BUY) t=1;
if(OrderType()==OP_SELL) t=2;
}
}
}
return(t);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
void CloseOrders()
{
bool cl;
for(int i=0; i<OrdersTotal(); i++)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
{
if(OrderSymbol()==Symbol())
{
if(OrderType()==OP_BUY)
{
if(Bid>tp1)
{
if(CloseTP1) cl=OrderClose(OrderTicket(),Lot1,Bid,Slip,Blue);
if(cl)CloseTP1=false;
}
if(Bid>tp2)
{
if(CloseTP2) cl=OrderClose(OrderTicket(),Lot2,Bid,Slip,Blue);
if(cl)CloseTP2=false;
}
if(Bid>tp3)
{
if(CloseTP3) cl=OrderClose(OrderTicket(),Lot3,Bid,Slip,Blue);
if(cl)CloseTP3=false;
}
}
if(OrderType()==OP_SELL)
{
if(Ask<tp1)
{
if(CloseTP1) cl=OrderClose(OrderTicket(),Lot1,Ask,Slip,Red);
if(cl)CloseTP1=false;
}
if(Ask<tp2)
{
if(CloseTP2) cl=OrderClose(OrderTicket(),Lot2,Ask,Slip,Red);
if(cl)CloseTP2=false;
}
if(Ask<tp3)
{
if(CloseTP3) cl=OrderClose(OrderTicket(),Lot3,Ask,Slip,Red);
if(cl)CloseTP3=false;
}
}
}
}
}
}
//+------------------------------------------------------------------+
//| Expert tick function |
//+------------------------------------------------------------------+
void OnTick()
{
if(IsTesting())
{
if(OrdersTotal()<1) int r=OrderSend(NULL,1,1,Bid,5,0,0,"",0,0,Green);
//int r=OrderSend(NULL,0,1,Ask,5,0,0,"",0,0,Green);
}
if(OrdersType()==1)
{
PutLable("TP1",Time[0],Bid+TakeProfit1*Point);
PutLable("TP2",Time[0],Bid+TakeProfit2*Point);
PutLable("TP3",Time[0],Bid+TakeProfit3*Point);
}
if(OrdersType()==2)
{
PutLable("TP1",Time[0],Ask-TakeProfit1*Point);
PutLable("TP2",Time[0],Ask-TakeProfit2*Point);
PutLable("TP3",Time[0],Ask-TakeProfit3*Point);
}
for(int i=ObjectsTotal()-1;i>=0;i--)
{
if(ObjectName(i)=="TP1") tp1=ObjectGetDouble(0,"TP1",OBJPROP_PRICE);
if(ObjectName(i)=="TP2") tp2=ObjectGetDouble(0,"TP2",OBJPROP_PRICE);
if(ObjectName(i)=="TP3") tp3=ObjectGetDouble(0,"TP3",OBJPROP_PRICE);
}
CloseOrders();
Comment("\n TP1: ",DoubleToString(tp1,Digits),
"\n TP2: ",DoubleToString(tp2,Digits),
"\n TP3: ",DoubleToString(tp3,Digits),
"\n CloseTP1: ",CloseTP1,
"\n CloseTP2: ",CloseTP2,
"\n CloseTP3: ",CloseTP3,
"\n Orders Type: ",OrdersType());
RefreshRates();
}
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
//| CloseScript.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
#property show_inputs
extern int TakeProfit1 = 100;
extern int TakeProfit2 = 200;
extern int TakeProfit3 = 300;
extern int Slip=30;
extern double Lot1 = 0.01;
extern double Lot2 = 0.02;
extern double Lot3 = 0.03;
double tp1=0,tp2=0,tp3=0;
bool CloseTP1=true,CloseTP2=true,CloseTP3=true;
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
int OnInit()
{
//---
Comment("");
//---
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Expert deinitialization function |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
Comment("");
ObjectsDeleteAll();
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
void PutLable(string name,datetime time,double price)
{
ObjectCreate(0,name,OBJ_ARROW_LEFT_PRICE,0,time,price);
//--- установим цвет метки
ObjectSetInteger(0,name,OBJPROP_COLOR,Red);
//--- установим стиль окаймляющей линии
ObjectSetInteger(0,name,OBJPROP_STYLE,STYLE_SOLID);
//--- установим размер метки
ObjectSetInteger(0,name,OBJPROP_WIDTH,1);
//--- включим (true) или отключим (false) режим перемещения метки мышью
//--- при создании графического объекта функцией ObjectCreate, по умолчанию объект
//--- нельзя выделить и перемещать. Внутри же этого метода параметр selection
//--- по умолчанию равен true, что позволяет выделять и перемещать этот объект
ObjectSetInteger(0,name,OBJPROP_SELECTABLE,true);
ObjectSetInteger(0,name,OBJPROP_SELECTED,true);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
int OrdersType()
{
int t=0;
for(int i=OrdersTotal()-1;i>=0;i--)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
{
if(OrderSymbol()==Symbol())
{
if(OrderType()==OP_BUY) t=1;
if(OrderType()==OP_SELL) t=2;
}
}
}
return(t);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
void CloseOrders()
{
bool cl;
for(int i=0; i<OrdersTotal(); i++)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
{
if(OrderSymbol()==Symbol())
{
if(OrderType()==OP_BUY)
{
if(Bid>tp1)
{
if(CloseTP1) cl=OrderClose(OrderTicket(),Lot1,Bid,Slip,Blue);
if(cl)CloseTP1=false;
}
if(Bid>tp2)
{
if(CloseTP2) cl=OrderClose(OrderTicket(),Lot2,Bid,Slip,Blue);
if(cl)CloseTP2=false;
}
if(Bid>tp3)
{
if(CloseTP3) cl=OrderClose(OrderTicket(),Lot3,Bid,Slip,Blue);
if(cl)CloseTP3=false;
}
}
if(OrderType()==OP_SELL)
{
if(Ask<tp1)
{
if(CloseTP1) cl=OrderClose(OrderTicket(),Lot1,Ask,Slip,Red);
if(cl)CloseTP1=false;
}
if(Ask<tp2)
{
if(CloseTP2) cl=OrderClose(OrderTicket(),Lot2,Ask,Slip,Red);
if(cl)CloseTP2=false;
}
if(Ask<tp3)
{
if(CloseTP3) cl=OrderClose(OrderTicket(),Lot3,Ask,Slip,Red);
if(cl)CloseTP3=false;
}
}
}
}
}
}
//+------------------------------------------------------------------+
//| Script program start function |
//+------------------------------------------------------------------+
void OnStart()
{
while(!IsStopped())
{
if(OrdersType()==1)
{
PutLable("TP1",Time[0],Bid+TakeProfit1*Point);
PutLable("TP2",Time[0],Bid+TakeProfit2*Point);
PutLable("TP3",Time[0],Bid+TakeProfit3*Point);
}
if(OrdersType()==2)
{
PutLable("TP1",Time[0],Ask-TakeProfit1*Point);
PutLable("TP2",Time[0],Ask-TakeProfit2*Point);
PutLable("TP3",Time[0],Ask-TakeProfit3*Point);
}
for(int i=ObjectsTotal()-1;i>=0;i--)
{
if(ObjectName(i)=="TP1") tp1=ObjectGetDouble(0,"TP1",OBJPROP_PRICE);
if(ObjectName(i)=="TP2") tp2=ObjectGetDouble(0,"TP2",OBJPROP_PRICE);
if(ObjectName(i)=="TP3") tp3=ObjectGetDouble(0,"TP3",OBJPROP_PRICE);
}
CloseOrders();
Comment("\n TP1: ",DoubleToString(tp1,Digits),
"\n TP2: ",DoubleToString(tp2,Digits),
"\n TP3: ",DoubleToString(tp3,Digits),
"\n CloseTP1: ",CloseTP1,
"\n CloseTP2: ",CloseTP2,
"\n CloseTP3: ",CloseTP3,
"\n Orders Type: ",OrdersType());
RefreshRates();
}
}
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
//| Limit.mq4 |
//| Copyright 2015, AM2 |
//| http://www.forexsystems.biz |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2015, AM2"
#property link "http://www.forexsystems.biz"
#property description "Simple expert advisor"
//--- Inputs
extern double Lots = 0.1; // лот
extern int StopLoss = 500; // лось
extern int TakeProfit = 500; // язь
extern int Delta = 100; // отступ
extern int Slip = 30; // реквот
extern int StartHour = 9; // час начала торговли
extern int StartMin = 30; // минута начала торговли
extern int EndHour = 23; // час окончания торговли
extern int EndMin = 30; // минута окончания торговли
extern int Magic = 123; // магик
//+------------------------------------------------------------------+
//| Expert initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
//---
Comment("");
//---
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Expert deinitialization function |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
Comment("");
}
//+----------------------------------------------------------------------------+
//| Автор : Ким Игорь В. aka KimIV, http://www.kimiv.ru |
//+----------------------------------------------------------------------------+
//| Версия : 30.04.2009 |
//| Описание : Возвращает флаг разрешения торговли по времени. |
//+----------------------------------------------------------------------------+
//| Параметры: |
//| hb - часы времени начала торговли |
//| mb - минуты времени начала торговли |
//| he - часы времени окончания торговли |
//| me - минуты времени окончания торговли |
//+----------------------------------------------------------------------------+
bool isTradeTimeInt(int hb=0,int mb=0,int he=0,int me=0)
{
datetime db, de; // Время начала и окончания работы
int hc; // Часы текущего времени торгового сервера
db=StrToTime(TimeToStr(TimeCurrent(), TIME_DATE)+" "+hb+":"+mb);
de=StrToTime(TimeToStr(TimeCurrent(), TIME_DATE)+" "+he+":"+me);
hc=TimeHour(TimeCurrent());
if(db>=de)
{
if(hc>=he) de+=24*60*60; else db-=24*60*60;
}
if(TimeCurrent()>=db && TimeCurrent()<=de) return(True);
else return(False);
}
//+------------------------------------------------------------------+
//| Check for open order conditions |
//+------------------------------------------------------------------+
void PutOrder(int type,double price)
{
int r=0;
double sl=0,tp=0;
if(type==1 || type==3 || type==5)
{
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)
{
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,Green);
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);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
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()==2 || OrderType()==3) del=OrderDelete(OrderTicket());
}
}
}
return;
}
//+------------------------------------------------------------------+
//| OnTick function |
//+------------------------------------------------------------------+
void OnTick()
{
/*
Тип операции текущего выбранного ордера. Mожет быть одной из следующих величин:
0 - OP_BUY - ордер на покупку,
1 - OP_SELL - ордер на продажу,
2 - OP_BUYLIMIT - отложенный ордер на покупку по достижении заданного уровня, текущая цена выше уровня,
3 - OP_SELLLIMIT - отложенный ордер на продажу по достижении заданного уровня, текущая цена ниже уровня,
4 - OP_BUYSTOP - отложенный ордер на покупку по достижении заданного уровня, текущая цена ниже уровня,
5 - OP_SELLSTOP - отложенный ордер на продажу по достижении заданного уровня, текущая цена выше уровня.
*/
if(isTradeTimeInt(StartHour,StartMin,EndHour,EndMin))
{
if(CountTrades()<1)
{
if(CountOrders(2)<1)PutOrder(2,Bid-Delta*Point);
if(CountOrders(3)<1)PutOrder(3,Bid+Delta*Point);
}
}
if(CountTrades()>0) DelOrder();
}
//+------------------------------------------------------------------+
При чем здесь задержка Sleep?
//+------------------------------------------------------------------+
//| PutOrders.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
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
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);
}
//+------------------------------------------------------------------+
//| Script program start function |
//+------------------------------------------------------------------+
void OnStart()
{
while(!IsStopped())
{
if(CountTrades()<10)int r=OrderSend(NULL,1,0.01,NormalizeDouble(Bid,Digits),55,0,NormalizeDouble(Bid-20*Point,Digits),"",0,0,Red);
//int r=OrderSend(NULL,0,0.01,NormalizeDouble(Ask,Digits),55,0,Ask-1*Point,"",0,0,Blue);
RefreshRates();
Sleep(100);
Comment("\n Count Trades: ",CountTrades());
}
}
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
//| PutOrders.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
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
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);
}
//+------------------------------------------------------------------+
//| Script program start function |
//+------------------------------------------------------------------+
void OnStart()
{
while(!IsStopped())
{
if(CountTrades()<10)int r=OrderSend(NULL,1,0.01,NormalizeDouble(Bid,Digits),55,0,NormalizeDouble(Bid-20*Point,Digits),"",0,0,Red);
//int r=OrderSend(NULL,0,0.01,NormalizeDouble(Ask,Digits),55,0,Ask-1*Point,"",0,0,Blue);
Sleep(100);
Comment("\n Count Trades: ",CountTrades());
}
}
//+------------------------------------------------------------------+
Отработала функция OnStart, скрипт выгружается. Он не будет ждать, когда цена дойдет до какой-нибудь отметки, чтобы ордер частично закрылся.
//+------------------------------------------------------------------+
//| cm Script TrailingStop.mq4 |
//| Copyright 2015, cmillion@narod.ru |
//| http://cmillion.ru |
//+------------------------------------------------------------------+
#property copyright "Copyright 2015, cmillion@narod.ru"
#property link "http://cmillion.ru"
#property version "1.00"
#property strict
#property show_inputs
//--------------------------------------------------------------------
input int StopLoss=0; //стоплосс
input int TakeProflt=0; //желаемая прибыль
input int TrailingStop = 10; //как прибыль ордера достигает этого значения в пунктах, Stop Loss переносится на на цену открытия ордера и далее тралится по профиту.
input int StepTrall = 10; //Шаг Трала.
//--------------------------------------------------------------------
void OnStart()
{
while(!IsStopped())
{
double OSL,OTP,OOP,StLo,SL,TP;
int tip;
for(int i=0; i<OrdersTotal(); i++)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
{
if(OrderSymbol()==Symbol())
{
tip = OrderType();
OSL = NormalizeDouble(OrderStopLoss(),Digits);
OTP = NormalizeDouble(OrderTakeProfit(),Digits);
OOP = NormalizeDouble(OrderOpenPrice(),Digits);
SL=OSL;TP=OTP;
if(tip==OP_BUY)
{
//ставим стоп если нет
if(OSL==0 && StopLoss!=0)
{
SL=NormalizeDouble(OOP-StopLoss*Point,Digits);
}
//ставим тейк если нет
if(OTP==0 && TakeProflt!=0)
{
TP=NormalizeDouble(OOP+TakeProflt*Point,Digits);
}
//если трал не равен 0
if(TrailingStop!=0)
{
//стоп равен бид минус трал
StLo=NormalizeDouble(Bid-TrailingStop*Point,Digits);
//если стоп StLo больше цены открытия
if(StLo>=OOP && StLo>=OSL+StepTrall*Point) SL=StLo;
}
if(SL!=OSL || TP!=OTP)
{
if(!OrderModify(OrderTicket(),OOP,SL,TP,0,White)) Print("Error OrderModify ",GetLastError());
}
}
if(tip==OP_SELL)
{
if(OSL==0 && StopLoss!=0)
{
SL=NormalizeDouble(OOP+StopLoss*Point,Digits);
}
if(OTP==0 && TakeProflt!=0)
{
TP=NormalizeDouble(OOP-TakeProflt*Point,Digits);
}
if(TrailingStop!=0)
{
StLo=NormalizeDouble(Ask+TrailingStop*Point,Digits);
if(StLo<=OOP && (StLo<=OSL-StepTrall*Point || OSL==0)) SL=StLo;
}
if(SL!=OSL || TP!=OTP)
{
if(!OrderModify(OrderTicket(),OOP,SL,TP,0,White)) Print("Error OrderModify ",GetLastError());
}
}
}
}
}
RefreshRates();
Sleep(500);
}
return;
}
//+------------------------------------------------------------------+
AM2