0% found this document useful (0 votes)
17 views3 pages

Martingale Trigger Expert Advisor

This document outlines the code for a Martingale Trigger Expert Advisor in trading, which uses a strategy based on high and low prices of hourly candles. It includes functions for detecting new bars, drawing high/low lines, and managing buy/sell stop orders based on price triggers. The advisor also implements a martingale strategy to increase lot sizes after losses, aiming to recover previous losses with new trades.

Uploaded by

Zero One
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)
17 views3 pages

Martingale Trigger Expert Advisor

This document outlines the code for a Martingale Trigger Expert Advisor in trading, which uses a strategy based on high and low prices of hourly candles. It includes functions for detecting new bars, drawing high/low lines, and managing buy/sell stop orders based on price triggers. The advisor also implements a martingale strategy to increase lot sizes after losses, aiming to recover previous losses with new trades.

Uploaded by

Zero One
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

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

//| Expert Advisor: Martingale Trigger |


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

extern double lotAwal = 0.02;


extern double tp = 5; // Take profit dalam poin penuh
extern double sl = 5; // Stop loss dalam poin penuh
extern double pemicuJarak = 2.5; // Jarak trigger dari high/low sebelumnya

double lot = lotAwal;


bool pemicu = false;
bool sudahJalanSekali = false;
int magicNumber = MathRand();
datetime waktuBarTerakhir = 0;

//+------------------------------------------------------------------+
//| Fungsi utama saat tick |
//+------------------------------------------------------------------+
void OnTick()
{

if (!pemicu)
{
if (barH1Baru())
{
gambarHiLo();
}
cekPemicuAtas();
cekPemicuBawah();
}
else
{
awasi();
}
}

//+------------------------------------------------------------------+
//| Cek apakah ada bar H1 baru |
//+------------------------------------------------------------------+
bool barH1Baru()
{
datetime waktuBarSekarang = iTime(Symbol(), PERIOD_H1, 0);
if (waktuBarSekarang != waktuBarTerakhir)
{
waktuBarTerakhir = waktuBarSekarang;
return true;
}
return false;
}

//+------------------------------------------------------------------+
//| Menggambar high low candle H1 sebelumnya |
//+------------------------------------------------------------------+
void gambarHiLo()
{
// Hapus semua objek dulu
int totalObj = ObjectsTotal();
for (int i = totalObj - 1; i >= 0; i--)
{
string namaObj = ObjectName(i);
ObjectDelete(namaObj);
}

double hi = iHigh(Symbol(), PERIOD_H1, 1);


double lo = iLow(Symbol(), PERIOD_H1, 1);

string namaHi = "GarisHi_" + TimeToStr(TimeCurrent(), TIME_SECONDS);


ObjectCreate(namaHi, OBJ_HLINE, 0, Time[0], hi);
ObjectSetInteger(0, namaHi, OBJPROP_COLOR, clrRed);
ObjectSetInteger(0, namaHi, OBJPROP_WIDTH, 1);

string namaLo = "GarisLo_" + TimeToStr(TimeCurrent(), TIME_SECONDS);


ObjectCreate(namaLo, OBJ_HLINE, 0, Time[0], lo);
ObjectSetInteger(0, namaLo, OBJPROP_COLOR, clrBlue);
ObjectSetInteger(0, namaLo, OBJPROP_WIDTH, 1);
}

//+------------------------------------------------------------------+
//| Cek pemicu Atas dari high H1 |
//+------------------------------------------------------------------+
void cekPemicuAtas()
{
double highH1 = iHigh(Symbol(), PERIOD_H1, 1);
double hargaPemicu = highH1 + pemicuJarak;

if (Bid >= hargaPemicu and Bid <= hargaPemicu+1)


{
double hargaBuyStop = hargaPemicu + pemicuJarak;
double hargaSellStop = hargaPemicu - pemicuJarak;

OrderSend(Symbol(), OP_BUYSTOP, lot, hargaBuyStop, 3, hargaBuyStop - sl,


hargaBuyStop + tp, "BuyStop", magicNumber, 0, clrGreen);
OrderSend(Symbol(), OP_SELLSTOP, lot, hargaSellStop, 3, hargaSellStop + sl,
hargaSellStop - tp, "SellStop", magicNumber, 0, clrRed);

pemicu = true;
}
}

//+------------------------------------------------------------------+
//| Cek pemicu bawah dari low H1 |
//+------------------------------------------------------------------+
void cekPemicuBawah()
{
double lowH1 = iLow(Symbol(), PERIOD_H1, 1);
double hargaPemicu = lowH1 - pemicuJarak;

if (Ask <= hargaPemicu and Ask >= hargaPemicu)


{
double hargaBuyStop = hargaPemicu + pemicuJarak;
double hargaSellStop = hargaPemicu - pemicuJarak;

OrderSend(Symbol(), OP_BUYSTOP, lot, hargaBuyStop, 3, hargaBuyStop - sl,


hargaBuyStop + tp, "BuyStop", magicNumber, 0, clrGreen);
OrderSend(Symbol(), OP_SELLSTOP, lot, hargaSellStop, 3, hargaSellStop + sl,
hargaSellStop - tp, "SellStop", magicNumber, 0, clrRed);

pemicu = true;
}
}

//+------------------------------------------------------------------+
//| Cek apakah ada order aktif (running, bukan pending) |
//+------------------------------------------------------------------+
bool adaOrderAktif()
{
for (int i = 0; i < OrdersTotal(); i++)
{
if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
{
if ((OrderType() == OP_BUY || OrderType() == OP_SELL) &&
OrderMagicNumber() == magicNumber)
return true;
}
}
return false;
}

//+------------------------------------------------------------------+
//| Fungsi pengawasan martingale |
//+------------------------------------------------------------------+
void awasi()
{
if (!sudahJalanSekali)
{
sudahJalanSekali = true;

// Hapus semua pending order


for (int i = OrdersTotal() - 1; i >= 0; i--)
{
if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
{
if ((OrderType() == OP_BUYSTOP || OrderType() == OP_SELLSTOP) &&
OrderMagicNumber() == magicNumber)
OrderDelete(OrderTicket());
}
}

if (adaOrderAktif())
{
double slAktif = OrderStopLoss();
double lotBaru = OrderLots() * 2;

if (OrderType() == OP_BUY)
{
OrderSend(Symbol(), OP_SELLSTOP, lotBaru, slAktif, 3, slAktif + sl,
slAktif - tp, "Sell Recovery", magicNumber, 0, clrRed);
}

if (OrderType() == OP_SELL)
{
OrderSend(Symbol(), OP_BUYSTOP, lotBaru, slAktif, 3, slAktif - sl,
slAktif + tp, "Buy Recovery", magicNumber, 0, clrGreen);
}
}
}
}

Common questions

Powered by AI

The main risks of using a Martingale strategy include substantial drawdowns since it requires increasing trade sizes in response to losses. This can lead to significant financial exposure if the market continues against the original trade direction. Additionally, the strategy relies on having large capital reserves to withstand long loss streaks, failing which could lead to potential account depletion .

The "OnTick" function serves as the main activation routine that checks whether the conditions for initiating a trade based on the Martingale strategy are met. If no initial trigger is detected, it checks for new hourly bars and the conditions for potential trade triggers. If a trigger is active, it monitors and manages running orders .

The Expert Advisor ensures only relevant pending orders are deleted by iterating over all orders and selecting based on the position. It checks if the order is either a buy stop or sell stop and matches the unique magic number corresponding to the runs of this EA instance. Only those orders are set for deletion, preventing interference with other trades .

The "pemicuJarak" determines how far from the previous candle's high or low a trigger point will be set for trade entries. If the distance is too small, it could lead to frequent triggering, increasing transaction costs and risk exposure. Conversely, a large distance might reduce trade frequency but could miss out on viable opportunities, highlighting the need for balance based on market volatility .

The function "barH1Baru" determines the opening of a new H1 bar by comparing current bar time with the last recorded bar time. If a new bar is detected, it updates the last recorded time and triggers the drawing of the high and low lines of the previous H1 candle on the chart, which are crucial for identifying trigger levels .

The Expert Advisor visually indicates important price levels by dynamically creating horizontal line objects at the high and low prices of the previous hourly candle. These lines are assigned distinct colors—red for the high and blue for the low, allowing traders to easily identify trigger levels on the chart .

The Expert Advisor decides to place a buy or sell stop order by first determining if the current bid exceeds or equals the calculated trigger price above the previous high (for buy) or below the previous low (for sell), adjusted by the trigger distance. If these conditions are met, it sends buy and sell stop orders at prices adjusted by the trigger distance .

The Martingale Trigger checks for active orders by iterating through all orders and selecting those with a unique magic number. It confirms the presence of an active order if the order type is either a buy or sell and if it matches the magic number associated with the specific instances of the Expert Advisor's trades .

In the event of a losing position, the strategy doubles the lot size of the subsequent counter trade order from the original losing position. If a buy order is losing, a sell stop order is placed with the doubled lot size. This allows not only for the potential recovery of the lost amounts but also to benefit if the price reverses, covering previous losses .

In a Martingale strategy, adjusting stop loss (SL) and take profit (TP) levels is crucial to manage risk and ensure that profits are captured before reversals occur. The strategy relies on covering previous losses by increasing trade sizes, so precise SL and TP adjustments can help in reaching a profitable position sooner, potentially reducing accumulated risk .

You might also like