AltrTrendexp(23SEP05)

Author: Smoky's Trendscalper modified by TR_n00btrader@frisurf.no
This script is designed to automatically trade on the Forex market using the MetaTrader platform. It analyzes price trends and places buy or sell orders based on these trends. Here's a breakdown of how it works:

**1. Initial Setup:**

*   **Customizable Settings:** The script starts with a few settings you can change, like how much money to risk on each trade, how far away to set the stop-loss (a safety net to limit losses), how much price movement to allow before a trade is executed (slippage), and how to manage the size of trades based on your account balance.

**2. Money Management:**

*   **Risk-Based Trading:** The script can automatically adjust the size of each trade based on your account balance and the risk level you set. A higher risk setting means larger trades, which can lead to bigger profits but also bigger losses. Alternatively, it can use a fixed lot size, regardless of your account balance.

**3. Trend Analysis and Trade Execution:**

*   **Trend Detection:** The script uses a custom indicator called "AltrTrend\_Signal\_v2\_2" to determine the direction of the current price trend. Think of this indicator as a tool that helps identify whether the price is generally going up or down.

*   **Opening Trades:**
    *   If the custom indicator signals an upward trend, and there are no open orders for the current trading pair, the script will place a buy order.
    *   If the custom indicator signals a downward trend, and there are no open orders for the current trading pair, the script will place a sell order.

*   **Closing Existing Trades (opposite signal):**
    *   When the custom indicator signals an upward trend and the script detects any existing open orders for the current trading pair, it first closes any existing orders, and then it opens a new buy order.
    *   When the custom indicator signals a downward trend and the script detects any existing open orders for the current trading pair, it first closes any existing orders, and then it opens a new sell order.

**In Simple Terms:**

The script watches the market for trends. When it sees a trend forming (using the "AltrTrend\_Signal\_v2\_2" indicator), it places a trade in that direction. If the trend changes, it closes the existing trade (if any) and opens a new trade aligned with the new trend. The size of the trades and the amount of risk are managed based on the settings you choose.
Price Data Components
Series array that contains tick volumes of each bar
Orders Execution
Checks for the total of open ordersIt Closes Orders by itself It automatically opens orders when conditions are reachedIt can change open orders parameters, due to possible stepping strategy
Indicators Used
Bollinger bands indicatorIndicator of the average true range
2 Views
0 Downloads
0 Favorites
AltrTrendexp(23SEP05)
/*-----------------------------+
|			       |
| Shared by www.Aptrafx.com    |
|			       |
+------------------------------*/

//+------------------------------------------------------------------+
//|                                              TrendScalper_TR.mq4 |
//|                      Copyright © 2004, MetaQuotes Software Corp. |
//|                                       http://www.metaquotes.net/ |
//+------------------------------------------------------------------+
#property copyright "Smoky's Trendscalper modified by TR_n00btrader@frisurf.no"
#property link      "http://www.metaquotes.net/"

//---- input parameters
extern double    LotsIfNoMM=0.1;
extern int       Stoploss=200;
extern int       Slip=5;
extern int       InitialTarget=999;
extern int       MM_Mode=1;
extern int       MM_Risk=40;
//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
  {
//---- 

//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
  {
//---- 
   
//----
   return(0);
  }


double Opentrades,orders,first,mode,cnt,Ilo,sym,b;
double b4signal,Signal,Triggerline,b4Triggerline,Nowsignal,NowTriggerline,sl,LastOpByExpert,LastBarChecked;


//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
  {

   Comment(" Trailingstop    ",  b, "\n","      Tick no. ", iVolume(NULL,0,0),
     "\n"," Lots    ",Ilo);


   /**********************************Money and Risk Management***************************************
   Changing the value of mm will give you several money management options
   mm = 0 : Single 1 lot orders.
   mm = -1 : Fractional lots/ balance X the risk factor.(use for Mini accts)
   mm = 1 : Full lots/ balance X the risk factor up to 100 lot orders.(use for Regular accounts)
   ***************************************************************************************************
   RISK FACTOR:
   risk can be anything from 1 up. 
   Factor of 5 adds a lot for every $20,000.00 added to the balance. 
   Factor of 10 adds a lot with every $10.000.00 added to the balance.
   The higher the risk,  the easier it is to blow your margin..
   **************************************************************************************************/

   if (MM_Mode < 0)  {
   Ilo = MathCeil(AccountBalance()*MM_Risk/10000)/10;
     if (Ilo > 100) {  
     Ilo = 100;  
     }
   } else {
   Ilo = LotsIfNoMM;
   }
   if (MM_Mode > 0)  
    {
   Ilo = MathCeil(AccountBalance()*MM_Risk/10000)/10;
    if (Ilo > 1)  
    {
    Ilo = MathCeil(Ilo);
    }
    if (Ilo < 1)  
    {
    Ilo = 1;
    }
    if (Ilo > 100)  
    {  
     Ilo = 100;  
     }
   }


     if (LastBarChecked == Time[0]) 
         return(0); 
      else 
  	      {
  	      LastBarChecked = Time[0]; 


         Opentrades=0;
         for (cnt=0;cnt<OrdersTotal();cnt++) 
         {
            if ( OrderSelect (cnt, SELECT_BY_POS) == false )  continue;
            if ( OrderSymbol()==Symbol()) Opentrades=Opentrades+1;
         }


         if (iCustom(NULL,0,"AltrTrend_Signal_v2_2",30,0.5,150,14,999999,0,1)!=0)
            {
            if (Opentrades != 0)  
   
              {
     
               orders=0;
               sym=0;
               for (cnt=0;cnt<40;cnt++) 
                  { 
                  Print("asdf");


                  if ( OrderSelect (cnt, SELECT_BY_POS) == false )  continue;

                  if ( OrderSymbol()==Symbol())
                     {
                        mode=OrderType();
                        if (mode==OP_BUY)   
                         {
                             OrderClose(OrderTicket(),OrderLots(),Bid,Slip,Red);
                             } 

                         if (mode==OP_SELL) 
                                 {
                                 OrderClose(OrderTicket(),OrderLots(),Ask,Slip,Red);
                                 }

                     }
                  } 

            }

            OrderSend(Symbol(),OP_BUY,Ilo,Ask,Slip,Ask-Stoploss*Point,0,"Tscalp",0,0,White);
            return(0);


  	      }


         if (iCustom(NULL,0,"AltrTrend_Signal_v2_2",30,0.5,150,14,999999,1,1)!=0)
            {
            if (Opentrades != 0)  
   
              {
     
               orders=0;
               sym=0;
               for (cnt=0;cnt<40;cnt++) 
                  { 
                  if ( OrderSelect (cnt, SELECT_BY_POS) == false )  continue;

                  if ( OrderSymbol()==Symbol())
                     {
                        mode=OrderType();
                        
                        if (mode==OP_BUY)   
                         {
                             OrderClose(OrderTicket(),OrderLots(),Bid,Slip,Red);
                             } 

                         if (mode==OP_SELL) 
                                 {
                                 OrderClose(OrderTicket(),OrderLots(),Ask,Slip,Red);
                                 }

                     }
                  } 

            }

            OrderSend(Symbol(),OP_SELL,Ilo,Bid,Slip,Bid+Stoploss*Point,0,"Tscalp",0,0,Red);
            return(0);


  	      }




  	      }

   return(0);
  }

/*
   //------------------------------------------------------------------------------------------------ 
   Opentrades=0;
   for (cnt=0;cnt<OrdersTotal();cnt++) 
   {
      if ( OrderSelect (cnt, SELECT_BY_POS) == false )  continue;
      if ( OrderSymbol()==Symbol()) Opentrades=Opentrades+1;
   }


   if (Opentrades==0)  //and iATR(5,2)<StopLoss*Point 
   
     {
  
     if (LastBarChecked == Time[0]) 
         return(0); 
      else 
  	      {
  	      LastBarChecked = Time[0]; 
  	      if (! High[1]>iBands(NULL,0,100,2,0,PRICE_CLOSE,MODE_UPPER,1) && ! Low[1]<iBands(NULL,0,100,2,0,PRICE_CLOSE,MODE_LOWER,1)) 
  	         return(0);
  	      }
   	
	   //Signal =iCustom("TrendScalpIndic",15,0,5,0.7,0,1000,0,MODE_FIRST,1);
	   //b4signal=iCustom("TrendScalpIndic",15,0,5,0.7,0,1000,0,MODE_FIRST,2);
	   //NowSignal=iCustom("TrendScalpIndic",15,0,5,0.7,0,1000,0,MODE_FIRST,0);
	   //Triggerline =iCustom("TrendScalpIndic",15,0,5,0.7,0,1000,0,MODE_SECOND,0);
	   //b4Triggerline =iCustom("TrendScalpIndic",15,0,5,0.7,0,1000,0,MODE_SECOND,1);

      double HHR,HHO,LLR,LLO;
      double BP,SP;

	  HHR=High[Highest(NULL, 0, MODE_HIGH,1,0+1)];
	  HHO =High[Highest(NULL, 0, MODE_HIGH,15,0+1+1)];
	  LLR =Low [Lowest (NULL, 0, MODE_LOW,1,0+1)];
	  LLO =Low [Lowest (NULL, 0, MODE_LOW,15,0+1+1)];
	  BP =HHR-LLO;
	  SP=HHO -LLR;
	  Triggerline=(BP-SP)/(0.5*(BP+SP))*100;
	  if (Triggerline>=0) Triggerline= 100; else Triggerline=-100;

	  HHR=High[Highest(NULL, 0, MODE_HIGH,1,1+1)];
	  HHO =High[Highest(NULL, 0, MODE_HIGH,15,1+1+1)];
	  LLR =Low [Lowest (NULL, 0, MODE_LOW ,1,1+1)];
	  LLO =Low [Lowest (NULL, 0, MODE_LOW ,15,1+1+1)];
	  BP =HHR-LLO;
	  SP=HHO -LLR;
	  b4Triggerline=(BP-SP)/(0.5*(BP+SP))*100;
	  if (b4Triggerline>=0) b4Triggerline=100; else b4Triggerline=-100;

     //Print (Triggerline, "   ", b4Triggerline);



     if (b4Triggerline>=100
     && High[1]>iBands(NULL,0,100,2,0,PRICE_CLOSE,MODE_UPPER,1)
     && Triggerline<100) 
  
        {
        OrderSend(Symbol(),OP_SELL,Ilo,Bid,Slip,Bid+Stoploss*Point,0,"Tscalp",0,0,Red);
        LastOpByExpert=CurTime();
        //	Print (TimeToStr(Time[1]),"   ", Triggerline);
        return(0);            
        }

     if 
     (b4Triggerline<=(-100) 
     && Low[1]<iBands(NULL,0,100,2,0,PRICE_CLOSE,MODE_LOWER,1)
     && Triggerline>(-100)) //and iATR(10,1)>6*Point

        {
        OrderSend(Symbol(),OP_BUY,Ilo,Ask,Slip,Ask-Stoploss*Point,0,"Tscalp",0,0,White);
        LastOpByExpert=CurTime();
        return(0);
        }

     }

   //----------------------------------------Order Control-------------------------------------------
 
   if (Opentrades != 0)  
   
     {
      orders=0;
      sym=0;
      for (cnt=0;cnt<OrdersTotal();cnt++) 
         { 
         if ( OrderSelect (cnt, SELECT_BY_POS) == false )  continue;

         if ( OrderSymbol()==Symbol())
            {
            sym=cnt;
            orders=1;
	         HHR=High[Highest(NULL, 0, MODE_HIGH,1,0+1)];
	         HHO =High[Highest(NULL, 0, MODE_HIGH,15,0+1+1)];
	         LLR =Low [Lowest (NULL, 0, MODE_LOW,1,0+1)];
	         LLO =Low [Lowest (NULL, 0, MODE_LOW,15,0+1+1)];
	         BP =HHR-LLO;
	         SP=HHO -LLR;
	         NowTriggerline=(BP-SP)/(0.5*(BP+SP))*100;
	         if (NowTriggerline>=0) NowTriggerline= 100; else NowTriggerline=-100;
            b=5*Point+iATR(NULL,0,3,1)*4;
            }
         } 

      mode=OrderType();

      if (mode==OP_BUY)   
       {
       if (
       OrderOpenPrice()>OrderStopLoss()
       && Bid-OrderOpenPrice()>InitialTarget*Point  
       && NowTriggerline<100 || High[1]>iBands(NULL,0,100,1,0,PRICE_CLOSE,MODE_UPPER,1) && NowTriggerline<100)
           {
           OrderClose(OrderTicket(),OrderLots(),Bid,Slip,Red);
           LastOpByExpert=CurTime();
           return(0);
           } 
       if (Bid-OrderOpenPrice()>b)
        {
        if (OrderStopLoss()<Bid-b) 
         {
         OrderModify(OrderTicket(),OrderOpenPrice(),Bid-b,OrderTakeProfit(),0,LimeGreen);
         LastOpByExpert=CurTime();
         return(0);
         } 
        }
       }  

       if (mode==OP_SELL) 
           {
           if (OrderOpenPrice()<OrderStopLoss()
           && OrderOpenPrice()-Ask>InitialTarget*Point 
           && NowTriggerline>-100 || Low[1]<iBands(NULL,0,100,1,0,PRICE_CLOSE,MODE_LOWER,1) && NowTriggerline>-100)
             
               {
               OrderClose(OrderTicket(),OrderLots(),Ask,Slip,Red);
               LastOpByExpert=CurTime();
               return(0);
               }
         if (OrderOpenPrice()-Ask>(b)) 
            {
            if (OrderStopLoss()>(Ask+b)) 
              {
              OrderModify(OrderTicket(),OrderOpenPrice(),Ask+b,OrderTakeProfit(),0,HotPink);
              LastOpByExpert=CurTime();
              return(0);
              }
            }
         }
   }




   return(0);
  }
//+------------------------------------------------------------------+

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 ---