Orders Execution
Indicators Used
Miscellaneous
0
Views
0
Downloads
0
Favorites
LiveMA
//+------------------------------------------------------------------+
//| LiveMA.mq4 |
//| Copyright © 2011, MetaQuotes Software Corp. |
//| http://www.mql4.com/ru/users/rustein |
//-------------------------------------------------------------------+
// Trend Detection function
#define BULL 1
#define BEAR 2
// Input variables
//+------------------------------------------------------------------+
extern int Slippage = 5;
//-------------------------------------------------------------------+
extern int Magic = 55;
//-------------------------------------------------------------------+
extern bool MoneyMangement = true;
extern double Lots = 0.1;
extern double MaximumRisk = 0.8;
//-------------------------------------------------------------------+
extern int MAPeriod = 19;
//-------------------------------------------------------------------+
extern int MAMode = 2; // 0=SMA,1=EMA,2=SSMA,3=LWMA
//-------------------------------------------------------------------+
extern int TrailPeriod = 75;
//-------------------------------------------------------------------+
extern int SLPeriod = 42;
//-------------------------------------------------------------------+
extern int TotalOrders = 12;
//-------------------------------------------------------------------+
string OrderComments = "LiveMA";
//-----
color BuyColor = CLR_NONE;
color SellColor = CLR_NONE;
// Global variables
int Cnt = 0; // counter variable, used in for() loops
bool InitVariables; // init variable when program starts
datetime PreviousBar; // record the candle/bar time
int LastTrendDirection; // record the previous trends direction
int FileHandle; // variable for file handling
//-------------------------------------------------------------------+
//+------------------------------------------------------------------+
//| DO NOT MODIFY ANYTHING BELOW THIS LINE!!! |
//+------------------------------------------------------------------+
//-------------------------------------------------------------------+
int init()
{
if(IsTesting()==false)
{
InitVariables = true; // Allows us to init variables in the start function because
// we cannot do this in the init function because the variables
// use values from the chart
//create data file if it does not exist and load variables if file does exist
FileHandle=FileOpen("LiveMA.dat",FILE_BIN | FILE_READ);
if(FileHandle<1)
{
Print("LiveMA.dat not found.");
FileHandle=FileOpen("LiveMA.dat",FILE_BIN | FILE_WRITE);
if(FileHandle>0)
{
LastTrendDirection=0;
FileWriteInteger(FileHandle,LastTrendDirection,SHORT_VALUE);
FileClose(FileHandle);
Print("LiveMA.dat has been successfully created.");
}
else
{
FileClose(FileHandle);
Print("Failed to create LiveMA.dat file.");
}
}
else
{
LastTrendDirection=FileReadInteger(FileHandle,SHORT_VALUE);
Print("LiveMA variables loaded from file.");
FileClose(FileHandle);
}
}
return(0);
}
//+------------------------------------------------------------------+
//|-----------------------// Save Variables //---------------------|
//+------------------------------------------------------------------+
int SaveVariables()
{
if(IsTesting()==false)
{
//save variables to file
FileHandle=FileOpen("LiveMA.dat",FILE_BIN | FILE_WRITE);
if(FileHandle<1)
{
Print("LiveMA.dat not found.");
FileHandle=FileOpen("LiveMA.dat",FILE_BIN | FILE_WRITE);
if(FileHandle>0)
{
FileWriteInteger(FileHandle,LastTrendDirection,SHORT_VALUE);
FileClose(FileHandle);
Print("LiveMA.dat has been successfully created.");
}
else
{
FileClose(FileHandle);
Print("Failed to create LiveMA.dat file.");
}
}
else
{
FileWriteInteger(FileHandle,LastTrendDirection,SHORT_VALUE);
FileClose(FileHandle);
Print("LiveMA Variables successfully saved to file.");
}
}
return(0);
}
//+------------------------------------------------------------------+
int deinit()
{
return(0);
}
//+------------------------------------------------------------------+
int start()
{
//-----
if(MoneyMangement == true)
Lots = NormalizeDouble(AccountBalance()*MaximumRisk/100.00/100.00,1);
else Lots = Lots;
//-----
// init variables when the expert advisor first starts running
if(InitVariables == true)
{
PreviousBar = Time[0]; // record the current canle/bar open time
// place code here that you only wnat to run one time
InitVariables = false; // change to false so we only init variable once
}
// record trends direction
if(LastTrendDirection==0)
{
if(TrendDetection()==BULL)
{
LastTrendDirection=BULL;
}
if(TrendDetection()==BEAR)
{
LastTrendDirection=BEAR;
}
}
//+------------------------------------------------------------------+
//|------------------------// Trail By MA //-------------------------|
//+------------------------------------------------------------------+
double BuyySL = NormalizeDouble(iMA(NULL,0,TrailPeriod,0,MAMode,PRICE_OPEN,0)*(1+1/100/1.6180339887),Digits);
double SellSL = NormalizeDouble(iMA(NULL,0,TrailPeriod,0,MAMode,PRICE_OPEN,0)*(1-1/100/1.6180339887),Digits);
//-----
int Orders = OrdersTotal();
for (int i=0; i<Orders; i++)
{
if(!(OrderSelect(i, SELECT_BY_POS, MODE_TRADES))) continue;
if(OrderSymbol() != Symbol()) continue;
{
if(OrderType() == OP_BUY && OrderMagicNumber()==Magic && BuyySL > OrderOpenPrice()
&& OrderStopLoss() != BuyySL && BuyySL > 0 && BuyySL!=EMPTY_VALUE && BuyySL > OrderStopLoss())
{
OrderModify(OrderTicket(),OrderOpenPrice(),BuyySL,OrderTakeProfit(),0,CLR_NONE);
}
if(OrderType() == OP_SELL && OrderMagicNumber()==Magic && SellSL < OrderOpenPrice()
&& OrderStopLoss() != SellSL && SellSL > 0 && SellSL!=EMPTY_VALUE && SellSL < OrderStopLoss())
{
OrderModify(OrderTicket(),OrderOpenPrice(),SellSL,OrderTakeProfit(),0,CLR_NONE);
}
}
}
//+------------------------------------------------------------------+
//|------------------------// Open Orders //-----------------------|
//+------------------------------------------------------------------+
// perform analysis and open orders on new candle/bar
if(NewBar() == true)
{
// only perform analysis and open new order if we have not reached our TotalOpenOrders max
if(TotalOpenOrders() < TotalOrders)
{
//-----
double BuyyStopLoss = NormalizeDouble(iMA(NULL,0,SLPeriod,0,MAMode,PRICE_OPEN,0)*(1-1/100/1.6180339887),Digits);
double SellStopLoss = NormalizeDouble(iMA(NULL,0,SLPeriod,0,MAMode,PRICE_OPEN,0)*(1+1/100/1.6180339887),Digits);
//----- // Open BUY
if(TrendDetection() == BULL && LastTrendDirection == BEAR)
{
{
OrderSend(Symbol(),OP_BUY,Lots,NormalizeDouble(Ask,Digits),Slippage,BuyyStopLoss,0,OrderComments,Magic,0,BuyColor);
}
LastTrendDirection=BULL;
// save variables to file
SaveVariables();
}
//----- // Open SELL
if(TrendDetection() == BEAR && LastTrendDirection == BULL)
{
{
OrderSend(Symbol(),OP_SELL,Lots,NormalizeDouble(Bid,Digits),Slippage,SellStopLoss,0,OrderComments,Magic,0,SellColor);
}
LastTrendDirection=BEAR;
// save variables to file
SaveVariables();
}
}
}
return(0);
}
//+------------------------------------------------------------------+
//|-----------------------// Orders Count //-----------------------|
//+------------------------------------------------------------------+
// This function returns the total amount of orders the expert advisor has open
int TotalOpenOrders()
{
Cnt=OrdersTotal();
int TotalOpenOrders = 0;
if(Cnt==0)
{
return(0);
}
else
{
for(;Cnt>=0;Cnt--)
{
RefreshRates();
OrderSelect(Cnt,SELECT_BY_POS);
if(OrderMagicNumber()==Magic)
{
TotalOpenOrders++;
}
}
}
return(TotalOpenOrders);
}
//+------------------------------------------------------------------+
//|--------------------------// New Bar //-------------------------|
//+------------------------------------------------------------------+
// This function return the value true if the current bar/candle was just formed
bool NewBar()
{
if(PreviousBar<Time[0])
{
PreviousBar = Time[0];
return(true);
}
else
{
return(false);
}
return(false); // in case if - else statement is not executed
}
//+------------------------------------------------------------------+
//|---------------------// Trend Detection //----------------------|
//+------------------------------------------------------------------+
int TrendDetection()
{
//-------------------------------------------------------------------+
double MA1=iMA(NULL,0,MAPeriod,0,MAMode,PRICE_CLOSE,0);
double MA2=iMA(NULL,0,MAPeriod,0,MAMode,PRICE_WEIGHTED,0);
double MA3=iMA(NULL,0,MAPeriod,0,MAMode,PRICE_TYPICAL,0);
double MA4=iMA(NULL,0,MAPeriod,0,MAMode,PRICE_MEDIAN,0);
double MA5=iMA(NULL,0,MAPeriod,0,MAMode,PRICE_OPEN,0);
//-------------------------------------------------------------------+
//BULL trend
if(MA1 > MA2 && MA2 > MA3 && MA3 > MA4 && MA4 > MA5)
{
return(BULL);
}
//BEAR trend
if(MA1 < MA2 && MA2 < MA3 && MA3 < MA4 && MA4 < MA5)
{
return(BEAR);
}
//-----
return(0);
}
//+------------------------------------------------------------------+
//|---------------------------// END //------------------------------|
//+------------------------------------------------------------------+
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
---