EA Programing > ปัญหาที่พบในการเขียน EA
ช่วยแก้ปัญหา การกำหนด lots size ให้หน่อยครับ
(1/1)
gunlanutt:
สวัสดีครับ ทุกท่าน
ผมได้ทำการทดสอบอีเอตัวนี้มาหลายวันแล้ว และลองแก้ไขออย่างไรก็ไม่ได้ไม่รู้เป็นเพราะอะไรสักทีครับ จึงเรียนให้ท่าน ช่วยชี้แนะด้วยครับ
อีเอตัวนี้ทำงานโดยการเปิดออร์เดอร์เมื่อราคาตัดเส้น ema และตั้ง sl tp ปกติ เมื่อราคาวิ่งไปถูกทางก็จะเริ่มต้นใหม่ด้วย lots size ที่ตั้งต้น
หากผิดทางโดน sl ก็จะเปิดออร์เดอร์ใหม่ตามระบบ โดยที่ ใช้ lots size ตามกำหนดครับ
ปัญหามันอยู่ตรงที่ เมื่อไหร่ที่ราคาติดลบจนถึงออร์เดอร์ที่ lots size เป็น 0.35 ออร์เดอร์ถัดไป ต้องเป็น lots size = 0.45 แต่มันกลับไปเริ่มต้นที่ 0.2 (ค่าเริ่มต้น) ตลอดเลยครับ ผมแก้อย่างไรก็แก้ไม่ได้ครับ ท่าน ช่วยชี้แนะด้วยครับ
extern int MagicNumber = 101;
extern int TakeProfit = 400;
extern int Stoploss = 200;
extern double Lots = 0.2;
extern int TrailingStop = 200;
extern int EMA = 14;
int OpenOrders=0, cnt=0;
int mode=0, myOrderType=0;
int slippage=5;
double sl=0, tp=0;
double BuyPrice=0, SellPrice=0;
double mylotsi=0;
int B_bar,Bar ;
double EMA1, EMA0;
int ticket, order_Type;
datetime openTime, closeTime;
double orderSize, openPrice, closePrice, sf, tf, comm, swap, profit;
string order_Symbol, order_TypeToStr;
//+------------------------------------------------------------------+
//| expert initialization function |
//+------------------------------------------------------------------+
int init()
{
//----
//----
return(0);
}
//+------------------------------------------------------------------+
//| expert deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
//----
//----
return(0);
}
//+------------------------------------------------------------------+
//| expert start function |
//+------------------------------------------------------------------+
int start()
{
//----
for(int i=0;i<OrdersHistoryTotal();i++)
{
GetHistoryOrder(i,MODE_HISTORY);
}
OpenOrders=0;
for(cnt=0;cnt<OrdersTotal();cnt++)
{
int result1 = OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
if (OrderSymbol()==Symbol() && OrderMagicNumber()==MagicNumber)
{
OpenOrders++;
}
}
//---------------------------------------------------------------------------------------------------
Bar = iBars(NULL,0);
EMA1 = iMA(NULL,0,EMA,0,MODE_EMA,PRICE_CLOSE,1);
EMA0 = iMA(NULL,0,EMA,0,MODE_EMA,PRICE_CLOSE,0);
myOrderType=3;
if(Close[0]>=EMA0 && Close[1]<EMA1) {myOrderType=2;}
if(Close[0]<=EMA0 && Close[1]>EMA1) {myOrderType=1;}
Comment( " profit : ",profit," orderSize : ",orderSize);
//------------------------------------------------------Start Trailling Stop-------------------------------
for(cnt=OrdersTotal();cnt>=0;cnt--)
{
int result2 = OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
if (OrderSymbol() == Symbol() && OrderMagicNumber()==MagicNumber)
{
if (OrderType()==OP_SELL)
{
if (OrderOpenPrice()-Ask>=TrailingStop*Point)
{
if (OrderStopLoss()!= OrderOpenPrice() && TrailingStop!=0)
{
bool tic1 = OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice(),OrderTakeProfit(),800,Purple);
return(0);
}
}
}
if (OrderType()==OP_BUY)
{
if (Bid-OrderOpenPrice()>=TrailingStop*Point)
{
if (OrderStopLoss()!= OrderOpenPrice() && TrailingStop!=0)
{
bool tic2 = OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice(),OrderTakeProfit(),800,Yellow);
return(0);
}
}
}
}
}
//------------------------------------------------------End Trailling Stop-------------------------------
if (OpenOrders<1)
{
if (myOrderType==2)
{
BuyPrice=Ask;
if (TakeProfit==0) { tp=0; }
else { tp=BuyPrice+TakeProfit*Point; }
if (Stoploss==0) { sl=0; }
else { sl=BuyPrice-Stoploss*Point; }
mylotsi=Lots;
if (openPrice!=0)
{
for(cnt=OrdersTotal();cnt>=0;cnt--)
{
int result3 = OrderSelect(cnt, SELECT_BY_POS, MODE_HISTORY);
if (OrderSymbol()==Symbol() && OrderMagicNumber()==MagicNumber)
{
if (closePrice == sf)
{
if(orderSize==0.20 && profit==0) { mylotsi = 0.20; }
if(orderSize==0.20 && profit<0 ) { mylotsi = 0.21; }
if(orderSize==0.21 && profit<=0) { mylotsi = 0.21; }
if(orderSize==0.21 && profit<0) { mylotsi = 0.30; }
if(orderSize==0.30 && profit<=0) { mylotsi = 0.30; }
if(orderSize==0.30 && profit<0) { mylotsi = 0.35; }
if(orderSize==0.35 && profit<=0) { mylotsi = 0.35; }
if(orderSize==0.35 && profit<0) { mylotsi = 0.45; }
if(orderSize==0.45 && profit<=0) { mylotsi = 0.45; }
if(orderSize==0.45 && profit<0) { mylotsi = 0.60; }
if(orderSize==0.60 && profit<=0) { mylotsi = 0.60; }
if(orderSize==0.60 && profit<0) { mylotsi = 0.85; }
if(orderSize==0.85 && profit<=0) { mylotsi = 0.85; }
if(orderSize==0.85 && profit<0) { mylotsi = 1.25; }
if(orderSize==1.25 && profit<=0) { mylotsi = 1.25; }
if(orderSize==1.25 && profit<0) { mylotsi = 1.90; }
if(orderSize==1.90 && profit<=0) { mylotsi = 1.90; }
if(orderSize==1.90 && profit<0) { mylotsi = 2.95; }
} else { mylotsi=Lots; }
}
}
}
int ticket1 = OrderSend(Symbol(),OP_BUY,mylotsi,BuyPrice,slippage,sl,tp,NULL,MagicNumber,0,Blue);
return(0);
}
if (myOrderType==1)
{
SellPrice=Bid;
if (TakeProfit==0) { tp=0; }
else { tp=SellPrice-TakeProfit*Point; }
if (Stoploss==0) { sl=0; }
else { sl=SellPrice+Stoploss*Point; }
mylotsi=Lots;
if (openPrice!=0)
{
for(cnt=0;cnt<=OpenOrders;cnt++)
{
int result4 = OrderSelect(cnt, SELECT_BY_POS, MODE_HISTORY);
if (OrderSymbol()==Symbol() && OrderMagicNumber()==MagicNumber)
{
if (closePrice == sf)
{
if(orderSize==0.20 && profit==0) { mylotsi = 0.20; }
if(orderSize==0.20 && profit<0 ) { mylotsi = 0.21; }
if(orderSize==0.21 && profit<=0) { mylotsi = 0.21; }
if(orderSize==0.21 && profit<0) { mylotsi = 0.30; }
if(orderSize==0.30 && profit<=0) { mylotsi = 0.30; }
if(orderSize==0.30 && profit<0) { mylotsi = 0.35; }
if(orderSize==0.35 && profit<=0) { mylotsi = 0.35; }
if(orderSize==0.35 && profit<0) { mylotsi = 0.45; }
if(orderSize==0.45 && profit<=0) { mylotsi = 0.45; }
if(orderSize==0.45 && profit<0) { mylotsi = 0.60; }
if(orderSize==0.60 && profit<=0) { mylotsi = 0.60; }
if(orderSize==0.60 && profit<0) { mylotsi = 0.85; }
if(orderSize==0.85 && profit<=0) { mylotsi = 0.85; }
if(orderSize==0.85 && profit<0) { mylotsi = 1.25; }
if(orderSize==1.25 && profit<=0) { mylotsi = 1.25; }
if(orderSize==1.25 && profit<0) { mylotsi = 1.90; }
if(orderSize==1.90 && profit<=0) { mylotsi = 1.90; }
if(orderSize==1.90 && profit<0) { mylotsi = 2.95; }
} else { mylotsi=Lots; }
}
}
}
int ticket2 = OrderSend(Symbol(),OP_SELL,mylotsi,SellPrice,slippage,sl,tp,NULL,MagicNumber,0,Red);
return(0);
}
}
//-------------------------------------Cut OrderClose-----------------------------
//----
return(0);
}
//+------------------------------------------------------------------+
void GetHistoryOrder(int count,int mod)
{
for(int x=0;x<OrdersHistoryTotal();x++)
{
if(OrderSelect(count, SELECT_BY_POS, mod)==true)
{
mode=OrderType();
if (OrderSymbol()==Symbol())
{
ticket=OrderTicket();
order_Type=OrderType();
openTime=OrderOpenTime();
closeTime=OrderCloseTime();
orderSize=OrderLots();
openPrice=OrderOpenPrice();
closePrice=OrderClosePrice();
sf=OrderStopLoss();
tf=OrderTakeProfit();
comm=OrderCommission();
swap=OrderSwap();
profit=OrderProfit();
order_Symbol=OrderSymbol();
}
else
Print("OrderSelect returned the error of ",GetLastError());
}}
}
//+------------------------------------------------------------------+//
admin:
ดูผ่านๆ code ไม่น่าผิด แต่ถามหน่อย 0.2 นี้คือกลับไปค่าแรกกรณีเป็นบวกใช่ไหม ถ้าใช่ลองไปดูว่า ที่ติดลบนี้ติดลบเพราะค่า commission หรือว่าขนาดทุนจริงๆ เพราะเงื่อนไขจะเป็น 0.2 ได้คือ else { mylotsi=Lots; } เท่านั่น
gunlanutt:
--- Quote from: admin on มีนาคม 21, 2019, 12:53:01 pm ---ดูผ่านๆ code ไม่น่าผิด แต่ถามหน่อย 0.2 นี้คือกลับไปค่าแรกกรณีเป็นบวกใช่ไหม ถ้าใช่ลองไปดูว่า ที่ติดลบนี้ติดลบเพราะค่า commission หรือว่าขนาดทุนจริงๆ เพราะเงื่อนไขจะเป็น 0.2 ได้คือ else { mylotsi=Lots; } เท่านั่น
--- End quote ---
เรียน ท่าน admin
0.2 เป็นค่าแรก กรณีที่กำไรเป็นบวกครับ
และเท่าที่เบ็คเทสมันปิดขาดทุน sl ติดลบ เพราะราคาชน sl แต่ดันไปตั้ง lots size ที่เริ่มต้นครับ ผมแนบรูปภาพมาให้ดูด้วยครับ
จากรูปจะเห็นว่า ออร์เดอร์ที่ 44 เปิดล็อตที่ 0.35 และราคาชน sl ทำให้ติดลบ ตามระบบมันควรจะเข้าล็อตต่อไปที่ 0.45 แต่มันดันเข้าออร์เดอร์ถัดไปที่ 0.2 เหมือนทุกครั้งครับ
ท่านช่วยชี้แนะด้วยนะครับ ขอบพระคุณเป็นอย่างสูงครับ
gunlanutt:
ผมเข้ามาดู Admin และผู้ใจดีช่วยตอบทุกวันเลยนะครับ ขอบคุณมากมากครับ
นำร่อง
[0] ดัชนีข้อความ
Go to full version