3MA02EXP1
Download scriptThis automated trading script is designed to monitor price movements and execute trades based on the relationship between two different moving averages. It acts as a systematic way to enter and exit positions by identifying specific "crossover" moments in the market trend.
Core Trading Logic
The system functions by calculating two types of moving averages (Linear Weighted Moving Averages) to determine market momentum:
- Fast Moving Average: A short-term indicator that reacts quickly to recent price changes.
- Slow Moving Average: A longer-term indicator that represents the broader trend of the market.
The logic relies on the interaction between these two lines. When the Fast moving average crosses over or under the Slow moving average, it signals a potential shift in momentum.
Key Operational Steps
To ensure safe and organized trading, the script follows these specific logical steps:
1. Safety and Environment Checks Before any action is taken, the script performs two essential checks:
- It ensures there is enough historical data (at least 100 bars) available to make accurate calculations.
- It verifies that the account has sufficient available balance (free margin) to cover the cost of the trades based on the lot size you have selected.
2. Crossover Detection The script constantly scans the current market prices. It looks for a "Crossover" event, which occurs when:
- The Fast average was previously below the Slow average but has moved above it.
- OR the Fast average was previously above the Slow average but has moved below it.
3. Trade Management and Execution Once a crossover is detected, the script identifies if there is already an active position open for that specific currency pair. If a position exists, the logic determines what to do based on the direction of the cross:
- If the market trends in a way that contradicts your current open trade (for example, you have a "Buy" order but the averages show a downward shift), the script is designed to identify that conflict.
- The system provides visual feedback on the chart to notify you of these detections and manages the closing of orders to help manage risk during these trend reversals.
In summary, this tool automates the process of watching for momentum shifts, ensuring that trades are reacted to consistently based on moving average behavior rather than manual observation.
/*-----------------------------+
| |
| Shared by www.Aptrafx.com |
| |
+------------------------------*/
//+------------------------------------------------------------------+
//| 3MA Bunny Cross Expert |
//+------------------------------------------------------------------+
#property copyright "Ron Thompson"
#property link "http://www.lightpatch.com/forex"
// User Input
extern double Lots = 0.1;
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//|------------------------------------------------------------------|
int init()
{
return(0);
}
//+------------------------------------------------------------------+
//| Custor indicator deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function |
//+------------------------------------------------------------------+
int start()
{
double cMAfst=0, pMAfst=0;
double cMAslo=0, pMAslo=0;
double MA100=0;
int total=0;
bool found=false;
int otype=0;
int otime=0;
int cnt=0;
string cmmt="";
//
// Error checking
//
if(Bars<100)
{
Print("bars less than 100");
return(0);
}
if(AccountFreeMargin()<(1000*Lots))
{
Print("We have no money");
return(0);
}
cMAfst=iMA(Symbol(),0,5 ,0,MODE_LWMA,PRICE_CLOSE, 1);
pMAfst=iMA(Symbol(),0,5 ,0,MODE_LWMA,PRICE_CLOSE, 2);
cMAslo=iMA(Symbol(),0,25,0,MODE_LWMA,PRICE_CLOSE, 1);
pMAslo=iMA(Symbol(),0,25,0,MODE_LWMA,PRICE_CLOSE, 2);
found=false;
// rising or falling
if ((pMAfst<=pMAslo && cMAfst>=cMAslo) || (pMAfst>=pMAslo && cMAfst<=cMAslo))
{
// check for existing order
cmmt=cmmt+"crossover ";
Comment(cmmt);
total=OrdersTotal();
for(cnt=0;cnt<OrdersTotal();cnt++)
{
OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
if(OrderSymbol()==Symbol())
{
found=true;
otype=OrderType();
otime=OrderOpenTime();
break;
}
}
}
if (found==true)
{
Comment("experimental close");
OrderClose(OrderTicket(),Lots,Ask,0,White);
}
return(0);
if (found==true)
{
cmmt=cmmt+" infound";
Comment(cmmt);
if (pMAfst<=pMAslo && cMAfst>=cMAslo && otype==1) //exist sell
{
cmmt=cmmt+" CLOSE sell BUY buy";
Comment(cmmt);
OrderClose(OrderTicket(),Lots,Bid,0,Red);
//Print("BUY Order started ",Ask);
//OrderSend(Symbol(),OP_BUY,Lots,Ask,3,0,0,"BC Buy ",16123,0,White);
//if(GetLastError()==0)Comment("BC_BUY Order opened : ",Ask);
}
if (pMAfst>=pMAslo && cMAfst<=cMAslo && otype==0) // exist buy
{
cmmt=cmmt+" CLOSE buy BUY sell";
Comment(cmmt);
OrderClose(OrderTicket(),Lots,Ask,0,White);
//Print("SELL Order started ",Bid);
//OrderSend(Symbol(),OP_SELL,Lots,Bid,3,0,0,"BC Sell",16321,0,Red);
//if(GetLastError()==0)Comment("BC_SELL Order opened : ",Bid );
}
}
return(0);
}
Comments