// this is just example for that what I am thinking datetime time = 0; // I just want to ignore this because I just want to give to X = 20; fixed mode ( should not change ever ) double price = ObjectGetDouble( 0, "line", OBJPROP_PRICE, 0 );
int x,y;
ChartTimePriceToXY( long chart_id, int sub_window, datetime time, double price, int& x, int& y );
//+------------------------------------------------------------------+ //| CrossHair.mq4 | //| Copyright 2017, Marco vd Heijden, MetaQuotes Software Corp. | //| https://www.mql5.com | //+------------------------------------------------------------------+ #property copyright"Copyright 2017, Marco vd Heijden, MetaQuotes Software Corp." #property link"https://www.mql5.com" #property version"1.00" #property strict //+------------------------------------------------------------------+ //| Expert initialization function | //+------------------------------------------------------------------+ intOnInit() { //--- create some lines ObjectCreate(0,"X",OBJ_HLINE,0,0,Ask); // X = Horizontal Axis (time==0) ObjectCreate(0,"Y",OBJ_VLINE,0,TimeCurrent(),0);// Y = Vertical Axis (price==0) //--- return(INIT_SUCCEEDED); } //+------------------------------------------------------------------+ //| Expert deinitialization function | //+------------------------------------------------------------------+ voidOnDeinit(constint reason) { //--- clean up ObjectsDeleteAll(0,0,EMPTY); } //+------------------------------------------------------------------+ //| Expert tick function | //+------------------------------------------------------------------+ voidOnTick() { //---
} //+------------------------------------------------------------------+ //| ChartEvent function | //+------------------------------------------------------------------+ voidOnChartEvent(constint id, constlong &lparam, constdouble &dparam, conststring &sparam) { //--- If this is an event of a mouse click on the chart if(id==CHARTEVENT_CLICK) { //--- Prepare variables int x =(int)lparam; int y =(int)dparam; datetime time =0; double price =0; int window=0; //--- Convert the X and Y coordinates in terms of date/time if(ChartXYToTimePrice(0,x,y,window,time,price)) { ObjectMove(0,"X",0,0,price); // notice time==0 because horizontal axis ObjectMove(0,"Y",0,time,0); // notice price==0 because vertical axis } else Print("ChartXYToTimePrice return error code: ",GetLastError()); Print("+--------------------------------------------------------------+"); } } //+------------------------------------------------------------------+
// actually here is a graphical objects functin() // here one of them // also which one soon I will try to below objects could moves together " tphline " - but I can't take a time to research about your latest comment and so... ObjectCreate( "recl object", OBJ_RECTANGLE, 0, 0, tpprice ); // I feel I could add something to " string name " - I already tried few things not good results
或者你可以在检测到拖动时启动一个计数器,有很多方法可以做到这一点。
(我现在只有一种方法 可以做到--就是你最新评论中的方法)。
马可先生,你拯救了我,非常感谢。
暂时没有,但我以后会试着在这个 "SL 和TP"功能 上再增加几个功能。我只是在开始写脚本之前,真的需要研究很多这方面的东西。
祝你一切顺利!
如果我没记错的话--我在很久以前看到过一个 "订单修改器 "的EA是这样工作的:其中一个是在拖动时不更新的,当我 "拖动关闭 "拖线时,拖动后 "止损 和止盈"的数值可以改变一次。
所以我三番五次地阅读你的最新评论,也试着多改了一点,我可以在拖动时停止更新。
问:那么 ,请问这是不是不可能呢?
谢谢。
好吧,只要布尔标志拖==1,你就可以用这个相同的标志来禁止更新。
就像我说的,你也可以启动一个计数器,这是一个例子。
//| Drag Hline2.mq4 |
//| Copyright 2017, Marco vd Heijden, MetaQuotes Software Corp. |
//| https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2017, Marco vd Heijden, MetaQuotes Software Corp."
#property link "https://www.mql5.com"
#property version "1.00"
#property strict
double price;
bool drag;
int cnt=0;
//+------------------------------------------------------------------+
//| Expert initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
//--- create timer
EventSetTimer(1);
ObjectCreate(0,"line",OBJ_HLINE,0,0,Ask);
price=ObjectGetDouble(0,"line",OBJPROP_PRICE,0);
//---
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Expert deinitialization function |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
//--- destroy timer
EventKillTimer();
}
//+------------------------------------------------------------------+
//| Expert tick function |
//+------------------------------------------------------------------+
void OnTick()
{
//---
}
//+------------------------------------------------------------------+
//| Timer function |
//+------------------------------------------------------------------+
void OnTimer()
{
//---
if(price!=ObjectGetDouble(0,"line",OBJPROP_PRICE,0))
{
drag=1;
}
if(drag==1)
{
cnt++; // increase counter
{
if(cnt>=2)// if counter == 2 seconds
{
price=ObjectGetDouble(0,"line",OBJPROP_PRICE,0); // store new value
Print(" New price: ",DoubleToString(price));
PlaySound("ok.wav");
cnt=0; // reset counter
drag=0; // reset drag
}
}
}
}
//+------------------------------------------------------------------+
或者干脆使用一个while循环,这个循环非常好用。
//| Timer function |
//+------------------------------------------------------------------+
void OnTimer()
{
//---
while(price!=ObjectGetDouble(0,"line",OBJPROP_PRICE,0))
{
PlaySound("ok.wav");
price=ObjectGetDouble(0,"line",OBJPROP_PRICE,0);
Comment(price);
}
}
//+------------------------------------------------------------------+
好吧,只要布尔标志拖==1,你就可以用这个相同的标志来禁止更新。
就像我说的,你也可以启动一个计数器,这是一个例子。
或者干脆使用一个while循环,这个循环非常好用。
它只是说,当(我正在拖动)更新价格,直到它们变得相等(当我停止拖动)。
(和你的代码)
多么精彩的 例子啊!这真的是非常非常有用的评论,非常感谢。现在我正试图为那条线添加更多东西(设计和功能)。
(请不要责怪我的这个问题。我有 "graphics()",我把它用于图形对象,我通过Init()调用它,我需要通过OnTimer()调用它,所以我的问题是:我做错了吗?)
祝你一切顺利!
你可能遇到错误4200,即创建对象 失败,因为它已经存在。
所以你必须在尝试(重新)创建或修改对象之前看看对象是否存在。
你可以使用。
{
//Object does not exist
}
and/or
if(ObjectFind(0,"Long")>=0)
{
// Object already exists
}
你可以使用。
嗯,这对我来说是个新功能。
我可以在研究后尝试一下。(我看到过很多次,但我从来没有用过它。)
非常感谢。
#将价格转换为像素 - 打开
我使用 "HLine "来实现止盈 功能。
我有几个对象(Label, RecLabel...等等),我想让这些对象能与 "HLine "对象一起移动。所以我找到了以下函数。但我不希望转换 "X"。我只想把价格转换为Y。
我从未尝试过下面的代码。
问: 因为我想确定它是否能帮助我解决这个问题?
(如果我知道它能帮助我,那么我将开始为我的问题而尝试)。
问: 是否有任何方法可以让一些对象用 "HLine "移动?
datetime time = 0; // I just want to ignore this because I just want to give to X = 20; fixed mode ( should not change ever )
double price = ObjectGetDouble( 0, "line", OBJPROP_PRICE, 0 );
int x,y;
ChartTimePriceToXY( long chart_id, int sub_window, datetime time, double price, int& x, int& y );
提前感谢。
你将不得不更具体一些,你可以用很多方法做很多事情。
这些函数在OnChartEvent()函数中使用。
ChartPriceToXY - 将给你的价格时间转换为坐标,这真的是你需要的吗?
也许你是指ChartXYToTimePrice()?
https://www.mql5.com/en/docs/chart_operations/chartxytotimeprice
如果你不需要这个值,你可以直接把它作为一个零传递,见下面的例子。
//| CrossHair.mq4 |
//| Copyright 2017, Marco vd Heijden, MetaQuotes Software Corp. |
//| https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2017, Marco vd Heijden, MetaQuotes Software Corp."
#property link "https://www.mql5.com"
#property version "1.00"
#property strict
//+------------------------------------------------------------------+
//| Expert initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
//--- create some lines
ObjectCreate(0,"X",OBJ_HLINE,0,0,Ask); // X = Horizontal Axis (time==0)
ObjectCreate(0,"Y",OBJ_VLINE,0,TimeCurrent(),0);// Y = Vertical Axis (price==0)
//---
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Expert deinitialization function |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
//--- clean up
ObjectsDeleteAll(0,0,EMPTY);
}
//+------------------------------------------------------------------+
//| Expert tick function |
//+------------------------------------------------------------------+
void OnTick()
{
//---
}
//+------------------------------------------------------------------+
//| ChartEvent function |
//+------------------------------------------------------------------+
void OnChartEvent(const int id,
const long &lparam,
const double &dparam,
const string &sparam)
{
//--- If this is an event of a mouse click on the chart
if(id==CHARTEVENT_CLICK)
{
//--- Prepare variables
int x =(int)lparam;
int y =(int)dparam;
datetime time =0;
double price =0;
int window=0;
//--- Convert the X and Y coordinates in terms of date/time
if(ChartXYToTimePrice(0,x,y,window,time,price))
{
ObjectMove(0,"X",0,0,price); // notice time==0 because horizontal axis
ObjectMove(0,"Y",0,time,0); // notice price==0 because vertical axis
}
else
Print("ChartXYToTimePrice return error code: ",GetLastError());
Print("+--------------------------------------------------------------+");
}
}
//+------------------------------------------------------------------+
我已经读过那份文件,但直到你的最新评论,我才清楚地意识到它。事实上,我还没有试过,因为我在下面的代码中挣扎,我已经尝试了一些方法来获得好的结果,但我做不到。
马可先生,谢谢你的最新评论,我觉得我可以用它来解决我的物体移动问题。
我真的花了很多时间来寻找解决我的问题的方法,我不能得到好的结果。
但无论如何,我可以得到(目前的开仓)获利 价格,也就是我想让 "hline "对象可以自己调用获利价格,并且它是有效的。而且它只是在最初的时候起作用。是的,我知道,因为它是在 "Init() "中。但我试着把它放在 "OnTimer() "中,但它没有正确工作。
请让我知道一些情况,以帮助我找出我可以学到的东西,并为我的这个问题做些什么。
我将在7小时后再次开始研究这个问题。
如果我得到好的评论,那对我来说会更好。
for ( i = OrdersTotal() - 1; i >= 0; i-- )
{
if ( ! OrderSelect( i, SELECT_BY_POS, MODE_TRADES ) ) continue;
if ( OrderSymbol() == Symbol() ) tpprice = OrderTakeProfit();
ObjectCreate (
"#" + IntegerToString( OrderTicket() ) + " -" + "tphline", // name
OBJ_HLINE,
0, // subwindow
0, // time
tpprice // price1
);
tpprice =
ObjectGetDouble (
0,
"#" + IntegerToString( OrderTicket() ) + " -" + "tphline", // name
OBJPROP_PRICE, 0
);
}
// ontimer() --------------------------------------------------------
if ( tpprice != ObjectGetDouble( 0, "#" + IntegerToString( OrderTicket() ) + " -" + "tphline", OBJPROP_PRICE, 0 ) )
{
tpdrag = 1;
}
if ( tpdrag == 1 )
{
tpprice = ObjectGetDouble( 0, "#" + IntegerToString( OrderTicket() ) + " -" + "tphline", OBJPROP_PRICE, 0 );
Print( "tpdrag: ", tpdrag, " - Price: ", DoubleToString( tpprice, Digits ) );
// actually here is a graphical objects functin()
// here one of them
// also which one soon I will try to below objects could moves together " tphline " - but I can't take a time to research about your latest comment and so...
ObjectCreate( "recl object", OBJ_RECTANGLE, 0, 0, tpprice ); // I feel I could add something to " string name " - I already tried few things not good results
tpdrag = 0;
}
//---
return;
谢谢你。
祝你一切顺利!
请使用样式器,它在工具标签下。
我不知道你想达到什么目的,所以我不得不猜测你想做什么,这绝不是好事。
但你可以看一下这里的例子。
//| Stealth 4.mq4 |
//| Copyright 2017, Marco vd Heijden, MetaQuotes Software Corp. |
//| https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2017, Marco vd Heijden, MetaQuotes Software Corp."
#property link "https://www.mql5.com"
#property version "1.00"
#property strict
static input int takeprofit=500;// Take Profit
static input int stoploss=500; // Stop Loss
//+------------------------------------------------------------------+
//| Expert initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
//--- create timer
EventSetTimer(1);
//---
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Expert deinitialization function |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
//--- destroy timer
EventKillTimer();
}
//+------------------------------------------------------------------+
//| Expert tick function |
//+------------------------------------------------------------------+
void OnTick()
{
//---
}
//+------------------------------------------------------------------+
//| Timer function |
//+------------------------------------------------------------------+
void OnTimer()
{
for(int order=OrdersTotal(); order>=0; order--)
{
bool selected=OrderSelect(order,SELECT_BY_POS);
{
if(selected==1)
{
if(Symbol()==OrderSymbol()) // only for current chart symbol
{
switch(OrderType())
{
case OP_BUY: // for buy order
{
// if objects not found - create them
if(ObjectFind(0,"#"+IntegerToString(OrderTicket())+"-TP")<0)
{
ObjectCreate(0,"#"+IntegerToString(OrderTicket())+"-TP",OBJ_HLINE,0,0,OrderOpenPrice()+takeprofit*Point());
ObjectSet("#"+IntegerToString(OrderTicket())+"-TP",7,3);
}
if(ObjectFind(0,"#"+IntegerToString(OrderTicket())+"-SL")<0)
{
ObjectCreate(0,"#"+IntegerToString(OrderTicket())+"-SL",OBJ_HLINE,0,0,OrderOpenPrice()-stoploss*Point());
ObjectSet("#"+IntegerToString(OrderTicket())+"-SL",7,3);
}
// if objects exist
if(ObjectFind(0,"#"+IntegerToString(OrderTicket())+"-TP")>=0)
{
if(Bid>ObjectGetDouble(0,"#"+IntegerToString(OrderTicket())+"-TP",OBJPROP_PRICE,0))
{
bool close=OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),3,clrBlue);
{
if(close==0)
{
Alert(" Order Close Error! # "+IntegerToString(OrderTicket()));
}
if(close==1)
{
Alert(" Order: "+IntegerToString(OrderTicket())+" Closed due to TP Profit = "+DoubleToString(OrderProfit(),2));
ObjectDelete(0,"#"+IntegerToString(OrderTicket())+"-TP");
ObjectDelete(0,"#"+IntegerToString(OrderTicket())+"-SL");
}
}
}
}
if(ObjectFind(0,"#"+IntegerToString(OrderTicket())+"-SL")>=0)
{
if(Ask<ObjectGetDouble(0,"#"+IntegerToString(OrderTicket())+"-SL",OBJPROP_PRICE,0))
{
bool close=OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),3,clrBlue);
{
if(close==0)
{
Alert(" Order Close Error! # "+IntegerToString(OrderTicket()));
}
if(close==1)
{
Alert(" Order: "+IntegerToString(OrderTicket())+" Closed due to SL Profit = "+DoubleToString(OrderProfit(),2));
ObjectDelete(0,"#"+IntegerToString(OrderTicket())+"-TP");
ObjectDelete(0,"#"+IntegerToString(OrderTicket())+"-SL");
}
}
}
}
}
break;
case OP_SELL: // for sell order
{
// if objects not found - create them
if(ObjectFind(0,"#"+IntegerToString(OrderTicket())+"-TP")<0)
{
ObjectCreate(0,"#"+IntegerToString(OrderTicket())+"-TP",OBJ_HLINE,0,0,OrderOpenPrice()-takeprofit*Point());
ObjectSet("#"+IntegerToString(OrderTicket())+"-TP",7,3);
}
if(ObjectFind(0,"#"+IntegerToString(OrderTicket())+"-SL")<0)
{
ObjectCreate(0,"#"+IntegerToString(OrderTicket())+"-SL",OBJ_HLINE,0,0,OrderOpenPrice()+stoploss*Point());
ObjectSet("#"+IntegerToString(OrderTicket())+"-SL",7,3);
}
// if objects exist
if(ObjectFind(0,"#"+IntegerToString(OrderTicket())+"-TP")>=0)
{
if(Ask<ObjectGetDouble(0,"#"+IntegerToString(OrderTicket())+"-TP",OBJPROP_PRICE,0))
{
bool close=OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),3,clrBlue);
{
if(close==0)
{
Alert(" Order Close Error! # "+IntegerToString(OrderTicket()));
}
if(close==1)
{
Alert(" Order: "+IntegerToString(OrderTicket())+" Closed due to TP Profit = "+DoubleToString(OrderProfit(),2));
ObjectDelete(0,"#"+IntegerToString(OrderTicket())+"-TP");
ObjectDelete(0,"#"+IntegerToString(OrderTicket())+"-SL");
}
}
}
}
if(ObjectFind(0,"#"+IntegerToString(OrderTicket())+"-SL")>=0)
{
if(Bid>ObjectGetDouble(0,"#"+IntegerToString(OrderTicket())+"-SL",OBJPROP_PRICE,0))
{
bool close=OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),3,clrBlue);
{
if(close==0)
{
Alert(" Order Close Error! # "+IntegerToString(OrderTicket()));
}
if(close==1)
{
Alert(" Order: "+IntegerToString(OrderTicket())+" Closed due to SL Profit = "+DoubleToString(OrderProfit(),2));
ObjectDelete(0,"#"+IntegerToString(OrderTicket())+"-TP");
ObjectDelete(0,"#"+IntegerToString(OrderTicket())+"-SL");
}
}
}
}
}
break;
}
}
}
}
}
}
//+------------------------------------------------------------------+
所以你可以看到,你可以直接使用ObjectGetDouble,不需要把值复制到另一个double上,因为对象本身就持有这个值,当你拖动这一行时,这个值会自动改变,在你下次读它时就会看到。