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
Giao Dịch Theo Xu Hướng Để Kiếm Sống
Sách chia sẻ chiến lược giao dịch, tâm lý, phương pháp quản lý vốn thực chiến của Trader 18 năm kinh nghiệm giao dịch theo xu hướng
Bài viết liên quan