top of page

CODE FOR XAUUSD BOT TRADING

// Define trade instrument

input string tradeInstrument = "XAUUSD";


// Define trade size

input double tradeSize = 0.01;


// Define maximum open trades

input int maxOpenTrades = 5;


// Define maximum total open position

input double maxOpenPosition = 0.05;


// Define buy criteria

input double buyCriteria = 0.003; // 0.3% increase


// Define take profit criteria

input double takeProfitCriteria = 0.003; // 0.3% increase


// Define market hours

input datetime marketOpenTime = D'2023.08.20 08:00'; // Replace with the correct date

input datetime marketCloseTime = D'2023.08.20 16:00'; // Replace with the correct date


// Define variables

double previousBuyPrice = 0.0;

double currentBuyPrice = 0.0;

double currentSellPrice = 0.0;

int openTrades = 0;

double totalOpenPosition = 0.0;


//+------------------------------------------------------------------+

//| Expert initialization function |

//+------------------------------------------------------------------+

int OnInit()

{

EventSetMillisecondTimer(1000); // Set timer event to 1 second

return(INIT_SUCCEEDED);

}


//+------------------------------------------------------------------+

//| Expert deinitialization function |

//+------------------------------------------------------------------+

void OnDeinit(const int reason)

{

EventKillTimer(); // Remove timer event

}


//+------------------------------------------------------------------+

//| Timer event handler |

//+------------------------------------------------------------------+

void OnTimer()

{

if (TimeCurrent() >= marketOpenTime && TimeCurrent() <= marketCloseTime)

{

if (openTrades < maxOpenTrades && totalOpenPosition < maxOpenPosition)

{

if (previousBuyPrice > 0.0)

{

if (currentBuyPrice >= previousBuyPrice * (1 + buyCriteria))

BuyTrade(tradeInstrument, tradeSize);

}

else

BuyTrade(tradeInstrument, tradeSize);

}

if (currentSellPrice > 0.0)

{

if (currentSellPrice >= currentBuyPrice * (1 + takeProfitCriteria))

SellTrade(tradeInstrument, tradeSize);

}

}

}




//+------------------------------------------------------------------+

//| Function to execute buy trade |

//+------------------------------------------------------------------+

void BuyTrade(string instrument, double size)

{

double slippage = 3; // Define slippage value

double stopLoss = currentBuyPrice - (currentBuyPrice * 0.005); // Set stop loss

double takeProfit = currentBuyPrice + (currentBuyPrice * 0.01); // Set take profit


// Execute buy trade logic

MqlTradeRequest request = {0};

request.action = TRADE_ACTION_DEAL;

request.symbol = instrument;

request.volume = size;

request.type = ORDER_TYPE_BUY;

request.price = currentBuyPrice;

request.sl = stopLoss;

request.tp = takeProfit;

request.deviation = int(slippage); // Convert to integer

MqlTradeResult result = {0};

if (OrderSend(request, result))

{

Print("Buy trade executed for instrument: ", instrument, " with size: ", size);

openTrades++;

totalOpenPosition += size;

previousBuyPrice = currentBuyPrice;

}

else

{

Print("Error executing buy trade. Error code: ", GetLastError());

}

}


//+------------------------------------------------------------------+

//| Function to execute sell trade |

//+------------------------------------------------------------------+

void SellTrade(string instrument, double size)

{

double slippage = 3; // Define slippage value

double stopLoss = currentSellPrice + (currentSellPrice * 0.005); // Set stop loss

double takeProfit = currentSellPrice - (currentSellPrice * 0.01); // Set take profit


// Execute sell trade logic

MqlTradeRequest request = {0};

request.action = TRADE_ACTION_DEAL;

request.symbol = instrument;

request.volume = size;

request.type = ORDER_TYPE_SELL;

request.price = currentSellPrice;

request.sl = stopLoss;

request.tp = takeProfit;

request.deviation = int(slippage); // Convert to integer

MqlTradeResult result = {0};

if (OrderSend(request, result))

{

Print("Sell trade executed for instrument: ", instrument, " with size: ", size);

openTrades--;

totalOpenPosition -= size;

}

else

{

Print("Error executing sell trade. Error code: ", GetLastError());

}

}


 
 
 

Comments


KEEP UPDATED

Thanks for submitting!

Despite we are from different origins ,  but we will forever remain together till the end of time.

FreedomCentury online inventory shop (not released yet) COMING SOON !

pexels-engin-akyurt-1458867.jpg

POST SOME

pexels-cup-of-couple-8015674.jpg

Don't miss any major news

Thanks for submitting!

© Digitlio Hub created with Wix. Nicely Created and all rights reserved.

bottom of page