lossless_ma
Download scriptThis automated trading script is designed to manage and execute trades based on the relationship between two Moving Averages (MA). It follows a systematic approach to identify market trends and manage open positions automatically.
Core Trading Logic: Trend Following
The script relies on "Moving Average Crosses" to determine when to enter or exit the market. It monitors two different lines:
- Fast Moving Average: Reacts quickly to recent price changes.
- Slow Moving Average: Represents the broader, longer-term trend.
When the Fast Moving Average crosses above the Slow Moving Average, the script identifies an upward trend and initiates a Buy position. Conversely, when the Fast Moving Average drops below the Slow Moving Average, it identifies a downward trend and initiates a Sell position.
Key Features and Management Rules
To ensure disciplined trading, the script incorporates several safety and management protocols:
- Position Limits: The script includes a "Maximum Deals" limit. This prevents the system from opening an excessive number of trades simultaneously, helping to manage risk exposure on the account.
- Automated Exit Strategy: Once a trade is open, the script constantly monitors the Moving Averages. If the trend reverses (e.g., a Buy position is held but the Fast MA drops below the Slow MA), the script triggers a logic sequence to close the trade.
- Loss Management Modes: There are two distinct ways the script handles trades that are moving against the user:
- Standard Loss Closure: The script will immediately exit a losing trade as soon as the trend reversal is detected.
- Protective Adjustment: If configured, the script can attempt to "protect" a losing trade. Instead of closing it instantly, it may modify the position's parameters to minimize further exposure while waiting for a potential recovery, provided the trade is not already in a definitive state of loss.
- Trade Tracking: The script uses a unique identifier (Magic Number) to ensure it only manages the trades it opened itself, allowing it to run on one account without interfering with other manual or automated trades.
Summary of Workflow
- Scan: Constantly monitors price data to calculate the current position of the Fast and Slow Moving Averages.
- Execute: Opens a trade when a crossover occurs, provided the maximum number of allowed trades hasn't been reached.
- Monitor: Watches the open trade for trend reversals or profit targets.
- Resolve: Closes the trade automatically based on the user's preference for handling losses or trend changes.
//+------------------------------------------------------------------+
//| Lossless MA |
//| Copyright 2015, Vladimir V. Tkach |
//+------------------------------------------------------------------+
#property version "1.0"
#property copyright "Copyright © 2015, Vladimir V. Tkach"
#property description "EA trades two MA crossing."
#property description "Noloss function with maximum deals limitation."
#property strict
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
enum yes_no
{
yes=0, //yes
no=1, //no
};
extern int
slip=50, //Slippage (in pips)
fast_MA=10, //Fast ÌA
slow_MA=30, //Slow ÌÀ
MovingShift=6, //MA Shift
max_deals=5, //Maximum deals
magic=1133; //Magic number
extern yes_no losses=1; //Close losses
extern double
Lot=0.01; //Lot size
int
order_type=-1,
ticket=-1;
double
ma1,
ma2;
//+------------------------------------------------------------------+
//| OnTick function |
//+------------------------------------------------------------------+
void OnTick()
{
ma1=iMA(NULL,0,fast_MA,MovingShift,MODE_SMA,PRICE_CLOSE,1);
ma2=iMA(NULL,0,slow_MA,MovingShift,MODE_SMA,PRICE_CLOSE,1);
if(ticket!=-1) CheckForCloseMA();
if(CountDeals()==0) CheckForOpen();
else
{
if(CountDeals()<max_deals && EnumToString(losses)=="no") CheckForOpen();
}
}
//+------------------------------------------------------------------+
//| Check for open order conditions |
//+------------------------------------------------------------------+
void CheckForOpen()
{
//sell conditions
if(ma1<ma2 && order_type!=OP_SELL)
{
ticket=OrderSend(Symbol(),OP_SELL,Lot,Bid,slip,0,0,"",magic,0,Red);
if(ticket==-1) return;
if(!OrderSelect(ticket,SELECT_BY_TICKET)) {Print("Error during selection."); return;}
else order_type=OrderType();
return;
}
//buy conditions
if(ma1>ma2 && order_type!=OP_BUY)
{
ticket=OrderSend(Symbol(),OP_BUY,Lot,Ask,slip,0,0,"",magic,0,Blue);
if(ticket==-1) return;
if(!OrderSelect(ticket,SELECT_BY_TICKET)) {Print("Error during selection."); return;}
else order_type=OrderType();
return;
}
}
//+------------------------------------------------------------------+
//| Check for close order conditions |
//+------------------------------------------------------------------+
void CheckForCloseMA()
{
if(OrderSelect(ticket,SELECT_BY_TICKET)==false) return;
if(OrderMagicNumber()!=magic || OrderSymbol()!=Symbol()) return;
if(OrderType()==OP_BUY)
{
if(ma1<ma2)
{
if(EnumToString(losses)=="yes")
{
if(!OrderClose(OrderTicket(),OrderLots(),Bid,slip,White)) Print("Error during order close.");
}
else
{
if(OrderProfit()>0)
{
if(!OrderClose(OrderTicket(),OrderLots(),Bid,slip,White)) Print("Error during order close.");
}
else
{
if(!OrderModify(ticket,OrderOpenPrice(),0,OrderOpenPrice()+MarketInfo(Symbol(),MODE_SPREAD)*Point,0)) Print("Error during order modify.");
}
}
ticket=-1;
}
}
if(OrderType()==OP_SELL)
{
if(ma1>ma2)
{
if(EnumToString(losses)=="yes")
{
if(!OrderClose(OrderTicket(),OrderLots(),Ask,slip,White)) Print("Error during order close.");
}
else
{
if(OrderProfit()>0)
{
if(!OrderClose(OrderTicket(),OrderLots(),Ask,slip,White)) Print("Error during order close.");
}
else
{
if(!OrderModify(ticket,OrderOpenPrice(),0,OrderOpenPrice()-MarketInfo(Symbol(),MODE_SPREAD)*Point,0)) Print("Error during order modify.");
}
}
ticket=-1;
}
}
}
//+------------------------------------------------------------------+
//| Count the deals |
//+------------------------------------------------------------------+
int CountDeals()
{
int h=0;
for(int i=0;i<OrdersTotal();i++)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) continue;
if(OrderMagicNumber()==magic || OrderSymbol()==Symbol()) h++;
}
if(h==0) ticket=-1;
return(h);
}
//+------------------------------------------------------------------+
Comments