1. 입력 매개변수
#define ExtBotName "AK-47 스캘퍼 EA" //봇 이름 #define Version "1.00" //--- 입력 매개변수 설정 extern string EASettings = "---------------------------------------------"; //-------- <EA 설정> -------- input int InpMagicNumber = 124656; //매직 넘버 extern string TradingSettings = "---------------------------------------------"; //-------- <거래 설정> -------- input double Inpuser_lot = 0.01; //로트 수 input double InpSL_Pips = 3.5; //손절 (핍 단위) input double InpMax_spread = 0.5; //최대 허용 스프레드 (핍 단위) (0 = 플로팅) extern string MoneySettings = "---------------------------------------------"; //-------- <자금 설정> -------- input bool isVolume_Percent = true; //볼륨 비율 허용 input double InpRisk = 3; //계좌 잔고 대비 리스크 비율 (%) input string TimeSettings = "---------------------------------------------"; //-------- <거래 시간 설정> -------- input bool InpTimeFilter = true //거래 시간 필터 input int InpStartHour = 2; //시작 시간 input int InpStartMinute = 30; //시작 분 input int InpEndHour = 21; //종료 시간 input int InpEndMinute = 0 //종료 분
2. 로컬 변수 초기화
//--- 변수들 int Pips2Points; // 슬리피지 3 pips 3=points 30=points double Pips2Double; // 손절 15 pips 0.015 0.0150 int InpMax_slippage = 3; // 최대 슬리피지 허용 핍 수. bool isOrder = false; // 단 하나의 주문만 오픈 int slippage; string strComment = "";
3. 메인 코드
a/ 전문가 초기화 함수
int OnInit() { //--- //3 또는 5 자리 감지 //핍과 포인트 if (Digits % 2 == 1) { Pips2Double = _Point*10; Pips2Points = 10; slippage = 10* InpMax_slippage; } else { Pips2Double = _Point; Pips2Points = 1; slippage = InpMax_slippage; } //--- return(INIT_SUCCEEDED); }
b/ 전문가 틱 함수
void OnTick() { //--- if(IsTradeAllowed() == false) { Comment("AK-47 EA\n거래 불가."); return; } MqlDateTime structTime; TimeCurrent(structTime); structTime.sec = 0; //시작 시간 설정 structTime.hour = InpStartHour; structTime.min = InpStartMinute; datetime timeStart = StructToTime(structTime); //종료 시간 설정 structTime.hour = InpEndHour; structTime.min = InpEndMinute; datetime timeEnd = StructToTime(structTime); double acSpread = MarketInfo(Symbol(), MODE_SPREAD); StopLevel = MarketInfo(Symbol(), MODE_STOPLEVEL); strComment = "\n" + ExtBotName + " - v." + (string)Version; strComment += "\nGMT 시간 = " + TimeToString(TimeGMT(),TIME_DATE|TIME_SECONDS); strComment += "\n거래 시간 = [" + (string)InpStartHour + "h" + (string)InpStartMinute + " --> " + (string)InpEndHour + "h" + (string)InpEndMinute + "]"; strComment += "\n현재 스프레드 = " + (string)acSpread + " 포인트"; strComment += "\n현재 스톱레벨 = " + (string)StopLevel + " 포인트"; Comment(strComment); //값 업데이트 UpdateOrders(); TrailingStop(); //거래 시간 체크 if(InpTimeFilter) { if(TimeCurrent() >= timeStart && TimeCurrent() < timeEnd) { if(!isOrder) OpenOrder(); } } else { if(!isOrder) OpenOrder(); } }
3.1 신호를 계산하여 주문 전송
void OpenOrder(){ //int OrdType = OP_SELL;//-1; double TP = 0; double SL = 0; string comment = ExtBotName; //로트 수 계산 double lot1 = CalculateVolume(); //if(OrdType == OP_SELL){ double OpenPrice = NormalizeDouble(Bid - (StopLevel * _Point) - (InpSL_Pips/2) * Pips2Double, Digits); SL = NormalizeDouble(Ask + StopLevel * _Point + InpSL_Pips/2 * Pips2Double, Digits); if(CheckSpreadAllow()) //스프레드 체크 { if(!OrderSend(_Symbol, OP_SELLSTOP, lot1, OpenPrice, slippage, SL, TP, comment, InpMagicNumber, 0, clrRed)) Print(__FUNCTION__,"--> 주문 전송 오류 ",GetLastError()); } //} }
3.2 거래량 계산
double CalculateVolume() { double LotSize = 0; if(isVolume_Percent == false) { LotSize = Inpuser_lot; } else { LotSize = (InpRisk) * AccountFreeMargin(); LotSize = LotSize /100000; double n = MathFloor(LotSize/Inpuser_lot); //Comment((string)n); LotSize = n * Inpuser_lot; if(LotSize < Inpuser_lot) LotSize = Inpuser_lot; if(LotSize > MarketInfo(Symbol(),MODE_MAXLOT)) LotSize = MarketInfo(Symbol(),MODE_MAXLOT); if(LotSize < MarketInfo(Symbol(),MODE_MINLOT)) LotSize = MarketInfo(Symbol(),MODE_MINLOT); } return(LotSize); }
3.3 EA는 "트레일링 스탑" 기능이 있으며, 가격이 변할 때마다 SL이 변경됩니다.
void TrailingStop() { for(int i = OrdersTotal() - 1; i >= 0; i--) { if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) { if((OrderMagicNumber() == InpMagicNumber) && (OrderSymbol() == Symbol())) //_Symbol)) { //매도 주문의 경우 if(OrderType() == OP_SELL) { //--가격 변동 시 SL 계산 double SL_in_Pip = NormalizeDouble(OrderStopLoss() - (StopLevel * _Point) - Ask, Digits) / Pips2Double; if(SL_in_Pip > InpSL_Pips){ double newSL = NormalizeDouble(Ask + (StopLevel * _Point) + InpSL_Pips * Pips2Double, Digits); if(!OrderModify(OrderTicket(), OrderOpenPrice(), newSL, OrderTakeProfit(), 0, clrRed)) { Print(__FUNCTION__,"--> 주문 수정 오류 ",GetLastError()); { continue; } } //매도 스탑 주문의 경우 else if(OrderType() == OP_SELLSTOP) { double SL_in_Pip = NormalizeDouble(OrderStopLoss() - (StopLevel * _Point) - Ask, Digits) / Pips2Double; if(SL_in_Pip < InpSL_Pips/2){ double newOP = NormalizeDouble(Bid - (StopLevel * _Point) - (InpSL_Pips/2) * Pips2Double, Digits); double newSL = NormalizeDouble(Ask + (StopLevel * _Point) + (InpSL_Pips/2) * Pips2Double, Digits); if(!OrderModify(OrderTicket(), newOP, newSL, OrderTakeProfit(), 0, clrRed)) { Print(__FUNCTION__,"--> 미결 주문 수정 오류!", GetLastError()); continue; } } } } } }