haibgfx
Active Member
- 108
- 142
Dưới đây là thuật toán quản lý việc đặt 1 lệnh trên mỗi nến, các bạn tham khảo. Và biến đổi code cho phù hợp với nhu cầu của mình.
//-----------------------------------------------------------------------------------------
//------- dieu kien de dat lenh quan ly 1 lenh tren 1 nen-------------------
int total = OrdersTotal();
for ( int pos = 0; pos <= total; pos++ )
{
if ( OrderSelect( pos, SELECT_BY_POS ) == true )
{
//----------------------lenh thuc hien khac ngay-------------------------
if ( OrderSymbol() == "AUDUSD" && (TimeDay(TimeCurrent()) != TimeDay (OrderOpenTime())) )
{
if ( OrderType() == OP_BUY )
n01 = true;
if ( OrderType() == OP_SELL )
n11 = true;
}
if ( OrderSymbol() == "EURUSD" && (TimeDay(TimeCurrent()) != TimeDay (OrderOpenTime())) )
{
if ( OrderType() == OP_BUY )
n02 = true;
if ( OrderType() == OP_SELL )
n12 = true;
}
//--------------------lenh thuc hien cung ngay------------------------------
if ( OrderSymbol() == "AUDUSD" && (TimeDay(TimeCurrent()) == TimeDay (OrderOpenTime())) )
{
n21 = true;
}
if ( OrderSymbol() == "EURUSD" && (TimeDay(TimeCurrent()) == TimeDay (OrderOpenTime())) )
{
n22 = true;
}
}
}
//--------su dung history de quan ly truong hop lenh closed trong cung nen--------
int histTotal = OrdersHistoryTotal();
for(int pos_his =0 ; pos_his < histTotal ; pos_his++)
{
if ( OrderSelect(pos_his,SELECT_BY_POS,MODE_HISTORY) == true )
{
if ( OrderSymbol() == "AUDUSD" && (TimeDay(TimeCurrent()) == TimeDay (OrderOpenTime())) )
{
n31 = true;
}
if ( OrderSymbol() == "EURUSD" && (TimeDay(TimeCurrent()) == TimeDay (OrderOpenTime())) )
{
n32 = true;
}
}
//---- check selection result
if(OrderSelect(pos_his,SELECT_BY_POS,MODE_HISTORY)==false)
{
Print("Access to history failed with error (",GetLastError(),")");
break;
}
// some work with order
}
//-------------------------------- dat lenh-------------------------------------------------
if ( ( buy_condition == true && Symbol() == "AUDUSD" && n11 == false && n21 == false && n31 == false ) ||
( buy_condition == true && Symbol() == "EURUSD" && n12 == false && n22 == false && n32 == false ) )
{
if ( BarClose3 - BarLow3 < atr*1.5 )
{
ticket = OrderSend(Symbol(),OP_BUY,Lots,Ask, 3, BarLow3 - 50*Point, Ask + (Ask - BarLow3 + 50*Point)*2.1,"My EA",1245,0,Green); // buy order place
}
else
{
ticket = OrderSend(Symbol(),OP_BUY,Lots,Ask, 3, Ask - atr*1.5, Ask + atr*1.5*2.1,"My EA",1245,0,Green); // buy order pl
}
if(ticket>0)
{
if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES))
{
Print("BUY order opened : ",OrderOpenPrice());
return (0);
}
}
else
{
Print("Error opening BUY order : ",GetLastError());
return(0);
}
}
if ( ( sell_condition == true && Symbol() == "AUDUSD" && n01 == false && n21 == false && n31 == false ) ||
( sell_condition == true && Symbol() == "EURUSD" && n02 == false && n22 == false && n32 == false ) )
{
if ( BarHigh3 - BarClose3 < atr*1.5)
{
ticket_1 = OrderSend(Symbol(),OP_SELL,Lots, Bid, 3, BarHigh3 + 50*Point, Bid - ( BarHigh3 + 50*Point - Bid)*2.1 ,"My EA",12345,0,Red);
}
else
{
ticket_1 = OrderSend(Symbol(),OP_SELL,Lots, Bid, 3, Bid + atr*1.5, Bid - atr*1.5*2.1 ,"My EA",12345,0,Red);
}
if(ticket_1 > 0)
{
if(OrderSelect(ticket_1,SELECT_BY_TICKET,MODE_TRADES))
{
Print("SELL order opened : ",OrderOpenPrice());
return (0);
}
}
else
{
Print("Error opening SELL order : ",GetLastError());
return(0);
}
}
//-----------------------------------------------------------------------------------------------
Các bước cần kiểm soát khi xử lý của thuật toán gồm:
- Bước 1: Kiểm tra xem có lệnh đã đặt là lệnh đó đang tồn tại. Trong code thì mình sử dụng 2 biến quản lý (do mình chỉ code ví dụ trên 02 cặp ngoại tệ): n21, n22.
- Bước 2: Kiểm tra xem có lệnh đã đặt và lệnh đó đã CLOSED. Trong code mình sử dụng 2 biến quản lý: n31, n32.
Code trên mình viết trên khung thời gian D1, cho 2 cặp ngoại tệ. Các bạn tùy biến sử dụng hàm Period() và gia tăng số biến cho phù hợp với nhu cầu code trên nhiều khung thời gian và nhiều cặp ngoại tệ.
https://traderviet.org/members/tran-thanh-khoi.34982/ tham khảo nhá.
//-----------------------------------------------------------------------------------------
//------- dieu kien de dat lenh quan ly 1 lenh tren 1 nen-------------------
int total = OrdersTotal();
for ( int pos = 0; pos <= total; pos++ )
{
if ( OrderSelect( pos, SELECT_BY_POS ) == true )
{
//----------------------lenh thuc hien khac ngay-------------------------
if ( OrderSymbol() == "AUDUSD" && (TimeDay(TimeCurrent()) != TimeDay (OrderOpenTime())) )
{
if ( OrderType() == OP_BUY )
n01 = true;
if ( OrderType() == OP_SELL )
n11 = true;
}
if ( OrderSymbol() == "EURUSD" && (TimeDay(TimeCurrent()) != TimeDay (OrderOpenTime())) )
{
if ( OrderType() == OP_BUY )
n02 = true;
if ( OrderType() == OP_SELL )
n12 = true;
}
//--------------------lenh thuc hien cung ngay------------------------------
if ( OrderSymbol() == "AUDUSD" && (TimeDay(TimeCurrent()) == TimeDay (OrderOpenTime())) )
{
n21 = true;
}
if ( OrderSymbol() == "EURUSD" && (TimeDay(TimeCurrent()) == TimeDay (OrderOpenTime())) )
{
n22 = true;
}
}
}
//--------su dung history de quan ly truong hop lenh closed trong cung nen--------
int histTotal = OrdersHistoryTotal();
for(int pos_his =0 ; pos_his < histTotal ; pos_his++)
{
if ( OrderSelect(pos_his,SELECT_BY_POS,MODE_HISTORY) == true )
{
if ( OrderSymbol() == "AUDUSD" && (TimeDay(TimeCurrent()) == TimeDay (OrderOpenTime())) )
{
n31 = true;
}
if ( OrderSymbol() == "EURUSD" && (TimeDay(TimeCurrent()) == TimeDay (OrderOpenTime())) )
{
n32 = true;
}
}
//---- check selection result
if(OrderSelect(pos_his,SELECT_BY_POS,MODE_HISTORY)==false)
{
Print("Access to history failed with error (",GetLastError(),")");
break;
}
// some work with order
}
//-------------------------------- dat lenh-------------------------------------------------
if ( ( buy_condition == true && Symbol() == "AUDUSD" && n11 == false && n21 == false && n31 == false ) ||
( buy_condition == true && Symbol() == "EURUSD" && n12 == false && n22 == false && n32 == false ) )
{
if ( BarClose3 - BarLow3 < atr*1.5 )
{
ticket = OrderSend(Symbol(),OP_BUY,Lots,Ask, 3, BarLow3 - 50*Point, Ask + (Ask - BarLow3 + 50*Point)*2.1,"My EA",1245,0,Green); // buy order place
}
else
{
ticket = OrderSend(Symbol(),OP_BUY,Lots,Ask, 3, Ask - atr*1.5, Ask + atr*1.5*2.1,"My EA",1245,0,Green); // buy order pl
}
if(ticket>0)
{
if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES))
{
Print("BUY order opened : ",OrderOpenPrice());
return (0);
}
}
else
{
Print("Error opening BUY order : ",GetLastError());
return(0);
}
}
if ( ( sell_condition == true && Symbol() == "AUDUSD" && n01 == false && n21 == false && n31 == false ) ||
( sell_condition == true && Symbol() == "EURUSD" && n02 == false && n22 == false && n32 == false ) )
{
if ( BarHigh3 - BarClose3 < atr*1.5)
{
ticket_1 = OrderSend(Symbol(),OP_SELL,Lots, Bid, 3, BarHigh3 + 50*Point, Bid - ( BarHigh3 + 50*Point - Bid)*2.1 ,"My EA",12345,0,Red);
}
else
{
ticket_1 = OrderSend(Symbol(),OP_SELL,Lots, Bid, 3, Bid + atr*1.5, Bid - atr*1.5*2.1 ,"My EA",12345,0,Red);
}
if(ticket_1 > 0)
{
if(OrderSelect(ticket_1,SELECT_BY_TICKET,MODE_TRADES))
{
Print("SELL order opened : ",OrderOpenPrice());
return (0);
}
}
else
{
Print("Error opening SELL order : ",GetLastError());
return(0);
}
}
//-----------------------------------------------------------------------------------------------
Các bước cần kiểm soát khi xử lý của thuật toán gồm:
- Bước 1: Kiểm tra xem có lệnh đã đặt là lệnh đó đang tồn tại. Trong code thì mình sử dụng 2 biến quản lý (do mình chỉ code ví dụ trên 02 cặp ngoại tệ): n21, n22.
- Bước 2: Kiểm tra xem có lệnh đã đặt và lệnh đó đã CLOSED. Trong code mình sử dụng 2 biến quản lý: n31, n32.
Code trên mình viết trên khung thời gian D1, cho 2 cặp ngoại tệ. Các bạn tùy biến sử dụng hàm Period() và gia tăng số biến cho phù hợp với nhu cầu code trên nhiều khung thời gian và nhiều cặp ngoại tệ.
https://traderviet.org/members/tran-thanh-khoi.34982/ tham khảo nhá.
Đính kèm
Giới thiệu sách Trading hay
Đánh Bại Thị Trường Forex - Tư duy khác biệt và các kỹ thuật giao dịch của chuyên gia quản lý quỹ triệu đô
Sách chia sẻ kiến thức và kinh nghiệm trading từ một chuyên gia quản lý quỹ, cùng các kỹ thuật giao dịch giúp quỹ này đứng trong top nhiều năm
Bài viết liên quan