How to code? - page 69

 

Hi Wolfe

wolfe:
I know this has been covered before, but can someone show me some code to allow only 1 trade per bar? Thanks.

This works for me

datetime LastOpenTime=0;

//////////////////////////////////////////

int Order;

for(int t=0;t<OrdersTotal();t++)

{

if(OrderSelect(t, SELECT_BY_POS, MODE_TRADES))

{

if (OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumbers && OrderType() == OP_SELL)

{

if(OrderOpenTime()>LastOpenTime)LastOpenTime=OrderOpenTime();

}

}

}

Then I just stick it in my code

if(LastOpenTime0)

{ OrderSend(blah blah

Time[0] being the start of the candle of your timeframe so make sure you choose the right time frame

 
waltini:
This works for me

datetime LastOpenTime=0;

//////////////////////////////////////////

....................

Time[0] being the start of the candle of your timeframe so make sure you choose the right time frame

This is may a bit more simple :

datetime LastOpenTime; //Global var

[/CODE]

[CODE]

int init()

{

LastOpenTime=Time[0];

}

int start()

{

................

if(LastOpenTime!=Time[0])

{

// do your trade code

LastOpenTime=Time[0];

}

..............

return(0);

}

Your "trade code" will be execute one time only on any candle. There wont be any trade on that candle anymore even if you close your MT4 and turn it on again. But please notice, let say you activate the EA on a 5pm candle, there won't be any trade too on that candle, you have to wait for the next candle.

 
wolfe:
I know this has been covered before, but can someone show me some code to allow only 1 trade per bar? Thanks.

Global, put this before init()

datetime PreviousBar;[/CODE]

In the order placement and/or order close stuff, wrap it in this:

if(NewBar() == true)

{

if blah blah blah, gimme pips // not actual functions but you get the idea

if blah blah blah, close orders // um

}

Common function outside main loop:

[CODE]//--- returns true if current bar just formed

bool NewBar()

{

if(PreviousBar<Time[0])

{

PreviousBar = Time[0];

return(true);

}

else

{

return(false);

}

return(false);

}
 

Help on Programming

Hi,

I've been doing my bes to try programming some things, but I've hit a brick wall. So I tried something real simple. If I uncomment "Print("Five");" and test the program it will only print "Yahooooooo". (Well it prints One" as well). Now with "Print("Five");" commented out, even the "Yahooooooo" doesn't print. What am I missing??

Files:
trial.mq4  2 kb
 

Close all pending and open order when 1 trade hit TP

Hi,

I'm trying to write a codes in my EA that will close all pending and open orders once there is 1 trade that hit TP. Below is the codes that I used, seems not working. Can somebody take a look and advise what is missing. Thanks.

if( PreviousOpenOrders > OpenOrders )

{

for( cnt = OrdersTotal()-1; cnt >= 0; cnt-- )

{

OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);

mode = OrderType();

if( OrderSymbol() == Symbol() &&

OrderMagicNumber()==Magic)

{

if( mode == OP_BUY ) OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),slippage,Blue);

if( mode == OP_SELL ) OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),slippage,Red);

if (mode == OP_SELLLIMIT) OrderDelete(OrderTicket());

if (mode == OP_BUYLIMIT) OrderDelete(OrderTicket());

}

}

}

}

 

Use Bid and Ask respectively instead of OrderClosePrice()...

 
ralph.ronnquist:
Use Bid and Ask respectively instead of OrderClosePrice()...

Hello ralph,

can you help to give an example. Thanks for the help.

 
timebandit:
Hi, I've been doing my bes to try programming some things, but I've hit a brick wall. So I tried something real simple. If I uncomment "Print("Five");" and test the program it will only print "Yahooooooo". (Well it prints One" as well). Now with "Print("Five");" commented out, even the "Yahooooooo" doesn't print. What am I missing??

If you do not use brackets, only one line is executed. It depends of what you want to do, but you should do it like this:

if(d>5)

{

Print("Five");

Print("Yahoooooooooooooooooooooooo");

}
 

if( mode == OP_BUY ) OrderClose(OrderTicket(),OrderLots(),Bid,slippage,Blue);

if( mode == OP_SELL ) OrderClose(OrderTicket(),OrderLots(),Ask,slippage,Red);

 

Move Stop Once

Here is my bit of code for moving the stoploss to breakeven after a certain profit. Is there anything wrong with it?

//MoveOnce

if(MoveStopOnce && MoveStopWhenPrice > 0) {

if(Bid - OrderOpenPrice() == Point * MoveStopWhenPrice) {

OrderModify(OrderTicket(),OrderOpenPrice(), Bid - Point * MoveStopTo, OrderTakeProfit(), 0, Red);

if (!EachTickMode) BarCount = Bars;

continue;

}

}