This script is designed to automate a trading strategy based on "price trapping." Think of it as setting up a series of "traps" or pending orders around the current market price, hoping to catch profitable trades as the price moves. Here's the breakdown:
1. Initial Setup ("Price Catcher"):
-
The script starts by checking if there are any existing "traps" (pending orders) already set up for the specific currency pair and a unique identifier ("Magic Number") assigned to this script.
-
If no traps exist, it calculates two target prices: a "Buy Goal" (a price above the current market price) and a "Sell Goal" (a price below the current market price). These goals are set based on the input parameters "pips" (a measure of price movement) and "NbLevels" (number of pending orders).
-
It then creates a series of "Buy Stop" and "Sell Stop" orders. "Buy Stop" orders are placed at incrementally higher prices than the current market price, acting as traps if the price moves upwards. "Sell Stop" orders are placed at incrementally lower prices, acting as traps if the price moves downwards. The distance between each order level is determined by the "pips" setting. The quantity of currency to trade ("lots") is also set based on your settings.
2. Ongoing Monitoring and Adjustment:
-
If initial traps do exist, the script monitors the market and existing orders. It checks if the current market price has already reached the "Buy Goal" or "Sell Goal." If it has, it means the initial trap has been triggered, and the script assumes the profit target has been reached or missed. In this case, it removes all existing pending orders associated with the script.
-
If the price hasn't hit the goals, the script checks if the current market price is still within a range that makes the initial goals valid. It checks if the current market price is more than
pips
away from either the buy or sell goals, the script enters a profit-checking process.
3. Profit Target and Order Adjustment:
-
The script calculates the potential profit or loss if the price reaches either the "Buy Goal" or "Sell Goal." It considers all existing open positions (trades) and pending orders associated with the script.
-
It then compares this potential profit to the "ProfitTarget" parameter. If the potential profit is less than the target for either the buy or sell goal, it adds additional "Buy Stop" or "Sell Stop" orders to increase the potential profit. These new orders are placed outside the existing set of traps. The script limits adding an additional order to the "NbLevels" parameter.
In Simple Terms:
The script tries to make a profit by setting up a grid of orders around the current market price. If the price starts moving up or down, it triggers these orders. The script checks regularly if the original setup is still valid or has a high enough profit margin and adjusts the number and placement of those orders to ensure that its final profit is as high as the parameter "ProfitTarget". The script is set up in such a way that even if the price does a "u-turn" after triggering some of the orders, it should guarantee "ProfitTarget" pips profit.
//+------------------------------------------------------------------+
//| Williama PriceTrapper.mq4 |
//| Based on the great strategie of Williama |
//| Coded by Naufrage, All rights granted to you :) |
//| |
//+------------------------------------------------------------------+
#property copyright "Coded by Raphy, All rights granted to you :)"
#property link ""
#include <stdlib.mqh>
#include <stderror.mqh>
//---- input parameters
extern int pips=15; // number of pips between each level
extern double lots=0.01;
extern int NbLevels=3; // Number of levels of the pending orders (for each target, buy and sell)
extern int ProfitTarget=0; //Minimum profit target, in pips. Whatever happen, at least this profit is made (unless we run out of free margin...).
extern int Magic=17663;
double myPoint;
//+------------------------------------------------------------------+
//| expert initialization function |
//+------------------------------------------------------------------+
int init()
{
//----
myPoint = SetPoint(Symbol());
//----
return(0);
}
//+------------------------------------------------------------------+
//| expert deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
//----
//----
return(0);
}
//+------------------------------------------------------------------+
//| expert start function |
//+------------------------------------------------------------------+
int start()
{
int ticket, err, cpt, profit, total, BuyGoalProfit, SellGoalProfit;
double BuyGoal=0, SellGoal=0, spread=(Ask-Bid)/myPoint;
//----
// total=OrdersTotal();
total = CalculateCurrentOrders(Symbol(), Magic);
if(total<1) //we can set up a new "price catcher"
{
if(AccountFreeMargin()<(10000*lots))
{
Print("Not enough free margin to begin");
return(0);
}
SellGoal=Ask-(NbLevels+1)*pips*myPoint;
BuyGoal=Ask+(NbLevels+1)*pips*myPoint;
for(cpt=1;cpt<=NbLevels;cpt++)
{
ticket=OrderSend(Symbol(),OP_BUYSTOP,lots,Ask+cpt*pips*myPoint,2,SellGoal,BuyGoal,"InitialSetup",Magic,0,Green);
if(ticket==0)
{
err = GetLastError();
Print("Error opening BUYSTOP order : (" + err + ") " + ErrorDescription(err));
return(0);
}
ticket=OrderSend(Symbol(),OP_SELLSTOP,lots,Bid-cpt*pips*myPoint,2,BuyGoal+spread*myPoint,SellGoal-spread*myPoint,"InitialSetup",Magic,0,Green);
if(ticket==0)
{
err = GetLastError();
Print("Error opening SELLSTOP order : (" + err + ") " + ErrorDescription(err));
return(0);
}
}
} // initial setup done
else
{
OrderSelect(0, SELECT_BY_POS, MODE_TRADES);
cpt=0;
while(OrderType()!=OP_BUY && OrderType()!=OP_BUYSTOP)
{
OrderSelect(cpt, SELECT_BY_POS, MODE_TRADES);
cpt++;
}
BuyGoal=OrderTakeProfit();
SellGoal=OrderStopLoss();
// check if pending orders must be canceled
if(Ask>=BuyGoal || (Ask<=SellGoal))
{
for(cpt=0;cpt<total;cpt++)
{
OrderSelect(cpt,SELECT_BY_POS, MODE_TRADES);
if(OrderSymbol()==Symbol() && OrderMagicNumber()==Magic) OrderDelete(OrderTicket());
}
return(0);
}
else if((Ask+pips*myPoint)<=BuyGoal && (Ask-pips*myPoint)>=SellGoal)
// now we check if profit will be made no matter which goal is reached, and add pending orders if necessary
{
//first, check profit if the BuyGoal is reached
profit=0;
for(cpt=0;cpt<total;cpt++)
{
OrderSelect(cpt, SELECT_BY_POS, MODE_TRADES);
if(OrderSymbol()==Symbol() && OrderMagicNumber()==Magic)
{
if(OrderType()==OP_BUY) profit+=(BuyGoal-OrderOpenPrice())/myPoint;
if(OrderType()==OP_SELL) profit-=((BuyGoal+spread*myPoint)-OrderOpenPrice())/myPoint;
if(OrderType()==OP_BUYSTOP && OrderOpenPrice()>Ask) profit+=(BuyGoal-OrderOpenPrice())/myPoint;
}
}
cpt=1;
BuyGoalProfit=profit;
while(profit<ProfitTarget)
{
if((Ask+MarketInfo(Symbol(),MODE_STOPLEVEL)*myPoint)<(BuyGoal-cpt*pips*myPoint))
{
ticket=OrderSend(Symbol(),OP_BUYSTOP,lots,BuyGoal-cpt*pips*myPoint,2,SellGoal,BuyGoal,"",Magic,0,Green);
if(ticket==0)
{
err = GetLastError();
Print("Error opening BUYSTOP order : (" + err + ") " + ErrorDescription(err));
return(0);
}
profit+=cpt*pips;
}
cpt++;
if(cpt>NbLevels) cpt=1;
}
// then, check profit if SellGoal is reached
profit=0;
for(cpt=0;cpt<total;cpt++)
{
OrderSelect(cpt, SELECT_BY_POS, MODE_TRADES);
if(OrderSymbol()==Symbol() && OrderMagicNumber()==Magic)
{
if(OrderType()==OP_BUY) profit-=(OrderOpenPrice()-SellGoal)/myPoint;
if(OrderType()==OP_SELL) profit+=(OrderOpenPrice()-(SellGoal-spread*myPoint))/myPoint;
if(OrderType()==OP_SELLSTOP && OrderOpenPrice()<Bid) profit+=(OrderOpenPrice()-(SellGoal-spread*myPoint))/myPoint;
}
}
cpt=1;
SellGoalProfit=profit;
while(profit<ProfitTarget)
{
if((Bid+MarketInfo(Symbol(),MODE_STOPLEVEL)*myPoint)>(SellGoal+cpt*pips*myPoint))
{
ticket=OrderSend(Symbol(),OP_SELLSTOP,lots,(SellGoal-spread*myPoint)+cpt*pips*myPoint,2,BuyGoal+spread*myPoint,SellGoal-spread*myPoint,"",Magic,0,Green);
if(ticket==0)
{
err = GetLastError();
Print("Error opening SELLSTOP order : (" + err + ") " + ErrorDescription(err));
return(0);
}
profit+=cpt*pips;
}
cpt++;
if(cpt>NbLevels) cpt=1;
}
}
}
string sComment = "";
string sep = "----------------------------------------\n";
string nl = "\n";
sComment = "Williama EA v.0.1" + nl;
sComment = sComment + "Lots=" + DoubleToStr(lots,2) + nl;
sComment = sComment + "Buy Goal= " + DoubleToStr(BuyGoal,4) + nl;
sComment = sComment + "Buy Goal Profit (in pips)=" + BuyGoalProfit + nl + nl;
sComment = sComment + "Sell Goal= " + DoubleToStr(SellGoal,4) + nl;
sComment = sComment + "Sell Goal Profit (in pips)=" + SellGoalProfit + nl + nl;
sComment = sComment + "Pips of each level=" + pips + nl;
sComment = sComment + "Number of levels for each goal: " + NbLevels + nl;
sComment = sComment + "Spread= " + DoubleToStr(spread,1) + nl;
sComment = sComment + sep;
Comment(sComment);
//----
return(0);
}
double SetPoint(string mySymbol)
{
double mPoint, myDigits;
myDigits = MarketInfo (mySymbol, MODE_DIGITS);
if (myDigits < 4)
mPoint = 0.01;
else
mPoint = 0.0001;
return(mPoint);
}
int CalculateCurrentOrders(string mySymbol, int MagicNumber)
{
int buys = 0, sells = 0, num = 0;
for(int i=0;i<OrdersTotal();i++)
{
OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
if(OrderSymbol()!= mySymbol) continue;
if (OrderMagicNumber()!= MagicNumber) continue;
if(OrderType() == OP_BUY) buys++;
if(OrderType() == OP_SELL) sells++;
if(OrderType() == OP_BUYSTOP) buys++;
if(OrderType() == OP_SELLSTOP) sells++;
}
num = buys + sells;
return(num);
}
//+------------------------------------------------------------------+
Comments