starter_eurusd_!_2stor
Download scriptThis automated trading script is designed to manage investment positions by analyzing market trends and risk parameters in real-time. It focuses on identifying specific entry signals while maintaining strict control over trade volume based on your account's financial health.
Smart Trade Volume Management
One of the primary features of this script is its ability to automatically calculate the "Optimal Lot Size" for every trade. Instead of using a fixed amount, the system looks at your available account balance and your risk tolerance to determine the safest size for a new position. Furthermore, it includes a protective mechanism that reviews your trading history; if it detects a series of consecutive losses without a profit recovery, it automatically reduces the trade size to minimize further exposure.
Market Analysis Logic
The script makes decisions by monitoring three distinct technical indicators simultaneously:
- Trend Momentum: It uses a specialized oscillator (Laguerre) to determine where the price sits within a specific range.
- Price Movement: It tracks a Moving Average to see if the general direction of the market is currently shifting upward or downward.
- Strength Measurement: It utilizes a momentum index (CCI) to measure the velocity of price changes, ensuring that it only enters trades when there is sufficient "push" in the market.
Entry and Exit Rules
The script follows a strict set of rules for interacting with the market:
For Buying Positions (Long):
- Entry: The script looks for a combination of low momentum values, an upward-trending moving average, and a specific negative momentum strength reading. It also ensures that no opposing "Sell" positions are currently open.
- Exit: A position is closed if the momentum indicator rises above a specific high threshold (indicating a peak) or if the price moves against your position by a pre-defined distance (the "Stop" value).
For Selling Positions (Short):
- Entry: The script looks for high momentum values, a downward-trending moving average, and a specific positive momentum strength reading, ensuring no "Buy" positions are currently active.
- Exit: A position is closed if the momentum indicator falls below a specific low threshold (indicating a bottom) or if the price moves against your position by the pre-defined distance.
Safety and Execution
The script includes built-in safety checks to prevent over-leveraging. Before opening any trade, it verifies that your "Free Margin" is sufficient to cover the required investment. If the account balance is too low, it will print a warning message and wait rather than attempting a trade that could put the account at risk.
//+------------------------------------------------------------------+
//| Starter.mq4 |
//| Copyright © 2005, MetaQuotes Software Corp. |
//| http://www.metaquotes.net |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2005, MetaQuotes Software Corp."
#property link "http://www.metaquotes.net"
#define MAGICMA 20050610
extern double Lots = 1;
extern double MaximumRisk = 0.058;
extern double DecreaseFactor = 5000;
//extern double Lots = 4;
extern double Stop = 3;
extern double MAPeriod=12;
extern double Lm = 0.23;
extern double Lb = 0.77;
double spread=1.5;
//+-- extern double JuiceLevel = 0.0005;
//+------------------------------------------------------------------+
//| expert initialization function |
//+------------------------------------------------------------------+
int init()
{
//----
//----
return(0);
}
//+------------------------------------------------------------------+
//| expert deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
//----
//----
return(0);
}
//+------------------------------------------------------------------+
//| Calculate optimal lot size |
//+------------------------------------------------------------------+
double LotsOptimized()
{
double lot=Lots;
int orders=HistoryTotal(); // history orders total
int losses=0; // number of losses orders without a break
//---- select lot size
lot=NormalizeDouble(AccountFreeMargin()*MaximumRisk/500,1);
//---- calcuulate number of losses orders without a break
if(DecreaseFactor>0)
{
for(int i=orders-1;i>=0;i--)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)==false) { Print("Error in history!"); break; }
if(OrderSymbol()!=Symbol() || OrderType()>OP_SELL) continue;
//----
if(OrderProfit()>0) break;
if(OrderProfit()<0) losses++;
}
if(losses>1) lot=NormalizeDouble(lot-lot*losses/DecreaseFactor,1);
}
//---- return lot size
if(lot<1) lot=1;
if(lot>1000) lot=1000;
return(lot);
}
//+------------------------------------------------------------------+
//| expert start function |
//+------------------------------------------------------------------+
int start()
{
double Laguerre;
double Alpha;
double MA, MAprevious;
//+-- double Juice;
int cnt, ticket, total;
bool est_s, est_b;
Laguerre=iCustom(NULL, 0, "Laguerre", 0, 0);
Alpha=iCCI(NULL, 0, 14, PRICE_CLOSE, 0);
MA=iMA(NULL,0,MAPeriod,0,MODE_EMA,PRICE_MEDIAN,0);
MAprevious=iMA(NULL,0,MAPeriod,0,MODE_EMA,PRICE_MEDIAN,1);
//+-- Juice=iCustom(NULL,0,"Juice",0,0);
total=OrdersTotal();
est_s=true;
est_b=true;
for(cnt=0;cnt<total;cnt++)
{
OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
if(OrderType()==OP_SELL)
{
if(OrderSymbol()==Symbol())
{
est_s=false;
}
}
if(OrderType()==OP_BUY)
{
if(OrderSymbol()==Symbol())
{
est_b=false;
}
}
}
// if(total<1)
// {
// no opened orders identified
if(AccountFreeMargin()<(1000*Lots))
{
Print("We have no money. Free Margin = ", AccountFreeMargin());
return(0);
}
// check for long position (BUY) possibility
if((Laguerre<Lm) && (MA>MAprevious) && (Alpha<-5) && est_b) //+-- && Juice>JuiceLevel)
{
ticket=OrderSend(Symbol(),OP_BUY,LotsOptimized(),Ask,3,0,0,"starter",16384,0,Green);
}
// check for short position (SELL) possibility
if((Laguerre>Lb) && (MA<MAprevious) && (Alpha>5) && est_s) //+-- && Juice>JuiceLevel)
{
ticket=OrderSend(Symbol(),OP_SELL,LotsOptimized(),Bid,3,0,0,"starter",16384,0,Red);
}
// }
// it is important to enter the market correctly,
// but it is more important to exit it correctly...
for(cnt=0;cnt<total;cnt++)
{
OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
if(OrderType()<=OP_SELL && // check for opened position
OrderSymbol()==Symbol()) // check for symbol
{
if(OrderType()==OP_BUY) // long position is opened
{
// should it be closed?
if(Laguerre>Lb)
{
OrderClose(OrderTicket(),OrderLots(),Bid,3,Violet); // close position
return(0); // exit
}
// check for stop
if(Stop>0)
{
if(Bid-OrderOpenPrice()>Point*Stop)
{
OrderClose(OrderTicket(),OrderLots(),Bid,3,Violet); // close position
return(0);
}
}
}
else // go to short position
{
// should it be closed?
if(Laguerre<Lm)
{
OrderClose(OrderTicket(),OrderLots(),Ask,3,Violet); // close position
return(0); // exit
}
// check for stop
if(Stop>0)
{
if(OrderOpenPrice()-Ask>Point*Stop)
{
OrderClose(OrderTicket(),OrderLots(),Ask,3,Violet); // close position
return(0);
}
}
}
}
}
return(0);
}
// the end.
Profitability Reports
Expected payoff
Average expected payoff across the available backtests for each symbol.
Comments