#define EXPERT_MAGIC 123456 // MagicNumber of the expert
//+------------------------------------------------------------------+
//| 보류 중인 주문 수정 |
//+------------------------------------------------------------------+
void OnStart()
{
//--- 거래 요청과 거래 요청의 결과를 선언하고 초기화
MqlTradeRequest request={0};
MqlTradeResult result={0};
int total=OrdersTotal(); // 보류 중인 주문 총 수
//--- 보류 중인 주문을 전부 반복
for(int i=0; i<total; i++)
{
//--- 주문의 매개변수
ulong order_ticket=OrderGetTicket(i); // 주문 티켓
string order_symbol=Symbol(); // 기호
int digits=(int)SymbolInfoInteger(order_symbol,SYMBOL_DIGITS); // 소수점 이하 자릿수
ulong magic=OrderGetInteger(ORDER_MAGIC); // 주문의 매직넘버
double volume=OrderGetDouble(ORDER_VOLUME_CURRENT); // 주문의 현재 볼륨
double sl=OrderGetDouble(ORDER_SL); // 주문의 현재 Stop Loss
double tp=OrderGetDouble(ORDER_TP); // 주문의 현재 Take Profit
ENUM_ORDER_TYPE type=(ENUM_ORDER_TYPE)OrderGetInteger(ORDER_TYPE); // 주문의 유형
int offset = 50; // 주문을 발주할 현재 가격에서 상계(포인트)
double price; // 가격 유발 주문
double point=SymbolInfoDouble(order_symbol,SYMBOL_POINT); // 포인트의 값
//--- 주문 정보 출력
PrintFormat("#%I64u %s %s %.2f %s sl: %s tp: %s [%I64d]",
order_ticket,
order_symbol,
EnumToString(type),
volume,
DoubleToString(PositionGetDouble(POSITION_PRICE_OPEN),digits),
DoubleToString(sl,digits),
DoubleToString(tp,digits),
magic);
//--- 매직넘버가 일치하는 경우 Stop Loss 및 Take Profit는 지정되지 않습니다
if(magic==EXPERT_MAGIC && sl==0 && tp==0)
{
request.action=TRADE_ACTION_MODIFY; // 거래 작업 유형
request.order = OrderGetTicket(i); // 주문 티켓
request.symbol =Symbol(); // 기호
request.deviation=5; // 가격 허용 편차
//--- 주문의 유형에 따라 Take Profit 및 Stop Loss, 가격 수준 설정
if(type==ORDER_TYPE_BUY_LIMIT)
{
price = SymbolInfoDouble(Symbol(),SYMBOL_ASK)-offset*point;
request.tp = NormalizeDouble(price+offset*point,digits);
request.sl = NormalizeDouble(price-offset*point,digits);
request.price =NormalizeDouble(price,digits); // 정규 시가
}
else if(type==ORDER_TYPE_SELL_LIMIT)
{
price = SymbolInfoDouble(Symbol(),SYMBOL_BID)+offset*point;
request.tp = NormalizeDouble(price-offset*point,digits);
request.sl = NormalizeDouble(price+offset*point,digits);
request.price =NormalizeDouble(price,digits); // 정규 시가
}
else if(type==ORDER_TYPE_BUY_STOP)
{
price = SymbolInfoDouble(Symbol(),SYMBOL_BID)+offset*point;
request.tp = NormalizeDouble(price+offset*point,digits);
request.sl = NormalizeDouble(price-offset*point,digits);
request.price =NormalizeDouble(price,digits); // 정규 시가
}
else if(type==ORDER_TYPE_SELL_STOP)
{
price = SymbolInfoDouble(Symbol(),SYMBOL_ASK)-offset*point;
request.tp = NormalizeDouble(price-offset*point,digits);
request.sl = NormalizeDouble(price+offset*point,digits);
request.price =NormalizeDouble(price,digits); // 정규 시가
}
//--- 요청 전송
if(!OrderSend(request,result))
PrintFormat("OrderSend error %d",GetLastError()); // 요청 전송을 할 수 없는 경우 오류 코드 출력
//--- 작업 정보
PrintFormat("retcode=%u deal=%I64u order=%I64u",result.retcode,result.deal,result.order);
//--- 요청 및 결과 값 0 설정
ZeroMemory(request);
ZeroMemory(result);
}
}
}
//+------------------------------------------------------------------+
|