Need help with simple EA problem - newbie

 

Hello Everyone!

I'm trying to hobble together an EA from a template program and am running into a few problems with the code and was hoping that someone could help (it's probably riduculously easy but I'm still lost).

The theory is simple:

- enter and go short if the RSI crosses up over 50

- enter and go long if the RSI crosses down 50

The exists are strictly based on TP and SL.

With this strategy, multiple open orders can accumulate. The problem is that the EA seems to only open a new order if the previous one has closed.

Can anyone please help and show me how the code needs to be modified to allow multiple open positions at the same time each time a new entry signal is given and exit only if a TP or SL has been reached.

Thanks in advance!

...first learning to crawl...then one day run...

 
int numorder = OrderSend(Symbol(), OP_BUY, dLots, Ask, nSlippage, dStopLoss, dTakeProfit, sNameExpert, MAGIC, 0, colorOpenBuy);

[/php]

You're setting an OrderSend() function as an integer. Try changing to;

[php]

{

OrderSend(Symbol(), OP_BUY, dLots, Ask, nSlippage, dStopLoss, dTakeProfit, sNameExpert, MAGIC, 0, colorOpenBuy);

}

Should do the trick. It works on my computer.

 

thank you but still not working...

Eaglehawk,

Thanks for your input - it is appreciated!

I tried your recommendation but it didn't seem to fix it.

I've attached a 2 day sample backtest result as well as a screen shot to show what happened versus what should have happened. During the time frame it only executed 3 trades when it should have executed something like 30.

Do you or anyone else out there have any more ideas?

 

did you change the settings on both of the ordersend functions? (i found two).

on my tester it got much more than three orders.

hope this helps.

 

here are the results i've had.

notepad file has a number on the right, of the take profits and stop losses.

 
 

almost there...hopefully...

Guys - I know there's quite a few senior and experienced programmers out there - please help - again, I believe this is probably a simple problem that would take 5 min for the "mistake" to be found by someone with the skillset which alot of you have.

I'll put the code here to make it easier for someone to review...

Thanks again to Eaglehawk for stepping up and trying to help.

extern string sNameExpert = "RSI Cross";

extern int nAccount =0;

extern double dBuyStopLossPoint = 25;

extern double dSellStopLossPoint = 25;

extern double dBuyTakeProfitPoint = 50;

extern double dSellTakeProfitPoint = 50;

extern double dBuyTrailingStopPoint = 0;

extern double dSellTrailingStopPoint = 0;

extern double dLots = 1;

extern int nSlippage = 3;

extern color colorOpenBuy = Blue;

extern color colorCloseBuy = Aqua;

extern color colorOpenSell = Red;

extern color colorCloseSell = Aqua;

void deinit() {

Comment("");

}

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

//| |

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

int start(){

double LongRSI=iRSI(Symbol(),Period(),14,PRICE_OPEN,2);

double ShortRSI=iRSI(Symbol(),Period(),14,PRICE_OPEN,1);

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

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

return(0);

}

bool lFlagBuyOpen = false, lFlagSellOpen = false, lFlagBuyClose = false, lFlagSellClose = false;

lFlagBuyOpen = (LongRSI>=50 && ShortRSI<50);

lFlagSellOpen = (LongRSI50);

lFlagBuyClose = False;

lFlagSellClose = False;

if (!ExistPositions()){

if (lFlagBuyOpen){

OpenBuy();

return(0);

}

if (lFlagSellOpen){

OpenSell();

return(0);

}

}

if (ExistPositions()){

if(OrderType()==OP_BUY){

if (lFlagBuyClose){

bool flagCloseBuy = OrderClose(OrderTicket(), OrderLots(), Bid, nSlippage, colorCloseBuy);

return(0);

}

}

if(OrderType()==OP_SELL){

if (lFlagSellClose){

bool flagCloseSell = OrderClose(OrderTicket(), OrderLots(), Ask, nSlippage, colorCloseSell);

return(0);

}

}

}

if (dBuyTrailingStopPoint > 0 || dSellTrailingStopPoint > 0){

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

if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) {

bool lMagic = true;

if (MAGIC > 0 && OrderMagicNumber() != MAGIC)

lMagic = false;

if (OrderSymbol()==Symbol() && lMagic) {

if (OrderType()==OP_BUY && dBuyTrailingStopPoint > 0) {

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

if (OrderStopLoss()<Bid-dBuyTrailingStopPoint*Point)

ModifyStopLoss(Bid-dBuyTrailingStopPoint*Point);

}

}

if (OrderType()==OP_SELL) {

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

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

ModifyStopLoss(Ask+dSellTrailingStopPoint*Point);

}

}

}

}

}

}

return (0);

}

bool ExistPositions() {

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

if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) {

bool lMagic = true;

if (MAGIC > 0 && OrderMagicNumber() != MAGIC)

lMagic = false;

if (OrderSymbol()==Symbol() && lMagic) {

return(True);

}

}

}

return(false);

}

void ModifyStopLoss(double ldStopLoss) {

bool lFlagModify = OrderModify(OrderTicket(), OrderOpenPrice(), ldStopLoss, OrderTakeProfit(), 0, CLR_NONE);

}

void OpenBuy() {

double dStopLoss = 0, dTakeProfit = 0;

if (dBuyStopLossPoint > 0)

dStopLoss = Ask-dBuyStopLossPoint*Point;

if (dBuyTakeProfitPoint > 0)

dTakeProfit = Ask + dBuyTakeProfitPoint * Point;

OrderSend(Symbol(), OP_BUY, dLots, Ask, nSlippage, dStopLoss, dTakeProfit, sNameExpert, MAGIC, 0, colorOpenBuy);

}

void OpenSell() {

double dStopLoss = 0, dTakeProfit = 0;

if (dSellStopLossPoint > 0)

dStopLoss = Bid+dSellStopLossPoint*Point;

if (dSellTakeProfitPoint > 0)

dTakeProfit = Bid-dSellTakeProfitPoint*Point;

OrderSend(Symbol(),OP_SELL, dLots, Bid, nSlippage, dStopLoss, dTakeProfit, sNameExpert, MAGIC, 0, colorOpenSell);

}

 
mikep:
Guys - I know there's quite a few senior and experienced programmers out there - please help - again, I believe this is probably a simple problem that would take 5 min for the "mistake" to be found by someone with the skillset which alot of you have.

I'll put the code here to make it easier for someone to review...

Thanks again to Eaglehawk for stepping up and trying to help.

extern string sNameExpert = "RSI Cross";

extern int nAccount =0;

extern double dBuyStopLossPoint = 25;

extern double dSellStopLossPoint = 25;

extern double dBuyTakeProfitPoint = 50;

extern double dSellTakeProfitPoint = 50;

extern double dBuyTrailingStopPoint = 0;

extern double dSellTrailingStopPoint = 0;

extern double dLots = 1;

extern int nSlippage = 3;

extern color colorOpenBuy = Blue;

extern color colorCloseBuy = Aqua;

extern color colorOpenSell = Red;

extern color colorCloseSell = Aqua;

void deinit() {

Comment("");

}

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

//| |

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

int start(){

double LongRSI=iRSI(Symbol(),Period(),14,PRICE_OPEN,2);

double ShortRSI=iRSI(Symbol(),Period(),14,PRICE_OPEN,1);

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

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

return(0);

}

bool lFlagBuyOpen = false, lFlagSellOpen = false, lFlagBuyClose = false, lFlagSellClose = false;

lFlagBuyOpen = (LongRSI>=50 && ShortRSI<50);

lFlagSellOpen = (LongRSI50);

lFlagBuyClose = False;

lFlagSellClose = False;

if (!ExistPositions()){

if (lFlagBuyOpen){

OpenBuy();

return(0);

}

if (lFlagSellOpen){

OpenSell();

return(0);

}

}

if (ExistPositions()){

if(OrderType()==OP_BUY){

if (lFlagBuyClose){

bool flagCloseBuy = OrderClose(OrderTicket(), OrderLots(), Bid, nSlippage, colorCloseBuy);

return(0);

}

}

if(OrderType()==OP_SELL){

if (lFlagSellClose){

bool flagCloseSell = OrderClose(OrderTicket(), OrderLots(), Ask, nSlippage, colorCloseSell);

return(0);

}

}

}

if (dBuyTrailingStopPoint > 0 || dSellTrailingStopPoint > 0){

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

if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) {

bool lMagic = true;

if (MAGIC > 0 && OrderMagicNumber() != MAGIC)

lMagic = false;

if (OrderSymbol()==Symbol() && lMagic) {

if (OrderType()==OP_BUY && dBuyTrailingStopPoint > 0) {

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

if (OrderStopLoss()<Bid-dBuyTrailingStopPoint*Point)

ModifyStopLoss(Bid-dBuyTrailingStopPoint*Point);

}

}

if (OrderType()==OP_SELL) {

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

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

ModifyStopLoss(Ask+dSellTrailingStopPoint*Point);

}

}

}

}

}

}

return (0);

}

bool ExistPositions() {

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

if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) {

bool lMagic = true;

if (MAGIC > 0 && OrderMagicNumber() != MAGIC)

lMagic = false;

if (OrderSymbol()==Symbol() && lMagic) {

return(True);

}

}

}

return(false);

}

void ModifyStopLoss(double ldStopLoss) {

bool lFlagModify = OrderModify(OrderTicket(), OrderOpenPrice(), ldStopLoss, OrderTakeProfit(), 0, CLR_NONE);

}

void OpenBuy() {

double dStopLoss = 0, dTakeProfit = 0;

if (dBuyStopLossPoint > 0)

dStopLoss = Ask-dBuyStopLossPoint*Point;

if (dBuyTakeProfitPoint > 0)

dTakeProfit = Ask + dBuyTakeProfitPoint * Point;

OrderSend(Symbol(), OP_BUY, dLots, Ask, nSlippage, dStopLoss, dTakeProfit, sNameExpert, MAGIC, 0, colorOpenBuy);

}

void OpenSell() {

double dStopLoss = 0, dTakeProfit = 0;

if (dSellStopLossPoint > 0)

dStopLoss = Bid+dSellStopLossPoint*Point;

if (dSellTakeProfitPoint > 0)

dTakeProfit = Bid-dSellTakeProfitPoint*Point;

OrderSend(Symbol(),OP_SELL, dLots, Bid, nSlippage, dStopLoss, dTakeProfit, sNameExpert, MAGIC, 0, colorOpenSell);

}

perhaps turning your slippage to "0" or "1" might help a little?

Reason: