Understanding the Logic of a Metatrader MQL Script: bullbear.mq4
The bullbear.mq4
script is designed for use in MetaTrader, a popular trading platform. It leverages market indicators to manage open trades based on bullish or bearish conditions. Below is an explanation of its logic, broken down into simple terms:
Main Components
- Bulls Power Indicator:
- This technical indicator measures the strength of bulls (buyers) in the market.
- The script compares two values from this indicator:
pos1pre
: Bulls power value from a previous period.pos2cur
: Current Bulls power value.
Trading Logic
The core logic is based on comparing these two indicator values to decide whether to open or close trades:
Closing Trades
-
Closing Long Positions (Buy Orders):
- The script checks for existing buy orders (
OP_BUY
). - If
pos1pre > pos2cur
, it suggests weakening bullish strength. - It then looks at the current bid price and compares it to the open price of each buy order plus a trailing stop value.
- If conditions are met, it closes these long positions.
- The script checks for existing buy orders (
-
Closing Short Positions (Sell Orders):
- Similarly, the script checks for existing sell orders (
OP_SELL
). - If
pos2cur < 0
, it indicates bearish strength. - It examines the current ask price against the open price of each sell order minus a trailing stop value.
- If these conditions are satisfied, it closes short positions.
- Similarly, the script checks for existing sell orders (
Opening Trades
-
Opening Short Positions (Sell Orders):
- When there are no active orders and
pos1pre > pos2cur
withpos2cur > 0
, the script interprets this as a shift towards bearish momentum. - It opens a new sell order at the current bid price, setting stop-loss and take-profit levels.
- When there are no active orders and
-
Opening Long Positions (Buy Orders):
- If there are no open orders and
pos2cur < 0
, it signals bullish conditions. - The script then places a buy order at the ask price with predefined stop-loss and take-profit parameters.
- If there are no open orders and
Additional Functions
-
Trade Management:
- Functions like
MOrderSend
handle placing new trades, incorporating details such as order type (buy/sell), lot size, price, stop-loss, and take-profit.
- Functions like
-
Closing Orders:
- The script uses
OrderClose
to close existing positions based on the calculated conditions.
- The script uses
Summary
The bullbear.mq4
script automates trading decisions by analyzing changes in market sentiment using the Bulls Power indicator. It actively manages open trades and initiates new ones when specific conditions are met, aiming to capitalize on shifts between bullish and bearish trends. The logic is centered around maintaining an optimal position based on real-time market analysis, adjusting positions as necessary to secure profits or minimize losses.
Profitability Reports
/*-----------------------------+
| |
| Shared by www.Aptrafx.com |
| |
+------------------------------*/
//+------------------------------------------------------------------+
//| bullbear.mq4 |
//| Copyright © 2006, Yousky Soft |
//| http://yousky.free.fr |
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
//| Common External variables |
//+------------------------------------------------------------------+
extern double Lots = 1.00;
extern double StopLoss = 30.00;
extern double TakeProfit = 40.00;
extern double TrailingStop = 5.00;
//+------------------------------------------------------------------+
//| External variables |
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
//| Special Convertion Functions |
//+------------------------------------------------------------------+
int LastTradeTime;
bool MOrderDelete( int ticket )
{
LastTradeTime = CurTime();
return ( OrderDelete( ticket ) );
}
bool MOrderClose( int ticket, double lots, double price, int slippage, color Color=CLR_NONE)
{
LastTradeTime = CurTime();
price = MathRound(price*10000)/10000;
return ( OrderClose( ticket, lots, price, slippage, Color) );
}
bool MOrderModify( int ticket, double price, double stoploss, double takeprofit, datetime expiration, color arrow_color=CLR_NONE)
{
LastTradeTime = CurTime();
price = MathRound(price*10000)/10000;
stoploss = MathRound(stoploss*10000)/10000;
takeprofit = MathRound(takeprofit*10000)/10000;
return ( OrderModify( ticket, price, stoploss, takeprofit, expiration, arrow_color) );
}
int MOrderSend( string symbol, int cmd, double volume, double price, int slippage, double stoploss, double takeprofit, string comment="", int magic=0, datetime expiration=0, color arrow_color=CLR_NONE)
{
LastTradeTime = CurTime();
price = MathRound(price*10000)/10000;
stoploss = MathRound(stoploss*10000)/10000;
takeprofit = MathRound(takeprofit*10000)/10000;
return ( OrderSend( symbol, cmd, volume, price, slippage, stoploss, takeprofit, comment, magic, expiration, arrow_color ) );
}
int OrderValueTicket(int index)
{
OrderSelect(index, SELECT_BY_POS);
return(OrderTicket());
}
int OrderValueType(int index)
{
OrderSelect(index, SELECT_BY_POS);
return(OrderType());
}
double OrderValueLots(int index)
{
OrderSelect(index, SELECT_BY_POS);
return(OrderLots());
}
double OrderValueOpenPrice(int index)
{
OrderSelect(index, SELECT_BY_POS);
return(OrderOpenPrice());
}
double OrderValueStopLoss(int index)
{
OrderSelect(index, SELECT_BY_POS);
return(OrderStopLoss());
}
double OrderValueTakeProfit(int index)
{
OrderSelect(index, SELECT_BY_POS);
return(OrderTakeProfit());
}
double OrderValueClosePrice(int index)
{
OrderSelect(index, SELECT_BY_POS);
return(OrderClosePrice());
}
double OrderValueComission(int index)
{
OrderSelect(index, SELECT_BY_POS);
return(OrderCommission());
}
double OrderValueSwap(int index)
{
OrderSelect(index, SELECT_BY_POS);
return(OrderSwap());
}
double OrderValueProfit(int index)
{
OrderSelect(index, SELECT_BY_POS);
return(OrderProfit());
}
string OrderValueSymbol(int index)
{
OrderSelect(index, SELECT_BY_POS);
return(OrderSymbol());
}
string OrderValueComment(int index)
{
OrderSelect(index, SELECT_BY_POS);
return(OrderComment());
}
datetime OrderValueOpenTime(int index)
{
OrderSelect(index, SELECT_BY_POS);
return(OrderOpenTime());
}
datetime OrderValueCloseTime(int index)
{
OrderSelect(index, SELECT_BY_POS);
return(OrderCloseTime());
}
void PrintTrade(int index)
{
if (OrderSelect(index, SELECT_BY_POS)==true)
OrderPrint();
}
bool SetTextObject(string name, string text, string font, int font_size, color text_color=CLR_NONE)
{
ObjectSetText(name, text, font_size, font, text_color);
}
void SetLoopCount(int loops)
{
}
//+------------------------------------------------------------------+
//| End |
//+------------------------------------------------------------------+
int start()
{
//+------------------------------------------------------------------+
//| Local variables |
//+------------------------------------------------------------------+
double pos1pre = 0;
double pos2cur = 0;
int cnt = 0;
/*[[
Name := bullbear
Author := Copyright © 2003, Company
Link := http://www.company.com/
Lots := 1.00
Stop Loss := 30
Take Profit := 40
Trailing Stop := 5
]]*/
//previous position
//current position
pos1pre = iBullsPower(NULL, 0, 13,PRICE_WEIGHTED,1);
pos2cur = iBullsPower(NULL, 0, 13,PRICE_WEIGHTED,0);
if( pos1pre >pos2cur )
{
//close long position
for(cnt=1;cnt<=OrdersTotal();cnt++) {
if( OrderValueType(cnt) == OP_BUY ) // buy position open
{
if( Bid>(OrderValueOpenPrice(cnt)+TrailingStop*Point) )
{
OrderClose(OrderValueTicket(cnt),OrderValueLots(cnt),Bid,3,Violet); // close long
return(0);
}
}
}
}
if( pos2cur<0 )
//close short position
{
for(cnt=1;cnt<=OrdersTotal();cnt++) {
if( Ask<(OrderValueOpenPrice(cnt)-TrailingStop*Point) )
{
if( OrderValueType(cnt) == OP_SELL ) // sell position open
{
OrderClose(OrderValueTicket(cnt),OrderValueLots(cnt),Ask,3,Violet); // close short
return(0);
}
}
}
}
if( OrdersTotal()<1 )
{
//no current orders. Check for possible short conditions.
Print("pos1pre = "+pos1pre+" pos2cur ="+pos2cur);
if( pos1pre>pos2cur && pos2cur>0 )
{
MOrderSend(Symbol(),OP_SELL,Lots,Bid,3,0,Bid-TakeProfit*Point,"",16384,0,Red); // open short
return(0);
}
//check for long conditions.
if( pos2cur<0 )
{
MOrderSend(Symbol(),OP_BUY,Lots,Ask,3,0,Ask+TakeProfit*Point,"",16384,0,Red); // open long
return(0);
}
}
return(0);
}
Comments
Markdown Formatting Guide
# H1
## H2
### H3
**bold text**
*italicized text*
[title](https://www.example.com)

`code`
```
code block
```
> blockquote
- Item 1
- Item 2
1. First item
2. Second item
---