Williama_EA[2]

Author: Coded by Raphy, All rights granted to you :)
Profit factor:
0.98

Okay, here's an explanation of what this MetaTrader script does, written for someone who doesn't know programming:

This script, called "Williama PriceTrapper," is designed to automatically place and manage orders in the currency market with the goal of "trapping" profits based on price movements. It operates by setting up a series of pending orders both above and below the current price.

Here's the breakdown:

  1. Initial Setup:

    • The script first checks if it has already set up its initial orders. It counts the current orders associated with this specific script, identified by a "magic number." If there are no existing orders from this script, it proceeds to set them up.
    • It also checks if there's enough money (free margin) in your account to place the orders. If not, it stops.
    • It determines two "goal" prices: a "Buy Goal" and a "Sell Goal." These goals are calculated based on how many "pips" (a unit of price movement) you want to allow between each order and how many levels of orders you want to create.
    • Then, it places a series of "Buy Stop" orders above the current price and a series of "Sell Stop" orders below the current price. These orders are set to automatically trigger if the price moves up to the "Buy Stop" price or down to the "Sell Stop" price. The distances of each level depends on the input parameters.
  2. Ongoing Management:

    • After the initial setup, the script continuously monitors the market. It checks the current price against the "Buy Goal" and "Sell Goal."
    • Order Cancellation: If the price reaches or exceeds either the "Buy Goal" or "Sell Goal," the script assumes the initial setup is no longer valid and cancels all of its pending orders.
    • Profit Check & Order Adjustment: If the price is still within the range defined by the "Buy Goal" and "Sell Goal", the script checks if all of our orders together are projected to reach the profit target defined. If not, the script then places additional pending orders to increase the potential profit. It does this both above the current price (Buy Stop orders) and below the current price (Sell Stop orders). This is a continuous process, meaning the script will keep adding orders until the minimum profit is reached.
  3. How it makes money:

    • The script hopes to capture movement that is short lived. The additional orders are placed to ensure a profit is made no matter which way the price moves.
  4. Key Parameters:

    • Pips: The distance, in pips, between each level of the pending orders.
    • Lots: The size of each order (e.g., 0.01 lots).
    • NbLevels: The number of pending order levels to create above and below the current price.
    • ProfitTarget: The minimum profit, in pips, that the script aims to achieve.
  5. Display:

    • Finally, the script displays some information on the chart, such as the "Buy Goal," "Sell Goal," and the calculated profit.

In essence, this script automates a "price trapping" strategy by strategically placing pending orders to take advantage of price fluctuations. The parameters allow you to customize the risk and reward profile of the strategy.

Orders Execution
It automatically opens orders when conditions are reachedChecks for the total of open orders
3 Views
0 Downloads
0 Favorites
Williama_EA[2]
//+------------------------------------------------------------------+
//|                                        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...).

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);
  }

//+------------------------------------------------------------------+

Profitability Reports

USD/CAD Oct 2024 - Jan 2025
1.32
Total Trades 334
Won Trades 263
Lost trades 71
Win Rate 78.74 %
Expected payoff 0.40
Gross Profit 550.29
Gross Loss -418.09
Total Net Profit 132.20
-100%
-50%
0%
50%
100%
NZD/USD Oct 2024 - Jan 2025
0.54
Total Trades 258
Won Trades 129
Lost trades 129
Win Rate 50.00 %
Expected payoff -1.25
Gross Profit 384.00
Gross Loss -707.14
Total Net Profit -323.14
-100%
-50%
0%
50%
100%
GBP/USD Oct 2024 - Jan 2025
1.16
Total Trades 728
Won Trades 557
Lost trades 171
Win Rate 76.51 %
Expected payoff 0.31
Gross Profit 1651.89
Gross Loss -1427.42
Total Net Profit 224.47
-100%
-50%
0%
50%
100%
GBP/CAD Oct 2024 - Jan 2025
0.67
Total Trades 887
Won Trades 0
Lost trades 0
Win Rate 0.00 %
Expected payoff -0.74
Gross Profit 1343.60
Gross Loss -1998.03
Total Net Profit -654.43
-100%
-50%
0%
50%
100%
AUD/USD Oct 2024 - Jan 2025
1.21
Total Trades 275
Won Trades 0
Lost trades 0
Win Rate 0.00 %
Expected payoff 0.40
Gross Profit 625.28
Gross Loss -515.98
Total Net Profit 109.30
-100%
-50%
0%
50%
100%

Comments