Here's how the trading script works, explained in plain language:
Overall Goal: The script is designed to automatically trade based on certain market conditions, attempting to profit from price swings. It uses a strategy that can potentially increase the trade size after losing trades (a "doubling up" approach, though not a true martingale).
How it Decides to Trade:
-
Looking at Market Indicators: The script relies on two common technical indicators to gauge the market's direction:
- CCI (Commodity Channel Index): This indicator helps identify when an asset is overbought (likely to fall in price) or oversold (likely to rise in price). Think of it like a gauge of how "extreme" the current price is compared to its average.
- MACD (Moving Average Convergence Divergence): This indicator uses moving averages to identify potential trend changes in the market. It helps to spot when momentum is shifting.
-
Setting the Thresholds: The script uses specific levels for the CCI indicator to determine buy and sell signals. These are like trigger points:
- Buy Level: A value for the CCI is set. If the CCI falls below this, it indicates the market might be oversold, and the script considers buying.
- Sell Level: Similarly, if the CCI rises above a certain level, it suggests the market might be overbought, and the script considers selling.
-
Checking Conditions: The script constantly monitors the CCI and MACD indicators, comparing their values to the set levels.
What Happens When the Conditions are Met:
-
Buy Signal:
- If the CCI and MACD values fall to or below the "Buy Level", the script checks if there are currently any open "sell" trades. If sell trades exist, they are closed.
- If there are no open "buy" trades, the script opens a new "buy" trade. The size of this trade is determined by a starting amount (
lot
) and can be increased with consecutive losses.
-
Sell Signal:
- If the CCI and MACD values rise to or above the "Sell Level", the script checks if there are currently any open "buy" trades. If buy trades exist, they are closed.
- If there are no open "sell" trades, the script opens a new "sell" trade. The size of this trade is determined by a starting amount (
lot
) and can be increased with consecutive losses.
Managing Trades:
-
Closing Trades: The script has a function to close all trades of a specific type (buy or sell). This is usually triggered when a new signal appears or to potentially manage losses.
-
Increasing Trade Size After Losses: The script keeps track of losing trades using
pos
. After a losing trade,pos
increments by 1. Ifpos
reachespreWait
,pos
will be reset to 0 and the variablewait
increases to save the number of losing trades. On the next winning tradepos
will equal towait
.- After closing a losing trade the script increases the trade size for the next trade,
lot*MathPow(2,pos)
- After closing a losing trade the script increases the trade size for the next trade,
-
Magic Number: The number 343 is used by the script to keep track of the trades managed by itself.
Important Considerations (as implied by the code):
- Risk: This script uses a "doubling down" strategy which can be very risky. If the market moves against the trades, the script could quickly use up the trading account.
- Customization: The script can be customized by the user. The parameters, such as the periods for the MACD and CCI indicators and the buy/sell levels, can be changed. This allows the user to adjust the script to their own trading style and risk tolerance.
//+------------------------------------------------------------------+
//| DoubleUp.mq4 |
//| The # one Lotfy |
//| hmmlotfy@hotmail.com |
//+------------------------------------------------------------------+
#property copyright "The # one Lotfy"
#property link "hmmlotfy@hotmail.com"
//+------------------------------------------------------------------+
//| expert initialization function |
//+------------------------------------------------------------------+
double ma;
double ma2;
int buyTotal = 0;
int sellTotal = 0;
double lot = 0.01;
int pos = 0;
bool buy = false;
bool sell = false;
extern int period= 8;
double buyLevel = 230;
extern int fast = 13;
extern int slow = 33;
extern double wait = 0;
extern double preWait=2 ;
extern int back = 0;
int init()
{
//----
//----
return(0);
}
//+------------------------------------------------------------------+
//| expert deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
//----
//----
return(0);
}
//+------------------------------------------------------------------+
//| expert start function |
//+------------------------------------------------------------------+
int start()
{
//----
ma = iCCI( Symbol(), 0,period, PRICE_CLOSE, back);
ma2 = iMACD(NULL,0,fast,slow,2,PRICE_CLOSE,MODE_MAIN,back)*1000000;
buyTotal = 0;
sellTotal = 0;
for(int cnt=OrdersTotal()-1;cnt>=0;cnt--)// close all orders
{
if (!OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES)) continue;
if (OrderSymbol() != Symbol()) continue;
if (OrderMagicNumber() != 343) continue;
if (OrderType() == OP_BUY)
buyTotal++;
else
sellTotal++;
}
if(ma>buyLevel && ma2>buyLevel)
{
sell = true;
buy = false;
}
else if(ma<-buyLevel && ma2<-buyLevel)
{
buy = true;
sell = false;
}
if(buy && ma< buyLevel)
{
buy();
buy = false;
}
if(sell && ma<-buyLevel)
{
sell();
sell = false;
}
//----
return(0);
}
void buy()
{
closeAll(OP_SELL);
if(buyTotal == 0)
OrderSend(Symbol(),OP_BUY, lot*MathPow(2,pos),
Ask,2, 0/*Ask-(SL2)*Point*/,0/*Ask+SL3*Point*/, NULL, 343, 0, Blue);
}
void sell()
{
closeAll(OP_BUY);
if(sellTotal == 0)
OrderSend(Symbol(),OP_SELL, lot*MathPow(2,pos),
Bid,2,0/*Bid+(SL2)*Point*/,0/*Bid-SL3*Point*/, NULL, 343, 0, Red );
}
void closeAll(int op)
{
for(int cnt=OrdersTotal()-1;cnt>=0;cnt--)// close all orders
{
if (!OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES)) continue;
if (OrderSymbol() != Symbol()) continue;
if (OrderMagicNumber() != 343) continue;
if (OrderType() == op )
{
if(op == OP_BUY)
{
if(OrderProfit()<0)
{
pos++;
if(pos >= preWait)
{
wait += pos;
pos = 0;
}
}
else if(OrderProfit()>0)
{
pos = wait;///2;
wait = 0;//wait/2;
}
OrderClose(OrderTicket(),OrderLots(),Bid,3,Green); // or OrderClosePrice()
}
else
{
if(OrderProfit()<0)
{
pos++;
if(pos >= preWait)
{
wait += pos;
pos = 0;
}
}
else if(OrderProfit()>0)
{
pos = wait;///2;
wait = 0;//wait/2;
}
OrderClose(OrderTicket(),OrderLots(),Ask,3,Green);// or OrderClosePrice()
}
}
}
}
Comments