by Jennieh814 » Wed May 08, 2019 5:22 am
Hi would it assist if I provided the metatrader 4 code for the EA to be directly converted to trading station strategy?
thanks
- Code: Select all
input double pctDw = 60; // Doji, Min % of Range of Candle for Wicks
input int pipMin = 0; // Doji, Previous Candle Min Pip Body Size
//input bool sname = true; // Show Price Action Bar Names
//input bool cbar = false; // Highlight Harami & Doji Bars
//input bool sHm = false; // Show Only Harami Style Doji's
//input bool setalm = true; // Generate Alert for Harami & Doji Bars
input double lotVolume = 0.1;
input double TakeProfitPoints = 40;
input bool uha = true; // Use Heikin Ashi Candles for Calculations
input int bars = 3; // Doji, Number of Lookback Bars
input double MinFreeMarginPercent = 20;
input int BuyMagic = 144553;
input int SellMagic = 144222;
input int Slippage = 3;
input bool AllowHaramiBull = true;
input bool AllowHaramiBear = true;
input bool AllowDojiBull = true;
input bool AllowDojiBear = true;
input bool SendAlerts = false;
input bool SendPush = false;
input bool SendMail = false;
input bool AlertSound = false;
input string AlertSoundFile = "";
input bool DebugLog = false;
double pctCDw;
double pctCDb;
int SizeArrows = 3;
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int alert;
double BuyLot;
double SellLot;
double haramiBullPrice =0;
double haramiBearPrice=0;
double dojiBullPrice=0;
double dojiBearPrice=0;
datetime alertTime = 0;
int OnInit()
{
//--- indicator buffers mapping
// Calculate Doji/Harami Candles
pctCDw = (pctDw/2) * 0.01;
pctCDb = (100-pctDw) * 0.01;
alert = false;
BuyLot = SellLot = lotVolume;
//---
return(INIT_SUCCEEDED);
}
void OnDeinit(const int reason)
{
}
bool buy_market(int magic,int step,double stoploss, double takeprofit)
{
double free_margin = AccountFreeMarginCheck(Symbol(),OP_BUY,BuyLot);
double equity = AccountEquity();
if (free_margin/equity*100<=MinFreeMarginPercent)
{
Print ("not opening buy position as free margin less than MinFreeMarginPercent free margin:",free_margin," equity:",equity," oercentage:",free_margin/equity*100);
return false;
}
string prefix = "0";
if (step >= 10)
prefix = "";
double price = MarketInfo(Symbol(), MODE_ASK);
int ticket = OrderSend(Symbol(), OP_BUY, BuyLot,price, Slippage, stoploss, takeprofit, "hd" + prefix + step, magic,0,clrGreen);
if(ticket < 1)
{
int error = GetLastError();
Print("Error = ", error );
Print("buy parameters:",price," ",stoploss," ", takeprofit);
return false;
// return (INIT_FAILED);
}
Print("ticketid", ticket );
if(OrderSelect(ticket, SELECT_BY_TICKET) == true)
{
OrderPrint();
}
return true;
}
bool sell_market(int magic,int step, double stoploss, double takeprofit)
{
double free_margin = AccountFreeMarginCheck(Symbol(),OP_SELL,SellLot);
double equity = AccountEquity();
if (free_margin/equity*100<=MinFreeMarginPercent)
{
Print ("not opening sell position as free margin less than MinFreeMarginPercent free margin:",free_margin," equity:",equity," oercentage:",free_margin/equity*100);
return false;
}
string prefix = "0";
if (step >= 10)
prefix = "";
double price = MarketInfo(Symbol(), MODE_BID);
int ticket = OrderSend(Symbol(), OP_SELL, SellLot,price, Slippage, stoploss, takeprofit, "hd" + prefix + step,magic,0,clrRed);
if(ticket < 1)
{
int error = GetLastError();
Print("Error = ", error );
Print("sell parameters:",price," ",stoploss," ", takeprofit);
return false;
// return (INIT_FAILED);
}
Print("ticketid", ticket );
if(OrderSelect(ticket, SELECT_BY_TICKET) == true)
{
OrderPrint();
}
return true;
}
void DoAlerts(string alerttype, datetime t, double price)
{
if (SendAlerts)
{
Alert(TimeToStr(t)+" at price "+ price + " "+alerttype);
}
if (SendPush)
{
SendNotification(TimeToStr(t)+" at price "+ price + " "+alerttype);
}
if (SendMail)
{
SendMail("HaramiDoji expert",TimeToStr(t)+" at price "+ price + " "+alerttype);
}
if (AlertSound)
{
PlaySound(AlertSoundFile);
}
if (DebugLog)
{
Print(TimeToStr(t)+" at price "+ price + " "+alerttype);
}
}
void Calculate( const int rates_total,
const datetime &time[],
const double &open[],
const double &high[],
const double &low[],
const double &close[])
{
int heigh =(int)ChartGetInteger(0,CHART_HEIGHT_IN_PIXELS,0);
double WPMax = WindowPriceMax();
double WPMin = WindowPriceMin();
double distn = WPMax - WPMin;
//---
//Lookback Candles for bulls or bears
// Print("0?",time[0]);
// Print("1?",time[1]);
for (int i = rates_total-1-bars; i>=0;i--)
{
bool lbBull;
bool lbBear;
//double close = close[i];
//double srclow = low[i];
lbBull = bars==1? open[i+1]>close[i+1]: bars==2? (open[i+1]>close[i+1] && open[i+2]>close[i+2]): bars==3?(open[i+1]>close[i+1] && open[i+2]>close[i+2] && open[i+3]>close[i+3]):false;
lbBear = bars==1? open[i+1]<close[i+1]: bars==2? (open[i+1]<close[i+1] && open[i+2]<close[i+2]): bars==3?(open[i+1]<close[i+1] && open[i+2]<close[i+2] && open[i+3]<close[i+3]):false;
//Lookback C&&le Size only if mininum size is > 0
double pip ;
if (Digits ==5)
pip = 1./100000;
if (Digits ==4)
pip = 1./10000;
if (Digits ==3)
pip = 1./1000;
if (Digits ==2)
pip = 1./100;
if (Digits ==1)
pip = 1./10;
if (Digits ==0)
pip = 1./1;
//Print(Digits," ",pip);
double range = high[i] - low[i];
bool lbSize;
lbSize = pipMin==0? true : bars==1 ? (MathAbs(open[i+1]-close[i+1])>pipMin*pip) :
bars==2 ? (MathAbs(open[i+1]-close[i+1])>pipMin*pip && MathAbs(open[i+2]-close[i+2])>pipMin*pip) :
bars==3 ? (MathAbs(open[i+1]-close[i+1])>pipMin*pip && MathAbs(open[i+2]-close[i+2])>pipMin*pip && MathAbs(open[i+3]-close[i+3])>pipMin*pip) :
false;
bool dojiBu;
dojiBu = (open[i+1] >= MathMax(close[i],open[i]) && close[i+1]<= MathMin(close[i],open[i])) && lbSize &&
(MathAbs(close[i]-open[i])<range*pctCDb && (high[i]-MathMax(close[i],open[i]))>(pctCDw*range) && (MathMin(close[i],open[i])-low[i])>(pctCDw*range))? 1 : 0;
bool dojiBe;
dojiBe = (close[i+1] >= MathMax(close[i],open[i]) && open[i+1]<=MathMin(close[i],open[i])) && lbSize &&
(MathAbs(close[i]-open[i])<range*pctCDb && (high[i]-MathMax(close[i],open[i]))>(pctCDw*range) && (MathMin(close[i],open[i])-low[i])>(pctCDw*range))? 1 : 0;
bool haramiBull;
bool haramiBear;
haramiBull = (open[i]<=close[i] || (MathMax(close[i],open[i])-MathMin(close[i],open[i]))<pip*0.5) && lbBull && dojiBu;
haramiBear = (open[i]>=close[i] || (MathMax(close[i],open[i])-MathMin(close[i],open[i]))<pip*0.5) && lbBear && dojiBe;
bool dojiBull = ! haramiBull && ! haramiBear && lbBull && dojiBu;
bool dojiBear = ! haramiBull && ! haramiBear && lbBear && dojiBe;
if ( i==1)
{
if (alert && alertTime != iTime(Symbol(),0,0))
alert = false;
int bcolor = haramiBull ? 1 : haramiBear ? 2 : dojiBull ? 3 : dojiBear ? 4 : 0;
if (!alert && bcolor>0 && (haramiBullPrice == 0 && haramiBearPrice ==0 && dojiBullPrice == 0 && dojiBearPrice ==0 ))
{
// Alert("PACDOJI Alert"," / ",Symbol()," — ","PACDOJI Alert"," • (",TimeToStr(TimeCurrent(),TIME_MINUTES),")");
alert = true;
if (haramiBull && AllowHaramiBull)
{
haramiBullPrice = MarketInfo(Symbol(), MODE_ASK);
}
else if (haramiBear && AllowHaramiBear)
{
haramiBearPrice = MarketInfo(Symbol(), MODE_BID);
}
else if (dojiBull && AllowDojiBull)
{
dojiBullPrice = MarketInfo(Symbol(), MODE_ASK);
}
else if (dojiBear && AllowDojiBear)
{
dojiBearPrice = MarketInfo(Symbol(), MODE_BID);
}
alertTime = iTime(Symbol(),0,0);
}
if (haramiBullPrice>0 && iClose(Symbol(),0,1)>haramiBullPrice)
{
haramiBullPrice =0;
DoAlerts("haramiBull Buy",iTime(Symbol(),0,0),MarketInfo(Symbol(), MODE_ASK));
buy_market(BuyMagic, 1, 0, NormalizeDouble(MarketInfo(Symbol(), MODE_ASK)+ TakeProfitPoints* Point(),Digits()));
}
if (dojiBullPrice >0 && iClose(Symbol(),0,1)>dojiBullPrice)
{
dojiBullPrice = 0;
DoAlerts("dojiBull Buy",iTime(Symbol(),0,0),MarketInfo(Symbol(), MODE_ASK));
buy_market(BuyMagic, 1, 0, NormalizeDouble(MarketInfo(Symbol(), MODE_ASK)+ TakeProfitPoints* Point(),Digits()));
}
if (haramiBearPrice>0 && iClose(Symbol(),0,1)<haramiBearPrice)
{
haramiBearPrice = 0;
DoAlerts("haramiBear Sell",iTime(Symbol(),0,0),MarketInfo(Symbol(), MODE_BID));
sell_market(SellMagic, 1, 0, NormalizeDouble(MarketInfo(Symbol(), MODE_BID)- TakeProfitPoints* Point(),Digits()));
}
if (dojiBearPrice >0 && iClose(Symbol(),0,1)<dojiBearPrice)
{
dojiBearPrice = 0;
DoAlerts("dojiBear Sell",iTime(Symbol(),0,0),MarketInfo(Symbol(), MODE_BID));
sell_market(SellMagic, 1, 0, NormalizeDouble(MarketInfo(Symbol(), MODE_BID)- TakeProfitPoints* Point(),Digits()));
}
}
}
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function |
//+------------------------------------------------------------------+
void OnTick()
{
color color1 = Red;
color color2 = White;
color color3 = Red;
color color4 = White;
#define HAHIGH 0
#define HALOW 1
#define HAOPEN 2
#define HACLOSE 3
int rates_total = 5;
datetime time[];
double high1[];
double low1[];
double close1[];
double open1[];
ArrayResize(high1,rates_total);
ArrayResize(low1,rates_total);
ArrayResize(close1,rates_total);
ArrayResize(open1,rates_total);
ArrayResize(time,rates_total);
if (uha)
{
for (int i = rates_total-1; i>=0;i--)
{
high1[i]=iCustom(NULL,0,"Heiken Ashi", color1,color2,color3,color4, HAHIGH, i);
low1[i] = iCustom(NULL,0,"Heiken Ashi", color1,color2,color3,color4, HALOW, i);
open1[i] = iCustom(NULL,0,"Heiken Ashi", color1,color2,color3,color4, HAOPEN, i);
close1[i] = iCustom(NULL,0,"Heiken Ashi", color1,color2,color3,color4, HACLOSE, i);
time[i] = iTime(NULL,0,i);
}
Calculate( rates_total,
time,
open1,
high1,
low1,
close1);
}
else
{
for (int i = rates_total-1; i>=0;i--)
{
high1[i]=iHigh(NULL,0,i);
low1[i] = iLow(NULL,0,i);
open1[i] = iOpen(NULL,0,i);
close1[i] = iClose(NULL,0, i);
time[i] = iTime(NULL,0,i);
}
Calculate( rates_total,
time,
open1,
high1,
low1,
close1);
}
}
//+------------------------------------------------------------------+