Okay, here's a breakdown of what this trading script does, explained in plain language without getting into technical programming details.
This script, named "BlondeTrader," is designed to automatically trade in the Forex market. It essentially follows these steps:
-
Initial Checks: When it starts running, it first makes sure there's enough historical data available and that automated trading is allowed on the platform. If not, it stops.
-
Order Management: It checks if there are any existing open trades. If no trades are open, it looks for opportunities to open new ones. If there are already open trades, it checks if it should close them.
-
Opening New Trades (CheckForOpen):
- It looks back a specified number of periods (defined by "PeriodX") to find the highest high and the lowest low prices during that time.
- It calculates the lot size based on account balance (amount to trade).
- If the current asking price is significantly higher than the highest price found (defined by "Limit"), it places a "Buy" order. It also places a series of "Buy Limit" orders (orders to buy at a lower price) below the current asking price, spaced apart by a set "Grid" distance. The lot size increases by a power of 2 for each "Buy Limit" order. This is a grid trading strategy, betting the price will move up eventually.
- Conversely, if the current bidding price is significantly lower than the lowest price found (defined by "Limit"), it places a "Sell" order. It also places a series of "Sell Limit" orders (orders to sell at a higher price) above the current bidding price, spaced apart by a set "Grid" distance. The lot size increases by a power of 2 for each "Sell Limit" order. This is a grid trading strategy, betting the price will move down eventually.
-
Closing Trades (CheckForClose):
- It calculates the total profit from all open trades. If the total profit is equal to or greater than a defined target amount ("Amount"), it closes all open trades.
- It has a "LockDown" feature: If this value is greater than zero, the script tries to modify existing trades by setting a stop-loss price.
- For "Buy" orders, if the current bidding price is higher than the opening price by a certain amount ("LockDown"), the script sets the stop-loss to the order's open price plus one point. This creates a trailing stop.
- For "Sell" orders, if the current asking price is lower than the opening price by a certain amount ("LockDown"), the script sets the stop-loss to the order's open price minus one point. This creates a trailing stop.
-
Calculating Profit (getProfit): It adds up the profit and swap charges (interest) from all open orders to determine the total profit.
-
Closing All Trades (CloseAll): This function iterates through all open orders and closes them. It closes "Buy" orders at the current bidding price and "Sell" orders at the current asking price. It also deletes any pending orders (Buy Stop, Sell Stop, Buy Limit, Sell Limit).
In essence, the script attempts to identify potential price extremes, opens trades in anticipation of a reversal, uses a grid strategy to accumulate positions, and then closes all trades when a target profit is reached. It also features a trailing stop mechanism to protect profits once a trade moves favorably.
//+------------------------------------------------------------------+
//| BlondeTrader.mq4 |
//| Christopher |
//| http://expert-profit.webs.com |
//+------------------------------------------------------------------+
#property copyright "Christopher"
#property link "http://expert-profit.webs.com"
#define MAGICMA 20081109
extern int PeriodX = 1;
extern int Limit = 20;
extern int Grid = 20;
extern int Amount = 2;
extern int LockDown = 0;
int Slippage = 3;
//+------------------------------------------------------------------+
//| 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