Here's a breakdown of what this trading script does, explained in simple terms:
Overall Goal:
The script, named "Very Blondie System," is designed to automatically place and manage trades in the Forex market based on certain price movements. It aims to open new trades and close existing ones based on predefined rules.
How it Works:
-
Initial Setup:
- The script starts by checking if there's enough historical data available and if trading is allowed on the platform. If not, it stops.
- It then looks at whether there are any open trades at all. If there aren't, it proceeds to check for opportunities to open new trades. If there are open trades, it proceeds to check conditions to close those trades.
-
Opening New Trades (CheckForOpen):
- The script identifies the highest and lowest price points over a specific recent period (defined by "PeriodX").
- It calculates an initial trade size ("Lots") based on the account balance.
- The script determines whether the current price has moved significantly above the recent highest price or below the recent lowest price (as determined by a factor of
Limit
).- If the price is significantly higher: It opens a "buy" trade (expecting the price to continue upwards). It also places several pending "buy limit" orders (orders to buy if the price drops to specific levels), spaced out according to a "Grid" parameter. The size of each successive pending order increases exponentially.
- If the price is significantly lower: It opens a "sell" trade (expecting the price to continue downwards). Similarly, it places several pending "sell limit" orders (orders to sell if the price rises to specific levels), also spaced out according to the "Grid" parameter and with increasing size.
-
Closing Existing Trades (CheckForClose):
- Profit Target: It checks if the total profit from all open trades is greater than or equal to a specified amount ("Amount"). If so, it closes all open positions.
- "LockDown" Feature: If the "LockDown" feature is enabled (LockDown > 0), the script attempts to protect profits on existing trades by adjusting the stop-loss. If the price has moved a certain distance in a profitable direction (determined by "LockDown"), it sets a stop-loss order to the opening price plus or minus one "Point". The stop-loss is like an emergency exit, if the market reverse it can avoid losing more than the
LockDown
point.
-
Closing All Trades (CloseAll):
- This function simply iterates through all open trades and closes them, regardless of their individual profit or loss. It closes buy trades at the current "bid" price and sell trades at the current "ask" price. It also cancels any pending orders.
-
Calculating Profit (getProfit):
- This function calculates the total profit from all open trades, including any swap fees (interest for holding positions overnight).
Key Parameters:
- PeriodX: The period used to calculate the recent highest and lowest prices.
- Limit: A multiplier that determines how much the price needs to move above or below the recent high/low before a trade is opened.
- Grid: The spacing between the pending "limit" orders.
- Amount: The profit target for closing all trades.
- LockDown: The amount of profit a trade must have before a stop-loss is set to protect gains.
- Slippage: The maximum acceptable difference between the requested price and the actual price when closing a trade.
In essence, the script tries to capitalize on price breakouts by opening initial trades and then adding to those positions with pending orders at specific intervals. It also incorporates a profit-taking mechanism and a feature to protect existing profits.
//+------------------------------------------------------------------+
//| VBS - Very Blondie System.mq4 |
//| David |
//| Broker77@gmail.com |
//+------------------------------------------------------------------+
#property copyright "David"
#property link "broker77@gmail.com"
#define MAGICMA 20081109
extern int PeriodX = 60;
extern int Limit = 1000;
extern int Grid = 1500;
extern int Amount = 40;
extern int LockDown = 0;
int Slippage = 50;
//+------------------------------------------------------------------+
//| Start function |
//+------------------------------------------------------------------+
void start()
{
//---- check for history and trading
if(Bars<100 || IsTradeAllowed()==false) return;
//---- calculate open orders by current symbol
if(OrdersTotal()==0) CheckForOpen();
else CheckForClose();
//----
}
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
int CheckForOpen()
{
double L = Low[iLowest(NULL,0,MODE_LOW,PeriodX,0)];
double H = High[iHighest(NULL,0,MODE_HIGH,PeriodX,0)];
double Lots = MathRound(AccountBalance()/100)/1000;
if((H-Bid>Limit*Point))
{OrderSend(Symbol(),OP_BUY,Lots,Ask,1,0,0,"",MAGICMA,0,CLR_NONE);
for(int i=1; i<5; i++){OrderSend(Symbol(),OP_BUYLIMIT,MathPow(2,i)*Lots,Ask-i*Grid*Point,1,0,0,"",MAGICMA,0,CLR_NONE);}
}
if((Bid-L>Limit*Point))
{OrderSend(Symbol(),OP_SELL,Lots,Bid,1,0,0,"",MAGICMA,0,CLR_NONE);
for(int j=1; j<5; j++){OrderSend(Symbol(),OP_SELLLIMIT,MathPow(2,j)*Lots,Bid+j*Grid*Point,1,0,0,"",MAGICMA,0,CLR_NONE);}
}
}
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
int CheckForClose()
{
if(getProfit()>=Amount){CloseAll();}
if(LockDown>0)
{
for(int TradeNumber = OrdersTotal(); TradeNumber >= 0; TradeNumber--)
{
if (OrderSelect(TradeNumber, SELECT_BY_POS, MODE_TRADES)&&(LockDown>0))
{ int Pos=OrderType();
if((Pos==OP_BUY)&&(Bid-OrderOpenPrice()>Point*LockDown)&&(OrderStopLoss() == 0))
{OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice()+Point,OrderTakeProfit(),0,CLR_NONE);}
if((Pos==OP_SELL)&&(OrderOpenPrice()-Ask>Point*LockDown)&&(OrderStopLoss() == 0))
{OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice()-Point,OrderTakeProfit(),0,CLR_NONE);}
}
}
}
}
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
double getProfit()
{
double Profit = 0;
for (int TradeNumber = OrdersTotal(); TradeNumber >= 0; TradeNumber--)
{
if (OrderSelect(TradeNumber, SELECT_BY_POS, MODE_TRADES))
Profit = Profit + OrderProfit() + OrderSwap();
}
return (Profit);
}
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
void CloseAll()
{
bool Result;
int i,Pos,Error;
int Total=OrdersTotal();
if(Total>0)
{
for(i=Total-1; i>=0; i--)
{
if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES) == TRUE)
{
Pos=OrderType();
if(Pos==OP_BUY)
{Result=OrderClose(OrderTicket(), OrderLots(), Bid, Slippage, CLR_NONE);}
if(Pos==OP_SELL)
{Result=OrderClose(OrderTicket(), OrderLots(), Ask, Slippage, CLR_NONE);}
if((Pos==OP_BUYSTOP)||(Pos==OP_SELLSTOP)||(Pos==OP_BUYLIMIT)||(Pos==OP_SELLLIMIT))
{Result=OrderDelete(OrderTicket(), CLR_NONE);}
//-----------------------
if(Result!=true)
{
Error=GetLastError();
Print("LastError = ",Error);
}
else Error=0;
//-----------------------
}
}
}
return(0);
}
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
Comments