โค้ดโครงสร้างการเบิ้ลล๊อต
Sept 6, 2016 22:23:31 GMT 7
Post by iamgotzaa on Sept 6, 2016 22:23:31 GMT 7
//เพื่อทุกคนครับ
ท่าน Xa Em เปิดประเด็นมาได้ดีมาก กำลังอยากจะเขียนอยู่พอดี
โครงสร้าง ea ง่ายๆ
ในโค้ดข้างล่างนี้ ท่านจะได้เรียนรู้เรื่องดังต่อไปนี้
> การส่งออเดอร์ คำสั่ง OrderSend ข้อควรระวังสำหรับ ออเดอร์ buy and sell และ อื่นๆ
> OrderLot(), OrderType(), OrderSelect(), OrderOpenPrice(), OrderMagicNumber(), OrderSymbol()
> การหาออเดอร์สุดท้าย -เพิ่มเติม-> หาออเดอร์แรก หาออเดอร์ที่ต้องการได้
> โครงสร้างอีเอ
> คำสั่ง StringAdd เพื่อสร้างแถบข้อความแบบง่ายๆ ร่วมกับ Comment
> ดูรายละเอียดของ symbol แต่ะละตัวเป็นผ่าน คำสั่ง MarketInfo()
> แปลงค่าตัวเลข เป็นตัวอักษรได้ DoubleToString()
> แปลงราคาที่เป็นทศนิยมหลายๆตัวให้เป็น pips ได้อย่างสวยงาม
> วัดระยะว่าเราเสียหายไป pips ได้ แล้วแสดงออกมา
> สามารถพัฒนาไปทำ function trailing stop loss เหมือนใน mt4 ว่าให้ไล่ห่างจากราคาตลาดเท่าไร
> ป้องกันอาการเอ๋อของ server หลังจากส่งคำสั่งได้ด้วย IsTradeContextBusy() และ Sleep()
> ตัวอย่าง while loop อย่างง่าย
> ใช้ if จนคล่อง
//+------------------------------------------------------------------+
//| x2.mq4 |
//| Copyright 2016, MetaQuotes Software Corp. |
//| https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2016, MetaQuotes Software Corp."
#property link "https://www.mql5.com"
#property version "1.00"
#property strict
int LastTicket;
input int magic=0;
input int distanceToDoubleLot=50; //unit is in points
//+------------------------------------------------------------------+
//| Expert initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
//--- create timer
EventSetTimer(60);
//---
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Expert deinitialization function |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
//--- destroy timer
EventKillTimer();
}
//+------------------------------------------------------------------+
//| Expert tick function |
//+------------------------------------------------------------------+
void OnTick()
{
//---
string k="";
LastTicket=get_last_order(magic);
StringAdd(k,"last ticket="+IntegerToString(LastTicket));
trail(LastTicket);
if(OrderSelect(LastTicket,SELECT_BY_TICKET)==true)
{
double distance=0;
double mktquote=0;
if(OrderType()==OP_BUY)
{
//if distance < 0 then loss
distance=OrderOpenPrice()-Bid;
mktquote=Ask;
}
if(OrderType()==OP_SELL)
{
//if distance > 0 then loss
distance=Ask-OrderOpenPrice();
mktquote=Bid;
}
StringAdd(k," distance = "+DoubleToString(distance*MathPow(10,Digits-1),1)+" pips");
//if distance (loss) is more than 10 pips
if(distance>distanceToDoubleLot*Point)
{
//place new order
int t=OrderSend(Symbol(),OrderType(),2*OrderLots(),mktquote,2,0,0,"test ble",magic);
while(IsTradeContextBusy())
{
Sleep(100);
}
}
}
Comment(k);
}
//+------------------------------------------------------------------+
//| Timer function |
//+------------------------------------------------------------------+
void OnTimer()
{
//---
}
//+------------------------------------------------------------------+
//| ChartEvent function |
//+------------------------------------------------------------------+
void OnChartEvent(const int id,
const long &lparam,
const double &dparam,
const string &sparam)
{
//---
}
//+------------------------------------------------------------------+
void trail(int ticketno,double trailing_offset_pts=0)
{
double mkt=0;
double newstopprice=0;
int stoplevelPoints=0;
if(trailing_offset_pts==0)
{
trailing_offset_pts=(Ask-Bid)+2*Point;
}
if(!OrderSelect(ticketno,SELECT_BY_TICKET)==true)
{
stoplevelPoints=(int)MarketInfo(OrderSymbol(),MODE_STOPLEVEL);
if(OrderType()==OP_BUY)
{
newstopprice=Bid-trailing_offset_pts;
newstopprice=NormalizeDouble(newstopprice,(int)MarketInfo(OrderSymbol(),MODE_DIGITS));
if(newstopprice>=OrderOpenPrice() && (newstopprice==0 || newstopprice>OrderStopLoss()) )
{
int modok=OrderModify(OrderTicket(),OrderOpenPrice(),newstopprice,OrderTakeProfit(),OrderExpiration(),clrNONE);
}
}
//---
if(OrderType()==OP_SELL)
{
newstopprice=Ask-trailing_offset_pts;
newstopprice=NormalizeDouble(newstopprice,(int)MarketInfo(OrderSymbol(),MODE_DIGITS));
if(newstopprice<=OrderOpenPrice() && (newstopprice==0 || newstopprice<OrderStopLoss()))
{
int modok=OrderModify(OrderTicket(),OrderOpenPrice(),newstopprice,OrderTakeProfit(),OrderExpiration(),clrNONE);
}
}
}
}
//+------------------------------------------------------------------+
int get_last_order(int yourmagic,int type=-1,int mode=MODE_TRADES)
{
int orders_total = 0, ticket = -1;
string symbol = Symbol();
datetime opn_time = 0, ord_time = 0;
if(mode==MODE_HISTORY)
orders_total=OrdersHistoryTotal();
else
orders_total=OrdersTotal();
for(int i=0; i<orders_total; i++)
{
if(OrderSelect(i,SELECT_BY_POS,mode))
{
if(OrderSymbol()!=symbol)
continue;
if(OrderType()==type || type==-1)
{
if(OrderMagicNumber()==yourmagic || yourmagic==0)
{
if(mode==MODE_TRADES)
ord_time=OrderOpenTime();
else
ord_time=OrderCloseTime();
if(ord_time>opn_time)
{
opn_time=ord_time;
ticket=OrderTicket();
}
}
}
}
}//end for
return(ticket);
}