Here's an explanation of what this trading script does, avoiding technical jargon and focusing on the core logic:
This script is designed to automatically trade in the financial markets based on certain conditions. It's like having a robot that places buy and sell orders for you.
Here's the breakdown of what the script does:
-
Setup (Initialization): When the script starts, it prepares itself. It doesn't do much in the initial setup, but it's ready to go.
-
Closing Orders (Take Profit): The script constantly watches existing open trades.
- If you have a "buy" order and the price goes high enough (reaching a profit target), the script automatically closes that order to secure your profit.
- Similarly, if you have a "sell" order and the price goes low enough (reaching a profit target), the script closes the order to secure the profit.
-
Margin Check: The script checks if there's enough available money in the trading account to cover potential losses. If the available margin is low, the script stops opening new trades to avoid running out of funds.
-
Opening Initial Orders: If there aren't any trades currently open, the script looks for opportunities to buy or sell based on an indicator called "Stochastic Oscillator". The Stochastic Oscillator is used as a signal of overbought or oversold conditions:
- If the Stochastic Oscillator suggests the market is "oversold" (likely to go up), the script places a "buy" order.
- If the Stochastic Oscillator suggests the market is "overbought" (likely to go down), the script places a "sell" order.
-
Adding to Existing Orders (Martingale-like): If a trade is already open:
- If you have a "buy" order, and the price goes higher it does the same of closing orders (see item 2).
- If you have a "buy" order and the price goes against you (goes down), the script opens another "buy" order. The size of the new order is bigger than the original, using a multiplying factor to try and recover potential losses quickly when the price eventually goes back up.
- The same logic applies to "sell" orders, but in the opposite direction. If the price goes up against the "sell" order, the script opens another, larger "sell" order.
-
Calculating Profit Targets: After an order is open, the script calculates the "take profit" level, where it will automatically close the trade to secure a profit. The target take profit is bigger with bigger ammount of opened orders.
-
Displaying Information: The script displays information on the chart, such as the total number of open trades, the total size of all trades, and the calculated profit levels. It also shows the free margin and account balance.
In simple terms: This script tries to make money by automatically buying and selling, aiming to close trades for a profit when prices move in the desired direction. When prices move against the script, it tries to compensate by opening additional, larger orders.
//+------------------------------------------------------------------+
//| aaa.mq4 |
//| Copyright © 2007, MetaQuotes Software Corp. |
//| http://www.metaquotes.net |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2007, MetaQuotes Software Corp."
#property link "http://www.metaquotes.net"
extern double step=25;
extern double proffactor=9;
extern double mult=1.6;
extern double lots=0.3;
extern double per_K=20;
extern double per_D=6;
extern double slow=6;
extern double zoneBUY=50;
extern double zoneSELL=50;
double openprice,ask,n,lots2,tp,total,cnt,sm,rtpbay,rtpsell,free,balance;
//+------------------------------------------------------------------+
//| expert initialization function |
//+------------------------------------------------------------------+
int init()
{
//----
//----
return(0);
}
//+------------------------------------------------------------------+
//| expert deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
//----
//----
return(0);
}
//+------------------------------------------------------------------+
//| expert start function |
//+------------------------------------------------------------------+
int start()
{
//----
if (Ask>=openprice+tp*Point)
for(cnt = OrdersTotal(); cnt >= 0; cnt--)
{
OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
if(OrderSymbol() == Symbol())
{
if(OrderType() == OP_BUY)
{
OrderClose(OrderTicket(), OrderLots(), Ask, 3, Yellow);lots2=lots;
}
}
}
if (Bid<=openprice-tp*Point)
for(cnt = OrdersTotal(); cnt >= 0; cnt--)
{
OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
if(OrderSymbol() == Symbol())
{
if(OrderType() == OP_SELL)
{
OrderClose(OrderTicket(), OrderLots(), Bid, 3, Yellow);lots2=lots;
}
}
}
free=AccountFreeMargin();balance=AccountBalance();
if (AccountFreeMargin()<=AccountBalance()/2)return(0);
total=OrdersTotal();
if(total<1)
{
if(iStochastic(NULL,0,per_K,per_D,slow,MODE_LWMA,1,0,1)>iStochastic(NULL,0,per_K,per_D,slow,MODE_LWMA,1,1,1)
&& iStochastic(NULL,0,per_K,per_D,slow,MODE_LWMA,1,1,1)>zoneBUY)
n=OrderSend(Symbol(),OP_BUY,lots,Ask,3,0,0,"",0,0,Green);
if(iStochastic(NULL,0,per_K,per_D,slow,MODE_LWMA,1,0,1)<iStochastic(NULL,0,per_K,per_D,slow,MODE_LWMA,1,1,1)
&& iStochastic(NULL,0,per_K,per_D,slow,MODE_LWMA,1,1,1)<zoneSELL)
n=OrderSend(Symbol(),OP_SELL,lots,Bid,3,0,0,"",0,0,Red);
}
total=OrdersTotal();
OrderSelect(n,SELECT_BY_TICKET, MODE_TRADES);
openprice=OrderOpenPrice();
if(total>0)
{
if(OrderType() == OP_BUY)
{
if (Ask>=openprice+tp*Point)lots2=lots;
if (Ask>=openprice+tp*Point)n=OrderSend(Symbol(),OP_BUY,lots2,Ask,3,0,0,"",0,0,Blue);
if (Ask<=openprice-step*Point)lots2=lots2*mult;
if (Ask<=openprice-step*Point)n=OrderSend(Symbol(),OP_BUY,NormalizeDouble(lots2,1),Ask,3,0,0,"",0,0,Blue);
}
if(OrderType() == OP_SELL)
{
if (Bid<=openprice-tp*Point)lots2=lots;
if (Bid<=openprice-tp*Point)n=OrderSend(Symbol(),OP_SELL,lots2,Bid,3,0,0,"",0,0,Red);
if (Bid>=openprice+step*Point)lots2=lots2*mult;
if (Bid>=openprice+step*Point)n=OrderSend(Symbol(),OP_SELL,NormalizeDouble(lots2,1),Bid,3,0,0,"",0,0,Red);
}
}
total=OrdersTotal();
OrderSelect(n,SELECT_BY_TICKET, MODE_TRADES);
openprice=OrderOpenPrice();
if (total>0) tp=total*proffactor;
rtpbay=openprice+tp*Point;rtpsell=openprice-tp*Point;
{
double sm;
total = OrdersTotal();
for(cnt = 0; cnt < total; cnt++)
{
OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
{
sm = sm + OrderLots();
}
}
Comment("Total = ",total," Lot = ",sm," TakeProfitSell = ",rtpsell," TakeProfitBay = ",rtpbay,
" FreeMargin = ",free," Balance = ",balance);
}
//----
return(0);
}
//+------------------------------------------------------------------+
Comments