Price Data Components
Orders Execution
Indicators Used
Miscellaneous
0
Views
0
Downloads
0
Favorites
AnyMA_RSI_R2_EA_Opt_wLog
//+------------------------------------------------------------------+
//| |
//| RSI-R2.mq4 - Ver 1.0 @ 03/22/2007 by Bluto |
//| |
//| Changes by Rick Myers |
//| 4/8/2007 Modified code to use MFI instead of MFI |
//| Changes by Robert Hill |
//| 4/3/2007 added input for RSI_Period |
//| Modified code for MagicNumber calculation |
//| Added function to set value for RSI_Period |
//| Set value of AssignRSI to false during optimization |
//| Use optimized value in function AssignRSI_Period |
//| Set value of AssignRSI to true when running live |
//| 4/5/2007 Fixed bug that caused no more trades after a stoploss |
//| added 2 variable to hold pSar values to reduce calls |
//| Added GetLots as a function using MM code from start |
//| 4/6/2007 Combined with RSI_R2_AnyTimeFrame_Optimzable.mq4 |
//| Made more modular |
//| Added code for possible MA Angle filter |
//| Added 7 Trailing stop functions for testing |
//| Combined Bluots open trade code with mine |
//| 4/15/2007 Added optimized values for RSI period from bluto |
//| Added change to moneymanagement from bluto |
//| 4/16/2007 Added code to check RSI 4 bars back for confirmation |
//| Added new exit ideas from Gideon |
//| Both are selectable by input switches. |
//| 4/27/2007 Added check to prevent another trade from opening |
//| on the same dat a trade is closed |
//| 5/6/2007 Added new exit strategies |
//| RSI 14 as described by Loren |
//| My idea to use CCI 50 |
//| 5/8/2007 Added use of MFI by selection |
//| Changed input name useDefaultRSI_Period |
//| to useOptimizedRSI_Period |
//| 5/11/2007 Removed use of MFI and MA Angle for filter |
//| Added use of HMA3 for filter |
//| Added code to only check signal once per completed bar |
//| Made new exit methods selectable from menu for easier |
//| testing for best method |
//| |
//| Modifications suggested by Loren |
//| Require daily RSI 14 weighted close >= 35 for long |
//| to correct the premature entry problem where price |
//| price continues to decline for an indeterminate period.|
//| The reverse of this for shorts w/ RSI 14 <= 65. |
//| 5/19/2007 Optimized values using FXDD data
//| 6/8/07 Code modification using EMA instead of SMA, suggested |
//| by Loren |
//+------------------------------------------------------------------+
#property copyright "Bluto and Robert"
#property link "None"
#include <stdlib.mqh>
// This makes code easier to read
// Exit Method
#define NONE 1
#define RSI_EXIT 2
#define CCI_EXIT 3
// Currency pair
#define AUDCAD 1
#define AUDJPY 2
#define AUDNZD 3
#define AUDUSD 4
#define CHFJPY 5
#define EURAUD 6
#define EURCAD 7
#define EURCHF 8
#define EURGBP 9
#define EURJPY 10
#define EURUSD 11
#define GBPCHF 12
#define GBPJPY 13
#define GBPUSD 14
#define NZDJPY 15
#define NZDUSD 16
#define USDCAD 17
#define USDCHF 18
#define USDJPY 19
#define UNEXPECTED 999
extern string Expert_Name = "---- RSI_R2_EA_Opt ----";
// Create a trade log to determine if EA is trading correctly
extern string db="--- Debug file settings---";
extern bool Debug = false;
extern bool Debug_Trade = false; // Change to true to allow debug Trade log file to be created
extern string Trade_Log = " RSI_R2_TradeLog";
extern bool BTDebug_Trade = false; // Change to true to allow Backtest debug Trade log file to be created
extern string BT_Trade_Log = " RSI_R2_BTTradeLog"; // Backtest trade log. Writes when backtest is complete.
//+---------------------------------------------------+
//|Money Management |
//+---------------------------------------------------+
extern double LotSize=0.1;
extern double RiskPercent=2.0;
extern bool UseMoneyMgmt=false;
extern bool BrokerPermitsFractionalLots = true;
//+---------------------------------------------------+
//|Indicator Variables |
//| Change these to try your own system |
//| or add more if you like |
//+---------------------------------------------------+
extern string mi="--Moving Average settings--";
extern int MaTrend_Period = 200;
extern string m = "--Moving Average Types--";
extern string m0 = " 0 = SMA";
extern string m1 = " 1 = EMA";
extern string m2 = " 2 = SMMA";
extern string m3 = " 3 = LWMA";
extern int MA_Type = 0;
extern string ri="--RSI settings--";
extern int RSI_Period = 4; // input value to be optimized
extern bool UseOptimizedSettings = true;
extern int BuyWhenRsiBelow = 65;
extern int SellWhenRsiAbove = 35;
extern double RSI_Overbought_Value = 75.0;
extern double RSI_Oversold_Value = 25.0;
extern string u1="--RSI turnup/down confirmation--";
extern int UseTurnUpDown = 1;
extern string l1="--RSI 14 confirmation--";
extern int UseRSI_Confirmation = 1;
extern int RSI_ConfirmPeriod = 14;
extern int BuyConfirmLevel = 35;
extern int SellConfirmLevel = 65;
// Added Blutos newest exit using Price close vs EMA 200
extern int Use200EMA_Exit = 1;
extern string ex1="--New Trade Exit Method--";
extern string ex2=" 1. None";
extern string ex3=" 2. RSI ";
extern string ex4=" 3. CCI ";
extern int ExitMethod = 2;
extern int RSI_Exit_Period = 14;
extern int RSI_BuyExitLevel = 50;
extern int RSI_SellExitLevel = 50;
extern int CCI_Exit_Period = 50;
extern int CCI_BuyExitLevel = 0;
extern int CCI_SellExitLevel = 0;
extern string st1="--Signal_TimeFrame--";
extern int Signal_TimeFrame = 0;
extern string hd = " --Limit 1 trade per day --";
extern int UseDelay = 1;
extern int MaxTrades = 1;
// Added this for possible filter for flat markets
extern string ai="--HMA filter settings--";
extern string a2=" Set switch to 1 to use filter";
extern int UseFilter = 1;
extern double HMA_Period=200;
extern int Separation = 1;
extern int Filter_TimeFrame = 0;
//+---------------------------------------------------+
//|Profit controls |
//+---------------------------------------------------+
extern string st6 = "--Profit Controls--";
extern double StopLoss=0;
extern double TakeProfit=0;
extern int Slippage=3;
extern string tsp0 = "--Trailing Stop Types--";
extern string tsp1 = " 1 = Trail immediately";
extern string tsp2 = " 2 = Wait to trail";
extern string tsp3 = " 3 = Uses 3 levels before trail";
extern string tsp4 = " 4 = Breakeven + Lockin";
extern string tsp5 = " 5 = Step trail";
extern string tsp6 = " 6 = EMA trail";
extern string tsp7 = " 7 = pSAR trail";
extern string tsp8 = " 8 = Blutos pSar trail";
extern bool UseTrailingStop = true;
extern int TrailingStopType = 8;
extern string ts2 = "Settings for Type 2";
extern double TrailingStop = 15; // Change to whatever number of pips you wish to trail your position with.
extern string ts3 = "Settings for Type 3";
extern double FirstMove = 20; // Type 3 first level pip gain
extern double FirstStopLoss = 50; // Move Stop to Breakeven
extern double SecondMove = 30; // Type 3 second level pip gain
extern double SecondStopLoss = 30; // Move stop to lock is profit
extern double ThirdMove = 40; // type 3 third level pip gain
extern double TrailingStop3 = 20; // Move stop and trail from there
extern string ts4 = "Settings for Type 4";
extern double BreakEven = 30;
extern int LockInPips = 1; // Profit Lock in pips
extern string ts5 = "Settings for Type 5";
extern int eTrailingStop = 10;
extern int eTrailingStep = 2;
extern string ts6 = "Settings for Type 6";
extern int EMATimeFrame = 30;
extern int Price = 0;
extern int EMAPeriod = 13;
extern int EMAShift = 2;
extern int InitialStop = 0;
extern string ts7 = "Settings for Type 7";
extern string pi="--pSAR settings--";
extern double StepParabolic = 0.02;
extern double MaxParabolic = 0.2;
extern int Interval = 5;
extern string ts8 = "Settings for Type 8";
extern string pi2="--pSAR settings--";
extern double SarStep = 0.02;
extern double SarMax = 0.20;
//+---------------------------------------------------+
//|General controls |
//+---------------------------------------------------+
int MagicNumber=0;
string setup;
int TradesInThisSymbol = 0;
double MM_OrderLotSize=0;
datetime StopTime; // Time to wait after a trade is stopped out
bool StoppedOut=false;
datetime timeprev=0;
string filenameTradeLog,filenameBTTradeLog;
double HighBid, LowBid, HighAsk, LowAsk;
//+---------------------------------------------------+
//| Indicator values for signals and filters |
//| Add or Change to test your system |
//+---------------------------------------------------+
int myRSI_Period = 2; // Used by RSI indicator call
int myBuyWhenRsiBelow = 65;
int mySellWhenRsiAbove = 35;
int myUseTurnUpDown = 1;
int myUseRSI_Confirmation = 1;
int myExitMethod = 1;
int myUseFilter = 1;
//+------------------------------------------------------------------+
//| Calculate MagicNumber, setup comment and assign RSI Period |
//| |
//+------------------------------------------------------------------+
int init()
{
MagicNumber = 200000 + func_Symbol2Val(Symbol())*100 + func_TimeFrame_Const2Val(Period());
setup=Expert_Name + Symbol() + "_" + func_TimeFrame_Val2String(func_TimeFrame_Const2Val(Period()));
if (UseOptimizedSettings == true) UseOptimized(Symbol()); else UseDefaults();
if (Debug == true) SetupLogFiles();
return(0);
}
int deinit()
{
if (Debug == true) CloseLogFiles();
return(0);
}
void UseDefaults()
{
myRSI_Period = RSI_Period;
myBuyWhenRsiBelow = BuyWhenRsiBelow;
mySellWhenRsiAbove = SellWhenRsiAbove;
myUseTurnUpDown = UseTurnUpDown;
myUseRSI_Confirmation = UseRSI_Confirmation;
myExitMethod = ExitMethod;
myUseFilter = UseFilter;
}
//+------------------------------------------------------------------+
//| AssignRSI_Period |
//| Returns the optimized value for RSI period based on symbol |
//| |
//| This function will be changed when new optimized values are |
//| determined from backtest |
//| |
//+------------------------------------------------------------------+
void UseOptimized(string symbol)
{
myRSI_Period = 4;
int which = func_Symbol2Val(symbol);
switch (which)
{
case AUDCAD : myRSI_Period = 6;
myBuyWhenRsiBelow = 40;
mySellWhenRsiAbove = 65;
myUseTurnUpDown = 1;
myUseRSI_Confirmation = 1;
myExitMethod = 1;
myUseFilter = 0;
break;
case AUDJPY : myRSI_Period = 8;
myBuyWhenRsiBelow = 45;
mySellWhenRsiAbove = 65;
myUseTurnUpDown = 1;
myUseRSI_Confirmation = 0;
myExitMethod = 3;
myUseFilter = 1;
break;
case AUDNZD : myRSI_Period = 3;
myBuyWhenRsiBelow = 50;
mySellWhenRsiAbove = 75;
myUseTurnUpDown = 1;
myUseRSI_Confirmation = 1;
myExitMethod = 3;
myUseFilter = 1;
break;
case AUDUSD : myRSI_Period = 6;
myBuyWhenRsiBelow = 50;
mySellWhenRsiAbove = 75;
myUseTurnUpDown = 1;
myUseRSI_Confirmation = 1;
myExitMethod = 1;
myUseFilter = 1;
break;
case CHFJPY : myRSI_Period = 6;
myBuyWhenRsiBelow = 50;
mySellWhenRsiAbove = 50;
myUseTurnUpDown = 1;
myUseRSI_Confirmation = 0;
myExitMethod = 1;
myUseFilter = 0;
break;
case EURAUD : myRSI_Period = 3;
myBuyWhenRsiBelow = 45;
mySellWhenRsiAbove = 60;
myUseTurnUpDown = 0;
myUseRSI_Confirmation = 1;
myExitMethod = 2;
myUseFilter = 0;
break;
case EURCAD : myRSI_Period = 9;
myBuyWhenRsiBelow = 45;
mySellWhenRsiAbove = 75;
myUseTurnUpDown = 0;
myUseRSI_Confirmation = 1;
myExitMethod = 2;
myUseFilter = 0;
break;
case EURCHF : myRSI_Period = 8;
myBuyWhenRsiBelow = 50;
mySellWhenRsiAbove = 75;
myUseTurnUpDown = 0;
myUseRSI_Confirmation = 0;
myExitMethod = 1;
myUseFilter = 0;
break;
case EURGBP : myRSI_Period = 5;
myBuyWhenRsiBelow = 35;
mySellWhenRsiAbove = 60;
myUseTurnUpDown = 0;
myUseRSI_Confirmation = 1;
myExitMethod = 1;
myUseFilter = 1;
break;
case EURJPY : myRSI_Period = 4;
myBuyWhenRsiBelow = 40;
mySellWhenRsiAbove = 75;
myUseTurnUpDown = 1;
myUseRSI_Confirmation = 0;
myExitMethod = 3;
myUseFilter = 1;
break;
case EURUSD : myRSI_Period = 7;
myBuyWhenRsiBelow = 50;
mySellWhenRsiAbove = 70;
myUseTurnUpDown = 1;
myUseRSI_Confirmation = 0;
myExitMethod = 3;
myUseFilter = 1;
break;
case GBPCHF : myRSI_Period = 5;
myBuyWhenRsiBelow = 50;
mySellWhenRsiAbove = 70;
myUseTurnUpDown = 0;
myUseRSI_Confirmation = 1;
myExitMethod = 2;
myUseFilter = 1;
break;
case GBPJPY : myRSI_Period = 4;
myBuyWhenRsiBelow = 40;
mySellWhenRsiAbove = 75;
myUseTurnUpDown = 1;
myUseRSI_Confirmation = 0;
myExitMethod = 3;
myUseFilter = 1;
break;
case GBPUSD : myRSI_Period = 4;
myBuyWhenRsiBelow = 45;
mySellWhenRsiAbove = 70;
myUseTurnUpDown = 0;
myUseRSI_Confirmation = 0;
myExitMethod = 1;
myUseFilter = 1;
break;
case NZDJPY : myRSI_Period = 6;
myBuyWhenRsiBelow = 65;
mySellWhenRsiAbove = 35;
myUseTurnUpDown = 1;
myUseRSI_Confirmation = 1;
myExitMethod = 1;
myUseFilter = 1;
break;
case NZDUSD : myRSI_Period = 7;
myBuyWhenRsiBelow = 65;
mySellWhenRsiAbove = 35;
myUseTurnUpDown = 1;
myUseRSI_Confirmation = 1;
myExitMethod = 1;
myUseFilter = 1;
break;
case USDCAD : myRSI_Period = 3;
myBuyWhenRsiBelow = 40;
mySellWhenRsiAbove = 65;
myUseTurnUpDown = 0;
myUseRSI_Confirmation = 0;
myExitMethod = 2;
myUseFilter = 0;
break;
case USDCHF : myRSI_Period = 9;
myBuyWhenRsiBelow = 40;
mySellWhenRsiAbove = 75;
myUseTurnUpDown = 1;
myUseRSI_Confirmation = 0;
myExitMethod = 2;
myUseFilter = 0;
break;
case USDJPY : myRSI_Period = 3;
myBuyWhenRsiBelow = 40;
mySellWhenRsiAbove = 70;
myUseTurnUpDown = 0;
myUseRSI_Confirmation = 0;
myExitMethod = 3;
myUseFilter = 1;
break;
case UNEXPECTED : myRSI_Period = 4;
myBuyWhenRsiBelow = 65;
mySellWhenRsiAbove = 35;
myUseTurnUpDown = 1;
myUseRSI_Confirmation = 1;
myExitMethod = 1;
myUseFilter = 1;
}
}
//+------------------------------------------------------------------+
//| LastTradeStoppedOut |
//| Check History to see if last trade stopped out |
//| Return Time for next trade |
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
//| LastTradeClosedToday |
//| Check History to see if last trade closed today |
//+------------------------------------------------------------------+
bool LastTradeClosedToday()
{
int cnt, total;
bool Closed;
total = HistoryTotal();
for (cnt = total - 1; cnt >= 0; cnt--)
{
OrderSelect (cnt, SELECT_BY_POS, MODE_HISTORY);
if(OrderSymbol()!=Symbol()) continue;
if (OrderMagicNumber() != MagicNumber) continue;
Closed = false;
if (OrderType() == OP_BUY)
{
if (TimeDay(OrderCloseTime()) == TimeDay(TimeCurrent()))
{
Closed = true;
}
cnt = 0;
}
if (OrderType() == OP_SELL)
{
if (TimeDay(OrderCloseTime()) == TimeDay(TimeCurrent()))
{
Closed = true;
}
cnt = 0;
}
}
return (Closed);
}
//+------------------------------------------------------------------+
//| CheckFilter |
//| Uses HMA3 custom indicator |
//| Place trades only in the direction of Hull Moving Average |
//| No trade is placed if HMA is too flat |
//| |
//+------------------------------------------------------------------+
bool CheckFilter (int cmd)
{
double MACurrent, MAPrevious;
bool filter = false;
MACurrent=iCustom(NULL,Filter_TimeFrame,"Hma3",HMA_Period,0,0,0,1);
MAPrevious=iCustom(NULL,Filter_TimeFrame,"Hma3",HMA_Period,0,0,0,2);
filter = false;
switch (cmd)
{
case OP_BUY : if (MACurrent - MAPrevious >= Separation * Point) filter = true;
break;
case OP_SELL : if (MAPrevious - MACurrent >= Separation * Point) filter = true;
}
return (filter);
}
//+------------------------------------------------------------------+
//| CheckRSI_Exit |
//| Exit buy when RSI crosses below buy exit level default 50 |
//| Exit sell when RSI crosses above sell exit level default 50 |
//| |
//+------------------------------------------------------------------+
bool CheckRSI_Exit(int cmd)
{
double RSIcur, RSIprev;
RSIcur = iRSI(Symbol(),Signal_TimeFrame, RSI_Exit_Period, PRICE_CLOSE, 1);
RSIprev = iRSI(Symbol(),Signal_TimeFrame, RSI_Exit_Period, PRICE_CLOSE, 2);
switch (cmd)
{
case OP_BUY : if (RSIcur < RSI_BuyExitLevel && RSIprev > RSI_BuyExitLevel) return(true);
break;
case OP_SELL : if (RSIcur > RSI_SellExitLevel && RSIprev < RSI_SellExitLevel) return(true);
}
return(false);
}
//+------------------------------------------------------------------+
//| CheckCCI_Exit |
//| Exit buy when CCI crosses below buy exit level default 0 |
//| Exit sell when CCI crosses above sell exit level default 0 |
//| |
//+------------------------------------------------------------------+
bool CheckCCI_Exit(int cmd)
{
double CCIcur, CCIprev;
CCIcur = iCCI(Symbol(), Signal_TimeFrame, CCI_Exit_Period, PRICE_CLOSE, 1);
CCIprev = iCCI(Symbol(), Signal_TimeFrame, CCI_Exit_Period, PRICE_CLOSE, 2);
switch (cmd)
{
case OP_BUY : if (CCIcur < CCI_BuyExitLevel && CCIprev > CCI_BuyExitLevel) return(true);
break;
case OP_SELL : if (CCIcur > CCI_SellExitLevel && CCIprev < CCI_SellExitLevel) return(true);
}
return(false);
}
//+------------------------------------------------------------------+
//| CheckExitCondition |
//| Uses OP_BUY as cmd to check exit sell |
//| Uses OP_SELL as cmd to check exit buy |
//| |
//| First checks for exit using EMA 200 cross |
//| If new exit ideas are selected those are then checked for exit |
//| Finally use the original exit method |
//| Exit buy when RSI is overbought |
//| Exit sell when RSI is oversold |
//+------------------------------------------------------------------+
bool CheckExitCondition(int cmd)
{
double RSI;
RSI = iRSI(Symbol(), Signal_TimeFrame, myRSI_Period, PRICE_CLOSE, 1);
switch (cmd)
{
case OP_BUY : if (Use200EMA_Exit == 1)
{
if (CheckMA(OP_SELL) == true) return(true);
}
switch (myExitMethod)
{
case NONE : break;
case RSI_EXIT : return (CheckRSI_Exit(cmd));
break;
case CCI_EXIT : return(CheckCCI_Exit(cmd));
}
// Original Exit Strategies
if ( RSI > RSI_Overbought_Value) return(true);
break;
case OP_SELL : if (Use200EMA_Exit == 1)
{
if (CheckMA(OP_BUY) == true) return(true);
}
switch (myExitMethod)
{
case NONE : break;
case RSI_EXIT : return (CheckRSI_Exit(cmd));
break;
case CCI_EXIT : return(CheckCCI_Exit(cmd));
}
// Original Exit Strategies
if (RSI < RSI_Oversold_Value) return(true);
}
return (false);
}
//+------------------------------------------------------------------+
//| CheckMA |
//| Buy if price closes above EMA 200 |
//| Sell if price closes below EMA 200 |
//| |
//+------------------------------------------------------------------+
bool CheckMA(int cmd)
{
double EMA_Day3;
EMA_Day3 = iMA(Symbol(),Signal_TimeFrame,MaTrend_Period, 0, MA_Type, PRICE_CLOSE, 1);
switch (cmd)
{
case OP_BUY : if (iClose(Symbol(),Signal_TimeFrame,1) > EMA_Day3) return(true);
break;
case OP_SELL : if (iClose(Symbol(),Signal_TimeFrame,1) < EMA_Day3) return (true);
}
return(false);
}
//+------------------------------------------------------------------+
//| CheckRSI |
//| Buy when RSI is below buy level for 3 bars |
//| Sell when RSI is above sell level for 3 bars |
//| Optimized code for speed |
//| |
//+------------------------------------------------------------------+
bool CheckRSI(int cmd)
{
double RSI_Day_0 = 0, RSI_Day_1=0, RSI_Day_2=0, RSI_Day_3=0;
RSI_Day_1 = iRSI(Symbol(), Signal_TimeFrame, myRSI_Period, PRICE_CLOSE, 3);
switch (cmd)
{
case OP_BUY : if (RSI_Day_1 < myBuyWhenRsiBelow)
{
RSI_Day_2 = iRSI(Symbol(), Signal_TimeFrame, myRSI_Period, PRICE_CLOSE, 2);
if (RSI_Day_2 < RSI_Day_1)
{
RSI_Day_3 = iRSI(Symbol(), Signal_TimeFrame, myRSI_Period, PRICE_CLOSE, 1);
if (myUseTurnUpDown == 1)
{
if (RSI_Day_3 > RSI_Day_2) return(true);
}
else
{
if (RSI_Day_3 < RSI_Day_2) return(true);
}
}
}
break;
case OP_SELL : if (RSI_Day_1 > mySellWhenRsiAbove)
{
RSI_Day_2 = iRSI(Symbol(), Signal_TimeFrame, myRSI_Period, PRICE_CLOSE, 2);
if (RSI_Day_2 > RSI_Day_1)
{
RSI_Day_3 = iRSI(Symbol(), Signal_TimeFrame, myRSI_Period, PRICE_CLOSE, 1);
if (myUseTurnUpDown == 1)
{
if (RSI_Day_3 < RSI_Day_2) return(true);
}
else
{
if (RSI_Day_3 > RSI_Day_2) return(true);
}
}
}
}
return(false);
}
//+------------------------------------------------------------------+
//| CheckRSI_Confirmation |
//| Modifications suggested by Loren |
//| Require daily RSI 14 weighted close >= 35 for long |
//| to correct the premature entry problem where price |
//| price continues to decline for an indeterminate period. |
//| The reverse of this for shorts w/ RSI 14 <= 65. |
//+------------------------------------------------------------------+
bool CheckRSI_Confirmation(int cmd)
{
double RSI_Confirm;
RSI_Confirm = iRSI(Symbol(), Signal_TimeFrame, RSI_ConfirmPeriod, PRICE_WEIGHTED, 1);
switch (cmd)
{
case OP_BUY : if (RSI_Confirm > BuyConfirmLevel) return(true);
break;
case OP_SELL : if (RSI_Confirm < SellConfirmLevel) return(true);
}
return (false);
}
//+------------------------------------------------------------------+
//| CheckEntryCondition |
//| |
//| There are 3 possible rules for entry |
//| Rule 1 : Use EMA 200, buy when above, sell when below |
//| Rule 2 : Use RSI |
//| Rule 3 : Place trade only if filter allows |
//| Rule 4 : Lorens idea for confirmation |
//+------------------------------------------------------------------+
bool CheckEntryCondition(int cmd)
{
bool rule1, rule2, rule3, rule4;
rule1 = true;
rule2 = true;
rule3 = true;
rule4 = true;
rule1 = CheckMA(cmd);
if (rule1 == true)
{
rule2 = CheckRSI(cmd);
if(rule2 == true)
{
if (myUseFilter == 1) rule3 = CheckFilter(cmd);
if (rule3 == true)
{
if (myUseRSI_Confirmation == 1) rule4 = CheckRSI_Confirmation(cmd);
if (rule4 == true)
{
return(true);
}
}
}
}
return (false);
}
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
//| Start |
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
int start()
{
// Only run once per completed bar
// if(timeprev==Time[0]) return(0);
// timeprev = Time[0];
RefreshRates();
TrackPrices();
//+------------------------------------------------------------------+
//| Check for Open Position |
//+------------------------------------------------------------------+
HandleOpenPositions();
// Check if any open positions were not closed
TradesInThisSymbol = CheckOpenPositions();
// Only allow 1 trade per Symbol
if(TradesInThisSymbol >= MaxTrades) {
return(0);}
// Check if last trade stopped out
if (UseDelay == 1)
{
if (LastTradeClosedToday()) return(0);
}
MM_OrderLotSize = GetLots();
if(CheckEntryCondition(OP_BUY) == true)
{
OpenBuyOrder(MM_OrderLotSize, StopLoss,TakeProfit, Slippage, setup, MagicNumber, Green);
return(0);
}
if(CheckEntryCondition(OP_SELL) == true)
{
OpenSellOrder(MM_OrderLotSize, StopLoss,TakeProfit, Slippage, setup, MagicNumber, Red);
}
return(0);
}
double GetLots()
{
// variables used for money management
double MM_MinLotSize=0;
double MM_MaxLotSize=0;
double MM_LotStep=0;
double MM_Decimals=0;
int MM_AcctLeverage=0;
int MM_CurrencyLotSize=0;
double OrderLotSize;
//----- Money Management & Lot Sizing Stuff.
MM_AcctLeverage = AccountLeverage();
MM_MinLotSize = MarketInfo(Symbol(),MODE_MINLOT);
MM_MaxLotSize = MarketInfo(Symbol(),MODE_MAXLOT);
MM_LotStep = MarketInfo(Symbol(),MODE_LOTSTEP);
MM_CurrencyLotSize = MarketInfo(Symbol(),MODE_LOTSIZE);
if(MM_LotStep == 0.01) {MM_Decimals = 2;}
if(MM_LotStep == 0.1) {MM_Decimals = 1;}
if (UseMoneyMgmt == true)
{
OrderLotSize = AccountEquity() * (RiskPercent * 0.01) / (MM_CurrencyLotSize / MM_AcctLeverage);
if(BrokerPermitsFractionalLots == true)
OrderLotSize = StrToDouble(DoubleToStr(OrderLotSize,MM_Decimals));
else
OrderLotSize = MathRound(MM_OrderLotSize);
}
else
{
OrderLotSize = LotSize;
}
if (OrderLotSize < MM_MinLotSize) {OrderLotSize = MM_MinLotSize;}
if (OrderLotSize > MM_MaxLotSize) {OrderLotSize = MM_MaxLotSize;}
return(OrderLotSize);
}
//+------------------------------------------------------------------+
//| OpenBuyOrder |
//| If Stop Loss or TakeProfit are used the values are calculated |
//| for each trade |
//+------------------------------------------------------------------+
int OpenBuyOrder(double mLots, double mStopLoss, double mTakeProfit, int mSlippage, string mComment, int mMagic, color mColor)
{
int err,ticket;
double myPrice, myStopLoss = 0, myTakeProfit = 0;
RefreshRates();
HighBid = Bid;
LowBid = Bid;
HighAsk = Ask;
LowAsk = Ask;
myStopLoss = StopLong(Bid,mStopLoss);
myTakeProfit = TakeLong(Bid,mTakeProfit);
// Normalize all price / stoploss / takeprofit to the proper # of digits.
if (Digits > 0)
{
myPrice = NormalizeDouble( Ask, Digits);
myStopLoss = NormalizeDouble( myStopLoss, Digits);
myTakeProfit = NormalizeDouble( myTakeProfit, Digits);
}
ticket=OrderSend(Symbol(),OP_BUY,mLots,myPrice,mSlippage,myStopLoss,myTakeProfit,mComment,mMagic,0,mColor);
if (ticket > 0)
{
if (OrderSelect( ticket,SELECT_BY_TICKET, MODE_TRADES) )
{
Print("BUY order opened : ", OrderOpenPrice( ));
if (Debug == true)
{
Write_OpenTrade (" "+ Symbol() +" #"+ ticket + " OPEN BUY ORDER "+ DoubleToStr(mLots, 2) +" LOTS AT " + DoubleToStr(Ask,4)+" sl: "+ DoubleToStr(myStopLoss,Digits) + " tp : " + DoubleToStr(myTakeProfit,Digits));
}
// ModifyOrder(ticket,OrderOpenPrice( ), OrderStopLoss(), myTakeProfit);
}
}
else
{
err = GetLastError();
if(err==0)
{
return(ticket);
}
else
{
if(err==4 || err==137 ||err==146 || err==136) //Busy errors
{
Sleep(5000);
}
else //normal error
{
Print("Error opening BUY order [" + setup + "]: (" + err + ") " + ErrorDescription(err));
if (Debug == true) Write_OpenError("Error opening order : (" + err + ") " + ErrorDescription(err) + " SL:" + myStopLoss + " TP:" + myTakeProfit + " Lots:" + mLots );
}
}
}
return(ticket);
}
//+------------------------------------------------------------------+
//| OpenSellOrder |
//| If Stop Loss or TakeProfit are used the values are calculated |
//| for each trade |
//+------------------------------------------------------------------+
void OpenSellOrder(double mLots, double mStopLoss, double mTakeProfit, int mSlippage, string mComment, int mMagic, color mColor)
{
int err, ticket;
double myPrice, myStopLoss = 0, myTakeProfit = 0;
RefreshRates();
HighBid = Bid;
LowBid = Bid;
HighAsk = Ask;
LowAsk = Ask;
myStopLoss = StopShort(Ask,mStopLoss);
myTakeProfit = TakeShort(Ask,mTakeProfit);
// Normalize all price / stoploss / takeprofit to the proper # of digits.
if (Digits > 0)
{
myPrice = NormalizeDouble( Bid, Digits);
myStopLoss = NormalizeDouble( myStopLoss, Digits);
myTakeProfit = NormalizeDouble( myTakeProfit, Digits);
}
ticket=OrderSend(Symbol(),OP_SELL,mLots,myPrice,mSlippage,myStopLoss,myTakeProfit,mComment,mMagic,0,mColor);
if (ticket > 0)
{
if (OrderSelect( ticket,SELECT_BY_TICKET, MODE_TRADES) )
{
Print("Sell order opened : ", OrderOpenPrice());
if (Debug == true)
{
Write_OpenTrade (" "+ Symbol() +" #"+ ticket + " OPEN SELL ORDER "+ DoubleToStr(mLots,2) +" LOTS AT " + DoubleToStr(Bid,Digits)+" sl: "+ DoubleToStr(myStopLoss,Digits) + " tp : " + DoubleToStr(myTakeProfit, Digits));
}
// ModifyOrder(ticket,OrderOpenPrice( ), OrderStopLoss(), myTakeProfit);
}
}
else
{
err = GetLastError();
if(err==0)
{
return(ticket);
}
else
{
if(err==4 || err==137 ||err==146 || err==136) //Busy errors
{
Sleep(5000);
}
else //normal error
{
Print("Error opening Sell order [" + mComment + "]: (" + err + ") " + ErrorDescription(err));
if (Debug == true) Write_OpenError("Error opening order : (" + err + ") " + ErrorDescription(err) + " SL:" + myStopLoss + " TP:" + myTakeProfit + " Lots:" + mLots );
}
}
}
return(ticket);
}
double StopLong(double price,int stop)
{
if(stop==0)
return(0);
else
return(price-(stop*Point));
}
double StopShort(double price,int stop)
{
if(stop==0)
return(0);
else
return(price+(stop*Point));
}
double TakeLong(double price,int take)
{
if(take==0)
return(0);
else
return(price+(take*Point));
}
double TakeShort(double price,int take)
{
if(take==0)
return(0);
else
return(price-(take*Point));
}
//+------------------------------------------------------------------+
//| Handle Open Positions |
//| Check if any open positions need to be closed or modified |
//+------------------------------------------------------------------+
int HandleOpenPositions()
{
int cnt;
for(cnt=OrdersTotal()-1;cnt>=0;cnt--)
{
OrderSelect (cnt, SELECT_BY_POS, MODE_TRADES);
if ( OrderSymbol() != Symbol()) continue;
if ( OrderMagicNumber() != MagicNumber) continue;
if(OrderType() == OP_BUY)
{
if (CheckExitCondition(OP_BUY) == true)
{
CloseOrder(OrderTicket(),OrderOpenPrice(),OrderLots(),OP_BUY);
}
else
{
if (UseTrailingStop == true)
{
HandleTrailingStop(OP_BUY,OrderTicket(),OrderOpenPrice(),OrderStopLoss(),OrderTakeProfit());
}
}
}
if(OrderType() == OP_SELL)
{
if (CheckExitCondition(OP_SELL) == true)
{
CloseOrder(OrderTicket(),OrderOpenPrice(), OrderLots(),OP_SELL);
}
else
{
if (UseTrailingStop == true)
{
HandleTrailingStop(OP_SELL,OrderTicket(),OrderOpenPrice(),OrderStopLoss(),OrderTakeProfit());
}
}
}
}
}
//+------------------------------------------------------------------+
//| Check Open Position Controls |
//+------------------------------------------------------------------+
int CheckOpenPositions()
{
int cnt, total;
int NumTrades;
NumTrades = 0;
total=OrdersTotal();
for(cnt=OrdersTotal()-1;cnt>=0;cnt--)
{
OrderSelect (cnt, SELECT_BY_POS, MODE_TRADES);
if ( OrderSymbol() != Symbol()) continue;
if ( OrderMagicNumber() != MagicNumber) continue;
if(OrderType() == OP_BUY ) NumTrades++;
if(OrderType() == OP_SELL ) NumTrades++;
}
return (NumTrades);
}
int CloseOrder(int ticket, double op, double numLots,int cmd)
{
int CloseCnt, err, digits;
double myPrice;
string olStr, bidStr, askStr;
RefreshRates();
if (cmd == OP_BUY) myPrice = Bid;
if (cmd == OP_SELL) myPrice = Ask;
if (Digits > 0) myPrice = NormalizeDouble( myPrice, Digits);
olStr = DoubleToStr(numLots,2);
bidStr = DoubleToStr(Bid, Digits);
askStr = DoubleToStr(Ask, Digits);
// try to close 3 Times
CloseCnt = 0;
while (CloseCnt < 3)
{
if (OrderClose(ticket,numLots,myPrice,Slippage,Violet) == false)
{
err=GetLastError();
Print(CloseCnt," Error closing order : (", err , ") " + ErrorDescription(err));
if (err > 0) CloseCnt++;
}
else
{
if (Debug == true)
{
if (cmd == OP_BUY)
{
WriteExit(ticket, "Buy Exit Signal", OP_BUY, op, Bid);
if (Debug_Trade == true){
Write_Trade(" "+ Symbol() +" #"+ ticket +" CLOSE BUY ORDER "+ olStr +" LOTS AT "+ bidStr +".");}
if (BTDebug_Trade == true){
BTWrite_Trade(" "+ Symbol() +" #"+ ticket +" CLOSE BUY ORDER "+ olStr +" LOTS AT "+ bidStr +".");}
}
if (cmd == OP_SELL)
{
if (Debug == true)
{
WriteExit(ticket, "Sell Exit Signal", OP_SELL, op, Ask);
if (Debug_Trade == true){
Write_Trade(" "+ Symbol() +" #"+ ticket +" CLOSE SELL ORDER "+ olStr +" LOTS AT "+ askStr +".");}
if (BTDebug_Trade == true){
BTWrite_Trade(" "+ Symbol() +" #"+ ticket +" CLOSE SELL ORDER "+ olStr +" LOTS AT "+ askStr +".");}
}
}
}
CloseCnt = 3;
}
}
}
int ModifyOrder(int ord_ticket,double op, double price,double tp, color mColor)
{
int CloseCnt, err;
CloseCnt=0;
while (CloseCnt < 3)
{
if (OrderModify(ord_ticket,op,price,tp,0,mColor) == true)
{
CloseCnt = 3;
}
else
{
err=GetLastError();
Print(CloseCnt," Error modifying order : (", err , ") " + ErrorDescription(err));
if (err>0) CloseCnt++;
}
}
}
double ValidStopLoss(int type, double price, double SL)
{
double minstop, pp;
double newSL;
pp = MarketInfo(Symbol(), MODE_POINT);
minstop = MarketInfo(Symbol(),MODE_STOPLEVEL);
newSL = SL;
if (type == OP_BUY)
{
if((price - SL) < minstop*pp) newSL = price - minstop*pp;
}
if (type == OP_SELL)
{
if((SL-price) < minstop*pp) newSL = price + minstop*pp;
}
return(newSL);
}
//+------------------------------------------------------------------+
//| HandleTrailingStop |
//| Type 1 moves the stoploss without delay. |
//| Type 2 waits for price to move the amount of the trailStop |
//| before moving stop loss then moves like type 1 |
//| Type 3 uses up to 3 levels for trailing stop |
//| Level 1 Move stop to 1st level |
//| Level 2 Move stop to 2nd level |
//| Level 3 Trail like type 1 by fixed amount other than 1 |
//| Type 4 Move stop to breakeven + Lockin, no trail |
//| Type 5 uses steps for 1, every step pip move moves stop 1 pip |
//| Type 6 Uses EMA to set trailing stop |
//| Type 7 Uses Paraboloc SAR |
//| Type 8 Uses Parabolic SAR coded by bluto from original EA |
//+------------------------------------------------------------------+
int HandleTrailingStop(int type, int ticket, double op, double os, double tp)
{
switch (TrailingStopType)
{
case 1 : Immediate_TrailingStop (type, ticket, op, os, tp);
break;
case 2 : Delayed_TrailingStop (type, ticket, op, os, tp);
break;
case 3 : ThreeLevel_TrailingStop (type, ticket, op, os, tp);
break;
case 4 : BreakEven_TrailingStop (type, ticket, op, os, tp);
break;
case 5 : eTrailingStop (type, ticket, op, os, tp);
break;
case 6 : EMA_TrailingStop (type, ticket, op, os, tp);
break;
case 7 : pSAR_TrailingStop (type, ticket, op, os, tp);
break;
case 8 : BlutoParabolicSAR (type, ticket, op, os, tp);
break;
}
return(0);
}
//+------------------------------------------------------------------+
//| BreakEvenExpert_v1.mq4 |
//| Copyright © 2006, Forex-TSD.com |
//| Written by IgorAD,igorad2003@yahoo.co.uk |
//| http://finance.groups.yahoo.com/group/TrendLaboratory |
//+------------------------------------------------------------------+
void BreakEven_TrailingStop(int type, int ticket, double op, double os, double tp)
{
int digits;
double pBid, pAsk, pp, BuyStop, SellStop;
pp = MarketInfo(Symbol(), MODE_POINT);
digits = MarketInfo(Symbol(), MODE_DIGITS);
if (type==OP_BUY)
{
pBid = MarketInfo(Symbol(), MODE_BID);
if ( pBid-op > pp*BreakEven )
{
BuyStop = op + LockInPips * pp;
if (digits > 0) BuyStop = NormalizeDouble( BuyStop, digits);
BuyStop = ValidStopLoss(OP_BUY,pBid, BuyStop);
if (digits > 0) BuyStop = NormalizeDouble( BuyStop, digits);
if (os < BuyStop) ModifyOrder(ticket,op,BuyStop,tp,LightGreen);
return;
}
}
if (type==OP_SELL)
{
pAsk = MarketInfo(Symbol(), MODE_ASK);
if ( op - pAsk > pp*BreakEven )
{
SellStop = op - LockInPips * pp;
if (digits > 0) SellStop = NormalizeDouble( SellStop, digits);
SellStop = ValidStopLoss(OP_SELL, pAsk, SellStop);
if (digits > 0) SellStop = NormalizeDouble( SellStop, digits);
if (os > SellStop) ModifyOrder(ticket,op,SellStop,tp,DarkOrange);
return;
}
}
}
//+------------------------------------------------------------------+
//| e-Trailing.mq4 |
//| Êèì Èãîðü Â. aka KimIV |
//| http://www.kimiv.ru |
//| |
//| 12.09.2005 Àâòîìàòè÷åñêèé Trailing Stop âñåõ îòêðûòûõ ïîçèöèé |
//| Âåøàòü òîëüêî íà îäèí ãðàôèê |
//+------------------------------------------------------------------+
void eTrailingStop(int type, int ticket, double op, double os, double tp)
{
int digits;
double pBid, pAsk, pp, BuyStop, SellStop;
pp = MarketInfo(Symbol(), MODE_POINT);
digits = MarketInfo(Symbol(), MODE_DIGITS) ;
if (type==OP_BUY)
{
pBid = MarketInfo(Symbol(), MODE_BID);
if ((pBid-op)>eTrailingStop*pp)
{
if (os<pBid-(eTrailingStop+eTrailingStep-1)*pp)
{
BuyStop = pBid-eTrailingStop*pp;
if (digits > 0) BuyStop = NormalizeDouble( BuyStop, digits);
BuyStop = ValidStopLoss(OP_BUY, pBid, BuyStop);
if (digits > 0) BuyStop = NormalizeDouble( BuyStop, digits);
ModifyOrder(ticket,op,BuyStop,tp,LightGreen);
return;
}
}
}
if (type==OP_SELL)
{
pAsk = MarketInfo(Symbol(), MODE_ASK);
if (op - pAsk > eTrailingStop*pp)
{
if (os > pAsk + (eTrailingStop + eTrailingStep-1)*pp || os==0)
{
SellStop = pAsk + eTrailingStop * pp;
if (digits > 0) SellStop = NormalizeDouble( SellStop, digits);
SellStop = ValidStopLoss(OP_SELL, pAsk, SellStop);
if (digits > 0) SellStop = NormalizeDouble( SellStop, digits);
ModifyOrder(ticket,op,SellStop,tp,DarkOrange);
return;
}
}
}
}
//+------------------------------------------------------------------+
//| EMATrailingStop_v1.mq4 |
//| Copyright © 2006, Forex-TSD.com |
//| Written by IgorAD,igorad2003@yahoo.co.uk |
//| http://finance.groups.yahoo.com/group/TrendLaboratory |
//+------------------------------------------------------------------+
void EMA_TrailingStop(int type, int ticket, double op, double os, double tp)
{
int digits;
double pBid, pAsk, pp, BuyStop, SellStop, ema;
pp = MarketInfo(Symbol(), MODE_POINT);
digits = MarketInfo(Symbol(), MODE_DIGITS) ;
ema = iMA(Symbol(),EMATimeFrame,EMAPeriod,0,MODE_EMA,Price,EMAShift);
if (type==OP_BUY)
{
BuyStop = ema;
pBid = MarketInfo(Symbol(),MODE_BID);
if(os == 0 && InitialStop>0 ) BuyStop = pBid-InitialStop*pp;
if (digits > 0) BuyStop = NormalizeDouble(BuyStop, digits);
BuyStop = ValidStopLoss(OP_BUY, pBid, BuyStop);
if (digits > 0) BuyStop = NormalizeDouble(BuyStop, digits);
// Print("MA=",ema," BuyStop=",BuyStop);
if ((op <= BuyStop && BuyStop > os) || os==0)
{
ModifyOrder(ticket,op,BuyStop,tp,LightGreen);
return;
}
}
if (type==OP_SELL)
{
SellStop = ema;
pAsk = MarketInfo(Symbol(),MODE_ASK);
if (os==0 && InitialStop > 0) SellStop = pAsk+InitialStop*pp;
if (digits > 0) SellStop = NormalizeDouble(SellStop, digits);
SellStop = ValidStopLoss(OP_SELL, pAsk, SellStop);
if (digits > 0) SellStop = NormalizeDouble(SellStop, digits);
// Print("MA=",ema," SellStop=",SellStop);
if( (op >= SellStop && os > SellStop) || os==0)
{
ModifyOrder(ticket,op,SellStop,tp,DarkOrange);
return;
}
}
}
//+------------------------------------------------------------------+
//| b-TrailingSAR.mqh |
//| Êèì Èãîðü Â. aka KimIV |
//| http://www.kimiv.ru |
//| |
//| 21.11.2005 Áèáëèîòåêà ôóíêöèé òðàëà ïî ïàðàáîëèêó. |
//| Äëÿ èñïîëüçîâàíèÿ äîáàâèòü ñòðîêó â ìîäóëå start |
//| if (UseTrailing) TrailingPositions(); |
//+------------------------------------------------------------------+
void pSAR_TrailingStop(int type, int ticket, double op, double os, double tp)
{
int digits;
double pBid, pAsk, pp, BuyStop, SellStop, spr;
double sar1, sar2;
pp = MarketInfo(Symbol(), MODE_POINT);
digits = MarketInfo(Symbol(), MODE_DIGITS) ;
pBid = MarketInfo(Symbol(), MODE_BID);
pAsk = MarketInfo(Symbol(), MODE_ASK);
sar1=iSAR(NULL, 0, StepParabolic, MaxParabolic, 1);
sar2=iSAR(NULL, 0, StepParabolic, MaxParabolic, 2);
spr = pAsk - pBid;
if (digits > 0) spr = NormalizeDouble(spr, digits);
if (type==OP_BUY)
{
pBid = MarketInfo(Symbol(), MODE_BID);
if (sar2 < sar1)
{
BuyStop = sar1-Interval*pp;
if (digits > 0) BuyStop = NormalizeDouble(BuyStop, digits);
BuyStop = ValidStopLoss(OP_BUY, pBid, BuyStop);
if (digits > 0) BuyStop = NormalizeDouble(BuyStop, digits);
if (os<BuyStop) ModifyOrder(ticket,op,BuyStop,tp,LightGreen);
}
}
if (type==OP_SELL)
{
if (sar2 > sar1)
{
SellStop = sar1 + Interval * pp + spr;
if (digits > 0) SellStop = NormalizeDouble(SellStop, digits);
SellStop = ValidStopLoss(OP_SELL, pAsk, SellStop);
if (digits > 0) SellStop = NormalizeDouble(SellStop, digits);
if (os>SellStop || os==0) ModifyOrder(ticket,op,SellStop,tp,DarkOrange);
}
}
}
// Manage Paraolic SAR
void BlutoParabolicSAR(int type, int ticket, double op, double os, double tp)
{
double pSar1 = 0, pSar2 = 0;
int digits;
double pt, pBid, pAsk, pp, BuyStop, SellStop;
pp = MarketInfo(Symbol(), MODE_POINT);
digits = MarketInfo(Symbol( ), MODE_DIGITS);
// Added pSar1 and pSar2 for faster backtesting
pSar1 = iSAR(NULL,Signal_TimeFrame,SarStep,SarMax,1);
pSar2 = iSAR(NULL,Signal_TimeFrame,SarStep,SarMax,2);
if (type == OP_BUY)
{
pBid = MarketInfo(Symbol(), MODE_BID);
if ( (pSar1> os) && (pBid > pSar1) && (op < pSar1) && (pSar1 > pSar2))
{
if (digits > 0)pSar1 = NormalizeDouble(pSar1, digits);
ModifyOrder(ticket,op,pSar1,tp,Blue);
Print("Order # ",ticket," updated at ",Hour(),":",Minute(),":",Seconds());
return(0);
}
}
if (type == OP_SELL)
{
pAsk = MarketInfo(Symbol(), MODE_ASK);
if ((pSar1 < os) && (pAsk < pSar1) && (op > pSar1) && (pSar1 < pSar2))
{
ModifyOrder(ticket,op,pSar1,tp,Blue);
Print("Order # ",ticket," updated at ",Hour(),":",Minute(),":",Seconds());
return(0);
}
}
}
//+------------------------------------------------------------------+
//| ThreeLevel_TrailingStop.mq4 |
//| Copyright © 2006, Forex-TSD.com |
//| Written by MrPip,robydoby314@yahoo.com |
//| |
//| Uses up to 3 levels for trailing stop |
//| Level 1 Move stop to 1st level |
//| Level 2 Move stop to 2nd level |
//| Level 3 Trail like type 1 by fixed amount other than 1 |
//+------------------------------------------------------------------+
void ThreeLevel_TrailingStop(int type, int ticket, double op, double os, double tp)
{
int digits;
double pBid, pAsk, pp, BuyStop, SellStop;
pp = MarketInfo(Symbol(), MODE_POINT);
digits = MarketInfo(Symbol(), MODE_DIGITS) ;
if (type == OP_BUY)
{
pBid = MarketInfo(Symbol(), MODE_BID);
if (pBid - op > FirstMove * pp)
{
BuyStop = op + FirstMove*pp - FirstStopLoss * pp;
if (digits > 0) BuyStop = NormalizeDouble(BuyStop, digits);
BuyStop = ValidStopLoss(OP_BUY, pBid, BuyStop);
if (digits > 0) BuyStop = NormalizeDouble(BuyStop, digits);
if (os < BuyStop) ModifyOrder(ticket,op,BuyStop,tp,LightGreen);
}
if (pBid - op > SecondMove * pp)
{
BuyStop = op + SecondMove*pp - SecondStopLoss * pp;
if (digits > 0) BuyStop = NormalizeDouble(BuyStop, digits);
BuyStop = ValidStopLoss(OP_BUY, pBid, BuyStop);
if (digits > 0) BuyStop = NormalizeDouble(BuyStop, digits);
if (os < BuyStop) ModifyOrder(ticket,op,BuyStop,tp,LightGreen);
}
if (pBid - op > ThirdMove * pp)
{
BuyStop = pBid - ThirdMove*pp;
if (digits > 0) BuyStop = NormalizeDouble(BuyStop, digits);
BuyStop = ValidStopLoss(OP_BUY, pBid, BuyStop);
if (digits > 0) BuyStop = NormalizeDouble(BuyStop, digits);
if (os < BuyStop) ModifyOrder(ticket,op,BuyStop,tp,LightGreen);
}
}
if (type == OP_SELL)
{
pAsk = MarketInfo(Symbol(), MODE_ASK);
if (op - pAsk > FirstMove * pp)
{
SellStop = op - FirstMove * pp + FirstStopLoss * pp;
if (digits > 0) SellStop = NormalizeDouble(SellStop, digits);
SellStop = ValidStopLoss(OP_SELL, pAsk, SellStop);
if (digits > 0) SellStop = NormalizeDouble(SellStop, digits);
if (os > SellStop) ModifyOrder(ticket,op,SellStop,tp,DarkOrange);
}
if (op - pAsk > SecondMove * pp)
{
SellStop = op - SecondMove * pp + SecondStopLoss * pp;
if (digits > 0) SellStop = NormalizeDouble(SellStop, digits);
SellStop = ValidStopLoss(OP_SELL, pAsk, SellStop);
if (digits > 0) SellStop = NormalizeDouble(SellStop, digits);
if (os > SellStop) ModifyOrder(ticket,op,SellStop,tp,DarkOrange);
}
if (op - pAsk > ThirdMove * pp)
{
SellStop = pAsk + ThirdMove * pp;
if (digits > 0) SellStop = NormalizeDouble(SellStop, digits);
SellStop = ValidStopLoss(OP_SELL, pAsk, SellStop);
if (digits > 0) SellStop = NormalizeDouble(SellStop, digits);
if (os > SellStop) ModifyOrder(ticket,op,SellStop,tp,DarkOrange);
}
}
}
//+------------------------------------------------------------------+
//| 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, pp, BuyStop, SellStop;
pp = MarketInfo(Symbol(), MODE_POINT);
digits = MarketInfo(Symbol( ), MODE_DIGITS);
if (type==OP_BUY)
{
pBid = MarketInfo(Symbol(), MODE_BID);
pt = StopLoss * pp;
if(pBid-os > pt)
{
BuyStop = pBid - pt;
if (digits > 0) BuyStop = NormalizeDouble( BuyStop, digits);
BuyStop = ValidStopLoss(OP_BUY,pBid, BuyStop);
if (digits > 0) BuyStop = NormalizeDouble( BuyStop, digits);
if (os < BuyStop) ModifyOrder(ticket,op,BuyStop,tp,LightGreen);
return;
}
}
if (type==OP_SELL)
{
pAsk = MarketInfo(Symbol(), MODE_ASK);
pt = StopLoss * pp;
if(os - pAsk > pt)
{
SellStop = pAsk + pt;
if (digits > 0) SellStop = NormalizeDouble( SellStop, digits);
SellStop = ValidStopLoss(OP_SELL, pAsk, SellStop);
if (digits > 0) SellStop = NormalizeDouble( SellStop, digits);
if (os > SellStop) ModifyOrder(ticket,op,SellStop,tp,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, pp, BuyStop, SellStop;
pp = MarketInfo(Symbol(), MODE_POINT);
pt = TrailingStop * pp;
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 (digits > 0) BuyStop = NormalizeDouble( BuyStop, digits);
if (pBid-op > pt && os < BuyStop) ModifyOrder(ticket,op,BuyStop,tp,LightGreen);
return;
}
if (type==OP_SELL)
{
pAsk = MarketInfo(Symbol(), MODE_ASK);
pt = TrailingStop * pp;
SellStop = pAsk + pt;
if (digits > 0) SellStop = NormalizeDouble( SellStop, digits);
SellStop = ValidStopLoss(OP_SELL, pAsk, SellStop);
if (digits > 0) SellStop = NormalizeDouble( SellStop, digits);
if (op - pAsk > pt && os > SellStop) ModifyOrder(ticket,op,SellStop,tp,DarkOrange);
return;
}
}
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);
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);
}
}
//+------------------------------------------------------------------+
//| Time frame string appropriation function |
//+------------------------------------------------------------------+
string func_TimeFrame_Val2String(int Value ) {
switch(Value) {
case 1: // M1
return("PERIOD_M1");
case 2: // M1
return("PERIOD_M5");
case 3:
return("PERIOD_M15");
case 4:
return("PERIOD_M30");
case 5:
return("PERIOD_H1");
case 6:
return("PERIOD_H4");
case 7:
return("PERIOD_D1");
case 8:
return("PERIOD_W1");
case 9:
return("PERIOD_MN1");
default:
return("undefined " + Value);
}
}
//+------------------------------------------------------------------+
//| Track High and Low prices |
//+------------------------------------------------------------------+
void TrackPrices()
{
if (Bid > 0.01)
{
if (Bid < LowBid) LowBid = Bid;
if (Bid > HighBid) HighBid = Bid;
}
if (Ask > 0.01)
{
if (Ask > HighAsk) HighAsk = Ask;
if (Ask < LowAsk) LowAsk = Ask;
}
}
void CloseLogFiles()
{
int BThandleTrade;
if (BTDebug_Trade == true)
{
if (GlobalVariableCheck("BTMyHandleTrade") == true)
{
BThandleTrade = GlobalVariableGet("BTMyHandleTrade");
FileFlush(BThandleTrade);
FileClose(BThandleTrade);
GlobalVariableDel("BTMyHandleTrade");
}
}
}
void SetupLogFiles()
{
string BTfilenameTrade_Log;
int err,BThandleTrade;
BTfilenameTrade_Log=Symbol()+" " + BT_Trade_Log + ".txt";
if (BTDebug_Trade == true)
{
GlobalVariableSet("BTMyHandleTrade",0);
if (GlobalVariableCheck("BTMyHandleTrade") == false)
{
err = GetLastError();
Print("Error creating Global Variable BTMyHandleTrade: (" + err + ") " + ErrorDescription(err));
return(0);
}
BThandleTrade = FileOpen(BTfilenameTrade_Log,FILE_CSV|FILE_WRITE,"\t");
GlobalVariableSet("BTMyHandleTrade",BThandleTrade);
}
filenameTradeLog=AccountNumber() +" "+ Symbol() + Trade_Log + "_" + Month() + "-" + Day() +".txt";
}
void WriteDash()
{
if (BTDebug_Trade == true){
BTWrite_Trade ("-----------------------------------------------------------------");
}
if (Debug_Trade == true){
Write_Trade ("-----------------------------------------------------------------");
}
}
void WriteComment(string st)
{
if (BTDebug_Trade == true){
BTWrite_Trade (" " + st);
}
if (Debug_Trade == true){
Write_Trade (" " + st);
}
}
void Write_OpenError(string msg)
{
if (BTDebug_Trade == true) BTWrite_Trade(msg);
if (Debug_Trade == true) Write_Trade(msg);
}
void Write_OpenTrade(string msg)
{
if (BTDebug_Trade == true)
{
BTWrite_Trade ("****************************************************************");
BTWrite_Trade(msg);
}
if (Debug_Trade == true)
{
Write_Trade ("****************************************************************");
Write_Trade(msg);
}
}
void WriteExit(int myTicket, string msg, int cmd, double op, double cp)
{
if (BTDebug_Trade == true){
BTWrite_Trade ("-----------------------------------------------------------------");
BTWrite_Trade (" "+ Symbol() +" #" + myTicket + " " + msg );
if (op > 0.1) BTWrite_Prices(cmd, op, cp);
}
if (Debug_Trade == true){
Write_Trade ("-----------------------------------------------------------------");
Write_Trade (" "+ Symbol() +" #" + myTicket + " " + msg );
if (op > 0.1) Write_Prices(cmd, op, cp);
}
}
void Write_Trade(string str)
{
int handleTradeLog;
if (Debug_Trade == true)
{
handleTradeLog = FileOpen(filenameTradeLog,FILE_READ|FILE_WRITE|FILE_CSV,"/t");
FileSeek(handleTradeLog, 0, SEEK_END);
FileWrite(handleTradeLog,TimeToStr(CurTime(),TIME_DATE|TIME_SECONDS)+str);
FileClose(handleTradeLog);
}
}
void BTWrite_Trade(string str)
{
int BThandleTrade_Log;
if (BTDebug_Trade == true)
{
if (GlobalVariableCheck("BTMyHandleTrade"))
{
BThandleTrade_Log = GlobalVariableGet("BTMyHandleTrade");
FileWrite(BThandleTrade_Log,TimeToStr(CurTime(),TIME_DATE|TIME_SECONDS)+str);
}
}
}
void BTWrite_Prices(int cmd, double op, double cp)
{
double HighProfit, profit, drawdown;
if (BTDebug_Trade == true){
BTWrite_Trade (" PRICES");
BTWrite_Trade (" Low Ask : " + DoubleToStr(LowAsk,Digits) + " High Ask : " + DoubleToStr(HighAsk,Digits));
BTWrite_Trade (" Low Bid : " + DoubleToStr(LowBid,Digits) + " High Bid : " + DoubleToStr(HighBid,Digits));
if (cmd == OP_BUY)
{
HighProfit = (HighBid - op) / Point;
profit = (cp-op) / Point;
drawdown = (op - LowBid) / Point;
}
if (cmd == OP_SELL)
{
HighProfit = ( op - LowAsk) / Point;
profit = (op-cp)/Point;
drawdown = (HighAsk - op) / Point;
}
BTWrite_Trade (" High Profit : " + DoubleToStr(HighProfit,0));
BTWrite_Trade (" Actual Profit : " + DoubleToStr(profit,0));
BTWrite_Trade (" DrawDown : " + DoubleToStr(drawdown,0));
}
}
void Write_Prices(int cmd, double op, double cp)
{
double HighProfit, profit, drawdown;
if (Debug_Trade == true){
Write_Trade (" PRICES");
Write_Trade (" Low Ask : " + DoubleToStr(LowAsk,Digits) + " High Ask : " + DoubleToStr(HighAsk,Digits));
Write_Trade (" Low Bid : " + DoubleToStr(LowBid,Digits) + " High Bid : " + DoubleToStr(HighBid,Digits));
if (cmd == OP_BUY)
{
HighProfit = (HighBid - op) / Point;
profit = (cp-op) / Point;
drawdown = (op - LowBid) / Point;
}
if (cmd == OP_SELL)
{
HighProfit = ( op - LowAsk) / Point;
profit = (op-cp)/Point;
drawdown = (HighAsk - op) / Point;
}
Write_Trade (" High Profit : " + DoubleToStr(HighProfit,0));
Write_Trade (" Actual Profit : " + DoubleToStr(profit,0));
Write_Trade (" DrawDown : " + DoubleToStr(drawdown,0));
}
}
//+------------------------------------------------------------------+
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
---