Okay, here's a breakdown of what this program does, written for someone who doesn't know how to code.
In Simple Terms: What is this program trying to do?
This program is designed to automatically trade on the financial markets (like currencies) using a trading platform called MetaTrader. It's like a robot that looks at price charts and tries to make decisions about when to buy ("go long") or sell ("go short") based on a few simple rules.
Here's how it works:
-
Moving Averages: The program uses several "Moving Averages" (MAs). A moving average is like a line that smooths out the price data over a certain period. For example, a 10-day moving average calculates the average price over the last 10 days. The program uses four of these, each with a different period (10, 20, 50, and 200) giving different perspectives of the markets behavior.
-
Setup: When the program starts, it reads a few settings defined as inputs. These settings determine how the program will behave. The parameters configure period lengths of the MAs (MA1period, MA2period, MA3period, MA4period), the type of calculation for these moving averages (MA1mode, MA2mode, MA3mode, MA4mode), and other parameters for managing the trading strategy.
-
Checking for Trades:
- First, it checks if it has already made a trade. It figures this out by looking at the trades it has already placed.
- If it is already in a trade, it checks to see if the price behavior justifies closing the position. It then closes the trade if certain conditions based on a comparison between some of the moving averages are met.
- If it's not already in a trade, the program looks for new opportunities to buy or sell.
-
Finding Opportunities (New Signals):
- The program uses the moving averages to determine if it should buy or sell. Here's the basic idea:
- Buy (Long) Signal: If the shorter moving averages are above the longer moving averages (e.g., 10-day MA > 20-day MA > 50-day MA > 200-day MA), it indicates an upward trend, and the program will attempt to buy.
- Sell (Short) Signal: If the shorter moving averages are below the longer moving averages (e.g., 10-day MA < 20-day MA < 50-day MA < 200-day MA), it indicates a downward trend, and the program will attempt to sell.
- The program uses the moving averages to determine if it should buy or sell. Here's the basic idea:
-
Money Management (Risk):
- The program can automatically decide how much to invest on each trade based on the size of the account and a risk setting.
- It allows you to specify a fixed lot size or use a money management system that adjusts the lot size based on the account balance and risk level.
-
Placing Orders:
- If the program finds a buy or sell signal, it will automatically place an order with the broker (the company that executes the trades).
-
Order closing: The program will close the positions when MA1 and MA2 cross based on the selections of CloseMA_A and CloseMA_B parameters.
Key Things to Remember:
- Automated Trading: This program makes trades automatically without any human intervention (once it's running).
- Risk: Automated trading involves risk. The program can lose money if the market moves in the wrong direction.
- Moving Averages are Indicators: The moving averages are just one way to analyze the market. There are many other indicators and strategies that traders use. This program uses this as its main indication to buy or sell.
In short: The program is a simple trading robot that uses moving averages to identify potential buying and selling opportunities and automatically places trades based on those signals. It also includes some basic money management features to control risk.
/*-----------------------------+
| |
| Shared by www.Aptrafx.com |
| |
+------------------------------*/
//+------------------------------------------------------------------+
//| TrendScalper_TR.mq4 |
//| Copyright © 2004, MetaQuotes Software Corp. |
//| http://www.metaquotes.net/ |
//+------------------------------------------------------------------+
#property copyright "Testing"
#property link "http://www.metaquotes.net/"
//---- input parameters
extern int MA1period=10;
extern int MA1mode=1;
extern int MA2period=20;
extern int MA2mode=1;
extern int MA3period=50;
extern int MA3mode=1;
extern int MA4period=200;
extern int MA4mode=1;
extern int CloseMA_A=2;
extern int CloseMA_B=3;
extern double LotsIfNoMM=0.1;
extern int Stoploss=0;
extern int TakeProfit=0;
extern int Slip=5;
extern int MM_Mode=0;
extern int MM_Risk=40;
double Opentrades,orders,first,mode,Ilo,sym,b;
double b4signal,Signal,Triggerline,b4Triggerline,Nowsignal,NowTriggerline,sl,LastOpByExpert,LastBarChecked;
int cnt,cnt2,OpenPosition,notouchbar;
bool test;
double MA1,MA2,MA3,MA4,CloseMA1,CloseMA2;
#define Long 1
#define Short 2
//+------------------------------------------------------------------+
//| expert initialization function |
//+------------------------------------------------------------------+
int init()
{
//----
//----
return(0);
}
//+------------------------------------------------------------------+
//| expert deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
//----
//----
return(0);
}
//+------------------------------------------------------------------+
//| expert start function |
//+------------------------------------------------------------------+
int start()
{
//return(0);
if ( ! IsTesting() ) 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 (notouchbar == Time[0])
// return(0);
if (LastBarChecked == Time[0])
//if (1 == 2) //just so this part is never true for now
return(0);
else
{
LastBarChecked = Time[0];
MA1=iMA(NULL,0,MA1period,0,MA1mode,PRICE_CLOSE,1);
MA2=iMA(NULL,0,MA2period,0,MA2mode,PRICE_CLOSE,1);
MA3=iMA(NULL,0,MA3period,0,MA3mode,PRICE_CLOSE,1);
MA4=iMA(NULL,0,MA4period,0,MA4mode,PRICE_CLOSE,1);
switch (CloseMA_A)
{
case 1:
CloseMA1=MA1;
break;
case 2:
CloseMA1=MA2;
break;
case 3:
CloseMA1=MA3;
break;
case 4:
CloseMA1=MA4;
break;
default:
Alert("CloseMA_A must be in range 1-4");
break;
}
switch (CloseMA_B)
{
case 1:
CloseMA2=MA1;
break;
case 2:
CloseMA2=MA2;
break;
case 3:
CloseMA2=MA3;
break;
case 4:
CloseMA2=MA4;
break;
default:
Alert("CloseMA_B must be in range 1-4");
break;
}
Opentrades=0;
for (cnt=0;cnt<OrdersTotal();cnt++)
{
if ( OrderSelect (cnt, SELECT_BY_POS) == false ) continue;
if ( OrderSymbol()==Symbol())
{
Opentrades=Opentrades+1;
if(OrderType()==OP_BUY)
{
OpenPosition = Long;
if (IsTesting())
{
//LastLogTime=iTime(NULL,PERIOD_H1,1);
if (OrderTakeProfit()==OrderOpenPrice()+Point*10000)
OrderModify(OrderTicket(),OrderOpenPrice(),OrderStopLoss(),OrderOpenPrice()+Point*10001,0,Cyan);
else
OrderModify(OrderTicket(),OrderOpenPrice(),OrderStopLoss(),OrderOpenPrice()+Point*10000,0,Cyan);
}
}
else
{
OpenPosition = Short;
if (IsTesting())
{
//LastLogTime=iTime(NULL,PERIOD_H1,1);
if (OrderTakeProfit()==0)
OrderModify(OrderTicket(),OrderOpenPrice(),OrderStopLoss(),Point*1,0,Cyan);
else
OrderModify(OrderTicket(),OrderOpenPrice(),OrderStopLoss(),0,0,Cyan);
}
}
}
}
//----------------------------------------Order Control-------------------------------------------
if (Opentrades != 0)
{
if (OpenPosition == Long)
{
if (CloseMA1<CloseMA2)
{
OrderClose(OrderTicket(),OrderLots(),Bid,Slip,Red);
//Alert("DanMA close long ",Symbol());
//notouchbar=Time[0];
return(0);
}
}
if (OpenPosition == Short)
{
if (CloseMA1>CloseMA2)
{
OrderClose(OrderTicket(),OrderLots(),Ask,Slip,Red);
//Alert("DanMA close short ",Symbol());
//notouchbar=Time[0];
return(0);
}
}
}
//----------------------------------------New signals-------------------------------------------
if (Opentrades == 0)
{
if (MA1>MA2 && MA2 > MA3 && MA3>MA4)
{
OrderSend(Symbol(),OP_BUY,Ilo,Ask,Slip,0,0,"",0,0,White);
return(0);
}
if (MA1<MA2 && MA2 < MA3 && MA3<MA4)
{
OrderSend(Symbol(),OP_SELL,Ilo,Bid,Slip,0,0,"",0,0,Red);
//notouchbar=Time[0];
return(0);
}
}
} //Ends if (LastBarChecked == Time[0])
return(0);
}
Comments