หลักการก็คือ ผมเปิดออร์เดอร์ buystop && sellstop มาที่ high && low ตั้ง tp 1 เท่า sl อยู่ที่ราคาของอีกฝั่ง เมื่อฝั่งบายปิด tp ไป ก็อยากให้ปิด sellstop ไปด้วยของคู่มันเอง ผมก็เลยใส่คอมเม้นของคู่มันเป็น tp เพื่อตอนปิดจะได้เช็คว่าปิด tp นี้ ใช่คู่ออร์เดอร์ของมันหรือเปล่า ปัญหาก็คือเช็คได้หมดแล้ว เงื่อนไขเป็นจริงแล้ว แต่ตอนจะปิดมันไม่ใช่ orderSellstop orderticket มันพุ่งไปแต่ออร์เดอร์บายที่เพิ่งปิดไปครับ มันจึงไม่ยอมปิดให้ ลองมาหลายวิธีแล้วครับ
#property strict
//+------------------------------------------------------------------+
//| Expert initialization function |
//+------------------------------------------------------------------+
input string EA_Name = "inside bar breakout";
input int MagicNumber = 111;
input double TakeProfit = 1.0;
input bool UseRisk = false;
input double Risk = 1; //Risk ($)
input double FixLots = 0.01; //Fix Lots
input ENUM_TIMEFRAMES TimeFrame = PERIOD_CURRENT;
input string ___DoubleInsideBarSetup___ = "=============== Double Inside Bar Setup ===============";
input int MaxMotherBar = 300;
input int PointPending = 50;
input bool AlertOn = true;
int slippage = 0;
int OpenOrders=0, cnt=0;
int mode=0, myOrderType=0;
int CountB, CountS;
int CountBS, CountSS;
int result, ticket;
int CountBar, LastBar;
double BuyPrice=0, SellPrice=0;
double mylotsi=0;
double Profit;
double TotalProfitBuy, TotalProfitSell;
double B_Lot, S_Lot;
bool res;
int XDISTANCE;
int YDISTANCE;
int Bar1;
int NumberTime = 3;
int Ord_BS[100], Ord_SS[100];
string TimeBar[3] = {"00:05","00:35","01:40"};
string Comm_BS[100], Comm_SS[100];
double StopLevel;
double H1, H2, L1, L2;
double MiniLots, rangSL;
double tpBuy, tpSell;
double slBuy, slSell;
bool Condition;
int OnInit()
{
//---
string expire = "2020.08.31";
datetime time = StrToTime(expire);
if (TimeCurrent() >= time)
{
Comment(" expired on ",expire," Please Contact Admin!!!");
ExpertRemove();
}else{Comment("");}
//---
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Expert deinitialization function |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
//---
}
//+------------------------------------------------------------------+
//| Expert tick function |
//+------------------------------------------------------------------+
int start()
{
//---
//----------------------------
//----------------------------
OpenOrders = 0;
CountB = 0;
CountS = 0;
CountBS = 0;
CountSS = 0;
Profit = 0;
TotalProfitBuy = 0;
TotalProfitSell = 0;
for(cnt=0;cnt<OrdersTotal();cnt++)
{
result = OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
if (OrderSymbol()==Symbol() && OrderMagicNumber()==MagicNumber)
{
OpenOrders++;
Profit = Profit+OrderProfit();
if (OrderType()==OP_SELL )
{
CountS++;
TotalProfitSell += OrderProfit();
}
if (OrderType()==OP_BUY )
{
CountB++;
TotalProfitBuy += OrderProfit();
}
if (OrderType()==OP_BUYSTOP)
{
CountBS ++;
Comm_BS[CountBS] = OrderComment();
Ord_BS[CountBS] = OrderTicket();
}
if (OrderType()==OP_SELLSTOP)
{
CountSS ++;
Comm_SS[CountSS] = OrderComment();
Ord_SS[CountBS] = OrderTicket();
}
}
}
//------------------------------------------------------------------------------------
CountBar = iBars(NULL,0);
StopLevel = MarketInfo(Symbol(),MODE_STOPLEVEL);
MiniLots = MarketInfo(Symbol(),MODE_MINLOT);
for(int i=0;i<NumberTime;i++)
{
Condition = False;
Bar1 = iBarShift(Symbol(),TimeFrame,StrToTime(TimeBar));
if(Bar1==1)
{
H1 = iHigh(NULL,TimeFrame,1);
H2 = iHigh(NULL,TimeFrame,2);
L1 = iLow(NULL,TimeFrame,1);
L2 = iLow(NULL,TimeFrame,2);
if(H1<H2 && L1>L2 && H2-L2<=MaxMotherBar*Point)
{
Condition = true;
break;
}
//Print(" H1 : ",H1," L1 : ",L1);
}
//Print(i," Bar : ",Bar1);
}
//------------------------------------------------------------------------------------
//-------------------------------------------------------------------------------------------------------
//-------------------------------------------------------------------------------------------------------
if(Bid>(L2-PointPending*Point)+StopLevel*Point && Ask<(H2+PointPending*Point)-StopLevel*Point)
{
if (Condition && LastBar!=CountBar)
{
SellPrice= L2-PointPending*Point;
LastBar = CountBar;
slSell = H2+PointPending*Point;
tpSell = SellPrice-((slSell-SellPrice)*TakeProfit);
BuyPrice= H2+PointPending*Point;
slBuy = L2-PointPending*Point;
tpBuy = BuyPrice+((BuyPrice-slBuy)*TakeProfit);
mylotsi = FixLots;
if (UseRisk)
{
rangSL = (BuyPrice-slBuy)/Point;
mylotsi = NormalizeDouble(Risk/rangSL,2);
if(mylotsi<MiniLots){ mylotsi = MiniLots;}
}
ticket = OrderSend(Symbol(),OP_SELLSTOP,mylotsi,SellPrice,slippage,slSell,tpSell,DoubleToStr(tpBuy,Digits) ,MagicNumber,0,clrRed);
ticket = OrderSend(Symbol(),OP_BUYSTOP ,mylotsi,BuyPrice ,slippage,slBuy ,tpBuy ,DoubleToStr(tpSell,Digits),MagicNumber,0,clrBlue);
if(AlertOn){Alert(Symbol()," Open Inside bar : TimeFrame ",Timefram(TimeFrame));}
return(0);
}
}
//----------------------------------------------------------
if(CountSS>0)
{
for(int i=0;i<OrdersTotal();i++)
{
if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES)==false) break;
if (OrderSymbol()==Symbol() && OrderMagicNumber()==MagicNumber && CheckOrderClose(OP_BUY)== StrToDouble(Comm_SS) && CheckOrderClose(OP_BUY)!=0.0)
{
if (OrderType()==OP_SELLSTOP) {res = OrderDelete(OrderTicket(),clrDarkRed); Print(" error : ",GetLastError());}
}
}
}
if(CountBS>0)
{
for(int i=0;i<OrdersTotal();i++)
{
if (Symbol()==OrderSymbol() && OrderMagicNumber()==MagicNumber && CheckOrderClose(OP_SELL)== StrToDouble(CheckOrderComment(OP_BUYSTOP)))
{
if (OrderType()==OP_BUYSTOP) {res = OrderDelete(OrderTicket(),clrDarkBlue);}
}
}
}
//----------------------------------------------------------
//----------------------------------------------------------
//+-------------------------
//+-------------------------
return(0);
}
//+------------------------------------------------------------------+//
//---------------------------------------------------
string Timefram(int TimeF)
{
switch (TimeF)
{
case 1: return("M1");
case 5: return("M5");
case 15: return("M15");
case 30: return("M30");
case 60: return("H1");
case 240: return("H4");
case 1440: return("D1");
case 10080: return("W1");
case 43200: return("MN");
default: return("Current");
}
}
//+------------------------------------------------------------------+
//| Check Last OrderClose Price |
//+------------------------------------------------------------------+
double CheckOrderClose(int type)
{
double order=0;
//---
for(int i=0;i<OrdersHistoryTotal();i++)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)==false) break;
if(OrderSymbol()==Symbol() && OrderMagicNumber()==MagicNumber && OrderType()==type)
order = OrderClosePrice();
}
//--- return Order Close Price
return(order);
}
string CheckOrderComment(int type)
{
string order;
//---
for(int i=0;i<OrdersTotal();i++)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) break;
if(OrderSymbol()==Symbol() && OrderMagicNumber()==MagicNumber && OrderType()==type)
order = OrderComment();
}
//--- return Order Close Price
return(order);
}