일목균형표를 이용하여 매매를 하도록 개발하였다.
//********************************************************************
//| RangeOut Traiding IlmokCloud V0.1 |
//| Copyright @ 2012 |
//********************************************************************
//********************************************************************
//| X-Bars를 진입신호로 Range Out Strategy 관리툴로 매매 |
//********************************************************************
//********************************************************************
//| Version History 및 Library 불러오기 |
//********************************************************************
/*
V0.1
- 주문함수 수정(Additional/Hedge Comment)수정
- MaxSpread 기능 추가
- Code Clear
- Latency 측정 및 표시기능 추가
- Trailing / BreakEven 일원화
- MaxSpread 초과시 진입불가 기능 추가
*/
//***********************************************
// Error Code 처리를 위한 Library Import
#import "stdlib.ex4"
string ErrorDescription(int a0);
//*********************************************************************************************
//********************************************************************
//| 일반외생변수 및 기타 내생변수 설정 |
//********************************************************************
//***********************************************
// 일반 매매관련 기본 변수 설정
extern bool ECN = TRUE; // ECN Broker의 경우 Ordermodify
extern double Slippage = 3.0; // 슬리피지 허용 변수
extern double StopOutBalance = 500; // 강제청산 기준 잔고
extern double MaxSpread=4.0; // 최고 허용 스프레드 한도
extern double TargetProfit = 20; // Close All Target Profit
datetime PreviousBarTime1; // NewBar 처리를 위한 변수
datetime PreviousBarTime2; // NewBar 처리를 위한 변수
int mypoint; // 4,5 Digit Broker 처리 변수
int execution=-1; // 주문시 RoundTrip 속도 측정 변수
double ask00, bid00 ; // MarketInfo 정보를 받는 변수(Double)
double minlot,LotStep,sympoint, stopout,reqmargin,equity; // MarketInfo 정보를 받는 변수(Double)
int spread, mydigit,stoplevel,freeze, leverage; // MarketInfo 정보를 받는 변수(Integer)
string servername,StartDay,mytime; // MarketInfo 정보를 받는 변수(String)
//***********************************************
// MagicNumber와 Step설정을 위한 변수 설정
extern bool AutoMagic = TRUE; // 통화별 자동 매직넘버 부여
extern bool AutoStep = FALSE; // 통화별 자동 스텝 부여
double buystep=80; // 통화별 자동 스텝 미부여시 초기값
double sellstep=80; // 통화별 자동 스텝 미부여시 초기값
extern int buydivider = 5; // 통화별 자동 스텝 부여시 나누는 수(Buy)
extern int selldivider = 5; // 통화별 자동 스텝 부여시 나누는 수(Sell)
extern int HL_Days = 76; // Range 설정 기준일수
extern int TimeFrame = 1440; // TF : PERIOD_M1(1), PERIOD_M5(5), PERIOD_M15(15), PERIOD_M30(30), PERIOD_H1(60), PERIOD_H4(240), PERIOD_D1(1440), 0(Timeframe used on the chart.)
//*********************************************************************************************
//********************************************************************
//| 매수/매도 관련 변수 설정 |
//********************************************************************
int ShortTerm = 9;
int MidTerm = 26;
int LongTerm = 52;
int LastTerm = 2;
int Huhang = 26;
int Seonhang = 26;
//********************************************************************
//| 1st Entry 관련 변수설정 |
//********************************************************************
//***********************************************
// 1st Entry 매매를 위한 변수 설정
extern double lotsbuy=0.1; // 1st Buy 랏수
extern double lotssell=0.1; // 1st Sell 랏수
bool InitEntry = TRUE; // 1st Entry 여부 설정
extern int InitOrders = 1; // 최대 1st Order 개수
extern int StopLoss=0; // 1st Entry SL, SL > 0경우 작동
extern int TakeProfit=0; // 1st Entry TP
extern double TrailingStop = 0; // 트레일링 설정시 > 0
extern double BreakEven = 0; // Break Even 설정시 > 0
extern double BreakPoint = 0; // Break Even 설정시 손절치(본전 + 손절치)
extern string OutComment = "GROT_IlmokCloud_V0.1"; // 1st Entry 매매비고
int Magicbuy = 101; // 매수 매직넘버(오토시 의미없음)
int Magicsell = 111; // 매도 매직넘버(오토시 의미없음)
int ticketbuy,ticketsell; // 1st Entry Ticket
//********************************************************************
//| 2nd Entry Order 관련 변수설정 |
//********************************************************************
//***********************************************
// 2st Entry 매매를 위한 변수 설정
extern double AddMultiply=1.5; // 2nd Entry 랏 승수
extern bool AddEntry = TRUE; // 2nd Entry Enbled or Disable
extern int Addbuy = 50; // 최대 2nd Entry Buy 개수
extern int Addsell = 50; // 최대 2nd Entry Sell 개수
extern int AddTP = 0; // 2nd Entry TP
extern int AddSL = 0; // 2nd Entry SL
extern double AddTrailingStop = 0; // 2nd Entry 트레일링 설정시 > 0
extern string AddOutComment = "2nd_GROT_IlmokCloud_V0.1"; // 2nd Entry 매매비고
int AddTicketbuy,AddTicketsell; // 2nd Entry Ticket
int AddMagicBuy,AddMagicSell; // 2nd Entry Magic Number
//********************************************************************
//| 3rd Entry Order 관련 변수설정 |
//********************************************************************
//***********************************************
// 3rd Entry 매매를 위한 변수 설정
extern double HgMultiply=2.0; // 3rd Entry 랏 승수
extern bool HgEntry = TRUE; // 3rd Entry Enbled or Disable
extern int Hgbuy = 1; // 최대 3rd Entry Buy 개수
extern int Hgsell = 1; // 최대 3rd Entry Sell 개수
extern int HgTP = 0; // 3rd Entry TP
extern int HgSL = 0; // 3rd Entry SL
extern double HgTrailingStop = 0; // 3rd Entry 트레일링 설정시 > 0
extern string HGOutComment = "3rd_GROT_IlmokCloud_V0.1"; // Hedge Entry 매매비고
int HgTicketbuy,HgTicketsell,HgMagicBuy,HgMagicSell; // 3rd Entry Ticket & Magic Number
//********************************************************************
//| Initialization |
//********************************************************************
int init()
{
StartDay = TimeToStr(TimeCurrent(),TIME_DATE);
return(0);
}
//********************************************************************
//| Deinitialization |
//********************************************************************
int deinit()
{
return(0);
}
//********************************************************************
//| EA Start |
//********************************************************************
int start() { // EA 시작
//***********************************************
// 통화별 매직넘버 설정 함수 가져오기
getmagicbuy();
getmagicsell();
//***********************************************
// AutoStep설정시 자동계산 Step설정 함수 가져오기
getbuystep();
getsellstep();
//***********************************************
// StopOut Balance도달시 강제청산함수 가져오기
StopOutClose();
//***********************************************
// 통화별 2nd/3rd Entry 매직넘버 설정
AddMagicSell = Magicsell+1;
AddMagicBuy = Magicbuy+1;
HgMagicSell = Magicsell+2;
HgMagicBuy = Magicbuy+2;
//***********************************************
// MarketInfo 정보를 받는 변수 설정
ask00 = MarketInfo(Symbol(),MODE_ASK);
bid00 = MarketInfo(Symbol(),MODE_BID);
mydigit = MarketInfo(Symbol(), MODE_DIGITS);
spread = MarketInfo(Symbol(),MODE_SPREAD);
sympoint = MarketInfo(Symbol(),MODE_POINT);
stoplevel = MarketInfo(Symbol(), MODE_STOPLEVEL);
freeze = MarketInfo(Symbol(), MODE_FREEZELEVEL);
minlot = MarketInfo(Symbol(), MODE_MINLOT);
LotStep = MarketInfo(Symbol(), MODE_LOTSTEP);
servername = AccountServer();
reqmargin = MarketInfo(Symbol(), MODE_MARGINREQUIRED);
stopout = AccountStopoutLevel();
leverage = AccountLeverage();
mytime = TimeToStr(TimeCurrent(),TIME_SECONDS);
equity = AccountEquity();
//***********************************************
// Trailing 및 BreakEven 설정
if(TrailingStop > 0) MoveTrailingStop(Magicbuy, Magicsell, TrailingStop);
if(AddTrailingStop > 0) MoveTrailingStop(AddMagicBuy, AddMagicSell, AddTrailingStop);
if(HgTrailingStop > 0) MoveTrailingStop(HgMagicBuy, HgMagicSell, HgTrailingStop);
if(BreakEven>0) MoveBreakEven(Magicbuy, Magicsell, BreakEven, BreakPoint);
//***********************************************
// New Bar 설정(One Order / One Bar)
if(!NewBar()) return(0);
//***********************************************
// Max Spread Limitation 설정
if(spread > MaxSpread*PointValue()) {
InitEntry = FALSE;
AddEntry = FALSE;
HgEntry = FALSE;
}
//********************************************************************
//| 1st Entry Buy/Sell 조건 설정 |
//********************************************************************
//********************************************************************
// Buy Condition
/*
매수조건 : 양운구름상단과 저가와의 차이가 최대이고, 후행주가와 후행스팬과의 차이가 최대인 경우
- 이이전저가(LPrice2)가 26(HL_Days)일 최저가 : LPrice2 = LMin2
- 이전저가(LPrice1)가 이이전저가(LPrice2)보다 크고(반등), 현재봉의 시가가 이전저가(LPrice1)보다 크다. : LPrice1 > LPrice2 && OPrice0 > LPrice1
- 역배열상태 - 선행스팬1 > 선행스팬2 > 기준선 > 전환선 > 이전종가 : SunHangA1 > SunHangB1 && SunHangB1 > KiJun1 && KiJun1 > JunHwan1 && JunHwan1 > BPrice1
- 후행스팬(27) < 후행고가(27) && 선행스팬1(0) > 선행스팬상단(25) : HuHang1 < HPrice27 && SunHangA1 > MathMax(SunHangA25 , SunHangB25)
- 후행고가(27)-후행스팬(27) > 선행스팬1(1) - 이전저가(LPrice1) : HPrice27 - HuHang1 > SunHangA1 - LPrice1
*/
double HMax2 = High[iHighest(NULL,0,MODE_HIGH,HL_Days,2)] ; //기준기간내 최고가
double LMin2 = Low[iLowest(NULL,0,MODE_LOW,HL_Days,2)] ; //기준기간내 최저가
double JunHwan0 = iIchimoku(Symbol(), 0, ShortTerm, MidTerm, LongTerm, MODE_TENKANSEN, 0);
double JunHwan1 = iIchimoku(Symbol(), 0, ShortTerm, MidTerm, LongTerm, MODE_TENKANSEN, 1);
double JunHwan2 = iIchimoku(Symbol(), 0, ShortTerm, MidTerm, LongTerm, MODE_TENKANSEN, 2);
double KiJun0 = iIchimoku(Symbol(), 0, ShortTerm, MidTerm, LongTerm, MODE_KIJUNSEN, 0);
double KiJun1 = iIchimoku(Symbol(), 0, ShortTerm, MidTerm, LongTerm, MODE_KIJUNSEN, 1);
double KiJun2 = iIchimoku(Symbol(), 0, ShortTerm, MidTerm, LongTerm, MODE_KIJUNSEN, 2);
double OPrice0 = iOpen(Symbol(), 0, 0);
double BPrice1 = iClose(Symbol(), 0, 1);
double LPrice1 = iLow(Symbol(), 0, 1);
double LPrice2 = iLow(Symbol(), 0, 2);
double LPrice27 = iLow(Symbol(), 0, HL_Days+1);
double HPrice1 = iHigh(Symbol(), 0, 1);
double HPrice2 = iHigh(Symbol(), 0, 2);
double HPrice27 = iHigh(Symbol(), 0, HL_Days+1);
double HuHang0 = iIchimoku(Symbol(), 0, ShortTerm, MidTerm, LongTerm, MODE_CHINKOUSPAN, Huhang);
double HuHang1 = iIchimoku(Symbol(), 0, ShortTerm, MidTerm, LongTerm, MODE_CHINKOUSPAN, Huhang+1);
double SunHangA0 = iIchimoku(Symbol(), 0, ShortTerm, MidTerm, LongTerm, MODE_SENKOUSPANA, 0);
double SunHangB0 = iIchimoku(Symbol(), 0, ShortTerm, MidTerm, LongTerm, MODE_SENKOUSPANB, 0);
double SunHangA1 = iIchimoku(Symbol(), 0, ShortTerm, MidTerm, LongTerm, MODE_SENKOUSPANA, 1);
double SunHangB1 = iIchimoku(Symbol(), 0, ShortTerm, MidTerm, LongTerm, MODE_SENKOUSPANB, 1);
double SunHangA25 = iIchimoku(Symbol(), 0, ShortTerm, MidTerm, LongTerm, MODE_SENKOUSPANA, -Seonhang+1);
double SunHangB25 = iIchimoku(Symbol(), 0, ShortTerm, MidTerm, LongTerm, MODE_SENKOUSPANB, -Seonhang+1);
double LastPrice26 = iClose(Symbol(), 0, Huhang);
double LastPrice27 = iClose(Symbol(), 0, Huhang+1);
bool buycon1 = FALSE;
bool buycon2 = FALSE;
bool buycon3 = FALSE;
bool buycon4 = FALSE;
bool buycon5 = FALSE;
bool buycon6 = FALSE;
bool BuyCon = FALSE;
if ( LPrice2 == LMin2 ) buycon1=TRUE;
if ( LPrice1 > LPrice2 && OPrice0 > LPrice1 ) buycon2=TRUE;
if ( SunHangA1 > SunHangB1 && SunHangB1 > KiJun1 && KiJun1 > JunHwan1 && JunHwan1 > BPrice1 ) buycon3=TRUE;
if ( HuHang1 < HPrice27 && SunHangA1 > MathMax(SunHangA25 , SunHangB25) ) buycon4=TRUE;
if ( HPrice27 - HuHang1 > SunHangA1 - LPrice1 ) buycon5=TRUE;
if( buycon1 && buycon2 && buycon3 && buycon4 && buycon5 ) BuyCon=TRUE;
//==========================================================================================================================================================================================================
//********************************************************************
// Sell Condition
/*
매도조건 : 음운구름하단과 고가와의 차이가 최대이고, 후행주가와 후행스팬과의 차이가 최대인 경우
- 이이전고가(HPrice2)가 26(HL_Days)일 최고가 : HPrice2 = HMax2
- 이전고가(HPrice1)가 이이전고가(HPrice2)보다 작고(반락), 현재봉의 시가가 이전고가(HPrice1)보다 작다. : HPrice1 < HPrice2 && OPrice0 < HPrice1
- 정배열상태 - 선행스팬1 < 선행스팬2 < 기준선 < 전환선 < 이전종가 : SunHangA1 < SunHangB1 && SunHangB1 < KiJun1 && KiJun1 < JunHwan1 && JunHwan1 < BPrice1
- 후행스팬(27) > 후행저가(27) && 선행스팬1(1) < 선행스팬하단(25) : HuHang1 > LPrice27 && SunHangA1 < MathMin(SunHangA25 , SunHangB25)
- 후행스팬(27)-후행저가(27) > 이전고가(HPrice1)-선행스팬1(1) : HuHang1 - LPrice27 > HPrice1 - SunHangA1
*/
bool sellcon1 = FALSE;
bool sellcon2 = FALSE;
bool sellcon3 = FALSE;
bool sellcon4 = FALSE;
bool sellcon5 = FALSE;
bool sellcon6 = FALSE;
bool SellCon = FALSE;
if ( HPrice2 == HMax2 ) sellcon1=TRUE;
if ( HPrice1 < HPrice2 && OPrice0 < HPrice1 ) sellcon2=TRUE;
if ( SunHangA1 < SunHangB1 && SunHangB1 < KiJun1 && KiJun1 < JunHwan1 && JunHwan1 < BPrice1 ) sellcon3=TRUE;
if ( HuHang1 > LPrice27 && SunHangA1 < MathMin(SunHangA25 , SunHangB25) ) sellcon4=TRUE;
if ( HuHang1 - LPrice27 > HPrice1 - SunHangA1 ) sellcon5=TRUE;
if( sellcon1 && sellcon2 && sellcon3 && sellcon4 && sellcon5 ) SellCon=TRUE;
//==========================================================================================================================================================================================================
//********************************************************************
//| 1st Entry Buy/Sell Order |
//********************************************************************
//********************************************************************
// Buy Order (1st Entry)
if( InitEntry && TotalOrders(Magicbuy) < InitOrders ) { // 3
if( BuyCon ) { // 4
RefreshRates();
ticketbuy = OpenOrders(Symbol(),OP_BUY, lotsbuy, Slippage, StopLoss, TakeProfit, Magicbuy,0);
} // 4
} // 3
else
//********************************************************************
// Sell Order (1st Entry)
if( InitEntry && TotalOrders(Magicsell) < InitOrders ) { // 5
if( SellCon ) { // 6
RefreshRates();
ticketsell = OpenOrders(Symbol(),OP_SELL, lotssell, Slippage, StopLoss, TakeProfit, Magicsell,0);
} // 6
} // 5
//********************************************************************
//| 2nd Entry 주문입력 함수 |
//********************************************************************
//***********************************************
// 2nd Entry 변수 설정
bool AddInitBuyEntry = FALSE;
bool AddInitSellEntry = FALSE;
bool AddBuyEntry = FALSE;
bool AddSellEntry = FALSE;
/*
double InitBuyPrice = FindLastMaxBuyPrice(Magicbuy);
double AddBuyPrice = FindLastMaxBuyPrice(AddMagicBuy);
double InitSellPrice = FindLastMinSellPrice(Magicsell);
double AddSellPrice = FindLastMinSellPrice(AddMagicSell);
*/
double InitBuyPrice = FindLastBuyPrice(Magicbuy);
double AddBuyPrice = FindLastBuyPrice(AddMagicBuy);
double InitSellPrice = FindLastSellPrice(Magicsell);
double AddSellPrice = FindLastSellPrice(AddMagicSell);
double buystepadjust = buystep*PointValue()*Point;
double sellstepadjust = sellstep*PointValue()*Point;
double Addlotbuy=NormalizeDouble(AddMultiply*lotsbuy,2); // 2nd Buy 랏수
double Addlotsell=NormalizeDouble(AddMultiply*lotssell,2); // 2nd Sell 랏수
//***********************************************
// 2nd Entry 조건 설정
if ( TotalOrders(Magicsell)==1 && TotalOrders(AddMagicBuy)==0 && ask00 - InitSellPrice > sellstepadjust ) AddInitBuyEntry = TRUE;
else AddInitBuyEntry = FALSE;
if ( TotalOrders(Magicbuy)==1 && TotalOrders(AddMagicSell)==0 && InitBuyPrice-bid00 > buystepadjust ) AddInitSellEntry = TRUE;
else AddInitSellEntry = FALSE;
if ( TotalOrders(AddMagicBuy)>1 && AddBuyPrice > 0 && ask00 - AddBuyPrice > sellstepadjust ) AddBuyEntry = TRUE;
else AddBuyEntry = FALSE;
if ( TotalOrders(AddMagicSell)>1 && AddSellPrice > 0 && AddSellPrice - bid00 > buystepadjust ) AddSellEntry = TRUE;
else AddSellEntry = FALSE;
RefreshRates();
//***********************************************
// 2nd Buy Order
if(AddEntry && (TotalOrders(AddMagicBuy) < Addbuy) && AddInitBuyEntry) { // 7
AddTicketbuy = OpenOrders(Symbol(),OP_BUY, Addlotbuy, Slippage, AddSL , AddTP, AddMagicBuy,1);
} // 7
else
if(AddEntry && (TotalOrders(AddMagicBuy) < Addbuy) && AddBuyEntry) { // 8
AddTicketbuy = OpenOrders(Symbol(),OP_BUY, Addlotbuy, Slippage, AddSL , AddTP, AddMagicBuy,1);
} // 8
//***********************************************
// 2nd Sell Order
if(AddEntry && (TotalOrders(AddMagicSell) < Addsell) && AddInitSellEntry) { // 9
AddTicketsell = OpenOrders(Symbol(),OP_SELL, Addlotsell, Slippage, AddSL, AddTP, AddMagicSell,1);
} // 9
else
if(AddEntry && (TotalOrders(AddMagicSell) < Addsell) && AddSellEntry) { // 10
AddTicketsell = OpenOrders(Symbol(),OP_SELL, Addlotsell, Slippage, AddSL, AddTP, AddMagicSell,1);
} // 10
//********************************************************************
//| 3rd Entry 주문입력 함수 |
//********************************************************************
//***********************************************
// 3rd Entry 변수 설정
bool HgInitBuyEntry = FALSE;
bool HgInitSellEntry = FALSE;
bool HgBuyEntry = FALSE;
bool HgSellEntry = FALSE;
double Hglotbuy = NormalizeDouble(lotsbuy*HgMultiply,2); // 3rd Buy 랏수
double Hglotsell = NormalizeDouble(lotssell*HgMultiply,2); // 3rd Sell 랏수
//***********************************************
// 3rd Entry 조건 설정
if ( TotalOrders(Magicbuy)==1 && ask00-InitBuyPrice > buystepadjust && InitBuyPrice>0 ) HgInitBuyEntry = TRUE;
else HgInitBuyEntry = FALSE;
if ( TotalOrders(Magicsell)==1 && InitSellPrice-bid00 > sellstepadjust && InitSellPrice>0 ) HgInitSellEntry = TRUE;
else HgInitSellEntry = FALSE;
if ( TotalOrders(HgMagicBuy)>1 && ask00-InitBuyPrice > buystepadjust && InitBuyPrice>0 ) HgBuyEntry = TRUE;
else HgBuyEntry = FALSE;
if ( TotalOrders(HgMagicSell)>1 && InitSellPrice-bid00 > sellstepadjust && InitSellPrice>0 ) HgSellEntry = TRUE;
else HgSellEntry = FALSE;
RefreshRates();
//***********************************************
// 3rd Buy Order
if(HgEntry && (TotalOrders(HgMagicBuy) < Hgbuy) && HgInitBuyEntry) { // 11
HgTicketbuy = OpenOrders(Symbol(),OP_BUY,Hglotbuy,Slippage,HgSL,HgTP,HgMagicBuy,2);
} // 11
else
if(HgEntry && (TotalOrders(HgMagicBuy) < Hgbuy) && HgBuyEntry) { // 12
HgTicketbuy = OpenOrders(Symbol(),OP_BUY,Hglotbuy,Slippage,HgSL,HgTP,HgMagicBuy,2);
} // 12
//***********************************************
// 3rd Sell Order
if(HgEntry && (TotalOrders(HgMagicSell) < Hgsell) && HgInitSellEntry) { // 13
RefreshRates();
HgTicketsell = OpenOrders(Symbol(),OP_SELL,Hglotsell,Slippage,HgSL,HgTP,HgMagicSell,2);
} // 13
else
if(HgEntry && (TotalOrders(HgMagicSell) < Hgsell) && HgSellEntry) { // 14
RefreshRates();
HgTicketsell = OpenOrders(Symbol(),OP_SELL,Hglotsell,Slippage,HgSL,HgTP,HgMagicSell,2);
} // 14
//********************************************************************
//| Hedge 일괄 청산 함수(Initial + Additional Entry + Hedge Entry) |
//********************************************************************
bool TargetCloseAll = false;
if( Profit(Magicbuy) + Profit(Magicsell) + Profit(HgMagicBuy) + Profit(HgMagicSell) + Profit(AddMagicSell) + Profit(AddMagicBuy) > TargetProfit ) TargetCloseAll = true;
else TargetCloseAll = false;
if(TargetCloseAll) { // 22
int totl = OrdersTotal();
for(int a=totl-1; a >= 0 ; a--) { // 23
OrderSelect(a, SELECT_BY_POS, MODE_TRADES);
int typ = OrderType();
bool results = false;
RefreshRates();
if(OrderMagicNumber()==Magicbuy || OrderMagicNumber()==HgMagicBuy || OrderMagicNumber()==Magicsell || OrderMagicNumber()==HgMagicSell || OrderMagicNumber()==AddMagicBuy || OrderMagicNumber()==AddMagicSell) { // 24
execution = GetTickCount();
switch(typ) { //switch
case OP_BUY : results = OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_BID),Slippage,Red);
break;
case OP_SELL : results = OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_ASK),Slippage, Blue);
} //switch
execution = GetTickCount()-execution;
if(results == false) { // 25
Print("Order ", OrderTicket(), " failed to close. Error:", GetLastError() );
} // 25
} // 24
} // 23
} // 22
//********************************************************************
//| 화면표시정보 |
//********************************************************************
//***********************************************
// 화면표시 변수 설정
string ScreenStr;
int hborders = HBuyOrders(Magicbuy)+HBuyOrders(AddMagicBuy)+HBuyOrders(HgMagicBuy);
int hsorders = HSellOrders(Magicsell)+HSellOrders(HgMagicSell)+HSellOrders(AddMagicSell);
int horders = hborders+hsorders;
double hbprofit = HBProfit(Magicbuy)+HBProfit(AddMagicBuy)+HBProfit(HgMagicBuy);
double hsprofit = HSProfit(Magicsell)+HSProfit(AddMagicSell)+HSProfit(HgMagicSell);
double hprofit = hbprofit+hsprofit;
int SellOrd = TotalOrders(Magicsell) + TotalOrders(HgMagicSell) + TotalOrders(AddMagicSell);
int BuyOrd = TotalOrders(Magicbuy) + TotalOrders(HgMagicBuy) + TotalOrders(AddMagicBuy);
double ProfitBuy = Profit(Magicbuy) + Profit(HgMagicBuy) + Profit(AddMagicBuy);
double ProfitSell = Profit(Magicsell) + Profit(HgMagicSell) + Profit(AddMagicSell);
string buysteps = DoubleToStr(buystepadjust, mydigit);
string sellsteps = DoubleToStr(sellstepadjust, mydigit);
string SHMax01 = DoubleToStr(iHigh(Symbol(),TimeFrame,iHighest(Symbol(),TimeFrame,MODE_HIGH,HL_Days,1)),mydigit);
string SLMin01 = DoubleToStr(iLow(Symbol(),TimeFrame,iLowest(Symbol(),TimeFrame,MODE_LOW,HL_Days,1)),mydigit);
string SHLAvg01 = DoubleToStr(NormalizeDouble((iHigh(Symbol(),TimeFrame,iHighest(Symbol(),TimeFrame,MODE_HIGH,HL_Days,1)) + iLow(Symbol(),TimeFrame,iLowest(Symbol(),TimeFrame,MODE_LOW,HL_Days,1)))/2.0, Digits),mydigit);
string SBPrice01 = DoubleToStr(iClose(Symbol(), 1, 1),mydigit);
string SUpRange = DoubleToStr((iHigh(Symbol(),TimeFrame,iHighest(Symbol(),TimeFrame,MODE_HIGH,HL_Days,1))-iClose(Symbol(), 1, 1)), mydigit);
string SDownRange = DoubleToStr((iClose(Symbol(), 1, 1)-iLow(Symbol(),TimeFrame,iLowest(Symbol(),TimeFrame,MODE_LOW,HL_Days,1))), mydigit);
//***********************************************
// 화면표시정보
ScreenStr = StringConcatenate("Server : ",servername," Start Date_",StartDay," , My Time : ",mytime," 평가잔액 = ",equity," Leverage_",leverage,":1, 1Lot당 증거금_$",reqmargin," Target Profit_$",TargetProfit," Execution : ",execution,"ms")+"\n";
ScreenStr = ScreenStr + StringConcatenate("MinLot_",minlot," Lot Step_",LotStep," StopLevel_",stoplevel," , Freeze Level_",freeze," , StopOut_",stopout,"%, Spread_",spread,", Max Spread_",MaxSpread*PointValue()," buy divider_",buydivider," sell divider_",selldivider)+"\n";
ScreenStr = ScreenStr + StringConcatenate("기준일수 : ",HL_Days," 통화 : ",Symbol(),"_최고 : ",SHMax01,"_최저 : ",SLMin01," 중간값 : ",SHLAvg01," 현재가 : ",SBPrice01," 상단갭 : ",SUpRange," 하단갭 : ",SDownRange)+"\n";
ScreenStr = ScreenStr + StringConcatenate("Init Buy Price : ",FindLastBuyPrice(Magicbuy)," Add Buy Price : ",FindLastBuyPrice(AddMagicBuy)," Init Sell Price : ",FindLastSellPrice(Magicsell)," AddSellPrice : ", FindLastSellPrice(AddMagicSell)," BuyStep : ",buysteps," SellStep ",sellsteps)+"\n";
ScreenStr = ScreenStr + StringConcatenate("매수조건_",BuyCon," 매도조건_",SellCon," 2nd Buy_",AddBuyEntry," 2nd Sell_", AddSellEntry," 3rd Buy_",HgBuyEntry," 3rd Sell_",HgSellEntry)+"\n";
ScreenStr = ScreenStr + StringConcatenate("New Profits : ",ProfitBuy+ProfitSell,", New Orders : ",BuyOrd+SellOrd,"건 = Buy ",BuyOrd,"건 / Sell",SellOrd,"건") + "\n";
ScreenStr = ScreenStr +"==============================================================================" + "\n";
ScreenStr = ScreenStr + StringConcatenate("Total Profits : $",hprofit,", Buy : $",hbprofit,", Sell : $",hsprofit,", Total Orders : ",horders,"건 = Total Buy ",hborders,"건 / Total Sell",hsorders,"건")+ "\n";
Comment(ScreenStr);
return(0);
} // EA 종료
//********************************************************************
//| Allow One Action Per Bar |
//********************************************************************
// ***** 현재 바에서 하나만 주문처리시 사용 ****
bool NewBar()
{
static datetime lastbar = 0;
if(lastbar!=Time[0])
{
lastbar=Time[0];
return (true);
}
return(false);
}
/*
bool NewBarBuy()
{
if(PreviousBarTime1<Time[0])
{
PreviousBarTime1=Time[0];
return(true);
}
return(false);
}
bool NewBarSell()
{
if(PreviousBarTime2<Time[0])
{
PreviousBarTime2=Time[0];
return(true);
}
return(false);
}
*/
//********************************************************************
//| 통화별 매직넘버 설정 함수 |
//********************************************************************
int getmagicbuy()
{
//buy
if( AutoMagic == TRUE )
{
if(Symbol() == "AUDUSDm" || Symbol() == "AUDUSD") Magicbuy = 3021300;
if(Symbol() == "EURGBPm" || Symbol() == "EURGBP") Magicbuy = 5021300;
if(Symbol() == "EURUSDm" || Symbol() == "EURUSD") Magicbuy = 1021300;
if(Symbol() == "GBPUSDm" || Symbol() == "GBPUSD") Magicbuy = 2021300;
if(Symbol() == "USDCADm" || Symbol() == "USDCAD") Magicbuy = 7021300;
if(Symbol() == "USDCHFm" || Symbol() == "USDCHF") Magicbuy = 6021300;
if(Symbol() == "USDJPYm" || Symbol() == "USDJPY") Magicbuy = 4021300;
}
return(Magicbuy);
}
int getmagicsell()
{
//sell
if( AutoMagic == TRUE )
{
if(Symbol() == "AUDUSDm" || Symbol() == "AUDUSD") Magicsell = 3021351;
if(Symbol() == "EURGBPm" || Symbol() == "EURGBP") Magicsell = 5021351;
if(Symbol() == "EURUSDm" || Symbol() == "EURUSD") Magicsell = 1021351;
if(Symbol() == "GBPUSDm" || Symbol() == "GBPUSD") Magicsell = 2021351;
if(Symbol() == "USDCADm" || Symbol() == "USDCAD") Magicsell = 7021351;
if(Symbol() == "USDCHFm" || Symbol() == "USDCHF") Magicsell = 6021351;
if(Symbol() == "USDJPYm" || Symbol() == "USDJPY") Magicsell = 4021351;
}
return(Magicsell);
}
//********************************************************************
//| 통화별 Buy/ Sell Step 설정함수 |
//********************************************************************
double getsellstep() {
int mydigit=MarketInfo(Symbol(), MODE_DIGITS);
if( AutoStep == TRUE ) {
double HMax01 = iHigh(Symbol(),TimeFrame,iHighest(Symbol(),TimeFrame,MODE_HIGH,HL_Days,1)) ; // 기준기간내 최고가
double LMin01 = iLow(Symbol(),TimeFrame,iLowest(Symbol(),TimeFrame,MODE_LOW,HL_Days,1)) ; // 기준기간내 최저가
double BPrice01 = iClose(Symbol(), 1, 1); // 1분봉 기준 현재 종가
double UpRange = NormalizeDouble( (HMax01-BPrice01), mydigit) ; // 최고가 - 현재가 차이 계산
sellstep = NormalizeDouble(UpRange / (selldivider*PointValue()*Point),0); // Sell Step 계산
}
return(sellstep);
}
double getbuystep() {
int divider = 25;
int mydigit=MarketInfo(Symbol(), MODE_DIGITS);
if( AutoStep == TRUE ) {
double HMax01 = iHigh(Symbol(),TimeFrame,iHighest(Symbol(),TimeFrame,MODE_HIGH,HL_Days,1)) ; // 기준기간내 최고가
double LMin01 = iLow(Symbol(),TimeFrame,iLowest(Symbol(),TimeFrame,MODE_LOW,HL_Days,1)) ; // 기준기간내 최저가
double BPrice01 = iClose(Symbol(), 1, 1); // 1분봉 기준 현재 종가
double DownRange = NormalizeDouble( (BPrice01-LMin01), mydigit) ; // 현재가-최저가 차이 계산
buystep = NormalizeDouble(DownRange / (buydivider*PointValue()*Point),0); // Buy Step 계산
}
return(buystep);
}
//********************************************************************
//| 주문 함수 (OpenOrders) |
//********************************************************************
int OpenOrders( string SymbolName, int CmdType, double Lots, int Slippages, double StopLosses, double TakeProfit, int magic, int EntryType) { // 함수 시작
static string OrderTypeToString[6] = {"OP_BUY", "OP_SELL", "OP_BUYLIMIT", "OP_SELLLIMIT", "OP_BUYSTOP", "OP_SELLSTOP"};
int Ticket = -1;
int ErrorCodes;
int Slips = 0;
double Vol = 0;
string Comments;
string ETComments;
color CLRs;
RefreshRates();
double stopbuy = NormalizeDouble(ask00-(StopLosses*PointValue()*Point),Digits);
double stopsell = NormalizeDouble(bid00+(StopLosses*PointValue()*Point),Digits);
double tpbuy = NormalizeDouble(ask00+(TakeProfit*PointValue()*Point),Digits);
double tpsell = NormalizeDouble(bid00-(TakeProfit*PointValue()*Point),Digits);
// EntryType = 0 : Init Entry, 1 : Additional Entry, 2 ; Hedge Entry
switch(EntryType) { //switch
case 0 : ETComments = OutComment;
break;
case 1 : ETComments = AddOutComment;
break;
case 2 : ETComments = HGOutComment;
} //switch
if (StopLosses == 0) {
stopbuy = 0;
stopsell = 0;
}
if (TakeProfit == 0) {
tpbuy = 0;
tpsell = 0;
}
if (StopLosses == 0 && TakeProfit == 0) ECN = FALSE;
if (CmdType == OP_BUY) { // Buy 시작
Comments = ETComments+"샀어";
CLRs = Red;
if (ECN == true) { // ECN 시작
execution = GetTickCount();
Ticket = OrderSend(SymbolName, CmdType, Lots, ask00, Slippages, 0, 0,Comments,magic,0,CLRs);
if(Ticket == -1) { // check for errors
ErrorCodes = GetLastError();
Print("아씨!, Error: " + ErrorDescription(ErrorCodes));
return(-1);
} // check for errors
OrderSelect(Ticket, SELECT_BY_TICKET);
if (!OrderSelect(Ticket, SELECT_BY_TICKET)) { // check for errors
ErrorCodes = GetLastError();
Print("아씨!, Error: " + ErrorDescription(ErrorCodes));
return(-1);
} // check for errors
OrderModify(Ticket,OrderOpenPrice(),stopbuy,tpbuy,0,CLRs);
execution = GetTickCount()-execution;
return(0);
} // ECN 종료
else
{ // No ECN
execution = GetTickCount();
Ticket = OrderSend(SymbolName, CmdType, Lots, ask00, Slippages, stopbuy, tpbuy,Comments,magic,0,CLRs);
if(Ticket == -1) { // check for errors
ErrorCodes = GetLastError();
Print("아씨!, Error: " + ErrorDescription(ErrorCodes));
return(-1);
} // check for errors
execution = GetTickCount()-execution;
return(0);
} // No ECN 종료
} // Buy 종료
else
if (CmdType == OP_SELL) { // Sell 시작
Comments = ETComments+"팔았어";
CLRs = Blue;
if (ECN == true) { // ECN 시작
execution = GetTickCount();
Ticket = OrderSend(SymbolName, CmdType, Lots, bid00, Slippages, 0, 0,Comments,magic,0,CLRs);
if(Ticket == -1) { // check for errors
ErrorCodes = GetLastError();
Print("아씨!, Error: " + ErrorDescription(ErrorCodes));
return(-1);
} // check for errors
OrderSelect(Ticket, SELECT_BY_TICKET);
if (!OrderSelect(Ticket, SELECT_BY_TICKET)) { // check for errors
ErrorCodes = GetLastError();
Print("아씨!, Error: " + ErrorDescription(ErrorCodes));
return(-1);
} // check for errors
OrderModify(Ticket,OrderOpenPrice(),stopsell, tpsell,0,CLRs);
execution = GetTickCount()-execution;
return(0);
} // ECN 종료
else
{ // No ECN
execution = GetTickCount();
Ticket = OrderSend(SymbolName, CmdType, Lots, bid00, Slippages, stopsell, tpsell,Comments,magic,0,CLRs);
if(Ticket == -1) { // check for errors
ErrorCodes = GetLastError();
Print("아씨!, Error: " + ErrorDescription(ErrorCodes));
return(-1);
} // check for errors
execution = GetTickCount()-execution;
return(0);
} // No ECN 종료
} // Sell 종료
OrderSelect(OrdersTotal() - 1, SELECT_BY_POS, MODE_TRADES);
if (OrderSymbol() == SymbolName)
{
Slips = OrderSlipPage(OrderOpenPrice());
Ticket = OrderTicket();
Vol = OrderLots();
}
Print(Ticket, " = OrderSend( ", Comments,", ", SymbolName, ", ", OrderTypeToString[CmdType], ", ", Lots, ", ", DoubleToStr(OrderOpenPrice(), MarketInfo(SymbolName, MODE_DIGITS)),", Execution : ",execution,"ms",
", ", Slippages, ", ", StopLosses, ", ", TakeProfit, ") - ", ErrorDescription(ErrorCodes), ", SlipPage = ", Slips, ", Lots = ", Vol);
return(Ticket);
} // 함수 종료
/*
//********************************************************************
//| 주문 함수 (OpenStopOrders) |
//********************************************************************
int OpenStopOrders( string SymbolName, int CmdType, double Lots, double openprice, int Slippages, double StopLosses, double TakeProfit, int magic, int EntryType) { // 함수 시작
static string OrderTypeToString[6] = {"OP_BUY", "OP_SELL", "OP_BUYLIMIT", "OP_SELLLIMIT", "OP_BUYSTOP", "OP_SELLSTOP"};
int Ticket = -1;
int ErrorCodes;
int Slips = 0;
double Vol = 0;
string Comments;
string ETComments;
color CLRs;
RefreshRates();
double stopbuy = NormalizeDouble(openprice-(StopLosses*PointValue()*Point),Digits);
double stopsell = NormalizeDouble(openprice+(StopLosses*PointValue()*Point),Digits);
double tpbuy = NormalizeDouble(openprice+(TakeProfit*PointValue()*Point),Digits);
double tpsell = NormalizeDouble(openprice-(TakeProfit*PointValue()*Point),Digits);
// EntryType = 0 : Init Entry, 1 : Additional Entry, 2 ; Hedge Entry
switch(EntryType) { //switch
case 0 : ETComments = OutComment;
break;
case 1 : ETComments = AddOutComment;
break;
case 2 : ETComments = HGOutComment;
} //switch
if (StopLosses == 0) {
stopbuy = 0;
stopsell = 0;
}
if (TakeProfit == 0) {
tpbuy = 0;
tpsell = 0;
}
if (StopLosses == 0 && TakeProfit == 0) ECN = FALSE;
if (CmdType == OP_BUYSTOP) { // Buy 시작
Comments = ETComments+"샀어";
CLRs = Red;
if (ECN == true) { // ECN 시작
execution = GetTickCount();
Ticket = OrderSend(SymbolName, CmdType, Lots, openprice, Slippages, 0, 0,Comments,magic,0,CLRs);
if(Ticket == -1) { // check for errors
ErrorCodes = GetLastError();
Print("아씨!, Error: " + ErrorDescription(ErrorCodes));
return(-1);
} // check for errors
OrderSelect(Ticket, SELECT_BY_TICKET);
if (!OrderSelect(Ticket, SELECT_BY_TICKET)) { // check for errors
ErrorCodes = GetLastError();
Print("아씨!, Error: " + ErrorDescription(ErrorCodes));
return(-1);
} // check for errors
OrderModify(Ticket,OrderOpenPrice(),stopbuy,tpbuy,0,CLRs);
execution = GetTickCount()-execution;
return(0);
} // ECN 종료
else
{ // No ECN
execution = GetTickCount();
Ticket = OrderSend(SymbolName, CmdType, Lots, openprice, Slippages, stopbuy, tpbuy,Comments,magic,0,CLRs);
if(Ticket == -1) { // check for errors
ErrorCodes = GetLastError();
Print("아씨!, Error: " + ErrorDescription(ErrorCodes));
return(-1);
} // check for errors
execution = GetTickCount()-execution;
return(0);
} // No ECN 종료
} // Buy 종료
else
if (CmdType == OP_SELLSTOP) { // Sell 시작
Comments = ETComments+"팔았어";
CLRs = Blue;
if (ECN == true) { // ECN 시작
execution = GetTickCount();
Ticket = OrderSend(SymbolName, CmdType, Lots, openprice, Slippages, 0, 0,Comments,magic,0,CLRs);
if(Ticket == -1) { // check for errors
ErrorCodes = GetLastError();
Print("아씨!, Error: " + ErrorDescription(ErrorCodes));
return(-1);
} // check for errors
OrderSelect(Ticket, SELECT_BY_TICKET);
if (!OrderSelect(Ticket, SELECT_BY_TICKET)) { // check for errors
ErrorCodes = GetLastError();
Print("아씨!, Error: " + ErrorDescription(ErrorCodes));
return(-1);
} // check for errors
OrderModify(Ticket,OrderOpenPrice(),stopsell, tpsell,0,CLRs);
execution = GetTickCount()-execution;
return(0);
} // ECN 종료
else
{ // No ECN
execution = GetTickCount();
Ticket = OrderSend(SymbolName, CmdType, Lots, openprice, Slippages, stopsell, tpsell,Comments,magic,0,CLRs);
if(Ticket == -1) { // check for errors
ErrorCodes = GetLastError();
Print("아씨!, Error: " + ErrorDescription(ErrorCodes));
return(-1);
} // check for errors
execution = GetTickCount()-execution;
return(0);
} // No ECN 종료
} // Sell 종료
OrderSelect(OrdersTotal() - 1, SELECT_BY_POS, MODE_TRADES);
if (OrderSymbol() == SymbolName)
{
Slips = OrderSlipPage(OrderOpenPrice());
Ticket = OrderTicket();
Vol = OrderLots();
}
Print(Ticket, " = OrderSend( ", Comments,", ", SymbolName, ", ", OrderTypeToString[CmdType], ", ", Lots, ", ", DoubleToStr(OrderOpenPrice(), MarketInfo(SymbolName, MODE_DIGITS)),", Execution : ",execution,"ms",
", ", Slippages, ", ", StopLosses, ", ", TakeProfit, ") - ", ErrorDescription(ErrorCodes), ", SlipPage = ", Slips, ", Lots = ", Vol);
return(Ticket);
} // 함수 종료
//********************************************************************
//| 주문 함수 (OpenLimitOrders) |
//********************************************************************
int OpenLimitOrders( string SymbolName, int CmdType, double Lots, double openprice, int Slippages, double StopLosses, double TakeProfit, int magic, int EntryType) { // 함수 시작
static string OrderTypeToString[6] = {"OP_BUY", "OP_SELL", "OP_BUYLIMIT", "OP_SELLLIMIT", "OP_BUYSTOP", "OP_SELLSTOP"};
int Ticket = -1;
int ErrorCodes;
int Slips = 0;
double Vol = 0;
string Comments;
string ETComments;
color CLRs;
RefreshRates();
double stopbuy = NormalizeDouble(openprice-(StopLosses*PointValue()*Point),Digits);
double stopsell = NormalizeDouble(openprice+(StopLosses*PointValue()*Point),Digits);
double tpbuy = NormalizeDouble(openprice+(TakeProfit*PointValue()*Point),Digits);
double tpsell = NormalizeDouble(openprice-(TakeProfit*PointValue()*Point),Digits);
// EntryType = 0 : Init Entry, 1 : Additional Entry, 2 ; Hedge Entry
switch(EntryType) { //switch
case 0 : ETComments = OutComment;
break;
case 1 : ETComments = AddOutComment;
break;
case 2 : ETComments = HGOutComment;
} //switch
if (StopLosses == 0) {
stopbuy = 0;
stopsell = 0;
}
if (TakeProfit == 0) {
tpbuy = 0;
tpsell = 0;
}
if (StopLosses == 0 && TakeProfit == 0) ECN = FALSE;
if (CmdType == OP_BUYLIMIT) { // Buy 시작
Comments = ETComments+"샀어";
CLRs = Red;
if (ECN == true) { // ECN 시작
execution = GetTickCount();
Ticket = OrderSend(SymbolName, CmdType, Lots, openprice, Slippages, 0, 0,Comments,magic,0,CLRs);
if(Ticket == -1) { // check for errors
ErrorCodes = GetLastError();
Print("아씨!, Error: " + ErrorDescription(ErrorCodes));
return(-1);
} // check for errors
OrderSelect(Ticket, SELECT_BY_TICKET);
if (!OrderSelect(Ticket, SELECT_BY_TICKET)) { // check for errors
ErrorCodes = GetLastError();
Print("아씨!, Error: " + ErrorDescription(ErrorCodes));
return(-1);
} // check for errors
OrderModify(Ticket,OrderOpenPrice(),stopbuy,tpbuy,0,CLRs);
execution = GetTickCount()-execution;
return(0);
} // ECN 종료
else
{ // No ECN
execution = GetTickCount();
Ticket = OrderSend(SymbolName, CmdType, Lots, openprice, Slippages, stopbuy, tpbuy,Comments,magic,0,CLRs);
if(Ticket == -1) { // check for errors
ErrorCodes = GetLastError();
Print("아씨!, Error: " + ErrorDescription(ErrorCodes));
return(-1);
} // check for errors
execution = GetTickCount()-execution;
return(0);
} // No ECN 종료
} // Buy 종료
else
if (CmdType == OP_SELLLIMIT) { // Sell 시작
Comments = ETComments+"팔았어";
CLRs = Blue;
if (ECN == true) { // ECN 시작
execution = GetTickCount();
Ticket = OrderSend(SymbolName, CmdType, Lots, openprice, Slippages, 0, 0,Comments,magic,0,CLRs);
if(Ticket == -1) { // check for errors
ErrorCodes = GetLastError();
Print("아씨!, Error: " + ErrorDescription(ErrorCodes));
return(-1);
} // check for errors
OrderSelect(Ticket, SELECT_BY_TICKET);
if (!OrderSelect(Ticket, SELECT_BY_TICKET)) { // check for errors
ErrorCodes = GetLastError();
Print("아씨!, Error: " + ErrorDescription(ErrorCodes));
return(-1);
} // check for errors
OrderModify(Ticket,OrderOpenPrice(),stopsell, tpsell,0,CLRs);
execution = GetTickCount()-execution;
return(0);
} // ECN 종료
else
{ // No ECN
execution = GetTickCount();
Ticket = OrderSend(SymbolName, CmdType, Lots, openprice, Slippages, stopsell, tpsell,Comments,magic,0,CLRs);
if(Ticket == -1) { // check for errors
ErrorCodes = GetLastError();
Print("아씨!, Error: " + ErrorDescription(ErrorCodes));
return(-1);
} // check for errors
execution = GetTickCount()-execution;
return(0);
} // No ECN 종료
} // Sell 종료
OrderSelect(OrdersTotal() - 1, SELECT_BY_POS, MODE_TRADES);
if (OrderSymbol() == SymbolName)
{
Slips = OrderSlipPage(OrderOpenPrice());
Ticket = OrderTicket();
Vol = OrderLots();
}
Print(Ticket, " = OrderSend( ", Comments,", ", SymbolName, ", ", OrderTypeToString[CmdType], ", ", Lots, ", ", DoubleToStr(OrderOpenPrice(), MarketInfo(SymbolName, MODE_DIGITS)),", Execution : ",execution,"ms",
", ", Slippages, ", ", StopLosses, ", ", TakeProfit, ") - ", ErrorDescription(ErrorCodes), ", SlipPage = ", Slips, ", Lots = ", Vol);
return(Ticket);
} // 함수 종료
*/
//********************************************************************
//| Slippage 계산 함수 |
//********************************************************************
int OrderSlipPage( double OriginalPrice ) {
double Tmp;
int Res;
if (OrderCloseTime() == 0)
{
if (OrderType() == OP_BUY)
Tmp = OriginalPrice - OrderOpenPrice();
else if (OrderType() == OP_SELL)
Tmp = OrderOpenPrice() - OriginalPrice;
}
else
{
if (OrderType() == OP_BUY)
Tmp = OrderClosePrice() - OriginalPrice;
else if (OrderType() == OP_SELL)
Tmp = OriginalPrice - OrderClosePrice();
}
if (Tmp > 0)
Res = Tmp / MarketInfo(OrderSymbol(), MODE_POINT) + 0.1;
else
Res = Tmp / MarketInfo(OrderSymbol(), MODE_POINT) - 0.1;
return(Res);
}
//********************************************************************
//| 통화별 Point조정 설정 |
//********************************************************************
// 5 digit broker adjustment
int PointValue() {
if(MarketInfo(Symbol(), MODE_DIGITS)==3 || MarketInfo(Symbol(), MODE_DIGITS)==5)
{
mypoint = 10;
}
else
{
mypoint = 1;
}
return(mypoint);
}
//********************************************************************
//| Stop Out조건발생시 전체 일괄 청산 함수 |
//********************************************************************
void StopOutClose() { // 0
bool CloseAllOrders = false;
if(AccountFreeMargin() < StopOutBalance || AccountBalance() < StopOutBalance) CloseAllOrders = true;
else CloseAllOrders = false;
if(CloseAllOrders) { // 15
int totalord = OrdersTotal();
for(int jk=totalord-1;jk >= 0;jk--) { // 16
OrderSelect(jk, SELECT_BY_POS, MODE_TRADES);
int types = OrderType();
bool resultall = false;
RefreshRates();
switch(types) { //switch
case OP_BUY : resultall = OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_BID),5,Red);
break;
case OP_SELL : resultall = OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_ASK),5, Blue);
} //switch
if(resultall == false) { // 17
Print("Order ", OrderTicket(), " failed to close. Error:", GetLastError() );
} // 17
} // 16
} // 15
} // 0
//********************************************************************
//| Profit Calculate Function(수익 계산 함수) |
//********************************************************************
double Profit(int Magic) { // 0
double Prof=0;
for (int k=0; k < OrdersTotal(); k++)
{ // 1
if (OrderSelect(k, SELECT_BY_POS, MODE_TRADES))
{ // 2
if (OrderSymbol()==Symbol() && OrderMagicNumber()==Magic)
{ // 3
Prof = Prof + OrderProfit();
} // 3
} // 2
} // 1
return(Prof);
} // 0
//********************************************************************
//| Total order Calculate Function(총오더수 계산 함수) |
//********************************************************************
int TotalOrders(int MAGIC) { // 0
int cnt=0;
for (int i=0; i < OrdersTotal(); i++)
{ // 1
if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
{ // 2
if (OrderMagicNumber()==MAGIC && (OrderType()==OP_BUY || OrderType()==OP_SELL))
{ // 3
cnt++;
} // 3
} // 2
} // 1
return(cnt);
} // 0
//********************************************************************
//| 총매수수익 계산 함수(과거치 포함) |
//********************************************************************
double HBProfit(int Magic) { // 0
double Prof=0;
for (int k=0; k < OrdersHistoryTotal(); k++)
{ // 1
if (OrderSelect(k, SELECT_BY_POS, MODE_HISTORY))
{ // 2
if (OrderSymbol()==Symbol() && OrderMagicNumber()==Magic && OrderType()==OP_BUY)
{ // 3
Prof = Prof + OrderProfit();
} // 3
} // 2
} // 1
return(Prof);
} // 0
//********************************************************************
//| 총매도수익 계산 함수(과거치 포함) |
//********************************************************************
double HSProfit(int Magic) { // 0
double Prof=0;
for (int k=0; k < OrdersHistoryTotal(); k++)
{ // 1
if (OrderSelect(k, SELECT_BY_POS, MODE_HISTORY))
{ // 2
if (OrderSymbol()==Symbol() && OrderMagicNumber()==Magic && OrderType()==OP_SELL)
{ // 3
Prof = Prof + OrderProfit();
} // 3
} // 2
} // 1
return(Prof);
} // 0
//********************************************************************
//| 총Buy오더수 계산 함수(과거치 포함) |
//********************************************************************
int HBuyOrders(int MAGIC) { // 0
int cnt=0;
for (int i=0; i < OrdersHistoryTotal(); i++)
{ // 1
if (OrderSelect(i, SELECT_BY_POS, MODE_HISTORY))
{ // 2
if (OrderMagicNumber()==MAGIC && OrderType()==OP_BUY)
{ // 3
cnt++;
} // 3
} // 2
} // 1
return(cnt);
} // 0
//********************************************************************
//| 총Sell오더수 계산 함수(과거치 포함) |
//********************************************************************
int HSellOrders(int MAGIC) { // 0
int cnt=0;
for (int i=0; i < OrdersHistoryTotal(); i++)
{ // 1
if (OrderSelect(i, SELECT_BY_POS, MODE_HISTORY))
{ // 2
if (OrderMagicNumber()==MAGIC && OrderType()==OP_SELL)
{ // 3
cnt++;
} // 3
} // 2
} // 1
return(cnt);
} // 0
//********************************************************************
//| Trailing Stop |
//********************************************************************
void MoveTrailingStop(int magicbuy, int magicsell, double trailing) {
int ct,totl=OrdersTotal();
RefreshRates();
for(ct=0;ct<totl;ct++)
{
OrderSelect(ct,SELECT_BY_POS,MODE_TRADES);
if(OrderType()==OP_BUY && OrderSymbol()==Symbol() && OrderMagicNumber()==magicbuy)
{
if( trailing > 0 && Bid > NormalizeDouble(OrderOpenPrice()+trailing*PointValue()*Point,Digits) )
{
if((NormalizeDouble(OrderStopLoss(),Digits) < NormalizeDouble(Bid-PointValue()*trailing*Point,Digits))||(OrderStopLoss()==0))
{
OrderModify(OrderTicket(),OrderOpenPrice(),NormalizeDouble(Bid-PointValue()*trailing*Point,Digits),OrderTakeProfit(),0,Blue);
return(0);
}
}
}
if(OrderType()==OP_SELL && OrderSymbol()==Symbol() && OrderMagicNumber()==magicsell)
{
if( trailing > 0 && Ask < NormalizeDouble(OrderOpenPrice()-trailing*PointValue()*Point,Digits) )
{
if((NormalizeDouble(OrderStopLoss(),Digits) > (NormalizeDouble(Ask+(PointValue()*trailing*Point),Digits)))||(OrderStopLoss()==0))
{
OrderModify(OrderTicket(),OrderOpenPrice(),NormalizeDouble(Ask+(PointValue()*trailing*Point),Digits),OrderTakeProfit(),0,Red);
return(0);
}
}
}
}
}
//********************************************************************
//| Break Even Strategy |
//********************************************************************
void MoveBreakEven(int magicbuy, int magicsell, double breakeven, double breakpoint) {
int cnt,total=OrdersTotal();
RefreshRates();
for(cnt=0;cnt<total;cnt++)
{
OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES);
if( OrderType()<=OP_SELL && OrderSymbol()==Symbol() && (OrderMagicNumber()==magicbuy || OrderMagicNumber()==magicsell) )
{
if(OrderType()==OP_BUY)
{
if(breakeven>0)
{
if(NormalizeDouble((Bid-OrderOpenPrice()),Digits) > breakeven*PointValue()*Point)
{
if(NormalizeDouble((OrderStopLoss()-OrderOpenPrice()),Digits) < 0)
{
OrderModify(OrderTicket(),OrderOpenPrice(),NormalizeDouble(OrderOpenPrice()+breakpoint*PointValue()*Point,Digits),OrderTakeProfit(),0,Blue);
return(0);
}
}
}
}
else
{
if(breakeven>0)
{
if(NormalizeDouble((OrderOpenPrice()-Ask),Digits) > breakeven*PointValue()*Point)
{
if(NormalizeDouble((OrderOpenPrice()-OrderStopLoss()),Digits) < 0)
{
OrderModify(OrderTicket(),OrderOpenPrice(),NormalizeDouble(OrderOpenPrice()-breakpoint*PointValue()*Point,Digits),OrderTakeProfit(),0,Red);
return(0);
}
}
}
}
}
}
}
//********************************************************************
//| Find Last Buy Price Function |
//********************************************************************
double FindLastBuyPrice(int Magics) {
double ord_open_price;
int ord_ticket;
double buffers = 0;
int ticket_compair = -1;
for (int cnt = 0; cnt < OrdersTotal(); cnt++) {
OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
if (OrderSymbol() != Symbol() || OrderMagicNumber() != Magics) continue;
if (OrderSymbol() == Symbol() && OrderMagicNumber() == Magics) {
ord_ticket = OrderTicket();
if (ord_ticket > ticket_compair) {
ord_open_price = OrderOpenPrice();
buffers = ord_open_price;
ticket_compair = ord_ticket;
}
}
}
return (ord_open_price);
}
//********************************************************************
//| Find Last Sell Price Function |
//********************************************************************
double FindLastSellPrice(int Magics) {
double ord_open_price;
int ord_ticket;
double buffers = 0;
int ticket_compair = -1;
for (int cnt = 0; cnt < OrdersTotal(); cnt++) {
OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
if (OrderSymbol() != Symbol() || OrderMagicNumber() != Magics) continue;
if (OrderSymbol() == Symbol() && OrderMagicNumber() == Magics && OrderType() == OP_SELL) {
ord_ticket = OrderTicket();
if (ord_ticket > ticket_compair) {
ord_open_price = OrderOpenPrice();
buffers = ord_open_price;
ticket_compair = ord_ticket;
}
}
}
return (ord_open_price);
}
/*
//********************************************************************
//| Find Last Max Buy Price Function |
//********************************************************************
double FindLastMaxBuyPrice(int Magics) {
double ord_open_price=0;
double ord_compair = 0;
double buffers = 0;
for (int cnt = 0; cnt < OrdersTotal(); cnt++) {
OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
if (OrderSymbol() != Symbol() || OrderMagicNumber() != Magics) continue;
if (OrderSymbol() == Symbol() && OrderMagicNumber() == Magics) {
ord_compair = OrderOpenPrice();
if (ord_open_price < ord_compair) {
ord_open_price = ord_compair;
buffers = ord_open_price;
}
}
}
return (ord_open_price);
}
//********************************************************************
//| Find Last Min Sell Price Function |
//********************************************************************
double FindLastMinSellPrice(int Magics) {
double ord_open_price=10000;
double ord_compair = 10000;
double buffers = 0;
for (int cnt = 0; cnt < OrdersTotal(); cnt++) {
OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
if (OrderSymbol() != Symbol() || OrderMagicNumber() != Magics) continue;
if (OrderSymbol() == Symbol() && OrderMagicNumber() == Magics) {
ord_compair = OrderOpenPrice();
if (ord_open_price > ord_compair) {
ord_open_price = ord_compair;
buffers = ord_open_price;
}
}
}
return (ord_open_price);
}
//********************************************************************
//| Pending Buy Order Delete Function |
//********************************************************************
// Additional Buy Order 진입시 기존 Pending Buy Order 삭제
void DeletePendingBuyOrder(int magic) { // 0
for(int i=0;i<OrdersTotal();i++) { // 1
OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
if( OrderSymbol()==Symbol() && OrderMagicNumber()==magic && ( OrderType()==OP_BUYSTOP || OrderType()==OP_BUYLIMIT ) ) { // 3
OrderDelete(OrderTicket());
} // 3
} // 1
} // 0
//********************************************************************
//| Pending Sell Order Delete Function |
//********************************************************************
// Additional Sell Order 진입시 기존 Pending Sell Order 삭제
void DeletePendingSellOrder(int magic) { // 0
for(int i=0;i<OrdersTotal();i++) { // 1
OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
if( OrderSymbol()==Symbol() && OrderMagicNumber()==magic && ( OrderType()==OP_SELLSTOP || OrderType()==OP_SELLLIMIT ) ) { // 3
OrderDelete(OrderTicket());
} // 3
} // 1
} // 0
//********************************************************************
//| Pending Sell Limit Order Delete Function(가격비교후) |
//********************************************************************
void DeleteSellLimitOrder(int magic, double compair_price) { // 0
double ord_price;
double buffer = 0;
for(int i=0;i<OrdersTotal();i++) { // 1
OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
if (OrderSymbol() != Symbol() || OrderMagicNumber() != magic || OrderType() != OP_SELLLIMIT ) continue;
if( OrderSymbol()==Symbol() && OrderMagicNumber()==magic && OrderType()==OP_SELLLIMIT ) { // 2
ord_price = OrderOpenPrice();
if (ord_price < compair_price) { // 3
buffer = ord_price;
OrderDelete(OrderTicket());
} // 3
} // 2
} // 1
} // 0
//********************************************************************
//| Pending Buy Limit Order Delete Function(가격비교후) |
//********************************************************************
void DeleteBuyLimitOrder(int magic, double compair_price) { // 0
double ord_price;
double buffer = 0;
for(int i=0;i<OrdersTotal();i++) { // 1
OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
if (OrderSymbol() != Symbol() || OrderMagicNumber() != magic || OrderType() != OP_BUYLIMIT ) continue;
if( OrderSymbol()==Symbol() && OrderMagicNumber()==magic && OrderType()==OP_BUYLIMIT ) { // 2
ord_price = OrderOpenPrice();
if (ord_price > compair_price) { // 3
buffer = ord_price;
OrderDelete(OrderTicket());
} // 3
} // 2
} // 1
} // 0
*/
'IT관련' 카테고리의 다른 글
메타트레이더 EA - BaseCurrency_RSI 2. Reverse Version (0) | 2019.09.10 |
---|---|
메타트레이더 EA - BaseCurrency_RSI 1. 기본 버전 (0) | 2019.09.09 |
메타트레이더 EA - USDCHF_Reverse (0) | 2019.09.08 |
메타트레이더 EA 4 (2) | 2019.08.30 |
메타트레이더 EA 2 (0) | 2019.08.30 |