1-tradechannel2_638

Author: Copyright � 2005, Yuri Makarov

Okay, here's a breakdown of the script's logic in plain English, avoiding technical jargon and code snippets.

Overall Purpose

This script appears to be designed to automatically manage and potentially close existing trades based on a set of predefined levels (price targets) that are visually represented on the chart. It also attempts to open new trades under certain conditions. It's essentially a system for automating aspects of trade management.

Key Components and How They Work

  1. Chart Levels (Visual Targets):

    • The script relies on lines or objects drawn on the chart. These lines represent price levels that the system uses to make decisions. These levels are labeled with names like "Buy Stop," "Sell Take," "Buy Take," etc.
    • The script scans the chart to find these lines and reads their current price values.
  2. Existing Trade Management:

    • Checking Open Trades: The script examines all currently open trades for the specific currency pair (e.g., EURUSD).
    • Trade Modification: If an open trade's stop-loss or take-profit levels are significantly different from the chart levels, the script attempts to modify those levels. This means it tries to adjust the trade's stop-loss and take-profit to align with the chart levels.
    • Trade Closure: Under certain conditions (e.g., the current price crossing a chart level), the script will attempt to close an existing trade.
  3. New Trade Opening:

    • Conditions: The script checks for specific price conditions. For example, it might look to open a buy trade if the price is below a "Sell Stop" level and above a "Buy Stop" level. It might open a sell trade if the price is above a "Buy Stop" level.
    • Order Placement: If the conditions are met, the script attempts to place a new trade (either a buy or sell order) with a specified lot size and using the current market price.

Detailed Breakdown of the Logic

  • Initialization: The script starts by finding the price levels drawn on the chart.
  • Trade Evaluation: It then looks at any existing trades. If a trade's stop-loss or take-profit is far from the chart levels, it tries to adjust them.
  • Closing Trades: If the price crosses a certain level, the script might close the trade.
  • Opening New Trades: If the price meets certain conditions (e.g., being above or below specific chart levels), the script attempts to open a new trade.
  • Magic Number: The script uses a "Magic Number" to identify trades that it has opened. This is useful if you have multiple trading systems running on the same account.
  • Trailing Stop: The script has a trailing stop feature, which means that the stop-loss level will automatically move as the price moves in your favor.
  • Slippage: The script includes a slippage parameter, which is the difference between the expected price and the actual price at which the trade is executed.

Important Considerations

  • Chart Levels are Crucial: The script's behavior is entirely dependent on the accuracy and placement of the chart levels. If the levels are incorrect, the script will make incorrect decisions.
  • Risk Management: This script appears to automate trade management, but it's essential to understand the risks involved and to use proper risk management techniques.
  • Backtesting: Before using this script on a live account, it's highly recommended to backtest it on historical data to see how it would have performed.
  • Customization: The script has several parameters that can be customized, such as the lot size, slippage, and trailing stop distance.

I hope this explanation helps you understand the script's logic. Let me know if you have any more questions.

Orders Execution
Checks for the total of open ordersIt Closes Orders by itself It can change open orders parameters, due to possible stepping strategyIt automatically opens orders when conditions are reached
3 Views
0 Downloads
0 Favorites
1-tradechannel2_638
//+------------------------------------------------------------------+
//|                                                TradeChannel2.mq4 |
//|                                   Copyright © 2005, Yuri Makarov |
//|                                       http://mak.tradersmind.com |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2005, Yuri Makarov"
#property link      "http://mak.tradersmind.com"

extern double Lots  = 0.1;
extern int Slippage = 5;
extern int TimeOut  = 10000;
extern int Traling  = 0;
extern int Magic    = 0;

double SetLevel(double Level, double NewLevel, string ObjName = "", int Style = 0)
{
   switch (Style)
   {
   case 1:  // Buy Order line
      ObjectSet(ObjName,OBJPROP_COLOR,Blue);
      ObjectSet(ObjName,OBJPROP_STYLE,STYLE_SOLID);
      ObjectSet(ObjName,OBJPROP_WIDTH,1);
      break;
   case 2:  // Sell Order line
      ObjectSet(ObjName,OBJPROP_COLOR,Red);
      ObjectSet(ObjName,OBJPROP_STYLE,STYLE_SOLID);
      ObjectSet(ObjName,OBJPROP_WIDTH,1);
      break;
   case 3:  // Buy Stop line
      ObjectSet(ObjName,OBJPROP_COLOR,Blue);
      ObjectSet(ObjName,OBJPROP_STYLE,STYLE_DASH);
      ObjectSet(ObjName,OBJPROP_WIDTH,1);
      break;
   case 4:  // Sell Stop line
      ObjectSet(ObjName,OBJPROP_COLOR,Red);
      ObjectSet(ObjName,OBJPROP_STYLE,STYLE_DASH);
      ObjectSet(ObjName,OBJPROP_WIDTH,1);
      break;
   case 5:  // Buy Take line
      ObjectSet(ObjName,OBJPROP_COLOR,Blue);
      ObjectSet(ObjName,OBJPROP_STYLE,STYLE_DOT);
      ObjectSet(ObjName,OBJPROP_WIDTH,1);
      break;
   case 6:  // Sell Take line
      ObjectSet(ObjName,OBJPROP_COLOR,Red);
      ObjectSet(ObjName,OBJPROP_STYLE,STYLE_DOT);
      ObjectSet(ObjName,OBJPROP_WIDTH,1);
      break;
   }
   
   if (MathAbs(NewLevel - Close[0]) < MathAbs(Level - Close[0])) return (NewLevel);
   else return (Level);
}

int start()
{

   int    NumObj = ObjectsTotal();
   double Spread = Ask - Bid;
   
   double pBuy  = 0;
   double pSell = 0;
   double pBuyStop = 0;
   double pBuyTake = 0;
   double pSellStop = 0;
   double pSellTake = 0;

   for (int i = 0; i < NumObj; i++)
   {
      string ObjName = ObjectName(i);
      string ObjDesc = ObjectDescription(ObjName);
      double Price = 0;

      switch (ObjectType(ObjName))
      {
      case OBJ_HLINE:
         Price = ObjectGet(ObjName,OBJPROP_PRICE1); 
         break;
      case OBJ_TREND:
         Price = ObjectGetValueByShift(ObjName,0); 
         break;
      }

      if (Price > 0)
      {
         if (ObjDesc == "Buy")  
         {
            pBuy      = SetLevel(pBuy,  Price, ObjName, 1); 
            pSellTake = SetLevel(pSellTake,  Price); 
         } else
         if (ObjDesc == "Sell") 
         {
            pSell    = SetLevel(pSell, Price, ObjName, 2); 
            pBuyTake = SetLevel(pBuyTake, Price); 
         } else
         if (ObjDesc == "Stop") 
         {
            if (Price < Close[0]) 
            {
               pBuyStop = SetLevel(pBuyStop, Price, ObjName, 3);
               pSellTake = SetLevel(pSellTake,  Price); 
            } else 
            {
               pSellStop = SetLevel(pSellStop, Price, ObjName, 4); 
               pBuyTake = SetLevel(pBuyTake, Price); 
            }
         } else
         if (ObjDesc == "Take") 
         {
            if (Price > Close[0]) 
            {
               pBuyTake = SetLevel(pBuyTake, Price, ObjName, 5);
               pSellStop = SetLevel(pSellStop,  Price); 
            } else 
            {
               pSellTake = SetLevel(pSellTake, Price, ObjName, 6);
               pBuyStop = SetLevel(pBuyStop, Price); 
            }
         }
      }
   }
   
   int NumOrders = OrdersTotal();
   int NumPos = 0;

   for (i = 0; i < NumOrders; i++)
   {
      if (!OrderSelect(i, SELECT_BY_POS)) continue;
      if (OrderSymbol() != Symbol()) continue;
      if (Magic > 0 && Magic != OrderMagicNumber()) continue;
      
      NumPos++;

      double tp = OrderTakeProfit();
      double sl = OrderStopLoss();
      double ts = 0;

      if (OrderType() == OP_BUY)
      {
         if (Bid > pSell && pSell > 0)
         {
            OrderClose(OrderTicket(), OrderLots(), Bid, Slippage, Red);
            Sleep(TimeOut);
            return(0);
         }

         ts = pBuyStop;
         if (Traling > 10) ts = MathMax(pBuyStop, Bid - Traling*Point);
         
         if (MathAbs(tp - pBuyTake) > Spread || MathAbs(sl - ts) > Spread) 
         {
            OrderModify(OrderTicket(), Ask, ts, pBuyTake, 0);
            Sleep(TimeOut);
            return(0);
         }
      }

      if (OrderType() == OP_SELL)
      {
         if (Ask < pBuy)
         {
            OrderClose(OrderTicket(), OrderLots(), Ask, Slippage, Red);
            Sleep(TimeOut);
            return(0);
         }

         ts = pSellStop;
         if (Traling > 10)
         if (pSellStop > 0) ts = MathMin(pSellStop, Ask + Traling*Point);
         else               ts = Ask + Traling*Point;
         
         if (MathAbs(tp - pSellTake) > Spread || MathAbs(sl - ts) > Spread) 
         {
            OrderModify(OrderTicket(), Bid, ts, pSellTake, 0);
            Sleep(TimeOut);
            return(0);
         }
      }
   }
   
   if (NumPos > 0) return(0);
   if (pSell > 0 && (pSell - pBuy) < Spread*2) return(0);
      
   if (Bid > pSell && pSell > pBuyStop)
   {
      OrderSend(Symbol(), OP_SELL, Lots, Bid, Slippage, pSellStop, pSellTake,
         "Magic: "+Magic+" ", Magic);
      Sleep(TimeOut);
      return(0);
   }

   if (Ask < pBuy && (pBuy < pSellStop || pSellStop == 0))
   {
      OrderSend(Symbol(), OP_BUY, Lots, Ask, Slippage, pBuyStop, pBuyTake,
         "Magic: "+Magic+" ", Magic);
      Sleep(TimeOut);
      return(0);
   }
}

int init()   {}
int deinit() {}

Comments

Markdown supported. Formatting help

Markdown Formatting Guide

Element Markdown Syntax
Heading # H1
## H2
### H3
Bold **bold text**
Italic *italicized text*
Link [title](https://www.example.com)
Image ![alt text](image.jpg)
Code `code`
Code Block ```
code block
```
Quote > blockquote
Unordered List - Item 1
- Item 2
Ordered List 1. First item
2. Second item
Horizontal Rule ---