How to code? - page 291

 

last candle detail

Hi,

need a indicator to show the last candle high, low, open & close value...

 
mladen:
Why not using a trailing stop then? I mean it would do almost 100% (almost) of what your original idea is and it is much, much easier to code

The thing is (I don't think I worded it correct or wasn't 100% correct) the stops (for now anyway) will remain the same throughout the whole trade. How about an approach like this, when two trades are open, a code can check the number of open orders, if it's above 1 (basically 2) then both trades have their take profits modified to a certain amount which was higher than before. Essentially some sort of hedging goes on here.

Thanks,

madmax3

 

[langtitle=pl]Proszę o pomoc w napisaniu EA działającego n[/langtitle]

[lang=pl]Witam czy bugł by ktoś mi to dopracować był rym wdzięczny : Chciałbym rzeby kupywał LevelRSIbuy=20; a sprzeawał jak dojdzie LevelRSIsell=80; i żeby dało się ustawiać pozostałe żeczy kt�re są pod extern int MagicNumber

#define SIGNAL_NONE 0

#define SIGNAL_BUY 1

#define SIGNAL_SELL 2

#define SIGNAL_CLOSEBUY 3

#define SIGNAL_CLOSESELL 4

#property copyright "Expert Advisor Builder"

#property link "http://sufx.core.t3-ism.net/ExpertAdvisorBuilder/"

extern int MagicNumber = 0;

extern bool SignalMail = False;

extern bool EachTickMode = True;

extern double Lots = 0.1;

extern int Slippage = 1;

extern bool UseStopLoss = True;

extern int StopLoss = 10;

extern bool UseTakeProfit = True;

extern int TakeProfit = 5;

extern bool UseTrailingStop = False;

extern int TrailingStop = 30;

extern int LevelRSIbuy=20;

extern int LevelRSIsell=80;

int BarCount;

int Current;

bool TickCheck = False;

//+------------------------------------------------------------------+

//| expert initialization function |

//+------------------------------------------------------------------+

int init() {

BarCount = Bars;

if (EachTickMode) Current = 0; else Current = 1;

return(0);

}

//+------------------------------------------------------------------+

//| expert deinitialization function |

//+------------------------------------------------------------------+

int deinit() {

return(0);

}

//+------------------------------------------------------------------+

//| expert start function |

//+------------------------------------------------------------------+

int start() {

int Order = SIGNAL_NONE;

int Total, Ticket;

double StopLossLevel, TakeProfitLevel;

if (EachTickMode && Bars != BarCount) TickCheck = False;

Total = OrdersTotal();

Order = SIGNAL_NONE;

//+------------------------------------------------------------------+

//| Variable Begin |

//+------------------------------------------------------------------+

double Var1 = iRSI("EURUSD", PERIOD_M1, 14, PRICE_OPEN, Current + 0);

double Buy1_1 = iRSI("EURUSD", PERIOD_M1, 14, PRICE_OPEN, Current + 0);

double Sell1_1 = iRSI("EURUSD", PERIOD_M1, 14, PRICE_OPEN, Current + 0);

//+------------------------------------------------------------------+

//| Variable End |

//+------------------------------------------------------------------+

//Check position

bool IsTrade = False;

for (int i = 0; i < Total; i ++) {

OrderSelect(i, SELECT_BY_POS, MODE_TRADES);

if(OrderType() <= OP_SELL && OrderSymbol() == Symbol()) {

IsTrade = True;

if(OrderType() == OP_BUY) {

//Close

//+------------------------------------------------------------------+

//| Signal Begin(Exit Buy) |

//+------------------------------------------------------------------+

//+------------------------------------------------------------------+

//| Signal End(Exit Buy) |

//+------------------------------------------------------------------+

if (Order == SIGNAL_CLOSEBUY && ((EachTickMode && !TickCheck) || (!EachTickMode && (Bars != BarCount)))) {

OrderClose(OrderTicket(), OrderLots(), Bid, Slippage, MediumSeaGreen);

if (SignalMail) SendMail("[Signal Alert]", "[" + Symbol() + "] " + DoubleToStr(Bid, Digits) + " Close Buy");

if (!EachTickMode) BarCount = Bars;

IsTrade = False;

continue;

}

//Trailing stop

if(UseTrailingStop && TrailingStop > 0) {

if(Bid - OrderOpenPrice() > Point * TrailingStop) {

if(OrderStopLoss() < Bid - Point * TrailingStop) {

OrderModify(OrderTicket(), OrderOpenPrice(), Bid - Point * TrailingStop, OrderTakeProfit(), 0, MediumSeaGreen);

if (!EachTickMode) BarCount = Bars;

continue;

}

}

}

} else {

//Close

//+------------------------------------------------------------------+

//| Signal Begin(Exit Sell) |

//+------------------------------------------------------------------+

//+------------------------------------------------------------------+

//| Signal End(Exit Sell) |

//+------------------------------------------------------------------+

if (Order == SIGNAL_CLOSESELL && ((EachTickMode && !TickCheck) || (!EachTickMode && (Bars != BarCount)))) {

OrderClose(OrderTicket(), OrderLots(), Ask, Slippage, DarkOrange);

if (SignalMail) SendMail("[Signal Alert]", "[" + Symbol() + "] " + DoubleToStr(Ask, Digits) + " Close Sell");

if (!EachTickMode) BarCount = Bars;

IsTrade = False;

continue;

}

//Trailing stop

if(UseTrailingStop && TrailingStop > 0) {

if((OrderOpenPrice() - Ask) > (Point * TrailingStop)) {

if((OrderStopLoss() > (Ask + Point * TrailingStop)) || (OrderStopLoss() == 0)) {

OrderModify(OrderTicket(), OrderOpenPrice(), Ask + Point * TrailingStop, OrderTakeProfit(), 0, DarkOrange);

if (!EachTickMode) BarCount = Bars;

continue;

}

}

}

}

}

}

//+------------------------------------------------------------------+

//| Signal Begin(Entry) |

//+------------------------------------------------------------------+

if (False) Order = SIGNAL_BUY;

if (False) Order = SIGNAL_SELL;

//+------------------------------------------------------------------+

//| Signal End |

//+------------------------------------------------------------------+

//Buy

if (Order == SIGNAL_BUY && ((EachTickMode && !TickCheck) || (!EachTickMode && (Bars != BarCount)))) {

if(!IsTrade) {

//Check free margin

if (AccountFreeMargin() < (1000 * Lots)) {

Print("We have no money. Free Margin = ", AccountFreeMargin());

return(0);

}

if (UseStopLoss) StopLossLevel = Ask - StopLoss * Point; else StopLossLevel = 0.0;

if (UseTakeProfit) TakeProfitLevel = Ask + TakeProfit * Point; else TakeProfitLevel = 0.0;

Ticket = OrderSend(Symbol(), OP_BUY, Lots, Ask, Slippage, StopLossLevel, TakeProfitLevel, "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");

} else {

Print("Error opening BUY order : ", GetLastError());

}

}

if (EachTickMode) TickCheck = True;

if (!EachTickMode) BarCount = Bars;

return(0);

}

}

//Sell

if (Order == SIGNAL_SELL && ((EachTickMode && !TickCheck) || (!EachTickMode && (Bars != BarCount)))) {

if(!IsTrade) {

//Check free margin

if (AccountFreeMargin() < (1000 * Lots)) {

Print("We have no money. Free Margin = ", AccountFreeMargin());

return(0);

}

if (UseStopLoss) StopLossLevel = Bid + StopLoss * Point; else StopLossLevel = 0.0;

if (UseTakeProfit) TakeProfitLevel = Bid - TakeProfit * Point; else TakeProfitLevel = 0.0;

Ticket = OrderSend(Symbol(), OP_SELL, Lots, Bid, Slippage, StopLossLevel, TakeProfitLevel, "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");

} else {

Print("Error opening SELL order : ", GetLastError());

}

}

if (EachTickMode) TickCheck = True;

if (!EachTickMode) BarCount = Bars;

return(0);

}

}

if (!EachTickMode) BarCount = Bars;

return(0);

}

//+------------------------------------------------------------------+[/lang]

 

I'm trying to use this code to increase the take profit of the open trades (if their are two open trades).

if(total>2)

if(OrderType()==OP_BUY)

OrderModify(OrderTicket(),OrderOpenPrice(),OrderStopLoss(),Ask+((TakeProfit+20)*Point),0,Blue);

if(OrderType()==OP_SELL)

OrderModify(OrderTicket(),OrderOpenPrice(),OrderStopLoss(),Ask+((TakeProfit+20)*Point),0,Blue);

total=OrdersTotal(); btw.

I still can't get it to work though, any suggestions?

Thanks,

madmax3

 

Help Limiting Number of Daily Trades to 1

I am new to programming and need help in MQL4. I have studied some coder guides and figured out how to open and close trades based on my trading strategy. My difficulty is limiting the number of daily trades to 1. I want to open no more than 1 trade per day (according to server time). So I would like to check open and closed orders to see if an order was opened today. If an order was opened today, then do not open any additional orders for the day.

Please help.

 
Try something like this :
bool uniqueOnDay()

{

datetime startTime = iTime(NULL,PERIOD_D1,Time[0]);

datetime endTime = startTime+24*60*PERIOD_D1;

for (int i=OrdersHistoryTotal()-1; i>=0; i--)

{

if (!OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)) continue;

if (OrderSymbol() != Symbol()) continue;

if (OrderMagicNumber() != MagicNumber) continue;

if (OrderOpenTime()endTime) continue;

return(false);

}

return(true);

}
It is checking if any of the closed orders for current symbol has been opened on a current day. If there has been an order opened on current day it returns false (there has been already an order opened at that day) else it returns true (there have not been an ordered opened on that day). It does not check currently opened orders (just history) since I assume you do not want more than 1 opened order and that is checked differently. Usage would be something like this :

if (UniqueOnDay() && CurrentlyOpenedOrders==0) ...

PS: it assumes that your EA has a MagicNumber parameter (that exact name)

PPS: it will work on time frames up to daiy. It will not work OK on weekly and mothly charts

dresolo1:
I am new to programming and need help in MQL4. I have studied some coder guides and figured out how to open and close trades based on my trading strategy. My difficulty is limiting the number of daily trades to 1. I want to open no more than 1 trade per day (according to server time). So I would like to check open and closed orders to see if an order was opened today. If an order was opened today, then do not open any additional orders for the day. Please help.
 

[langtitle=pl]Convert function Tema to Tema(close,period)[/langtitle]

Witam

Jeszcze raz bo cos sie zle wkleilo. Jak przekazac wartosc funkcji Tema do Var1. Jak robie tak jak w kodzie ponizej to Var1 jest puste.

#property indicator_chart_window

#property indicator_buffers 1

#property indicator_color1 DarkBlue

#property indicator_width1 2

//---- input parameters

extern int EMA_period=4;

//---- buffers

string txt;

double TemaBuffer[];

double Ema[];

double EmaOfEma[];

double EmaOfEmaOfEma[];

double Var1;

int Var2;

int i,limit,limit2,limit3;

extern int Apply_To_Price=1;

//+------------------------------------------------------------------+

//| Custom indicator initialization function |

//+------------------------------------------------------------------+

int init()

{

//---- indicators

IndicatorBuffers(5);

SetIndexStyle(0,DRAW_LINE);

SetIndexBuffer(0,Var1);

SetIndexBuffer(1,TemaBuffer);

SetIndexBuffer(2,Ema);

SetIndexBuffer(3,EmaOfEma);

SetIndexBuffer(4,EmaOfEmaOfEma);

IndicatorShortName("TEMA("+EMA_period+")");

switch(Apply_To_Price)

{

case 1:

{txt="Open";break;}

case 2:

{txt="High";break;}

case 3:

{txt="Low"; break;}

case 4:

{txt="Median"; break;}

case 5:

{txt="Typical"; break;}

case 6:

{txt="WghtdClose"; break;}

default:

{txt="Close";}

}

//----

return(0);

}

//+------------------------------------------------------------------+

//| Custom indicator deinitialization function |

//+------------------------------------------------------------------+

int deinit()

{

//----

//----

return(0);

}

//+------------------------------------------------------------------+

//| Custom indicator iteration function |

//+------------------------------------------------------------------+

int start()

{

//----

int counted_bars=IndicatorCounted();

if (counted_bars==0)

{

limit=Bars-1;

limit2=limit-EMA_period;

limit3=limit2-EMA_period;

}

if (counted_bars>0)

{

limit=Bars-counted_bars-1;

limit2=limit;

limit3=limit2;

}

for (i=limit3;i>=0;i--)

{

Var1 = Tema(Apply_To_Price,4); //=======================TUTAJ JEST PROBLEM W VAR1 NIC NIE MA. DLACZEGO?

// Alert(Var1);

}

Var2 = dodawanie (2,3);

return(0);

}

double Tema(int Cena, int cykl)

{

//----

for (i=limit;i>=0;i--) Ema=iMA(NULL,0,cykl,0,MODE_EMA,Cena,i);

for (i=limit2;i>=0;i--) EmaOfEma=iMAOnArray(Ema,0,cykl,0,MODE_EMA,i);

for (i=limit3;i>=0;i--) EmaOfEmaOfEma=iMAOnArray(EmaOfEma,0,cykl,0,MODE_EMA,i);

for (i=limit3;i>=0;i--)

{

TemaBuffer=3*Ema-3*EmaOfEma+EmaOfEmaOfEma;

//Alert(TemaBuffer);

}

return(TemaBuffer);

}

int dodawanie (int a, int b)

{

int c;

c=a+b;

return(c);

}

 

Like this (TEMA) ...

If you want it like function, than it should be something like this :
double workTema[][3];

#define _ema1 0

#define _ema2 1

#define _ema3 2

double iTema(double price, double period, int r, int instanceNo=0)

{

if (ArrayRange(workTema,0)!= Bars) ArrayResize(workTema,Bars); instanceNo*=3; r = Bars-r-1;

//

//

//

//

//

double alpha = 2.0 / (1.0+period);

workTema[r][_ema1+instanceNo] = workTema[r-1][_ema1+instanceNo]+alpha*(price -workTema[r-1][_ema1+instanceNo]);

workTema[r][_ema2+instanceNo] = workTema[r-1][_ema2+instanceNo]+alpha*(workTema[r][_ema1+instanceNo]-workTema[r-1][_ema2+instanceNo]);

workTema[r][_ema3+instanceNo] = workTema[r-1][_ema3+instanceNo]+alpha*(workTema[r][_ema2+instanceNo]-workTema[r-1][_ema3+instanceNo]);

return(workTema[r][_ema3+instanceNo]+3.0*(workTema[r][_ema1+instanceNo]-workTema[r][_ema2+instanceNo]));

}[/PHP]

Attaching an example indicator too. The advatage of passing a value instead of the price type is that this way you can apply tema to any value (it does not have to be a tema of a price, it can be tema of another indicator for example)

lukibest:
Witam

Jeszcze raz bo cos sie zle wkleilo. Jak przekazac wartosc funkcji Tema do Var1. Jak robie tak jak w kodzie ponizej to Var1 jest puste.

[PHP]

#property indicator_chart_window

#property indicator_buffers 1

#property indicator_color1 DarkBlue

#property indicator_width1 2

//---- input parameters

extern int EMA_period=4;

//---- buffers

string txt;

double TemaBuffer[];

double Ema[];

double EmaOfEma[];

double EmaOfEmaOfEma[];

double Var1;

int Var2;

int i,limit,limit2,limit3;

extern int Apply_To_Price=1;

//+------------------------------------------------------------------+

//| Custom indicator initialization function |

//+------------------------------------------------------------------+

int init()

{

//---- indicators

IndicatorBuffers(5);

SetIndexStyle(0,DRAW_LINE);

SetIndexBuffer(0,Var1);

SetIndexBuffer(1,TemaBuffer);

SetIndexBuffer(2,Ema);

SetIndexBuffer(3,EmaOfEma);

SetIndexBuffer(4,EmaOfEmaOfEma);

IndicatorShortName("TEMA("+EMA_period+")");

switch(Apply_To_Price)

{

case 1:

{txt="Open";break;}

case 2:

{txt="High";break;}

case 3:

{txt="Low"; break;}

case 4:

{txt="Median"; break;}

case 5:

{txt="Typical"; break;}

case 6:

{txt="WghtdClose"; break;}

default:

{txt="Close";}

}

//----

return(0);

}

//+------------------------------------------------------------------+

//| Custom indicator deinitialization function |

//+------------------------------------------------------------------+

int deinit()

{

//----

//----

return(0);

}

//+------------------------------------------------------------------+

//| Custom indicator iteration function |

//+------------------------------------------------------------------+

int start()

{

//----

int counted_bars=IndicatorCounted();

if (counted_bars==0)

{

limit=Bars-1;

limit2=limit-EMA_period;

limit3=limit2-EMA_period;

}

if (counted_bars>0)

{

limit=Bars-counted_bars-1;

limit2=limit;

limit3=limit2;

}

for (i=limit3;i>=0;i--)

{

Var1 = Tema(Apply_To_Price,4); //=======================TUTAJ JEST PROBLEM W VAR1 NIC NIE MA. DLACZEGO?

// Alert(Var1);

}

Var2 = dodawanie (2,3);

return(0);

}

double Tema(int Cena, int cykl)

{

//----

for (i=limit;i>=0;i--) Ema=iMA(NULL,0,cykl,0,MODE_EMA,Cena,i);

for (i=limit2;i>=0;i--) EmaOfEma=iMAOnArray(Ema,0,cykl,0,MODE_EMA,i);

for (i=limit3;i>=0;i--) EmaOfEmaOfEma=iMAOnArray(EmaOfEma,0,cykl,0,MODE_EMA,i);

for (i=limit3;i>=0;i--)

{

TemaBuffer=3*Ema-3*EmaOfEma+EmaOfEmaOfEma;

//Alert(TemaBuffer);

}

return(TemaBuffer);

}

int dodawanie (int a, int b)

{

int c;

c=a+b;

return(c);

}

Files:
tema.mq4  3 kb
 

Help with HAMMER code please

Hello,

I am starting newly in mql4. Trying to pick black and white hammers in candlesticks with the following indicator. However, in the chart sometimes candlesticks get wrong identification text (white hammer candle is identified as black hammer candle and vice versa). Please help me with this situation, thanks in advance.

#property indicator_chart_window

int limit;

//---- buffers

string hammer[200000];

//+------------------------------------------------------------------+

//| CuStom indicator initialization function |

//+------------------------------------------------------------------+

int init()

{

return(0);

}

//+------------------------------------------------------------------+

//| CuStor indicator deinitialization function |

//+------------------------------------------------------------------+

int deinit()

{

//----

ObjectsDeleteAll(hammer,OBJ_TEXT);

//----

return(0);

}

//+------------------------------------------------------------------+

//| CuStom indicator iteration function |

//+------------------------------------------------------------------+

int start()

{

int N;

int N1;

int N2;

string text;

int counted_bars=IndicatorCounted();

limit=Bars-counted_bars;

for(N = 1; N < limit; N++) {

hammer[N]= CharToStr(N);

N1 = N + 1;

N2 = N + 2;

//----

//---- check for possible errors

if(counted_bars<0) {

Alert("NO Bars..");

return(-1);

}

// Check for Hammer White

if (((Close[N1]>Open[N1]) && ((Open[N1]-Low[N1])>=2*(Close[N1]-Open[N1])) && ((High[N1]-Close[N1])<=(Open[N1]-Low[N1])*0.10))) {

ObjectCreate(hammer[N], OBJ_TEXT, 0, Time[N1], Low[N1] - Point);

ObjectSetText(hammer[N], "WHmr", 9, "Times New Roman", LawnGreen);

}

// Check for Hammer Black

if (((Close[N1]=2*(Open[N1]-Close[N1])) && ((High[N1]-Open[N1])<=(Close[N1]-Low[N1])*0.10))) {

ObjectCreate(hammer[N], OBJ_TEXT, 0, Time[N1], Low[N1] - Point);

ObjectSetText(hammer[N], "BHmr", 9, "Times New Roman", LawnGreen);

}

//----

} // End of for loop

return(0);

}

//+------------------------------------------------------------------+

 
svezir:
Hello,

I am starting newly in mql4. Trying to pick black and white hammers in candlesticks with the following indicator. However, in the chart sometimes candlesticks get wrong identification text (white hammer candle is identified as black hammer candle and vice versa). Please help me with this situation, thanks in advance.

#property indicator_chart_window

int limit;

//---- buffers

string hammer[200000];

//+------------------------------------------------------------------+

//| CuStom indicator initialization function |

//+------------------------------------------------------------------+

int init()

{

return(0);

}

//+------------------------------------------------------------------+

//| CuStor indicator deinitialization function |

//+------------------------------------------------------------------+

int deinit()

{

//----

ObjectsDeleteAll(hammer,OBJ_TEXT);

//----

return(0);

}

//+------------------------------------------------------------------+

//| CuStom indicator iteration function |

//+------------------------------------------------------------------+

int start()

{

int N;

int N1;

int N2;

string text;

int counted_bars=IndicatorCounted();

limit=Bars-counted_bars;

for(N = 1; N < limit; N++) {

hammer[N]= CharToStr(N);

N1 = N + 1;

N2 = N + 2;

//----

//---- check for possible errors

if(counted_bars<0) {

Alert("NO Bars..");

return(-1);

}

// Check for Hammer White

if (((Close[N1]>Open[N1]) && ((Open[N1]-Low[N1])>=2*(Close[N1]-Open[N1])) && ((High[N1]-Close[N1])<=(Open[N1]-Low[N1])*0.10))) {

ObjectCreate(hammer[N], OBJ_TEXT, 0, Time[N1], Low[N1] - Point);

ObjectSetText(hammer[N], "WHmr", 9, "Times New Roman", LawnGreen);

}

// Check for Hammer Black

if (((Close[N1]=2*(Open[N1]-Close[N1])) && ((High[N1]-Open[N1])<=(Close[N1]-Low[N1])*0.10))) {

ObjectCreate(hammer[N], OBJ_TEXT, 0, Time[N1], Low[N1] - Point);

ObjectSetText(hammer[N], "BHmr", 9, "Times New Roman", LawnGreen);

}

//----

} // End of for loop

return(0);

}

//+------------------------------------------------------------------+

Hi Svezir,

Have this indicator for candle patterns maybe it will help.

Reason: