Hi traders
I have tryed the Chat GPT to write a code for me, but I cannot get it to work in the backtester. As I do know nothing about coding, I hope you will look at it and tell me whats wrong with it. It seems and sounds as if it have coded for me what I have told it to do, but something have went wrong. Maybe it have coded it in old version of mql4, or that the code is incomplete because any reason, but since I do not know anything about coding I might have considered it done.
Will appreciate any help of you.
// User input parameters
input int MagicNumber = 12345;
input int WaitingPips = 50;
input double LotSize = 0.1;
input int FixStopLoss = 0;
input int FixTakeProfit = 0;
input bool MoneyManagement = true;
input double BalanceValue = 1000;
input double SLPercentage = 10;
input double TPPercentage = 10;
// Global variables
int ticket;
double stopLoss;
double takeProfit;
// Entry conditions
bool CanOpenTrade()
{
// Check if the market has moved long or short by the number of waiting pips
double waitingPipsPrice = WaitingPips * Point;
if (Bid > waitingPipsPrice && Ask > waitingPipsPrice)
return true;
if (Bid < -waitingPipsPrice && Ask < -waitingPipsPrice)
return true;
return false;
}
// Calculate stop loss and take profit
void CalculateSLTP()
{
// Calculate stop loss and take profit based on percentage of waiting pips
stopLoss = WaitingPips * SLPercentage / 100 * Point;
takeProfit = WaitingPips * TPPercentage / 100 * Point;
}
// Trade management functions
void OpenTrade(string tradeType)
{
// Calculate stop loss and take profit
if (FixStopLoss > 0)
stopLoss = FixStopLoss * Point;
if (FixTakeProfit > 0)
takeProfit = FixTakeProfit * Point;
else
CalculateSLTP();
// Calculate lot size
double lots;
if (MoneyManagement)
{
double risk = (stopLoss / Point) * LotSize;
double riskPerTrade = BalanceValue * 0.01;
lots = riskPerTrade / risk;
}
else
lots = LotSize;
// Open trade
ticket = OrderSend(Symbol(), tradeType, lots, Ask, 3, stopLoss, takeProfit, "My Expert Advisor", MagicNumber, 0, Green);
if (ticket < 0)
Print("Error opening trade: ", GetLastError());
}
void CloseTrade(int ticket)
{
if (OrderClose(ticket, OrderLots(), OrderClosePrice(), 3))
Print("Trade closed successfully");
else
Print("Error closing trade: ", GetLastError());
}
// Expert Advisor function
void OnTick()
{
// Check if there are open trades
int totalTrades = OrdersTotal();
if (totalTrades > 0)
{
// Loop through all trades
for (int i = 0; i < totalTrades; i++)
{
// Get trade information
OrderSelect(i, SELECT_BY_POS);
if (OrderMagicNumber() == MagicNumber)
{
// Close trade if it is in profit or loss
if (OrderProfit() > 0 || OrderProfit() < 0)
CloseTrade(OrderTicket());
}
}
}
// Check if a new trade can be opened
if (CanOpenTrade())
{
// Check
I have tryed the Chat GPT to write a code for me, but I cannot get it to work in the backtester. As I do know nothing about coding, I hope you will look at it and tell me whats wrong with it. It seems and sounds as if it have coded for me what I have told it to do, but something have went wrong. Maybe it have coded it in old version of mql4, or that the code is incomplete because any reason, but since I do not know anything about coding I might have considered it done.
Will appreciate any help of you.
// User input parameters
input int MagicNumber = 12345;
input int WaitingPips = 50;
input double LotSize = 0.1;
input int FixStopLoss = 0;
input int FixTakeProfit = 0;
input bool MoneyManagement = true;
input double BalanceValue = 1000;
input double SLPercentage = 10;
input double TPPercentage = 10;
// Global variables
int ticket;
double stopLoss;
double takeProfit;
// Entry conditions
bool CanOpenTrade()
{
// Check if the market has moved long or short by the number of waiting pips
double waitingPipsPrice = WaitingPips * Point;
if (Bid > waitingPipsPrice && Ask > waitingPipsPrice)
return true;
if (Bid < -waitingPipsPrice && Ask < -waitingPipsPrice)
return true;
return false;
}
// Calculate stop loss and take profit
void CalculateSLTP()
{
// Calculate stop loss and take profit based on percentage of waiting pips
stopLoss = WaitingPips * SLPercentage / 100 * Point;
takeProfit = WaitingPips * TPPercentage / 100 * Point;
}
// Trade management functions
void OpenTrade(string tradeType)
{
// Calculate stop loss and take profit
if (FixStopLoss > 0)
stopLoss = FixStopLoss * Point;
if (FixTakeProfit > 0)
takeProfit = FixTakeProfit * Point;
else
CalculateSLTP();
// Calculate lot size
double lots;
if (MoneyManagement)
{
double risk = (stopLoss / Point) * LotSize;
double riskPerTrade = BalanceValue * 0.01;
lots = riskPerTrade / risk;
}
else
lots = LotSize;
// Open trade
ticket = OrderSend(Symbol(), tradeType, lots, Ask, 3, stopLoss, takeProfit, "My Expert Advisor", MagicNumber, 0, Green);
if (ticket < 0)
Print("Error opening trade: ", GetLastError());
}
void CloseTrade(int ticket)
{
if (OrderClose(ticket, OrderLots(), OrderClosePrice(), 3))
Print("Trade closed successfully");
else
Print("Error closing trade: ", GetLastError());
}
// Expert Advisor function
void OnTick()
{
// Check if there are open trades
int totalTrades = OrdersTotal();
if (totalTrades > 0)
{
// Loop through all trades
for (int i = 0; i < totalTrades; i++)
{
// Get trade information
OrderSelect(i, SELECT_BY_POS);
if (OrderMagicNumber() == MagicNumber)
{
// Close trade if it is in profit or loss
if (OrderProfit() > 0 || OrderProfit() < 0)
CloseTrade(OrderTicket());
}
}
}
// Check if a new trade can be opened
if (CanOpenTrade())
{
// Check