This script is designed to automatically trade on the Forex market based on a moving average indicator. Here's the breakdown:
Overall Goal: The script aims to identify potential buying or selling opportunities by comparing the current price of an asset to its moving average. It then automatically places trades based on these signals.
User-Defined Settings: The script starts by allowing the user to customize a few key parameters:
- Lots: This determines the size of the trades the script will make. It's essentially how much of your account balance you're willing to risk on each trade.
- MovingAvg: This is the period for calculating the moving average. A larger number means the average is calculated over a longer period, making it smoother and less sensitive to short-term price fluctuations.
- filter: This setting acts as a buffer zone around the moving average. The price has to move a certain distance beyond the moving average before the script considers it a valid signal. This helps to avoid false signals caused by minor price fluctuations.
- TakeProfit: This sets the price at which an open trade will automatically close with a profit. It's a way to lock in gains once the price moves in your favor.
- StopLoss: This sets the price at which an open trade will automatically close to limit your losses if the price moves against you.
How it Works:
-
Initialization: The script starts by initializing all of its user configurations
-
Checking the Market: Before placing any trades, the script performs a few checks:
- Account Balance: Ensures there's enough available money in your trading account to place the trade.
- Market History: Verifies that there's enough historical data available to calculate the moving average properly.
- Market Activity: Checks if a new price bar has been formed, preventing the script from placing redundant orders
-
Calculating the Moving Average: The script calculates a moving average (MA) using the asset's opening prices and the period specified by the user. It calculates not just the current MA but also the MA from the previous time period.
-
Generating Trading Signals: It compares the current moving average to the previous moving average. If the current moving average is above the previous one (plus the filter buffer), it signals a potential "buy" opportunity. If the current moving average is below the previous one (minus the filter buffer), it signals a potential "sell" opportunity.
-
Managing Existing Trades: Before placing new trades, the script checks if there are any existing trades open for the same asset. If there are, and a counter-indicator signal is generated (called CCI), it closes those trades to realize any accumulated gains or minimize losses.
-
Setting Take Profit and Stop Loss: Before sending a trade, the script calculates take profit and stop loss levels based on user-defined pip value.
-
Placing Orders: Based on the "buy" or "sell" signals, the script automatically places orders to buy or sell the asset. It sets the trade size according to the "Lots" parameter and includes the specified Stop Loss and Take Profit levels to manage risk and potential profits.
In Summary: The script automates trading decisions based on the relationship between the current price and its moving average. It uses user-defined settings to customize the trading strategy, manage risk, and define profit targets. It essentially takes away the need for manual monitoring and order placement, allowing the script to trade automatically according to the set rules.
Profitability Reports
/*-----------------------------+
| |
| Shared by www.Aptrafx.com |
| |
+------------------------------*/
//+-------------------+
//| 1MA Expert |
//+-------------------+
#property copyright "Ron Thompson"
#property link "http://www.lightpatch.com/forex"
// User Input
extern double Lots = 0.1;
extern double MovingAvg=10;
extern double filter=0;
extern int TakeProfit=0;
extern int StopLoss=0;
// Global scope
double barmove0 = 0;
double barmove1 = 0;
int risingcnt = 0;
int fallingcnt = 0;
int periodcnt = 0;
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//|------------------------------------------------------------------|
int init()
{
return(0);
}
//+------------------------------------------------------------------+
//| Custor indicator deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function |
//+------------------------------------------------------------------+
int start()
{
int cnt=0;
double cMA=0, pMA=0;
double p=Point();
double slA,slB,tpA,tpB;
double cCI=0;
bool rising=false;
bool falling=false;
// Error checking
if(AccountFreeMargin()<(1000*Lots)) {return(0);}
if(Bars<100) {return(0);}
if(barmove0==Open[0] && barmove1==Open[1]) {return(0);}
// bars moved, update current position
barmove0=Open[0];
barmove1=Open[1];
cMA=iMA(Symbol(), 0, MovingAvg, 0, MODE_LWMA, PRICE_OPEN, 0);
pMA=iMA(Symbol(), 0, MovingAvg, 0, MODE_LWMA, PRICE_OPEN, 1);
cCI=iCCI(Symbol(), 0, 30, 1,0);
if (pMA+(filter*p)<cMA) {rising=true; falling=false;}
if (pMA-(filter*p)>cMA) {rising=false; falling=true;}
for(cnt=0;cnt<OrdersTotal();cnt++)
{
OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
if(OrderSymbol()==Symbol())
{
if (OrderType()==0)
{
//bought=true;
if (cCI<0) OrderClose(OrderTicket(),Lots,Bid,0,White);
}
if (OrderType()==1)
{
//sold=true;
if (cCI>0) OrderClose(OrderTicket(),Lots,Ask,0,Red);
}
}
}
if (TakeProfit==0)
{
tpA=0;
tpB=0;
}
else
{
tpA=Ask+(p*TakeProfit);
tpB=Bid-(p*TakeProfit);
}
if (StopLoss==0)
{
slA=0;
slB=0;
}
else
{
slA=Ask-(p*StopLoss);
slB=Bid+(p*StopLoss);
}
if (rising)
{
OrderSend(Symbol(),OP_BUY,Lots,Ask,3,slA,tpA,"1MA Buy",11123,0,White);
}
if (falling)
{
OrderSend(Symbol(),OP_SELL,Lots,Bid,3,slB,tpB,"1MA Sell",11321,0,Red);
}
return(0);
}
Comments
Markdown Formatting Guide
# H1
## H2
### H3
**bold text**
*italicized text*
[title](https://www.example.com)

`code`
```
code block
```
> blockquote
- Item 1
- Item 2
1. First item
2. Second item
---