0% found this document useful (0 votes)
63 views8 pages

SDQDQ

The document defines parameters and logic for an expert advisor used for automated trading. It includes settings for indicators like MACD, trade parameters like lot size and take profit/stop loss levels, and logic to open, close and modify trades based on indicator values and other conditions.

Uploaded by

zinedinebedjaoui
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
63 views8 pages

SDQDQ

The document defines parameters and logic for an expert advisor used for automated trading. It includes settings for indicators like MACD, trade parameters like lot size and take profit/stop loss levels, and logic to open, close and modify trades based on indicator values and other conditions.

Uploaded by

zinedinebedjaoui
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 8

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

//| Akram |
//| Copyright 2018, CompanyName |
//| http://www.companyname.net |
//+------------------------------------------------------------------+
#define MODE_HISTOGRAM 2
// Input parameters
input int FastEMA = 12;
input int SlowEMA = 26;
input int SignalSMA = 9;
input double InitialBalance = 1000.0; // Change this value to your desired initial
account size in dollars
double CurrentBalance = AccountBalance();
double ProfitLoss = CurrentBalance - InitialBalance;
// Get current account equity and calculate maximum lot size for desired exposure
double account_equity = AccountInfoDouble(ACCOUNT_EQUITY);
double Exposure = AccountInfoDouble(ACCOUNT_MARGIN_FREE) / MarketInfo(Symbol(),
MODE_MARGINREQUIRED);
double max_lot_size = NormalizeDouble(account_equity * Exposure / 100 /
MarketInfo(Symbol(), MODE_MARGINREQUIRED), 2);

// Calculate lot size based on account balance


double takeProfitInPips = 50.0;
double stopLossInPips = 50.0;
// Convert pips to price distance
double pointValue = MarketInfo(Symbol(), MODE_POINT);
double takeProfitInPrice = takeProfitInPips * pointValue;
double stopLossInPrice = stopLossInPips * pointValue;
// Calculate take profit based on exposure and desired pips

// Default lot size


input double lot_size = 0.1; //
double account_balance = AccountBalance();
double take_profit = 0;
double stop_loss = 0;
//steps
double last_trade_open;
double pip_step;
input double PipStep = 10.0; // default value of 10 pips
double multiplier = 2.0; // multiplier for pip step value
double currentPipStep = PipStep; // variable to hold the current pip step value

double pip_distance = (last_trade_open - Ask) / Point;

//quity stop
double equity_stop_pct;
input double EquityStopPct = 10.0; // default value of 10%
double equity_stop_usd = AccountEquity() * equity_stop_pct / 100.0;

//lot size growth on each trade


double initial_lot_size;
input double InitialLotSize = 0.01; // default value of 0.01 lot
double current_lot_size = initial_lot_size;
input double LotSizeGrowthRate = 1.5; // default value of 1.5 (i.e. 50% growth
rate)
//sell with all buy lot size
double total_buy_lot_size = 0;
double new_trade_price = Bid; // Get the current bid price
double sell_lot_size = total_buy_lot_size;

input int MagicNumber = 1234;


int max_trades;
input int MaxTrades = 5; // default value of 5 trades
int open_trades = OrdersTotal();

// MACD parameters
int macd_fast = FastEMA;
int macd_slow = SlowEMA;
int macd_signal = SignalSMA;
// Define ErrorDescription function
string ErrorDescription(int error)
{
string errorString;
switch(error)
{
case 0:
errorString = "NO_ERROR";
break;
case 1:
errorString = "ERR_NO_RESULT";
break;
case 2:
errorString = "ERR_COMMON_ERROR";
break;
case 3:
errorString = "ERR_INVALID_TRADE_PARAMETERS";
break;
case 4:
errorString = "ERR_SERVER_BUSY";
break;
case 5:
errorString = "ERR_OLD_VERSION";
break;
case 6:
errorString = "ERR_NO_CONNECTION";
break;
case 7:
errorString = "ERR_NOT_ENOUGH_RIGHTS";
break;
case 8:
errorString = "ERR_TOO_FREQUENT_REQUESTS";
break;
case 9:
errorString = "ERR_MALFUNCTIONAL_TRADE";
break;
case 64:
errorString = "ERR_ACCOUNT_DISABLED";
break;
case 65:
errorString = "ERR_INVALID_ACCOUNT";
break;
case 128:
errorString = "ERR_TRADE_TIMEOUT";
break;
case 129:
errorString = "ERR_INVALID_PRICE";
break;
case 130:
errorString = "ERR_INVALID_STOPS";
break;
case 131:
errorString = "ERR_INVALID_TRADE_VOLUME";
break;
case 132:
errorString = "ERR_MARKET_CLOSED";
break;
case 133:
errorString = "ERR_TRADE_DISABLED";
break;
case 134:
errorString = "ERR_NOT_ENOUGH_MONEY";
break;
case 135:
errorString = "ERR_PRICE_CHANGED";
break;
case 136:
errorString = "ERR_OFF_QUOTES";
break;
case 137:
errorString = "ERR_BROKER_BUSY";
break;
case 138:
errorString = "ERR_REQUOTE";
break;
case 139:
errorString = "ERR_ORDER_LOCKED";
break;
case 140:
errorString = "ERR_LONG_POSITIONS_ONLY_ALLOWED";
break;
case 141:
errorString = "ERR_TOO_MANY_REQUESTS";
break;
case 145:
errorString = "ERR_TRADE_MODIFY_DENIED";
break;
case 146:
errorString = "ERR_TRADE_CONTEXT_BUSY";
break;
case 147:
errorString = "ERR_TRADE_EXPIRATION_DENIED";
break;
case 148:
errorString = "ERR_TRADE_TOO_MANY_ORDERS";
break;
case 149:
errorString = "ERR_TRADE_HEDGE_PROHIBITED";
break;
case 150:
errorString = "ERR_TRADE_PROHIBITED_BY_FIFO";
break;
default:
errorString = "Unknown error code: " + IntegerToString(error);
break;
}
return errorString;
}
// Global variables
double H4_MACD_main[];
double H4_MACD_signal[];
double H4_MACD_histogram[];
double H4_MACD_main_prev;
double H4_MACD_signal_prev;
double H4_MACD_histogram_prev;
double H1_MACD_main[];
double H1_MACD_signal[];
double H1_MACD_histogram[];
double H1_MACD_main_prev;
double H1_MACD_signal_prev;
double H1_MACD_histogram_prev;
double M30_MACD_main[];
double M30_MACD_signal[];
double M30_MACD_histogram[];
double M30_MACD_main_prev;
double M30_MACD_signal_prev;
double M30_MACD_histogram_prev;
double M5_MACD_main[];
double M5_MACD_signal[];
double M5_MACD_histogram[];
double M5_MACD_main_prev;
double M5_MACD_signal_prev;
double M5_MACD_histogram_prev;

//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
int OnInit()
{
double takeProfit = Exposure + takeProfitInPrice + currentPipStep;
double stop_loss = Exposure - stopLossInPrice - currentPipStep;
// Set take profit level
bool result = OrderModify(OrderTicket(), 0, takeProfit, OrderStopLoss(), 0,
CLR_NONE);
if(!result)
{
Print("Failed to modify order take profit: ",
ErrorDescription(GetLastError()));
}

Print("Initial Account Balance: $", DoubleToStr(InitialBalance, 2));


Print("Current Account Balance: $", DoubleToStr(CurrentBalance, 2));
Print("Profit/Loss: $", DoubleToStr(ProfitLoss, 2));
// Shift the chart to the left by 200 bars
int chart_shift = 200;
ChartSetInteger(0, CHART_SHIFT, chart_shift);

return(INIT_SUCCEEDED);
}

//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
// Close any open orders on deinitialization
if(OrderClose(OrderTicket(), OrderLots(), Bid, 3, Red))
{
Print("Order closed successfully");
}
else
{
Print("Error in closing order: ", GetLastError());
}

//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
void OnTick()
{
double takeProfit = Exposure + takeProfitInPrice + currentPipStep;
double stop_loss = Exposure - stopLossInPrice - currentPipStep;

for(int i=1; i<=3; i++) // loop through 3 trades


if(i == 3) // check if this is the third trade
{
currentPipStep = PipStep * multiplier; // update pip step value
}

for( i = OrdersTotal() - 1; i >= 0; i--)


{
if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
{
if(OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber)
{
if(OrderType() == OP_BUY)
{
total_buy_lot_size += OrderLots();
}
}
}
}

if(OrdersTotal() >= MaxTrades)


{
if(OrderSend(Symbol(), OP_SELL, sell_lot_size, new_trade_price, 3, stop_loss,
takeProfit, MagicNumber, 0, Red) > 0)
{
// Sell trade opened successfully
}
}

current_lot_size *= LotSizeGrowthRate;
if(OrderSend(Symbol(), OP_BUY, lot_size, SymbolInfoDouble(Symbol(), SYMBOL_ASK),
3, SymbolInfoDouble(Symbol(), SYMBOL_ASK) - stop_loss, takeProfit, "Buy Order",
MagicNumber, 0, Green) > 0)
{
// Trade opened successfully
}

if(AccountEquity() < equity_stop_usd)


{
// Close all open trades
}
for(i = OrdersTotal() - 1; i >= 0; i--)
{
if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
{
if(OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber)
{
if(OrderClose(OrderTicket(), OrderLots(), Bid, 3, Red) == true)
{
// Trade closed successfully
}
}
}
}

if(open_trades < max_trades)


{
// Open a new trade
}

// Open buy trade with calculated lot size, take profit and stop loss
int ticket = OrderSend(Symbol(), OP_BUY, lot_size, SymbolInfoDouble(Symbol(),
SYMBOL_ASK), 3, SymbolInfoDouble(Symbol(), SYMBOL_ASK) - stop_loss, takeProfit,
"Buy Order", MagicNumber, 0, Green);

// Check for errors in opening trade


if(ticket < 0)
{
Print("Error in opening trade: ", GetLastError());
}

// Calculate MACD on 4H timeframe


double macd_4h_main = iMACD(NULL, PERIOD_H4, macd_fast, macd_slow, macd_signal,
PRICE_CLOSE, MODE_MAIN, 0);
double macd_4h_signal = iMACD(NULL, PERIOD_H4, macd_fast, macd_slow,
macd_signal, PRICE_CLOSE, MODE_SIGNAL, 0);
double macd_4h_histogram = iMACD(NULL, PERIOD_H4, macd_fast, macd_slow,
macd_signal, PRICE_CLOSE, MODE_HISTOGRAM, 0);

// Calculate MACD on 1H timeframe


double macd_1h_main = iMACD(NULL, PERIOD_H1, macd_fast, macd_slow, macd_signal,
PRICE_CLOSE, MODE_MAIN, 0);
double macd_1h_signal = iMACD(NULL, PERIOD_H1, macd_fast, macd_slow,
macd_signal, PRICE_CLOSE, MODE_SIGNAL, 0);
double macd_1h_histogram = iMACD(NULL, PERIOD_H1, macd_fast, macd_slow,
macd_signal, PRICE_CLOSE, MODE_HISTOGRAM, 0);

// Calculate MACD on 30min timeframe


double macd_30min_main = iMACD(NULL, PERIOD_M30, macd_fast, macd_slow,
macd_signal, PRICE_CLOSE, MODE_MAIN, 0);
double macd_30min_signal = iMACD(NULL, PERIOD_M30, macd_fast, macd_slow,
macd_signal, PRICE_CLOSE, MODE_SIGNAL, 0);
double macd_30min_histogram = iMACD(NULL, PERIOD_M30, macd_fast, macd_slow,
macd_signal, PRICE_CLOSE, MODE_HISTOGRAM, 0);

// Calculate MACD on 5min timeframe


double macd_5min_main = iMACD(NULL, PERIOD_M5, macd_fast, macd_slow,
macd_signal, PRICE_CLOSE, MODE_MAIN, 0);
double macd_5min_signal = iMACD(NULL, PERIOD_M5, macd_fast, macd_slow,
macd_signal, PRICE_CLOSE, MODE_SIGNAL, 0);
double macd_5min_histogram = iMACD(NULL, PERIOD_M5, macd_fast, macd_slow,
macd_signal, PRICE_CLOSE, MODE_HISTOGRAM, 0);
int totalOrders = OrdersTotal();
// Exit the condition if there is at least one open order
if(totalOrders > 0)
{
return;
}

// Check if M5 MACD histogram crossed M5 MACD signal


if(M5_MACD_histogram_prev < M5_MACD_signal_prev && macd_5min_histogram >
macd_5min_signal)
{
// Execute the buy trade here
ticket = OrderSend(Symbol(), OP_BUY, lot_size, SymbolInfoDouble(Symbol(),
SYMBOL_ASK), 3, SymbolInfoDouble(Symbol(), SYMBOL_ASK) - stop_loss, takeProfit,
"Buy Order", MagicNumber, 0, Green);
if(ticket > 0)
{
Print("Buy trade executed successfully. Ticket number: ", ticket);
}
else
{
Print("Error executing buy trade. Error code: ", GetLastError());
}
}
// Check if MACD histogram on 4H timeframe is above MACD signal
if(macd_4h_histogram > macd_4h_signal)
{
// Check if MACD histogram on 1H timeframe is above MACD signal
if(macd_1h_histogram > macd_1h_signal)
{
// Check if MACD histogram on 30min timeframe is above MACD signal
if(macd_30min_histogram > macd_30min_signal)
{
// Check if MACD histogram on 5min timeframe crossed MACD signal
if(M5_MACD_histogram_prev < M5_MACD_signal_prev && macd_5min_histogram
> macd_5min_signal)

// Open buy trade with calculated lot size, take profit and stop
loss
bool result = OrderSend(Symbol(), OP_BUY, lot_size, Ask, 0,
stop_loss, takeProfit, "MyOrder", MagicNumber, 0, Green);
if(!result)
{
Print("Failed to place order: ", ErrorDescription(GetLastError()));
}
// Check for errors in opening trade
if(ticket < 0)
{
Print("Error in opening trade: ", GetLastError());
}
if(OrderSelect(OrdersTotal() - 1, SELECT_BY_POS, MODE_TRADES))
{
last_trade_open = OrderOpenPrice();
}

if(OrderSend(Symbol(), OP_BUY, lot_size, SymbolInfoDouble(Symbol(),


SYMBOL_ASK), 3, SymbolInfoDouble(Symbol(), SYMBOL_ASK) - stop_loss, takeProfit,
"Buy Order", MagicNumber, 0, Green) > 0)
{
// Trade opened successfully
}
else
{
// Shift chart to the right
ChartSetInteger(0, CHART_SHIFT, 1);
}

// Save current MACD values for next tick


M5_MACD_main_prev = macd_5min_main;
M5_MACD_signal_prev = macd_5min_signal;
M5_MACD_histogram_prev = macd_5min_histogram;
}
}
}
}
//+------------------------------------------------------------------+

You might also like