How to code? - page 250

 
mladen:

Hi Mladen, thanks for your help, I applied your changes but it does not really work, maybe it would be simpler if the EA just ignores any existing open trades, i.e just leave them open, and then opens and closes its own new single trade

At the moment it opens and closes its own trade, but it also closes any additional existing open trades

 

increase

From a partial code that is what I could suggest. If a Boolean parameter NoClosesis created, and if that parameter is setto true, it should not close orders if applied like the snippet (if CBand CSare not equal 1 that code will not close orders)

regards

Mladen

increase:
Hi Mladen, thanks for your help, I applied your changes but it does not really work, maybe it would be simpler if the EA just ignores any existing open trades, i.e just leave them open, and then opens and closes its own new single trade At the moment it opens and closes its own trade, but it also closes any additional existing open trades
 
mladen:
increase

From a partial code that is what I could suggest. If a Boolean parameter NoClosesis created, and if that parameter is setto true, it should not close orders if applied like the snippet (if CBand CSare not equal 1 that code will not close orders)

regards

Mladen

That is true but unless I misunderstood, that means that the trade opened by the EA will also never close so it just switches between buy and sell?

 

Yes. that is correct

As far as I understood that was your idea : to let the EA open orders and then you would manage the orders (closing them) manually. With that parameter you can even switch it back to false and then EA will resume closing orders - so it is "controllable from the outside", sort of speaking

increase:
That is true but unless I misunderstood, that means that the trade opened by the EA will also never close so it just switches between buy and sell?
 
mladen:
Yes. that is correct As far as I understood that was your idea : to let the EA open orders and then you would manage the orders (closing them) manually. With that parameter you can even switch it back to false and then EA will resume closing orders - so it is "controllable from the outside", sort of speaking

Ah sorry I was ambigous, I want the EA to open and close its order and ignore any existing orders, so for example

1. I have 5 sell orders open

2. I apply the EA it opens 1 buy order

3. I now have 5 sell and 1 buy

4. The EA now triggers a sell so it closes its buy

5. I now have 6 sell (5 existing, 1 EA sell) order

6. The EA now triggers a buy so it closes its sell

7. I now have 5 sell and 1 buy etc

I will close the existing sells, the EA ignores any other trades apart from its own as above

 

increase

It already does that if you set the Magicparameter to a value other than 0 (this is a part of code that checks should it close a buy order just before the close order)

if(OrderType()==OP_BUY&&OrderSymbol()==Symbol()&&( (OrderMagicNumber()==Magic)||Magic==0))

So if the "Magic" is not equal to 0 and provided that the value for Magic is unique, it will close only orders opened by itself

increase:
Ah sorry I was ambigous, I want the EA to open and close its order and ignore any existing orders, so for example

1. I have 5 sell orders open

2. I apply the EA it opens 1 buy order

3. I now have 5 sell and 1 buy

4. The EA now triggers a sell so it closes its buy

5. I now have 6 sell (5 existing, 1 EA sell) order

6. The EA now triggers a buy so it closes its sell

7. I now have 5 sell and 1 buy etc

I will close the existing sells, the EA ignores any other trades apart from its own as above
 

Trade OncePerBar for specific TimeFrame

Hello, dear fEllA's!

I have EA (yeeaa, who don't) that works fine on tester, takes multiple trades per 4H Bar, as instructed. Now, when testing on Demo (Alpari UK) with 11 pairs, EA only takes 1 trade, while it should take more (3 max, as instructed). Only every 4H bar, if conditions are met.

I have 'Semaphor' function with GlobalVariable coded in, and this piece of code, to prevent multiple orders opening within 1 predefined TimeFrame (4H in this case):

//Buy

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

{

if (SignalsOnly)

{

// ------ Alert Should be only once per signal OR once every 15 or 30 Minute

if (CheckAlertTime == iTime (NULL, 5, 0)) return(0);

else CheckAlertTime = iTime (NULL, 5, 0);

if (Alerts) Alert (Symbol() + " " + DoubleToStr (Ask, Digits) + " BUY Signal");

if (PlaySounds) PlaySound (WhichSound);

}

if (!IsTrade && !SignalsOnly)

{

if (CheckAlertTime == iTime (NULL, OncePerBarTF, 0)) return (0);

else CheckAlertTime = iTime (NULL, OncePerBarTF, 0);

//Check free margin

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

{

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

return(0);

}

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

{

if (MoneyManagement) Lots = NormalizeDouble ((AccountFreeMargin() * Risk / 100 / ((Ask - BuySL) / Point / TickValue)), 2);

else Lots = Lots;

}

if (TradeIsBusy() && IsTradingAllowed() < 0)

return (-1);

RefreshRates();

Ticket = OrderSend (Symbol(), OP_BUY, Lots, Ask, Slippage, StopLossLevel, TakeProfitLevel, "7Qi BUY " + MagicNo, MagicNo, 0, DodgerBlue);

if (Ticket > 0)

{

if (OrderSelect (Ticket, SELECT_BY_TICKET, MODE_TRADES))

{

Print ("7Qi BUY opened: ", OrderOpenPrice());

if (Alerts) Alert (Symbol() + " " + DoubleToStr (Ask, Digits) + " BUY opened");

if (PlaySounds) PlaySound (WhichSound);

}

else

{

Print ("Error opening BUY: ", Err);

}

}

// TradedThisBar = Bars;

if (EachTickMode) TickCheck = True;

if (!EachTickMode) BarCount = Bars;

TradeIsNotBusy();

return (0);

}

}

//-------------------------------------------------

Is it possible that this code:

if (CheckAlertTime == iTime (NULL, OncePerBarTF, 0)) return (0);

else CheckAlertTime = iTime (NULL, OncePerBarTF, 0);

prevents EA from trading in the middle of the Hour? Does it checks only at the beginning of the Bar?

If I use "TradedThisBar = Bars" function I would have to leave EA on 1 TimeFrame only, correct?

'Semaphor' function is coded in TrailingStops too, PartialCloseLots too.

Thank you ALL for your help,

Have fun,

Simon

Slovenia

 

Iiiiiik - I found the problem

Hi!

I have found the problem - it is most probably combination of:

if (TradeIsBusy() /*&& IsTradingAllowed()*/ < 0)

return (-1);

//-----------------------------------------------------------------------------------------

int IsTradingAllowed (int MaxWaitingSec = 30)

{

if (!IsTradeAllowed())

{

int StartWaitingTime = GetTickCount();

Print ("Trade context is busy! Wait until it is free...");

if (Alerts) Alert (Symbol() + " EA trading not allowed! Waiting...");

while (true)

{

if (IsStopped())

{

Print ("EA was terminated!");

return(-1);

}

if (GetTickCount() - StartWaitingTime > MaxWaitingSec * 1000 )

{

Print( "Waiting limit exceeded (" + MaxWaitingSec + " sec)!" );

return(-2);

}

if (IsTradeAllowed())

{

Print ("Trade context is now free!");

return(0);

}

Sleep (100);

}

}

else

{

Print ("Trade context is free!");

return(1);

}

}

*/

//-----------------------------------------------

int TradeIsBusy (int MaxWaitingSec = 30)

{

if (IsTesting()) return(1);

int Err = 0, StartWaitingTime = GetTickCount();

while (true)

{

if (IsStopped())

{

Print ("EA was terminated!");

return(-1);

}

if (GetTickCount() - StartWaitingTime > MaxWaitingSec * 1000)

{

Print ("Waiting time (" + MaxWaitingSec + " sec) exceeded!");

if (Alerts) Alert (Symbol() + " EA waiting time exceeded!");

return(-2);

}

if (GlobalVariableCheck ("TradeIsBusy"))

break;

else

{

Err = GetLastError();

if (Err != 0)

{

Print ("TradeIsBusy - GlobalVariableCheck - Error # ", Err);

Sleep (100);

continue;

}

}

if (GlobalVariableSet ("TradeIsBusy", 1.0) > 0) return(1);

else

{

Err = GetLastError();

if (Err != 0)

{

Print ("TradeIsBusy - GlobalVariableSet - Error # ", Err);

Sleep(100);

continue;

}

}

}

while (true)

{

if (IsStopped())

{

Print ("EA was terminated!");

return(-1);

}

if (GetTickCount() - StartWaitingTime > MaxWaitingSec * 1000)

{

Print ("Waiting time (" + MaxWaitingSec + " sec) exceeded!");

if (Alerts) Alert (Symbol() + " waiting time exceeded!");

return (-2);

}

if (GlobalVariableSetOnCondition ("TradeIsBusy", 1.0, 0.0 )) return (1);

else

{

Err = GetLastError();

if (Err != 0)

{

Print ("TradeIsBusy - GlobalVariableSetOnCondition - Error # ", Err);

continue;

}

}

if (Alerts) Alert (Symbol() + " - waiting another EA to finish trading...");

Sleep (1000);

}

}

//--------------------------------

void TradeIsNotBusy()

{

int Err;

if (IsTesting())

{

return(0);

}

while (true)

{

if (IsStopped())

{

Print (Symbol() + " EA was terminated!" );

return(-1);

}

if (GlobalVariableSet ("TradeIsBusy", 0.0) > 0) return(1);

else

{

Err = GetLastError();

if (Err != 0)

Print ("TradeIsNotBusy - GlobalVariableSet - Error # ", Err);

}

Sleep (100);

}

}

//-----------------------------------------------------------------------------------------------

I will try without IsTradeAllowed function.

Thanks anyway,

Have fun,

Simon

 

error code 130

----- deleted ----- it's not necessary any more......

 

Downloadable mq4 tutorial

Hi guys,

can't find a tutorial or reference book ( not online !! ) about the mq4 code.

can you help me ?

best regards

SOLVED, had to decompress the file with a utility.

Reason: