Skip to content

Instantly share code, notes, and snippets.

@zmlu
Last active November 19, 2024 02:21
Show Gist options
  • Save zmlu/e6187e468eb934a1f2bde927c97d65e5 to your computer and use it in GitHub Desktop.
Save zmlu/e6187e468eb934a1f2bde927c97d65e5 to your computer and use it in GitHub Desktop.
Gold Trader
//+------------------------------------------------------------------+
//| Expert initialization function |
//+------------------------------------------------------------------+
#property strict
#include <Trade/Trade.mqh>
CTrade trade;
input double LotSize = 100; // Default Lot Size
input int MagicNumber = 960713; // Magic Number for identifying trades
input int MaxBuyBarCount = 30; // Maximum number of bars to hold a buy position (-1 to ignore)
input int MaxSellBarCount = 30; // Maximum number of bars to hold a sell position (-1 to ignore)
// Indicators handles
int ichimokuHandle;
int rsiHandle;
// Global Variables
bool canSell = true;
bool canBuy = true;
int buyBarCount = 0;
int sellBarCount = 0;
//+------------------------------------------------------------------+
//| Expert initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
// Create Ichimoku and RSI handles
ichimokuHandle = iIchimoku(NULL, PERIOD_D1, 9, 26, 52);
rsiHandle = iRSI(NULL, PERIOD_M3, 14, PRICE_CLOSE);
if (ichimokuHandle == INVALID_HANDLE || rsiHandle == INVALID_HANDLE)
{
Print("Error creating indicator handles.");
return INIT_FAILED;
}
return INIT_SUCCEEDED;
}
//+------------------------------------------------------------------+
//| Expert deinitialization function |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
// Release indicator handles
if (ichimokuHandle != INVALID_HANDLE)
IndicatorRelease(ichimokuHandle);
if (rsiHandle != INVALID_HANDLE)
IndicatorRelease(rsiHandle);
}
//+------------------------------------------------------------------+
//| Expert tick function |
//+------------------------------------------------------------------+
void OnTick()
{
static datetime lastBarTime = 0;
// Check if a new bar has formed
MqlRates rates[];
if (CopyRates(NULL, PERIOD_M3, 0, 2, rates) > 0)
{
if (rates[1].time != lastBarTime)
{
lastBarTime = rates[1].time;
OnNewBar();
}
}
// Get the current RSI value
double rsi[];
if (CopyBuffer(rsiHandle, 0, 0, 1, rsi) <= 0)
{
Print("Error copying RSI values.");
return;
}
double currentRSI = rsi[0];
// Check if we can sell
if (currentRSI > 70 && canSell)
{
// Check if there is already a sell position
if (!IsPositionOpen(ORDER_TYPE_SELL))
{
trade.SetExpertMagicNumber(MagicNumber);
trade.Sell(LotSize, Symbol());
if (trade.ResultRetcode() == TRADE_RETCODE_DONE)
{
canSell = false;
sellBarCount = 0;
Print("Sold 0.1 lot at RSI: ", currentRSI);
}
}
}
// Check if we can buy
if (currentRSI < 30 && canBuy)
{
// Check if there is already a buy position
if (!IsPositionOpen(ORDER_TYPE_BUY))
{
trade.SetExpertMagicNumber(MagicNumber);
trade.Buy(LotSize, Symbol());
if (trade.ResultRetcode() == TRADE_RETCODE_DONE)
{
canBuy = false;
buyBarCount = 0;
Print("Bought 0.1 lot at RSI: ", currentRSI);
}
}
}
// Check if we need to close the buy position
if (IsPositionOpen(ORDER_TYPE_BUY))
{
buyBarCount++;
if (currentRSI >= 60 || (MaxBuyBarCount != -1 && buyBarCount >= MaxBuyBarCount))
{
trade.PositionClose(Symbol());
if (trade.ResultRetcode() == TRADE_RETCODE_DONE)
{
canBuy = true;
Print("Closed buy position at RSI: ", currentRSI);
}
}
}
// Check if we need to close the sell position
if (IsPositionOpen(ORDER_TYPE_SELL))
{
sellBarCount++;
if (currentRSI <= 40 || (MaxSellBarCount != -1 && sellBarCount >= MaxSellBarCount))
{
trade.PositionClose(Symbol());
if (trade.ResultRetcode() == TRADE_RETCODE_DONE)
{
canSell = true;
Print("Closed sell position at RSI: ", currentRSI);
}
}
}
}
//+------------------------------------------------------------------+
//| Check if position is open |
//+------------------------------------------------------------------+
bool IsPositionOpen(int orderType)
{
for (int i = PositionsTotal() - 1; i >= 0; i--)
{
if (PositionGetSymbol(i) == Symbol() && PositionGetInteger(POSITION_MAGIC) == MagicNumber && PositionGetInteger(POSITION_TYPE) == orderType)
return true;
}
return false;
}
//+------------------------------------------------------------------+
//| Expert new bar function |
//+------------------------------------------------------------------+
void OnNewBar()
{
canSell = true;
canBuy = true;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment