How to code? - page 132

 

Add position in this EA

Hello,

first I must apologies because english is not my native language.

How can I modify this EA to tell him to add a position (buy 0.1 lots) at the close of the bar whenever the RSI is over 67, and to sell whenever the RSI is less than 33?

For example:

time close RSI

17:31 1.3855 69.3 ---> buy 0.1

17:32 1.3858 70.5 ---> buy 0.1

17:33 1.3849 66.8 ---> no action

17:34 1.3856 68.4 ---> buy 0.1

Total position +0.03

Same for sell short.

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

//| This MQL is generated by Expert Advisor Builder |

//| Expert Advisor Builder for MetaTrader 4 |

//| |

//| In no event will author be liable for any damages whatsoever. |

//| Use at your own risk. |

//| |

//+------------------- DO NOT REMOVE THIS HEADER --------------------+

#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 = False;

extern double Lots = 0.1;

extern int Slippage = 3;

extern bool UseStopLoss = False;

extern int StopLoss = 30;

extern bool UseTakeProfit = False;

extern int TakeProfit = 60;

extern bool UseTrailingStop = False;

extern int TrailingStop = 30;

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 Buy1_1 = iRSI(NULL, 0, 10, PRICE_CLOSE, Current + 0);

double Buy1_2 = 67;

double Sell1_1 = iRSI(NULL, 0, 10, PRICE_CLOSE, Current + 0);

double Sell1_2 = 33;

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

//| 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 (Buy1_1 > Buy1_2) Order = SIGNAL_BUY;

if (Sell1_1 < Sell1_2) 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);

}

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

Thank you very much for your help

Panurgo

 

How to stop trading after profit made for the current day?

Hi.

I have a problem. I will code an EA.

When a Profit of 20 PIP of the day is taken so the EA should not open any order until the next day.

Please help me.

Thanks

fxbeginner

 
fxbeginner:
Hi.

I have a problem. I will code an EA.

When a Profit of 20 PIP of the day is taken so the EA should not open any order until the next day.

Please help me.

An idea would be to not allow trading after the first order of the day. After the ordersend() function, place a "trade = false;"

Then reset the trade allowance each new day:

if(timeprev!=iTime(Symbol(),PERIOD_D1,0)) { //---- This is a new day

timeprev = iTime(Symbol(),PERIOD_D1,0);

trade = true; }

Then when you check your signal:

if(trade) { your trading condition here }

Just an example ...

FerruFx

 

Hidden Orders

How do I code an EA to hide all the Buy or Sell Stop Orders from the prying eyes of the Brokers. Current EA has Buy Stops and Sell Stops pending orders.

Thanks

LF

 
LazyForex:
How do I code an EA to hide all the Buy or Sell Stop Orders from the prying eyes of the Brokers. Current EA has Buy Stops and Sell Stops pending orders.

Thanks

LF

Record the buystop and sellstop value inside the EA, once the price crossover the buystop level, send a buy order, vice versa for sellstop.

 
Devil2000:
Record the buystop and sellstop value inside the EA, once the price crossover the buystop level, send a buy order, vice versa for sellstop.

Thanks. Sent you a PM

 

Tell EA to stop work

Hi All,

I have nearly finished my first EA after nearly 7 days of trial and error and cutting and pasting and hours of research....

Can someone please tell me how to issue an exit or stop function to an EA?

I want the EA to delete all current and pending trades and exit after reaching 10,000 in equity. Below is that portion of the code so far:

bool StopTrade;

int total = OrdersTotal(), cnt = 0, ExitAtEquity=10000;

for (cnt = total ; cnt >=0 ; cnt-- )

{

OrderSelect(0,SELECT_BY_POS,MODE_TRADES);

if (AccountEquity ()== ExitAtEquity)//(TimeCurrent()- OrderOpenTime() >= ActiveMinutes*60 )

{if(OrderType()==OP_BUY)

OrderClose(OrderTicket(),OrderLots(),Bid,3,Red);

if(OrderType()==OP_SELL)

OrderClose(OrderTicket(),OrderLots(),Ask,3,Red);

if(OrderType()>OP_SELL)

OrderDelete(OrderTicket());

StopTrade=true;}

}

if (dclose==dopen && OrdersTotal() < ConcurrentTrades && StopTrade != true)

As you can see, the EA looks at the Equity and then sets the bool value of the StopTrade variable to true. The EA then acknowledges the command and does not process anything under the StopTrade != true for one cycle, but then the bool value of the StopTrade gets reset I guess and trades continue.

Firstly, is there anyway for me to get this to do what I need it to do in the manner in which I am doing it?

Secondly, is there a function that I can use to simply tell the EA to quit?

All assistance is greatly appreciated.

 

What is wrong with this code?

Can someone tell me why I'm generating an error with this command in my EA?

if(OrderSelect(bTicket,SELECT_BY_TICKET)==true)

buyOrder=OrderType();

else

Print("OrderSelect returned the error of ",GetLastError());

bTicket is assigned in this prior line:

bTicket=OrderSend(Symbol(),OP_BUYSTOP,Lots,buyPoint,0,sl,buy_tp,"Script entry",0);

if(bTicket<=0) Print("Error = ",GetLastError());

else Print("ticket = ",bTicket);
My pending order is placed and returns the ticket number as assigned to bTicket, but then bTicket doesn't work in my OrderSelect(). I get an error 0 returned. I tried referencing by SELECT_BY_POS and assigning the returned ticket number to bTicket and it worked fine, but there's no way of knowing what the position number of all of my orders will be just be running the script. Any help is greatly appreciated.
 

Moving my post didn't help.

I can't use magic number because I'm running the same EA on 8 different pairs. Where is my original post so I can follow it?

 

What is wrong with this code?

Can anyone tell me why I'm generating an error with this command in my EA?

if(OrderSelect(bTicket,SELECT_BY_TICKET)==true)

buyOrder=OrderType();

else

Print("OrderSelect returned the error of ",GetLastError());

bTicket is assigned in this prior line:

bTicket=OrderSend(Symbol(),OP_BUYSTOP,Lots,buyPoint,0,sl,buy_tp,"Script entry",0);

if(bTicket<=0) Print("Error = ",GetLastError());

else Print("ticket = ",bTicket);

My pending order is placed and returns the ticket number as assigned to bTicket, but then bTicket doesn't work in my OrderSelect(). I get an error 0 returned. I tried referencing by SELECT_BY_POS and assigning the returned ticket number to bTicket and it worked fine, but there's no way of knowing what the position number of all of my orders will be just be running the script. Any help is greatly appreciated.