MetaTrader4
AK-47 Scalper EA: เครื่องมือทำกำไรสำหรับ MetaTrader 4
1. พารามิเตอร์การตั้งค่า
#define ExtBotName "AK-47 Scalper EA" // ชื่อบอท
#define Version "1.00"
//--- พารามิเตอร์การตั้งค่า
extern string EASettings = "---------------------------------------------"; //-------- <การตั้งค่าบอท> --------
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 พิปส์ 3=points 30=points
double Pips2Double; // หยุดขาดทุน 15 พิปส์ 0.015 0.0150
int InpMax_slippage = 3; // สูงสุดที่อนุญาตการเลื่อน_Pips.
bool isOrder = false; // เปิด 1 คำสั่งเท่านั้น
int slippage;
string strComment = "";
3. โค้ดหลัก
a/ ฟังก์ชันการเริ่มต้นของ Expert
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/ ฟังก์ชันการติกของ Expert
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 time = " + TimeToString(TimeGMT(),TIME_DATE|TIME_SECONDS);
strComment += "\nเวลาเทรด = [" + (string)InpStartHour + "h" + (string)InpStartMinute + " --> " + (string)InpEndHour + "h" + (string)InpEndMinute + "]";
strComment += "\nสเปรดปัจจุบัน = " + (string)acSpread + " Points";
strComment += "\nระดับหยุดปัจจุบัน = " + (string)StopLevel + " Points";
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 มีฟังก์ชัน "Trailing Stop" 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;
}
}
//สำหรับคำสั่ง SellStop
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;
}
}
}
}
}
}
2023.01.14