我正在尝试找出一个EA,如果当前价格同时超过买入和卖出订单的未完成订单的SL级别,则EA将更新其未处理订单。如您所见,下面的图片当前价格低于挂单的SL点。因此,我要修改新的更新的挂单,其开盘价为那些SL点(+30和-30点),不仅用于窗口货币,而且还用于挂单列表中的其他货币。
我尝试了下面的代码,但没有成功。你能帮我吗?
//------------------------------------------------------------------------------- 1 --
int start()
{
string Symb=Symbol(); // Symbol
//------------------------------------------------------------------------------- 2 --
for(int i=1; i<=OrdersTotal(); i++)
{
if (OrderSelect(i-1,SELECT_BY_POS)==true)
{
//---------------------------------------------------------------------- 3 --
if (OrderSymbol()== Symb) continue;
if (OrderType()==OP_BUYSTOP || OrderType()==OP_SELLSTOP) continue; // Market order
//---------------------------------------------------------------------- 4 --
{
int Tip =OrderType();
int Ticket=OrderTicket();
double Price =OrderOpenPrice();
double SL =OrderStopLoss();
double TP =OrderTakeProfit();
}
}
}
//------------------------------------------------------------------------------- 5 --
if (Tip==0)
{
Alert("For ",Symb," no pending orders available");
return;
}
//------------------------------------------------------------------------------- 6 --
while(true)
{
RefreshRates(); // Update data
//------------------------------------------------------------------------- 7 --
double c_bid =MarketInfo(Symb,MODE_BID); // Request for the value of Bid
double c_ask =MarketInfo(Symb,MODE_ASK); // Request for the value of Ask
//------------------------------------------------------------------------- 8 --
string Text=""; // Not to be modified
double New_SL=0;
double New_TP=0;
switch(Tip) // By order type
{
case 4: // BuyStopt
if (NormalizeDouble(SL,Digits) > // If it is further than by
NormalizeDouble(c_ask,Digits))//..the preset value
{
double New_Price=SL+30*Point; // Its new price
if (NormalizeDouble(SL,Digits)>0)
New_SL=New_Price-(Price-SL); // New StopLoss
if (NormalizeDouble(TP,Digits)>0)
New_TP=New_Price+(TP-Price); // New TakeProfit
Text= "BuyStopt "; // Modify it.
}
break; // Exit 'switch'
case 5: // SellStop
if (NormalizeDouble(SL,Digits) < // If it is further than by
NormalizeDouble(c_bid,Digits))//..the preset value
{
New_Price=SL-30*Point; // Its new price
if (NormalizeDouble(SL,Digits)>0)
New_SL=New_Price+(SL-Price); // New StopLoss
if (NormalizeDouble(TP,Digits)>0)
New_TP=New_Price-(Price-TP); // New TakeProfit
Text= "SellStop "; // Modify it.
}
}
if (NormalizeDouble(New_SL,Digits)<0) // Checking SL
New_SL=0;
if (NormalizeDouble(New_TP,Digits)<0) // Checking TP
New_TP=0;
if (Text=="") // If it is not modified
{
Alert("No conditions for modification.");
break; // Exit 'while'
}
//------------------------------------------------------------------------ 10 --
Alert ("Modification ",Text,Ticket,". Awaiting response..");
bool Ans=OrderModify(Ticket,New_Price,New_SL,New_TP,0);//Modify it!
//------------------------------------------------------------------------ 11 --
if (Ans==true) // Got it! :)
{
Alert ("Modified order ",Text," ",Ticket," :)");
break; // Exit the closing cycle
}
//------------------------------------------------------------------------ 12 --
int Error=GetLastError(); // Failed :(
switch(Error) // Overcomable errors
{
case 4: Alert("Trade server is busy. Retrying..");
Sleep(3000); // Simple solution
continue; // At the next iteration
case 137:Alert("Broker is busy. Retrying..");
Sleep(3000); // Simple solution
continue; // At the next iteration
case 146:Alert("Trading subsystem is busy. Retrying..");
Sleep(500); // Simple solution
continue; // At the next iteration
}
switch(Error) // Critical errors
{
case 2 : Alert("Common error.");
break; // Exit 'switch'
case 64: Alert("Account is blocked.");
break; // Exit 'switch'
case 133:Alert("Trading is prohibited");
break; // Exit 'switch'
case 139:Alert("Order is blocked and is being processed");
break; // Exit 'switch'
case 145:Alert("Modification prohibited. ",
"Order is too close to the market");
break; // Exit 'switch'
default: Alert("Occurred error ",Error);//Other alternatives
}
break; // Exit the closing cycle
} // End of closing cycle
//------------------------------------------------------------------------------ 13 --
Alert ("The script has completed its operations -----------------------");
return; // Exit start()
}
//------------------------------------------------------------------------------ 14 --
答案 0 :(得分:1)
欢迎来到MQL-4.56789世界...
自从MQL4语言问世以来,规范已经发生了很大变化。
您的脚本遭受了“最近的”-新 -MQL4.56789 ...的更改,其中声明的变量的有效范围仅限于仅语法构造函数(这不在MQL4原始)。
因此,声明int Tip
等但在内部内 if(){...}
块不会导致 Tip
-变量以保持可见外部 {...} -块构造函数,而不再在那里知道了。
接下来,您的代码需要进行彻底的重新设计,以满足您的要求:
“ 不是 不仅适用于窗口货币,还适用于待处理订单列表中的其他货币。”
// ------------------------------------------------------------------------------- 2 --
for( int i=1; i <= OrdersTotal(); i++ )
{
if ( True == OrderSelect( i-1, SELECT_BY_POS ) )
{
//---------------------------------------------------------------------- 3 --
if ( OrderSymbol() == Symb ) continue; // ------------------------^ JIT/LOOP
if ( OrderType() == OP_BUYSTOP
|| OrderType() == OP_SELLSTOP
) continue; //-----------------------------------------------^ JIT/LOOP
// btw. was a pending { BUYSTOP || SELLSTOP }
//---------------------------------------------------------------------- 4 --
{ // a rest goes here :
int Tip = OrderType();
int Ticket = OrderTicket();
double Price = OrderOpenPrice();
double SL = OrderStopLoss();
double TP = OrderTakeProfit();
}
// ------------------------------ BUT ALL THESE GOT FORGOTTEN RIGHT HERE...
}
}
// ------------------------------------ OK, 've LOOPED THE WHOLE DB-POOL,
// BUT DID NOTHING SO FAR
// AND NOTHING SO FORTH
因此,由此产生的一些变化将有助于使脚本稳固地立足:
#define MASK "(for i==(%3d)) got OrderTKT(%20d) of TYPE{BUY|SELL|BUYLIMIT|SELLLIMIT|BUYSTOP|SELLSTOP}=[%d] in (%10s)-MARKET ... WILL LOOP for a NEXT(?) ONE"
while ( !IsStopped() ) // ----------------------------------------------------------- 1 --
{ // (almost) INFINITE SCRIPT SERVICE LOOP
// as (almost) HEADLESS Sir NICHOLAS :)
string TradeSYMBOL;
int Tip;
int Ticket;
double Price;
double SL;
double TP;
if ( 1 > OrdersTotal() ) { Sleep( 333 ); continue; } // NOP / NAP ----------^ NOP/LOOP
// ------------------------------------------------------------------------------ 2 --
for( int i=0; i < OrdersTotal(); i++ ) // DB-POOL LOOP
{
if ( OrderSelect( i, SELECT_BY_POS ) )
{
//---------------------------------------------------------------------- 3 --
if ( OrderType() != OP_BUYSTOP
&& OrderType() != OP_SELLSTOP
) {
PrintFormat( MASK, i, OrderTicket(), OrderType(), OrderSymbol() );
continue; //-------------------------------------------------^ JIT/LOOP
} // btw. ! a pending { BUYSTOP || SELLSTOP }
//---------------------------------------------------------------------- 4 --
else
{ // a { BUYSTOP || SELLSTOP } go here :
// -------------------------------------------------------------------- SET :
Tip = OrderType();
Ticket = OrderTicket();
Price = OrderOpenPrice();
SL = OrderStopLoss();
TP = OrderTakeProfit();
TradeSYMBOL = OrderSymbol();
// ------------------------------------------------------------- PROCESS it :
RefreshRates();
...
// Alert() - a blocking GUI operation, quite dangerous in auto-trading
// any
// OrderModify() - has also to respect the Broker-defined {Stop|Freeze}Level
...
} // { BUYSTOP || SELLSTOP }
} // .SELECT
} // .DB-POOL LOOP________________________________________________________
} // .SERVICE LOOP........................................................