#define VOLUME 1.0 // 주문 볼륨
#define DEVIATION 100 // 종가 전 포인트 수
//+------------------------------------------------------------------+
//| 스크립트 프로그램 시작 함수 |
//+------------------------------------------------------------------+
void OnStart()
{
string currency_profit=SymbolInfoString(_Symbol,SYMBOL_CURRENCY_PROFIT); //수익 통화
double close_price=ChartPriceOnDropped(); //마우스로 스크립트를 떨어뜨린 지점에 해당하는 가격 좌표가 종가 역할을 합니다.
//---
for(int i=0; i<=ORDER_TYPE_SELL; i++)
{
ENUM_ORDER_TYPE order_type=(ENUM_ORDER_TYPE)i; // 주문 유형: 0 - 매수, 1 - Sell
double open_price=PriceOpenByOrderType(_Symbol, order_type); // 시가: 매수의 경우 - Ask, 매도의 경우 - Bid
//--- 시가를 받지 못한 경우 루프를 계속 진행
if(open_price==0)
continue;
//--- 종가가 0인 경우(차트에 드래그하여 스크립트가 실행되지 않은 경우) 가격을 계산
if(close_price==0)
close_price=(order_type==ORDER_TYPE_BUY ? open_price + DEVIATION * _Point : open_price - DEVIATION * _Point);
//--- 전달된 매개변수를 기반으로 현재 계정 및 시장 환경에 대한 대략적인 이익 규모를 계산
double profit=0;
ResetLastError();
if(!OrderCalcProfit(order_type, _Symbol, VOLUME, open_price, close_price, profit))
{
Print("OrderCalcProfit() failed. Error ", GetLastError());
continue;
}
//--- 받은 수익 값을 저널에 인쇄
PrintFormat("Estimated profit for %.2f %s position on %s with opening price of %.*f and closing price of %.*f: %.2f %s",
VOLUME, OrderTypeDescription(order_type), _Symbol, _Digits, open_price, _Digits, close_price, profit, currency_profit);
}
/*
결과:
Estimated profit for 1.00 Buy position on EURUSD with opening price of 1.10757 and closing price of 1.10881: 124.00 USD
Estimated profit for 1.00 Sell position on EURUSD with opening price of 1.10753 and closing price of 1.10881: -128.00 USD
*/
}
//+------------------------------------------------------------------+
//| 주문 유형별 주문 진입 가격 반환 |
//+------------------------------------------------------------------+
double PriceOpenByOrderType(const string symbol, const ENUM_ORDER_TYPE order_type)
{
MqlTick tick={};
//--- 심볼별 마지막 가격 가격 획득
if(!SymbolInfoTick(symbol, tick))
{
Print("SymbolInfoTick() failed. Error ", GetLastError());
return 0;
}
//--- 주문 유형별 가격 반환
switch(order_type)
{
case ORDER_TYPE_BUY : return(tick.ask);
case ORDER_TYPE_SELL : return(tick.bid);
default : return(0);
}
}
//+------------------------------------------------------------------+
//| 주문 유형 설명을 반환 |
//+------------------------------------------------------------------+
string OrderTypeDescription(const ENUM_ORDER_TYPE order_type)
{
switch(order_type)
{
case ORDER_TYPE_BUY : return("Buy");
case ORDER_TYPE_SELL : return("Sell");
default : return("Unknown order type");
}
}
|