Price Data Components
Orders Execution
Miscellaneous
0
Views
0
Downloads
0
Favorites
Price Action EA2
//+------------------------------------------------------------------+
//| This MQL is generated by Expert Advisor Builder |
//| http://sufx.core.t3-ism.net/ExpertAdvisorBuilder/ |
//| |
//| In no event will author be liable for any damages whatsoever. |
//| Use at your own risk. |
//| |
//+------------------- DO NOT REMOVE THIS HEADER --------------------+
#include <stdlib.mqh>
#include <stderror.mqh>
// Add defines for signals
#define SIGNAL_NONE 0
#define SIGNAL_BUY 1
#define SIGNAL_SELL 2
#define SIGNAL_CLOSEBUY 3
#define SIGNAL_CLOSESELL 4
// Add defines for position status
#define NONE 0
#define OPENED 1
#define CLOSED 2
// MoneyManagement Account Usage
#define EQUITY 2
#define MARGIN 3
#property copyright "Expert Advisor Builder"
#property link "http://sufx.core.t3-ism.net/ExpertAdvisorBuilder/"
extern int MagicBase = 100000;
int MagicNumber = 0;
extern bool SignalMail = False;
extern int Slippage = 3;
extern int StopLoss = 50;
extern int TakeProfit = 5;
extern int UseHideSL=1;
extern double HideSL=40;
extern int Current = 0;
extern string ts0 = "---TrailingStopLoss---";
extern string ts1 = " 1. None";
extern string ts2 = " 2. Standard at input";
extern string ts3 = " 3. Trail immediately";
extern int TrailingStopMethod = 2;
extern string ts12 = "Settings for Type 2";
extern double TrailingStop = 30;
//+---------------------------------------------------+
//|Money Management |
//+---------------------------------------------------+
extern string mm = "---Money Management---";
extern double Lots=0.1;
extern bool UseMoneyManagement = false;
extern string mm0 = "Select One of the following";
extern string mm1 = " 1. Use Account Balance";
extern string mm2 = " 2. Use Account Equity";
extern string mm3 = " 3. Use Account Free Margin";
extern int mmAccount = 1;
extern bool BrokerIsIBFX = false;
extern string mm4="Set mini and micro to false for standard account";
extern bool AccountIsMini = true;
extern bool AccountIsMicro = false;
extern double TradeSizePercent = 1; // Change to whatever percent of equity you wish to risk.
extern bool BrokerPermitsFractionalLots = true;
double myPoint;
double mLots;
double TPprice,STprice, HiddenSTprice;
string gTPpriceName;
string gSTpriceName;
//+------------------------------------------------------------------+
//| expert initialization function |
//+------------------------------------------------------------------+
int init() {
if (MagicBase > 0) MagicNumber = MagicBase + func_Symbol2Val(Symbol())*100 + func_TimeFrame_Const2Val(Period());
GetGlobalVars();
myPoint = SetPoint();
return(0);
}
//+------------------------------------------------------------------+
//| expert deinitialization function |
//+------------------------------------------------------------------+
int deinit() {
return(0);
}
//+------------------------------------------------------------------+
//| expert start function |
//+------------------------------------------------------------------+
int start() {
int Position, signal = SIGNAL_NONE;
RefreshRates();
if (IsTradeAllowed()==false ) return(0);
Position = HandleOpenPositions();
if (Position == OPENED) return(0);
signal=GetSignal(); // wait until first tick after bar close to take any action;
mLots = GetLots();
//Check free margin
if (AccountFreeMargin() < (1000 * mLots)) {
Print("We have no money. Free Margin = ", AccountFreeMargin());
return(0);
}
//Buy
if (signal == SIGNAL_BUY) {
OpenTrade(OP_BUY, mLots);
return(0);
}
//Sell
if (signal == SIGNAL_SELL ) {
OpenTrade(OP_SELL, mLots);
return(0);
}
return(0);
}
//+------------------------------------------------------------------+
int GetSignal()
{
int signal = SIGNAL_NONE;
//+------------------------------------------------------------------+
//| Variable Begin |
//+------------------------------------------------------------------+
double close1 = iClose(NULL, 0, Current + 1);
double close0 = iClose(NULL, 0, Current + 0);
double high2 = iHigh(NULL, 0, Current + 2);
double high1 = iHigh(NULL, 0, Current + 1);
double low2 = iLow(NULL, 0, Current + 2);
double low1 = iLow(NULL, 0, Current + 1);
if (close1 <= high2 && close0 > high1 && close0 > high2) signal = SIGNAL_BUY;
if (close1 >= low2 && close0 < low1 && close0 < low2) signal = SIGNAL_SELL;
return(signal);
}
int OpenTrade(int signal, double mLots)
{
int err, ticket;
RefreshRates();
if (signal==OP_BUY)
{
ticket=OrderSend(Symbol(),OP_BUY,mLots,Ask,Slippage,0,0,"Buy(#" + MagicNumber + ")",MagicNumber,0,DodgerBlue);
if (ticket > 0)
{
if (OrderSelect(ticket, SELECT_BY_TICKET, MODE_TRADES))
{
Print("BUY order opened : ", OrderOpenPrice());
if (SignalMail) SendMail("[Signal Alert]", "[" + Symbol() + "] " + DoubleToStr(Ask, Digits) + " Open Buy");
if (UseHideSL == 1)
{
HiddenSTprice = StopLong(OrderOpenPrice(), HideSL);
if (Digits > 0)
{
HiddenSTprice = NormalizeDouble( HiddenSTprice, Digits);
}
}
if (StopLoss != 0 || TakeProfit != 0)
{
TPprice = 0;
if (TakeProfit > 0)
{
TPprice=TakeLong(OrderOpenPrice(), TakeProfit);
TPprice = ValidTakeProfit(OP_BUY,Ask, TPprice);
}
STprice = 0;
if (StopLoss > 0)
{
STprice=StopLong(OrderOpenPrice(), StopLoss);
STprice = ValidStopLoss(OP_BUY,Bid, STprice);
}
// Normalize stoploss / takeprofit to the proper # of digits.
if (Digits > 0)
{
STprice = NormalizeDouble( STprice, Digits);
TPprice = NormalizeDouble( TPprice, Digits);
}
SaveTPprice(TPprice);
SaveSTprice(STprice);
OrderModify(ticket, OrderOpenPrice(), STprice, TPprice, 0, LightGreen);
}
}
}
}
else if (signal==OP_SELL)
{
ticket=OrderSend(Symbol(),OP_SELL,mLots,Bid,Slippage,0,0,"Sell(#" + MagicNumber + ")",MagicNumber,0,DeepPink);
if (ticket > 0)
{
if (OrderSelect(ticket, SELECT_BY_TICKET, MODE_TRADES))
{
Print("SELL order opened : ", OrderOpenPrice());
if (SignalMail) SendMail("[Signal Alert]", "[" + Symbol() + "] " + DoubleToStr(Bid, Digits) + " Open Sell");
if (UseHideSL == 1)
{
HiddenSTprice = StopShort(OrderOpenPrice(), HideSL);
if (Digits > 0)
{
HiddenSTprice = NormalizeDouble( HiddenSTprice, Digits);
}
}
if (StopLoss != 0 || TakeProfit != 0)
{
TPprice = 0;
if (TakeProfit > 0)
{
TPprice=TakeShort(OrderOpenPrice(),TakeProfit);
TPprice = ValidTakeProfit(OP_SELL,Bid, TPprice);
}
STprice = 0;
if (StopLoss > 0)
{
STprice=StopShort(OrderOpenPrice() ,StopLoss);
STprice = ValidStopLoss(OP_SELL,Ask, STprice);
}
// Normalize stoploss / takeprofit to the proper # of digits.
if (Digits > 0)
{
STprice = NormalizeDouble( STprice, Digits);
TPprice = NormalizeDouble( TPprice, Digits);
}
SaveTPprice(TPprice);
SaveSTprice(STprice);
OrderModify(ticket, OrderOpenPrice(), STprice, TPprice, 0, LightGreen);
}
}
}
}
if(ticket<0)
{
err = GetLastError();
Print("OrderSend failed with error(" + err + ") " + ErrorDescription(err) );
}
return(ticket);
}
//+------------------------------------------------------------------+
//| Handle Open Positions |
//| Check if any open positions need to be closed or modified |
//+------------------------------------------------------------------+
int HandleOpenPositions()
{
bool Done;
int ticket, OrderTy;
double OrderLTS, OrderOP;
double OrderTP, OrderSL;
int cnt;
int Closed;
Closed = NONE;
for(cnt=OrdersTotal()-1;cnt>=0;cnt--)
{
OrderSelect (cnt, SELECT_BY_POS, MODE_TRADES);
if ( OrderSymbol() != Symbol()) continue;
if ( OrderMagicNumber() != MagicNumber) continue;
ticket = OrderTicket();
OrderTy = OrderType();
OrderLTS = OrderLots();
OrderOP = OrderOpenPrice();
OrderTP = OrderTakeProfit();
OrderSL = OrderStopLoss();
Closed = OPENED;
// Check for TakeProfit Exits
Done = false;
Done = CheckTPClose(OrderTy, ticket, OrderLTS, TPprice);
if (Done == true) return(CLOSED);
// Check for internal StopLoss
if (UseHideSL == 1)
{
Done = false;
Done = CheckSLClose(OrderTy, ticket, OrderLTS, HiddenSTprice);
if (Done == true) return(CLOSED);
}
// Check for regular StopLoss
Done = false;
Done = CheckSLClose(OrderTy, ticket, OrderLTS, STprice);
if (Done == true) return(CLOSED);
// Check for exit on signal
Done = false;
Done = CheckExitCondition(OrderTy, ticket, OrderLTS);
if (Done == true) return(CLOSED);
// If we get to here the trade is still Open
// check for trailing stop
if (TrailingStopMethod > 0)
{
HandleTrailingStop(OrderTy,ticket,OrderOP,OrderSL,OrderTP);
}
}
return(Closed);
}
int GetExitSignal(int cmd)
{
return(SIGNAL_NONE);
}
//+------------------------------------------------------------------+
//| Check for close signal |
//+------------------------------------------------------------------+
bool CheckExitCondition(int cmd, int ticket, double ol)
{
int exit_signal;
exit_signal = GetExitSignal(cmd);
if(cmd==OP_BUY)
{
if(exit_signal == SIGNAL_CLOSEBUY)
{
OrderClose(ticket, ol, Bid, Slippage, Green);
if (SignalMail) SendMail("[Signal Alert]", "[" + Symbol() + "] " + DoubleToStr(Bid, Digits) + " Close Buy");
return(true);
}
}
if(OrderType()==OP_SELL)
{
if(exit_signal == SIGNAL_CLOSESELL)
{
OrderClose(ticket, ol, Ask, Slippage, Red);
if (SignalMail) SendMail("[Signal Alert]", "[" + Symbol() + "] " + DoubleToStr(Ask, Digits) + " Close Sell");
return(true);
}
}
return(false);
//----
}
bool CheckSLClose(int cmd, int ticket, double ol, double IntSTprice)
{
if (cmd == OP_BUY)
{
if(Bid <= IntSTprice)
{
OrderClose(ticket,ol,Bid, Slippage, Green);
if (SignalMail) SendMail("[Signal Alert]", "[" + Symbol() + "] " + DoubleToStr(Bid, Digits) + " Close Buy");
return (true);
}
return(false);
}
if (cmd == OP_SELL)
{
if (IntSTprice > 0.1)
{
if(Ask >= IntSTprice)
{
OrderClose(ticket,ol,Ask, Slippage, Red);
if (SignalMail) SendMail("[Signal Alert]", "[" + Symbol() + "] " + DoubleToStr(Ask, Digits) + " Close Sell");
return(true);
}
}
}
return(false);
}
bool CheckTPClose(int cmd, int ticket, double ol, double IntTPprice )
{
if (cmd == OP_BUY)
{
if(Bid >= IntTPprice && IntTPprice > 0.1)
{
OrderClose(ticket,ol,Bid, Slippage, Green);
if (SignalMail) SendMail("[Signal Alert]", "[" + Symbol() + "] " + DoubleToStr(Bid, Digits) + " Close Buy");
return(true);
}
return(false);
}
if (cmd == OP_SELL)
{
if(Ask <= IntTPprice)
{
OrderClose(ticket,ol,Ask, Slippage, Red);
if (SignalMail) SendMail("[Signal Alert]", "[" + Symbol() + "] " + DoubleToStr(Ask, Digits) + " Close Sell");
return(true);
}
}
return(false);
}
double SetPoint()
{
double mPoint;
if (Digits < 4)
mPoint = 0.01;
else
mPoint = 0.0001;
return(mPoint);
}
double StopLong(double price,int stop)
{
if(stop==0)
return(0);
else
return(price-(stop*myPoint));
}
double StopShort(double price,int stop)
{
if(stop==0)
return(0);
else
return(price+(stop*myPoint));
}
double TakeLong(double price,int take)
{
if(take==0)
return(0);
else
return(price+(take*myPoint));
}
double TakeShort(double price,int take)
{
if(take==0)
return(0);
else
return(price-(take*myPoint));
}
double ValidStopLoss(int type, double price, double SL)
{
double minstop;
double newSL;
if (SL < 0.1) return(SL);
minstop = MarketInfo(Symbol(),MODE_STOPLEVEL);
if (Digits == 3 || Digits == 5) minstop = minstop / 10;
newSL = SL;
if (type == OP_BUY)
{
if((price - SL) < minstop*myPoint) newSL = price - minstop*myPoint;
}
if (type == OP_SELL)
{
if((SL-price) < minstop*myPoint) newSL = price + minstop*myPoint;
}
newSL = NormalizeDouble(newSL,Digits);
return(newSL);
}
double ValidTakeProfit(int type, double price, double TP)
{
double newTP, temp;
temp = MarketInfo(Symbol(), MODE_STOPLEVEL) * myPoint;
newTP = TP;
if (type == OP_BUY)
{
if((TP - price) < temp) newTP = price + temp;
}
if (type == OP_SELL)
{
if((price - TP) < temp) newTP = price - temp;
}
newTP = NormalizeDouble(newTP,Digits);
return(newTP);
}
//+------------------------------------------------------------------+
//| HandleTrailingStop |
//| Type 1 is do not trail |
//| Type 2 waits for price to move the amount of the trailStop |
//| before moving stop loss then moves like type 4 |
//| Type 3 moves the stoploss without delay. |
//+------------------------------------------------------------------+
void HandleTrailingStop(int cmd, int ticket, double op, double os, double tp)
{
switch (TrailingStopMethod)
{
case 2 : Delayed_TrailingStop (cmd, ticket, op, os, tp);
break;
case 3 : Immediate_TrailingStop (cmd, ticket, op, os, tp);
break;
}
}
//+------------------------------------------------------------------+
//| Immediate_TrailingStop.mq4 |
//| Copyright © 2006, Forex-TSD.com |
//| Written by MrPip,robydoby314@yahoo.com |
//| |
//| Moves the stoploss without delay. |
//+------------------------------------------------------------------+
void Immediate_TrailingStop(int type, int ticket, double op, double os, double tp)
{
int digits;
double pt, pBid, pAsk, BuyStop, SellStop;
digits = MarketInfo(Symbol( ), MODE_DIGITS);
if (type==OP_BUY)
{
pBid = MarketInfo(Symbol(), MODE_BID);
pt = StopLoss * myPoint;
if(pBid-os > pt)
{
BuyStop = pBid - pt;
if (digits > 0) BuyStop = NormalizeDouble( BuyStop, digits);
BuyStop = ValidStopLoss(OP_BUY,pBid, BuyStop);
if (os < BuyStop)
{
SaveSTprice(BuyStop);
OrderModify(ticket,op,BuyStop,tp,0,LightGreen);
}
return;
}
}
if (type==OP_SELL)
{
pAsk = MarketInfo(Symbol(), MODE_ASK);
pt = StopLoss * myPoint;
if(os - pAsk > pt)
{
SellStop = pAsk + pt;
if (digits > 0) SellStop = NormalizeDouble( SellStop, digits);
SellStop = ValidStopLoss(OP_SELL, pAsk, SellStop);
if (os > SellStop)
{
SaveSTprice(SellStop);
OrderModify(ticket,op,SellStop,tp,0,DarkOrange);
}
return;
}
}
}
//+------------------------------------------------------------------+
//| Delayed_TrailingStop.mq4 |
//| Copyright © 2006, Forex-TSD.com |
//| Written by MrPip,robydoby314@yahoo.com |
//| |
//| Waits for price to move the amount of the TrailingStop |
//| Moves the stoploss pip for pip after delay. |
//+------------------------------------------------------------------+
void Delayed_TrailingStop(int type, int ticket, double op, double os, double tp)
{
int digits;
double pt, pBid, pAsk, BuyStop, SellStop;
if (TrailingStop <= 0) return;
pt = TrailingStop * myPoint;
digits = MarketInfo(Symbol(), MODE_DIGITS);
if (type==OP_BUY)
{
pBid = MarketInfo(Symbol(), MODE_BID);
BuyStop = pBid - pt;
if (digits > 0) BuyStop = NormalizeDouble( BuyStop, digits);
BuyStop = ValidStopLoss(OP_BUY,pBid, BuyStop);
if (pBid-op > pt && os < BuyStop)
{
SaveSTprice(BuyStop);
OrderModify(ticket,op,BuyStop,tp,0,LightGreen);
}
return;
}
if (type==OP_SELL)
{
pAsk = MarketInfo(Symbol(), MODE_ASK);
pt = TrailingStop * myPoint;
SellStop = pAsk + pt;
if (digits > 0) SellStop = NormalizeDouble( SellStop, digits);
SellStop = ValidStopLoss(OP_SELL, pAsk, SellStop);
if (op - pAsk > pt && os > SellStop)
{
SaveSTprice (SellStop);
OrderModify(ticket,op,SellStop,tp,0,DarkOrange);
}
return;
}
}
//+------------------------------------------------------------------+
//| Get number of lots for this trade |
//+------------------------------------------------------------------+
double GetLots()
{
double lot, AccountValue;
double myMaxLot = MarketInfo(Symbol(), MODE_MAXLOT);
if (UseMoneyManagement == false) return(Lots);
if (BrokerIsIBFX == true)
{
lot = Calc_IBFX_Money_Management();
return(lot);
}
//+------------------------------------------------------------------+
//| Calculate optimal lot size |
//+------------------------------------------------------------------+
if (mmAccount == EQUITY) AccountValue = AccountEquity();
else if (mmAccount == MARGIN) AccountValue = AccountFreeMargin();
else AccountValue = AccountBalance();
lot=NormalizeDouble(AccountValue*TradeSizePercent/1000.0,1);
// lot = MathRound( lot/myStep ) * myStep;
// Use at least 1 micro lot
if (AccountIsMicro == true)
{
lot = MathFloor(lot*100)/100;
if (lot < 0.01) lot = 0.01;
if (lot > myMaxLot) lot = myMaxLot;
return(lot);
}
// Use at least 1 mini lot
if(AccountIsMini == true)
{
lot = MathFloor(lot*10)/10;
if (lot < 0.1) lot = 0.1;
if (lot > myMaxLot) lot = myMaxLot;
return(lot);
}
// Standard account
if( BrokerPermitsFractionalLots == false)
{
if (lot >= 1.0) lot = MathFloor(lot); else lot = 1.0;
}
if (lot < 1.0) lot = 1.0;
if (lot > myMaxLot) lot = myMaxLot;
return(lot);
}
//+------------------------------------------------------------------+
//| Calculate optimal lot size for IBFX |
//+------------------------------------------------------------------+
double Calc_IBFX_Money_Management()
{
// variables used for money management
double lot, AccountValue;
double myMaxLot = MarketInfo(Symbol(), MODE_MAXLOT);
if (mmAccount == EQUITY) AccountValue = AccountEquity();
else if (mmAccount == MARGIN) AccountValue = AccountFreeMargin();
else AccountValue = AccountBalance();
lot=NormalizeDouble(AccountValue*TradeSizePercent/1000.0,2);
// Use at least 1 micro lot
if (AccountIsMicro == true)
{
lot = lot * 10;
lot = MathFloor(lot*100)/100;
if (lot < 0.1) lot = 0.1;
if (lot > myMaxLot) lot = myMaxLot;
return(lot);
}
// Use at least 1 mini lot
if(AccountIsMini == true)
{
lot = lot * 10;
lot = MathFloor(lot*10)/10;
if (lot < 1) lot = 1;
if (lot > myMaxLot) lot = myMaxLot;
return(lot);
}
// Standard Account
if(BrokerPermitsFractionalLots == true)
lot = StrToDouble(DoubleToStr(lot, 2));
else
lot = MathFloor(lot);
if (lot > myMaxLot) lot = myMaxLot;
return(lot);
}
int func_Symbol2Val(string symbol)
{
string mySymbol = StringSubstr(symbol,0,6);
if(mySymbol=="AUDCAD") return(1);
if(mySymbol=="AUDJPY") return(2);
if(mySymbol=="AUDNZD") return(3);
if(mySymbol=="AUDUSD") return(4);
if(mySymbol=="CHFJPY") return(5);
if(mySymbol=="EURAUD") return(6);
if(mySymbol=="EURCAD") return(7);
if(mySymbol=="EURCHF") return(8);
if(mySymbol=="EURGBP") return(9);
if(mySymbol=="EURJPY") return(10);
if(mySymbol=="EURUSD") return(11);
if(mySymbol=="GBPCHF") return(12);
if(mySymbol=="GBPJPY") return(13);
if(mySymbol=="GBPUSD") return(14);
if(mySymbol=="NZDJPY") return(15);
if(mySymbol=="NZDUSD") return(16);
if(mySymbol=="USDCAD") return(17);
if(mySymbol=="USDCHF") return(18);
if(mySymbol=="USDJPY") return(19);
if(mySymbol=="CADJPY") return(20);
Comment("unexpected Symbol");
return(999);
}
//+------------------------------------------------------------------+
//| Time frame interval appropriation function |
//+------------------------------------------------------------------+
int func_TimeFrame_Const2Val(int Constant ) {
switch(Constant) {
case 1: // M1
return(1);
case 5: // M5
return(2);
case 15:
return(3);
case 30:
return(4);
case 60:
return(5);
case 240:
return(6);
case 1440:
return(7);
case 10080:
return(8);
case 43200:
return(9);
}
}
string tf2txt(int tf)
{
switch(tf)
{
case PERIOD_M1: return("M1");
case PERIOD_M5: return("M5");
case PERIOD_M15: return("M15");
case PERIOD_M30: return("M30");
case PERIOD_H1: return("H1");
case PERIOD_H4: return("H4");
case PERIOD_D1: return("D1");
case PERIOD_W1: return("W1");
case PERIOD_MN1: return("MN");
}
return("??");
}
void GetGlobalVars()
{
NameGlobalVars();
InitGlobalVars();
GetGlobalVarValues();
}
void GetGlobalVarValues()
{
double temp;
// Booleans stored as double so convert to boolean
// > 0 true, < 0 false
// temp = GlobalVariableGet(gFirst_TP_LevelName);
// if (temp > 0) First_TP_Level = true; else First_TP_Level = false;
TPprice = GlobalVariableGet (gTPpriceName);
STprice = GlobalVariableGet (gSTpriceName);
}
void InitGlobalVars()
{
// check variable before use
if(!GlobalVariableCheck(gTPpriceName))
GlobalVariableSet(gTPpriceName,0);
if(!GlobalVariableCheck(gSTpriceName))
GlobalVariableSet(gSTpriceName,0);
}
void NameGlobalVars()
{
string prefix;
prefix = "VCS_" + Symbol() + tf2txt(Period());
gTPpriceName = prefix + "_TPprice";
gSTpriceName = prefix + "_STprice";
}
void SaveSTprice (double myVal)
{
GlobalVariableSet(gSTpriceName,myVal);
}
void SaveTPprice (double myVal)
{
GlobalVariableSet(gTPpriceName,myVal);
}
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
---