我想制作一个通用代码,我可以在其中设置 pricepoint
,并在我的模拟账户上购买和销售该程序。
{
double pricepoint = 1.36900;
if ( Bid < pricepoint )
{
int ticket;
ticket = OrderSend( "EURUSD", OP_BUY, 1.0, Ask, 0, 0, 0, "My 1st Order!" );
}
else if ( ticket != -1 ) & ( Ask > pricepoint )
{
OrderClose( ticket, 1.0, Bid, 0 );;
}
答案 0 :(得分:1)
正如其他人提到的,首先您需要在 ticket
声明之外声明 if()
。如果您想稍后关闭订单,当它处于盈利状态时,您无法直接在 else{...}
块中执行此操作 - 价格不能同时低于pricepoint
和高于pricepoint
。
由于您的 EA
将在每次勾选时运行,因此变量ticket
将仅包含一个周期的新票号。如果您想要的话,您的代码将如下所示:
double pricepoint = 1.36900;
int ticket = -1;
// buy condition is met
if (Bid < pricepoint)
{
// you should consider using Symbol() to get the chart symbol
ticket = OrderSend("EURUSD", OP_BUY, 0.01, Ask, 0, 0, 0, "My 1st Order!");
if (ticket < 0)
{
Print("OrderSend failed with error #", GetLastError());
}
else
Print("OrderSend placed successfully");
}
// ... later in code
// if you want to close specifically the ticket that was opened in the same tick cycle
if (ticket > 0)
{
if (OrderSelect(ticket, SELECT_BY_TICKET) == true)
{
// use OrderTicket(), OrderLots() and other Order functions to get the ticket properties
OrderClose(OrderTicket(), OrderLots(), Bid, 0, clrNONE);
}
else
Print("OrderSelect returned the error of ",GetLastError());
}
我不建议以这种方式使用门票。您的订单盈利(如果有的话)需要一些时间。您可以遍历所有打开的票证并关闭那些有利可图的票据:
for (int i = 0; i < OrdersTotal(); i++)
{
if (OrderSelect(i, SELECT_BY_POS) == true)
{
// we only care about opened market orders, not pendings
if (OrderType() > OP_SELL)
continue;
// we only want to close orders that are in profit
if (OrderProfit() < 0)
continue;
if (OrderClose(OrderTicket(), OrderLots(), OrderClosePrice(), 0, clrNONE) == false)
Print("OrderClose returned the error of ",GetLastError());
// we need to adjust the control variable for the order we just closed
else
i--;
}
else
Print("OrderSelect returned the error of ",GetLastError());
}
答案 1 :(得分:0)
变量 if
需要在 ticket
块之外声明(并获取值)
/> if(){...}else{...}
变量来自哪里?
答案 2 :(得分:0)
虽然 MQL4
代码在语法上是微不足道的,但必须注意细节:
{ // -<BegOfBlock>- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
double pricepoint = 1.36900; // =OK: .DEFs a variable visible/known till the <EndOfBlock> "}"
if ( Bid < pricepoint ) // ERR: <pricepoint> refers to something not known yet, the compiler will object ( <Bid> is a known system-state variable of the code-execution platform [ MetaTrader Terminal 4] )
{ // -<BegOfBlock>- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
int ticket; // =OK: .DEFs a variable visible/known till the <EndOfBlock> "}" with an implicit value assignment of 0
ticket = OrderSend( "EURUSD",
OP_BUY,
1.0,
Ask,
0,
0,
0,
"My 1st Order!"
); // =OK: .SET a value { -1 onFailure | <aTicketNUMBER> onSuccess }
}
// ^------<EndOfBlock>- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// INF: @@<EndOfBlock> .UNDEF: <ticket>
//
else
if ( ticket != -1 ) & ( Ask > pricepoint )
// ^--------------|------------------------------ ERR: <ticket> refers to something not known here, the compiler will object
// ^------------------------------ ERR: "&" operator does not conform to syntax-rules, use "&&" and/or "||" for { .AND. | .OR. } boolean-constructions
{ // -<BegOfBlock>- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
OrderClose( ticket, 1.0, Bid, 0 );;
// ^---------------------^--------------ERR: <ticket> refers to something not known here, the compiler will object
// ^--------------ERR: remove the 2nd ";"
}
// ^------<EndOfBlock>- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// ^------<EndOfBlock>- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// ERR: an<EndOfBlock> "}" missing
// INF: @@<EndOfBlock> .UNDEF: <pricepoint>