Trading Strategy Logic Overview: "Starter" Script
This document provides a non-technical explanation of the automated trading logic contained within the provided MetaTrader script. The script is designed to monitor market conditions, manage risk automatically, and execute buy or sell orders based on specific technical indicators.
1. Dynamic Risk & Position Management
Before entering any trade, the script performs a "smart" calculation to determine how much capital to risk. It does not simply use a fixed amount; instead, it follows these rules:
- Margin Awareness: It calculates the trade size based on your available "Free Margin" (the cash you have available to trade) and a pre-set risk percentage.
- Loss Protection: The script reviews your recent trading history. If it detects a string of consecutive losses, it automatically reduces the position size for new trades. This acts as a safety mechanism to prevent "revenge trading" or rapid depletion of funds during a losing streak.
- Boundary Safety: The script ensures that the calculated trade size never falls below 1 unit or exceeds 100 units, keeping the trade volume within safe operational limits.
2. Entry Conditions (When to Trade)
The script looks for three specific technical "green lights" before opening a position. It will only initiate a trade if it is currently past 6:00 AM.
To Open a BUY Position:
- Laguerre Indicator: The price must be below a specific low threshold (indicating the asset might be oversold or reaching a floor).
- Moving Average Trend: The current Moving Average must be higher than the previous one, suggesting upward momentum.
- CCI Momentum: A specific momentum indicator (Commodity Channel Index) must be significantly low (below -5), confirming a downward exhaustion that is ready to reverse.
To Open a SELL Position:
- Laguerre Indicator: The price must be above a specific high threshold (indicating the asset might be overbought or reaching a ceiling).
- Moving Average Trend: The current Moving Average must be lower than the previous one, suggesting downward momentum.
- CCI Momentum: The momentum indicator must be significantly high (above 5), confirming an upward exhaustion that is ready to reverse.
3. Exit Strategy & Trade Management
Once a trade is active, the script constantly monitors the market to decide when to close the position. It uses three distinct methods to exit:
- Momentum Reversal: If the price hits the opposite "Laguerre" threshold (e.g., selling because the price hit the high ceiling or buying because it hit the low floor), the script closes the trade immediately to lock in gains or limit losses.
- Stop Loss: If the user has defined a specific "Stop" value, the script will automatically close the trade if the price moves against the position by that amount.
- Time & Price Stagnation: There is a safety check for "stagnant" trades. If a trade has been open for more than 30 minutes (1800 seconds) and the price has not moved in your favor (or has moved slightly against you), the script will close the position to prevent being stuck in a non-performing trade.
Summary Table of Indicators
| Indicator | Purpose in this Script | | :--- | :--- | | Laguerre | Identifies overbought/oversold zones (The "Boundaries"). | | Moving Average | Determines the immediate direction of the trend. | | CCI (Alpha) | Measures the strength of current price momentum. | | History Scan | Adjusts trade size based on past performance. |
//+------------------------------------------------------------------+
//| 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.053;
extern double DecreaseFactor = 5000;
//extern double Lots = 4;
extern double Stop = 1;
extern double MAPeriod=11;
extern double Lm = 0.14;
extern double Lb = 0.86;
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>100) lot=100;
return(lot);
}
//+------------------------------------------------------------------+
//| expert start function |
//+------------------------------------------------------------------+
int start()
{
double Laguerre;
double Alpha;
double MA, MAprevious;
//+-- double Juice;
int cnt, ticket, total, cmo;
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();
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) && (Hour()>6)) //+-- && 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) && (Hour()>6)) //+-- && 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
{
if(((CurTime()-OrderOpenTime())>1800) && (Bid-OrderOpenPrice()<=0))
{
OrderClose(OrderTicket(),OrderLots(),Bid,3,Violet); // close position
Print(CurTime(), " - ", OrderOpenTime(), " = ", (CurTime()-OrderOpenTime()));
return(0); // exit
}
// 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
{
if(((CurTime()-OrderOpenTime())>1800) && (OrderOpenPrice()-Ask<=0))
{
OrderClose(OrderTicket(),OrderLots(),Ask,3,Violet); // close position
Print(CurTime(), " - ", OrderOpenTime(), " = ", (CurTime()-OrderOpenTime()));
return(0); // exit
}
// 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.
Comments