2013年12月29日 星期日

MQL5 auto trading source. (that's why you cannot beat the speed of large exchanges)

/* Do not use this code to trading, some bug inside*/

/*
MQL5 source, this code trade in CFD. Using QQE, EMA, RSI, MACD,
to do buy, sell, close and the other best actions. Length of time is 24 hours.
*/
#property copyright "Copyright 2000-2024, MetaQuotes Ltd."
#property link      "https://www.mql5.com"

#include <Trade\Trade.mqh>
input int     InpQQE_RSI_Period     = 14;            // QQE RSI Period
input double  InpQQE_RSI_Smooth     = 5;             // QQE RSI Smoothing Factor
input double  InpQQE_RSI_REF        = 50;
input int     InpEMA_Period         = 50;                // EMA Period
input int     InpRSI_Period         = 14;                // RSI Period
input int     InpMACD_PeriodFast    = 12;           // MACD Fast Period
input int     InpMACD_PeriodSlow    = 26;           // MACD Slow Period
input int     InpMACD_PeriodSignal  = 9;          // MACD Signal Period
input int     InpATR_Period         = 14;                // ATR Period
input double  InpLotSize            = 0.1;               // Lot Size

double     QQE_Value, EMA_Value, RSI_Value, MACD_Main, MACD_Signal, ATR_Value;
datetime   lastTradeTime;
CTrade     tradeInstance; // Declare CTrade instance for tradeInstance functions

void OnInit()
{
   lastTradeTime = TimeCurrent();
   Print("Starting the enhanced QQE, EMA, RSI, MACD, ATR strategy for XAUUSD");
}

// Reset after 24 hours
void OnTick()
{
   double Ask;
   double Bid;
   
   CalculateIndicators();

   Ask = SymbolInfoDouble(_Symbol, SYMBOL_ASK);
   Bid = SymbolInfoDouble(_Symbol, SYMBOL_BID);

   if (CanOpenBuy())
      OpenBuy(Ask);
   
   if (CanOpenSell())
      OpenSell(Bid);

   // Manage open tradeInstances to close based on conditions
   ManageOpenTrades();
}

void CalculateIndicators()
{
   double emaArray[];
   double rsiArray[];
   double macdArray[];
   double signalArray[];
   double atrArray[];
      
   ArraySetAsSeries(emaArray,    true);
   ArraySetAsSeries(rsiArray,    true);
   ArraySetAsSeries(macdArray,   true);
   ArraySetAsSeries(signalArray, true);
   ArraySetAsSeries(atrArray,    true);
   
   if (TimeCurrent() - lastTradeTime > 24 * 3600)
   {
      lastTradeTime = TimeCurrent();
      CloseAllTrades();
   }

   // Calculate indicators
   QQE_Value   = CalculateQQE();
   
   //EMA_Value   = iMA(_Symbol,  0, InpEMA_Period, 0, MODE_EMA, PRICE_CLOSE);
   CopyBuffer(iMA(_Symbol, 0, InpEMA_Period, 0, MODE_EMA, PRICE_CLOSE), 0, 0, 1, emaArray);
   EMA_Value = emaArray[0];
   
   //RSI_Value   = iRSI(_Symbol, 0, InpRSI_Period, PRICE_CLOSE);
   CopyBuffer(iRSI(_Symbol, 0, InpRSI_Period, PRICE_CLOSE), 0, 0, 1, rsiArray);
   RSI_Value = rsiArray[0];
   
   CopyBuffer(iMACD(_Symbol, 0, InpMACD_PeriodFast, InpMACD_PeriodSlow, InpMACD_PeriodSignal, PRICE_CLOSE), 0, 0, 1, macdArray);
   CopyBuffer(iMACD(_Symbol, 0, InpMACD_PeriodFast, InpMACD_PeriodSlow, InpMACD_PeriodSignal, PRICE_CLOSE), 1, 0, 1, signalArray);
   MACD_Main   = macdArray[0]; // Main MACD value
   MACD_Signal = signalArray[0]; // Signal line value
   
   //ATR_Value   = iATR(NULL,   0, InpATR_Period);
   CopyBuffer(iATR(_Symbol, 0, InpATR_Period), 0, 0, 1, atrArray);
   ATR_Value = atrArray[0];
}

// Close all tradeInstances after 24 hours
void CloseAllTrades()
{
   for (int i = PositionsTotal() - 1; i >= 0; i--)
   {
      ulong ticket = PositionGetTicket(i);
      tradeInstance.PositionClose(ticket);
   }

   Print("All tradeInstances closed after 24 hours.");
}

double ExponentialMovingAverage(double& array[], int period)
{
   int      i;
   int      size;
   double   ema;
   double   multiplier;
      
   size = ArraySize(array);
   if (period <= 0 || period > size)
      return 0.0;

   multiplier = 2.0 / (period + 1);
   ema = array[0]; // Seed value for EMA, or optionally use SMA of the first period

   // Calculate EMA across the full array
   for (i = 1; i < size; i++)
      ema = (array[i] - ema) * multiplier + ema;

   return ema;
}

// Calculate QQE value based on smoothed RSI
double CalculateQQE()
{
   double smoothRSI;
   double qqeValue;
   double rsiArray[];
   
   ArraySetAsSeries(rsiArray, true);
   CopyBuffer(iRSI(_Symbol, 0, InpQQE_RSI_Period, PRICE_CLOSE), 0, 0, InpQQE_RSI_Period, rsiArray);

   smoothRSI   = ExponentialMovingAverage(rsiArray, (int)InpQQE_RSI_Smooth);
   qqeValue    = MathAbs(rsiArray[0] - smoothRSI) + 0.5 * ATR_Value;

   return qqeValue;
}

// Check if conditions are met to open a Buy position
bool CanOpenBuy()
{
   bool     ret;
   double   prevClose = iClose(_Symbol, PERIOD_CURRENT, 1);
   double   dynamicThreshold = 0.5 * ATR_Value;

   ret = (RSI_Value > InpQQE_RSI_REF
         &&
         MACD_Main > MACD_Signal
         &&
         QQE_Value > dynamicThreshold
         &&
         prevClose > EMA_Value);

   return ret;
}

// Check if conditions are met to open a Sell position
bool CanOpenSell()
{
   bool     ret;
   double   prevClose = iClose(_Symbol, PERIOD_CURRENT, 1);
   double   dynamicThreshold = 0.5 * ATR_Value;

   ret = (RSI_Value < InpQQE_RSI_REF
         &&
         MACD_Main < MACD_Signal
         &&
         QQE_Value < dynamicThreshold
         &&
         prevClose < EMA_Value);

   return ret;
}

void OpenBuy(double price)
{
   double stopLoss = price - ATR_Value * 1.5;
   double takeProfit = price + ATR_Value * 3;
   tradeInstance.Buy(InpLotSize, _Symbol, price, stopLoss, takeProfit, "Enhanced Buy XAUUSD");
   lastTradeTime = TimeCurrent();
   
   Alert("OpenBuy"); // Beep sound for buy
}

void OpenSell(double price)
{
   double stopLoss = price + ATR_Value * 1.5;
   double takeProfit = price - ATR_Value * 3;
   tradeInstance.Sell(InpLotSize, _Symbol, price, stopLoss, takeProfit, "Enhanced Sell XAUUSD");
   lastTradeTime = TimeCurrent();
   
   Alert("OpenSell"); // Beep sound for buy
}

void ManageOpenTrades()
{
   long     pos_type;
   double   prevClose;

   for (int i = PositionsTotal() - 1; i >= 0; i--)
   {
      ulong ticket = PositionGetTicket(i);
      if (PositionSelectByTicket(ticket))
      {
         pos_type    = PositionGetInteger(POSITION_TYPE);
         prevClose   = iClose(_Symbol, PERIOD_CURRENT, 1);

         if (pos_type == POSITION_TYPE_BUY && (RSI_Value < InpQQE_RSI_REF || MACD_Main < MACD_Signal || prevClose < EMA_Value))
         {
            tradeInstance.PositionClose(ticket);
            Alert("Buy close"); // Beep sound for buy
         }
         else if (pos_type == POSITION_TYPE_SELL && (RSI_Value > InpQQE_RSI_REF || MACD_Main > MACD_Signal || prevClose > EMA_Value))
         {
            tradeInstance.PositionClose(ticket);
            Alert("Sell close"); // Beep sound for buy
         }
      }
   }
}

沒有留言:

張貼留言