while(Trade==true)
- "Doesn't work" is meaningless - just like saying the car doesn't work. Doesn't start, won't go in gear, no electrical, missing the key, flat tires - meaningless. There are no mind readers here.
- You would never write if( (2+2 == 4) == true) would you? if(2+2 == 4) is sufficient. So Don't write if(bool == true), just use if(bool) or if(!bool). Code becomes self documenting when you use meaningful variable names, like bool isTrading. Trade sounds like a ticket number and "if trade" is an incomplete sentence.
- While you're in the while loop, who is doing the opening?
if(crossedLine("trade_once")) currentOpenOrdersNo=getNumOpenOrders(-1,magic); isTrading = getNumOpenOrders(-1,magic) == currentOpenOrdersNo; if(isTrading) ...
Thank you for your reply, WHRoeder.
I have solved the problem:
bool TradeOnce; // global variable int CurrentOpenOrdersNo; // global variable ... // ----- START FUNCTION ----- int start() { ... checkLines(); if(TradeOnce) tradeOnce(); ... }
// ----- CHECK LINE CROSSING FUNCTION ---------------------------------------------------------------- void checkLines() { ... if(crossedLine("once_trade")) {TradeOnce=true; CurrentOpenOrdersNo=getNumOpenOrders(-1,magic); // put Trade=true; here? Print("once_trade line crossed! CurrOpenOrders: ",CurrentOpenOrdersNo, ", Broker time: "+TimeToStr(TimeCurrent(),TIME_DATE|TIME_MINUTES));} ... } // ----- TRADE ONCE FUNCTION ----------------------------------------------------------------------------------- void tradeOnce() { Trade=true; // put Trade=true; in checkLines() function? if(getNumOpenOrders(-1,magic)>CurrentOpenOrdersNo) {TradeOnce=false; Trade=false;} // Print("TradeOnce: "+TradeOnce+", getNumOOrders: ",getNumOpenOrders(-1,magic),", CurrentOOrders#: ", // for test purpose // CurrentOpenOrdersNo,", Trade: ",Trade); }
This code works.
1. Tire not flat, keys present, starts, will go in gear, yes electrical...
2. After all these years helping people (= answering to newbies && wanna-be-coders - thank you, WHRoeder; bow) you are still surprised how un-educated people (like me) can be? I am not a coder, mq4 is simple enough to be learned... hit&&miss...
3. This did crossed my mind, but at the time being that was the solution (keys=!present && tire=flat LOL!).
4. ... not helpful... but thank you anyway.
Best regards,
Have fun,
Simon
S love nia
After some testing, some code has been added, since if TradeOnce line is crossed, TradeOnce parameters are set to true, and CurrentOpenOrdersNo is set, but if i.e. one order is closed, CurrentOpenOrderNo is not updated. Solution: reset TradeOnce function (to zero) if # of open orders is less than CurrentOpenOrdersNo.
... ... // ----- CHECK LINE CROSSING FUNCTION ---------------------------------------------------------------- void checkLines() { double PriceBuy=NormalizeDouble(Ask,Digits); double PriceSell=NormalizeDouble(Bid,Digits); string LineOrderTime=TimeToStr(TimeCurrent(),TIME_DATE|TIME_MINUTES); ... if(crossedLine("buy once")) {TradeOnce=true; Trade=true; BuyOnly=true; SellOnly=false; CurrentOpenOrdersNo=getNumOpenOrders(-1,magic); Print("BUY ONCE line crossed! CurrOpenOrders#: "+CurrentOpenOrdersNo+ ", Broker time: "+LineOrderTime+", "+Symbol()+", Price Ask: "+PriceBuy);} if(crossedLine("sell once")) {TradeOnce=true; Trade=true; SellOnly=true; BuyOnly=false; CurrentOpenOrdersNo=getNumOpenOrders(-1,magic); Print("SELL ONCE line crossed! CurrOpenOrders#: "+CurrentOpenOrdersNo+ ", Broker time: "+LineOrderTime+", "+Symbol()+", Price Bid: "+PriceSell);} ... } // ----- TRADE ONCE FUNCTION ----------------------------------------------------------------------------------- void tradeOnce() { double PriceBuy=NormalizeDouble(Ask,Digits); double PriceSell=NormalizeDouble(Bid,Digits); string TradeOnceTime=TimeToStr(TimeCurrent(),TIME_DATE|TIME_MINUTES); if(getNumOpenOrders(-1,magic)>CurrentOpenOrdersNo) {TradeOnce=false; Trade=false; Print("Trade Once order opened. Setting TradeOnce = false & Trade = false! Broker time: "+TradeOnceTime);} if(getNumOpenOrders(-1,magic)<CurrentOpenOrdersNo) {TradeOnce=false; Trade=false; Print("Open Orders # < CurrentOpenOrders#! Setting TradeOnce = false & Trade = false! Broker time: "+TradeOnceTime); } }
The rest of the code stays pretty the same.
Best regards,
Simon
S love nia
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
Hi!
I have EA which has global variable [bool Trade=false;].
I need a function that would check if a line on chart has been crossed, it will enable trade [Trade=true;] , then 'wait' for 1 order to be opened (when trading conditions are met), and then disable the trade [Trade=false;].
Currently this is the code (not working):
Obviously I tried with timing solution (LOL) too, also unsuccessfully.
Please advise.
Thank you,
Simon
S love nia