e có tập tành học và copy được đoạn code như dưới. Compile thì không thấy lỗi nhưng khi đưa vào MT4 thì im lìm không thấy chạy gì. Pro nào giúp e sửa lỗi này với ạ. E cảm ơn mọi người nhiều nhiều.
double takeProfit = 50 * Point;
double openPrice[5];
int ticketLimit[5], ticketStop[5];
void OnStart()
{
double prices[] = {1982, 1984, 1985, 1987, 1988};
for(int i=0; i<ArraySize(prices); i++)
{
// Đặt lệnh buy limit
ticketLimit = OrderSend(Symbol(), OP_BUYLIMIT, 0.1, prices, 3, prices-10*Point, prices+10*Point, "Buy Limit", 0, 0, Green);
openPrice = prices;
if(ticketLimit < 0)
{
Print("Error ", GetLastError());
}
else
{
Print("Buy Limit order placed at price ", prices);
}
// Đặt lệnh buy stop
ticketStop = OrderSend(Symbol(), OP_BUYSTOP, 0.1, prices, 3, prices+10*Point, prices-10*Point, "Buy Stop", 0, 0, Blue);
if(ticketStop < 0)
{
Print("Error ", GetLastError());
}
else
{
Print("Buy Stop order placed at price ", prices);
}
}
}
void OnTick()
{
for(int i=0; i<5; i++)
{
if(ticketLimit > 0)
{
// Kiểm tra nếu giá đạt take profit
if(Bid - openPrice >= takeProfit)
{
// Đóng vị thế buy limit
int closeResult1 = OrderClose(ticketLimit, 0.1, Bid, 3, Blue);
if(closeResult1 < 0)
{
Print("Error closing limit order: ", GetLastError());
}
else
{
Print("Limit order closed successfully");
}
// Đóng vị thế buy stop
int closeResult2 = OrderClose(ticketStop, 0.1, Bid, 3, Blue);
if(closeResult2 < 0)
{
Print("Error closing stop order: ", GetLastError());
}
else
{
Print("Stop order closed successfully");
}
// Đặt lại lệnh buy limit
ticketLimit = OrderSend(Symbol(), OP_BUYLIMIT, 0.1, openPrice, 3, openPrice-10*Point, openPrice+10*Point, "Buy Limit", 0, 0, Green);
openPrice = Bid;
if(ticketLimit < 0)
{
Print("Error ", GetLastError());
}
else
{
Print("Buy Limit order placed at price ", openPrice);
}
// Đặt lại lệnh buy stop
ticketStop = OrderSend(Symbol(), OP_BUYSTOP, 0.1, openPrice, 3, openPrice+10*Point, openPrice-10*Point, "Buy Stop", 0, 0, Blue);
if(ticketStop < 0)
{
Print("Error ", GetLastError());
}
else
{
Print("Buy Stop order placed at price ", openPrice);
}
}
}
}
}
double takeProfit = 50 * Point;
double openPrice[5];
int ticketLimit[5], ticketStop[5];
void OnStart()
{
double prices[] = {1982, 1984, 1985, 1987, 1988};
for(int i=0; i<ArraySize(prices); i++)
{
// Đặt lệnh buy limit
ticketLimit = OrderSend(Symbol(), OP_BUYLIMIT, 0.1, prices, 3, prices-10*Point, prices+10*Point, "Buy Limit", 0, 0, Green);
openPrice = prices;
if(ticketLimit < 0)
{
Print("Error ", GetLastError());
}
else
{
Print("Buy Limit order placed at price ", prices);
}
// Đặt lệnh buy stop
ticketStop = OrderSend(Symbol(), OP_BUYSTOP, 0.1, prices, 3, prices+10*Point, prices-10*Point, "Buy Stop", 0, 0, Blue);
if(ticketStop < 0)
{
Print("Error ", GetLastError());
}
else
{
Print("Buy Stop order placed at price ", prices);
}
}
}
void OnTick()
{
for(int i=0; i<5; i++)
{
if(ticketLimit > 0)
{
// Kiểm tra nếu giá đạt take profit
if(Bid - openPrice >= takeProfit)
{
// Đóng vị thế buy limit
int closeResult1 = OrderClose(ticketLimit, 0.1, Bid, 3, Blue);
if(closeResult1 < 0)
{
Print("Error closing limit order: ", GetLastError());
}
else
{
Print("Limit order closed successfully");
}
// Đóng vị thế buy stop
int closeResult2 = OrderClose(ticketStop, 0.1, Bid, 3, Blue);
if(closeResult2 < 0)
{
Print("Error closing stop order: ", GetLastError());
}
else
{
Print("Stop order closed successfully");
}
// Đặt lại lệnh buy limit
ticketLimit = OrderSend(Symbol(), OP_BUYLIMIT, 0.1, openPrice, 3, openPrice-10*Point, openPrice+10*Point, "Buy Limit", 0, 0, Green);
openPrice = Bid;
if(ticketLimit < 0)
{
Print("Error ", GetLastError());
}
else
{
Print("Buy Limit order placed at price ", openPrice);
}
// Đặt lại lệnh buy stop
ticketStop = OrderSend(Symbol(), OP_BUYSTOP, 0.1, openPrice, 3, openPrice+10*Point, openPrice-10*Point, "Buy Stop", 0, 0, Blue);
if(ticketStop < 0)
{
Print("Error ", GetLastError());
}
else
{
Print("Buy Stop order placed at price ", openPrice);
}
}
}
}
}
Đính kèm
Giới thiệu sách Trading hay
Trading In The Zone - Thực hành Kiểm soát Cảm xúc bằng Tâm lý học Hành vi trong Đầu tư và Giao dịch Tài chính
Là quyển sách Top 1 toàn cầu về chủ đề đầu tư/trading, Trading In The Zone giúp thấu hiểu và quản trị cảm xúc cũng như giữ vững kỷ luật khi tham gia thị trường tài chính, nhằm nâng cao trình độ và hiệu quả đầu tư lên mức cao nhất có thể
Bài viết liên quan