คือพยายามให้ กำหนดระยะห่างจุด Pending Order และเมื่อเปิด Buyให้ปิดSell และ SellStop และเมื่อเปิด Sell ให้ปิดBuy และ BuyStop แต่ตอนนี้มันปิดเฉพาะ Order ตรงข้ามแต่ไม่ปิด Pending Order และก็วางระยะห่างของPending order ไม่ได้ครับ
//+------------------------------------------------------------------+
//| O_BuyStop&O_SellStop.mq4 |
//| Akalak |
//|
https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Akalak"
#property link "
https://www.mql5.com"
#property version "3.00"
#property strict
//+------------------------------------------------------------------+
//| Expert initialization function |
//+------------------------------------------------------------------+
extern int MagicNumber = 1234;
extern double Lotsize = 0.01;
extern int SL = 200;
extern int TP = 1000;
extern bool UseMoveToBreakevent = True;
extern int WhenTomoveToBe = 110;
extern int PipsToLockIn = 30;
extern bool UseTraillingStop = True;
extern int WhenToTrail = 110;
extern int TrailAmount = 110;
extern int MaxOrder = 5;
extern int Distance = 110;
extern int OrderDistand = 20;
extern bool UseDeletePending = True;
//--------------------------------------------------------------------------------
double Fma,Mma,Sma,FmaSh,MmaSh,SmaSh;
void GetMA(){
Fma = iMA(Symbol(),PERIOD_H4,5,0,MODE_EMA,PRICE_WEIGHTED,0);
// Mma = iMA(Symbol(),0,20,0,MODE_EMA,PRICE_WEIGHTED,0);
Sma = iMA(Symbol(),PERIOD_H4,20,0,MODE_EMA,PRICE_WEIGHTED,0);
// FmaSh = iMA(Symbol(),0,5,0,MODE_EMA,PRICE_WEIGHTED,1);
// MmaSh = iMA(Symbol(),0,20,0,MODE_EMA,PRICE_WEIGHTED,1);
// SmaSh = iMA(Symbol(),0,50,0,MODE_EMA,PRICE_WEIGHTED,1);
}
//-------------------------------------------------------------------------
void OpenBuy(){
double sl = Ask-SL*Point;
double tp = Ask+TP*Point;
OrderSend(Symbol(),OP_BUYSTOP, Lotsize, Ask+ Distance*Point, 3, Ask+ Distance*Point-SL*Point,Ask+Distance*Point+TP*Point, "", MagicNumber, 0, Blue);
if(OrdersTotal()>0){
double LastOpenPrice;
OrderSelect(OrdersTotal()-1,SELECT_BY_POS,MODE_TRADES);
LastOpenPrice = OrderOpenPrice();
if( LastOpenPrice-Ask>Point*OrderDistand && LastOpenPrice-Ask<Point*OrderDistand ){
OrderSend(Symbol(),OP_BUYSTOP, Lotsize, Ask+ Distance*Point, 3, Ask+ Distance*Point-SL*Point,Ask+Distance*Point+TP*Point, "", MagicNumber, 0, Blue);
}
}
}
//---------------------------------------------------------------------------------------------
void OpenSell(){
double sl = Bid+SL*Point;
double tp = Bid-TP*Point;
OrderSend(Symbol(), OP_SELLSTOP, Lotsize, Bid-Distance*Point, 3, Bid- Distance*Point+SL*Point,Bid-Distance*Point-TP*Point, "", MagicNumber, 0, Red);
if(OrdersTotal()>0){
double LastOpenPrice;
OrderSelect(OrdersTotal()-1,SELECT_BY_POS,MODE_TRADES);
LastOpenPrice = OrderOpenPrice();
if( LastOpenPrice-Bid>Point*OrderDistand && LastOpenPrice-Bid<Point*OrderDistand ){
OrderSend(Symbol(),OP_SELLSTOP, Lotsize, Ask+ Distance*Point, 3, Ask+ Distance*Point-SL*Point,Ask+Distance*Point+TP*Point, "", MagicNumber, 0, Blue);
}
}
}
//----------------------------------------------------------------------------------------------------------
void Delete(){
int total = OrdersTotal();
for(int i = 0; i < total ; i ++){
OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
int type = OrderType();
bool result = false;
switch(type){
case OP_BUYSTOP:
result = OrderDelete(OrderTicket());
break;
case OP_SELLSTOP:
result = OrderDelete(OrderTicket());
break;
}
}
}
//----------------------------------------------------------------------------------------------------------
void CloseOrderAll(int type){
for(int i= OrdersTotal()-1;i>=0;i--){
if(OrderSelect(i,SELECT_BY_POS)==True){
if(OrderMagicNumber() == MagicNumber && OrderSymbol()== Symbol()){
switch(type){
case OP_BUY:
OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_BID),0,0);
break;
case OP_SELL:
OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_ASK),0,0);
break;
}
}
}
}
}
int CountOrder(int type){
int CntOrder = 0;
for(int i=MaxOrder;i<OrdersTotal();i++){
OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
if(OrderType() == type && OrderMagicNumber() == MagicNumber){
if(OrderSymbol() == Symbol()){
CntOrder++;
}
}
}
return(CntOrder);
}
//--------------------------------------------------------------------------------------
void OnTick(){
if(UseMoveToBreakevent)MoveToBreakevent();
if(UseTraillingStop)AdjustTrail();
GetMA();
if(CountOrder(OP_BUY)==0 ){
if(Fma > Sma ){
OpenBuy();
CloseOrderAll(OP_SELL);
}
}
if(CountOrder(OP_SELL)==0 ){
if(Fma < Sma ){
OpenSell();
CloseOrderAll(OP_BUY);
}
}
}
//+-------------------------------------------------------------------------------------
void MoveToBreakevent(){
for(int i=OrdersTotal()-1;i>=0;i--){
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)){
if(OrderMagicNumber() == MagicNumber){
if(OrderSymbol() == Symbol()){
if(OrderType() == OP_BUY){
if(Bid-OrderOpenPrice() > WhenTomoveToBe*Point ){
if(OrderOpenPrice() > OrderStopLoss()){
OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice()+PipsToLockIn*Point,OrderTakeProfit(),0,Green);
}
}
}
if(OrderType() == OP_SELL){
if(OrderOpenPrice()-Ask > WhenTomoveToBe*Point){
if(OrderOpenPrice() < OrderStopLoss()){
OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice()-PipsToLockIn*Point,OrderTakeProfit(),0,Red);
}
}
}
}
}
}
}
}
//------------------------------------------------------------------------------------------
void AdjustTrail(){
for(int i=OrdersTotal()-1;i>=0;i--){
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)){
if(OrderMagicNumber() == MagicNumber){
if(OrderSymbol() == Symbol()){
if(OrderType() == OP_BUY){
if(Bid-OrderOpenPrice() > WhenToTrail*Point){
if(OrderStopLoss()<Bid-TrailAmount*Point){
OrderModify(OrderTicket(),OrderOpenPrice(),Bid-TrailAmount*Point,OrderTakeProfit(),0,0);
}
}
}
if(OrderType() == OP_SELL){
if(OrderOpenPrice()- Ask > WhenToTrail*Point){
if(OrderStopLoss()>Ask+TrailAmount*Point){
OrderModify(OrderTicket(),OrderOpenPrice(),Ask+TrailAmount*Point,OrderTakeProfit(),0,0);
}
}
}
}
}
}
}
}
//------------------------------------------------------------------------