MQL4 Script - open on new bar question
Page 1 of 843 123 LastLast
Results 1 to 10 of 23

Thread: MQL4 Script - open on new bar question

  1. #1
    Hi,

    Since there's an EA already running on my chart(s), I want to have my script available three (or less or more, does not matter however 1 n, ngt;=0 trades if it is implemented) trades, the first when it is implemented on the chart along with the remainder one per new pub.

    Currently, this is the way the logic resembles, however this only opens the initial trade, remain on the chart but does not open the upcoming few trades.
    It is probably something dumb, but I am tired and have appeared at code the majority of my waking hours till now and I am missing it.
    Or, I can lose out on the logic completely.

    Either way, any help will be greatly appreciated.


    Inserted Code int ExtraTrades = 3; datetime stopper=Time#91;0#93;; int counter = 1; //p------------------------------------------------------------------ //| Is new bar | //p------------------------------------------------------------------ bool IsNewBar() if (stopper == Time#91;1#93;-RRB- stopper = Time#91;0#93;; return true; else return false; // ------------------------------------------------------------------ void start() while (counter lt;= ExtraTrades) if (counter == 1) OpenIt(); counter ; if (IsNewBar()) OpenIt(); counter ; counter = 1;

  2. #2
    Well, after some debugging, looks like the IsNewBar() isn't evaluating correctly.
    I don't know why. Would it work for an EA but maybe not for a script?

    Code now appears like this

    Inserted Code // ------------------------------------------------------------------ //| is brand new bar | // ------------------------------------------------------------------ bool IsNewBar() if (stopper == Time#91;1#93;-RRB- stopper = Time#91;0#93;; return true; else return false; // ------------------------------------------------------------------ void start() OpenIt(); while (counter lt;= ExtraTrades) if (IsNewBar()) OpenIt();

  3. #3
    Quote Originally Posted by ;
    Why can it work for a EA but not to get a script?
    Scripts are (much more) intended as run-once. AFAIK they do not get (price etc) updates. Should you require data, you need to survey it. Try something together this (pseudo) code:
    Inserted Code time = 0 count = 0 while count lt; goal if time ! = iTime(...,0) if available() count time = iTime(...,0) Sleep(100)

  4. #4
    Quote Originally Posted by ;
    quote Scripts are (more) meant as run-once. AFAIK they don't get (price etc) upgrades. If you need data that is fresh, you need to poll it. Try something along this (pseudo) code: period = 0 count = 0 while count lt; goal if time ! = iTime(...,0) if available() count period = iTime(...,0) Sleep(100)
    Thank you for responding.

    I have substituted the IsNewBar with stopper ! =iTime(Symbol(),PERIOD_CURRENT,0) but still, it's either not passing through the if or maybe updating.
    I can not sleep it because it's going to run Renko, I might miss out a bar/entry out when the sleep is large enough.

  5. #5
    Quote Originally Posted by ;
    quote Thank You for replying. I've replaced the IsNewBar with stopper ! =iTime(Symbol(),PERIOD_CURRENT,0) but nevertheless, it's either not passing through the if or maybe updating. When the sleep is big 26, I can not sleep it because it is going to run on Renko, I might miss a bar/entry out.
    Regrettably we have to take action brute force with RefreshRates(). MetaTrader is bad. This functions:
    Inserted Code #property strict void OnStart() datetime time = 0; int count = 0; int target = 3; while((count lt; target) ! IsStopped()) if(time ! = iTime(_Symbol,PERIOD_CURRENT,0)) count ; Print(time changed: , count); time = iTime(_Symbol, PERIOD_CURRENT, 0); Sleep(1); RefreshRates();

  6. #6
    Quote Originally Posted by ;
    quote Regrettably we must take action brute force together with RefreshRates(). MetaTrader is awful. This functions: #property strict void OnStart() datetime period = 0; int count = 0; int target = 3; while((count lt; target) ! IsStopped()) if(time ! = iTime(_Symbol,PERIOD_CURRENT,0)) count ; Print(time altered: , count); period = iTime(_Symbol, PERIOD_CURRENT, 0); Sleep(1); RefreshRates();
    Thank you for that, had I seen it earlier it may have saved me the head butting against all that came my way before I figured out the RefreshRates() was the offender.

    I have done it using the Bars work, not certain it's bullet proof but I think it does the trick, also

    Inserted Code void start() OpenIt(); RefreshRates(); int nBars=Bars(Symbol(), PERIOD_CURRENT); while (counter lt;= ExtraTrades) RefreshRates(); should (nBars lt; Bars(Symbol(), PERIOD_CURRENT)) OpenIt(); nBars=Bars(Symbol(), PERIOD_CURRENT);

  7. #7
    Inserted Code static datetime T0; //p------------------------------------------------------------------ int OnInit() T0=Time#91;0#93;; // or T0 = 0; ??? Return(INIT_SUCCEEDED); // ------------------------------------------------------------------ void OnTick() if(T0!) =Time#91;0#93;-RRB- T0=Time#91;0#93;; RefreshRates(); OpenIt(); ...

  8. #8
    Quote Originally Posted by ;
    static datetime T0; //p------------------------------------------------------------------ int OnInit() T0=Timing[0]; // or T0 = 0; ??? Return(INIT_SUCCEEDED); // ------------------------------------------------------------------ void OnTick() if(T0! =Timing[0]) T0=Timing[0]; RefreshRates(); OpenIt(); ...
    Thanks, I am doing something like my EA, I am unsure OnTick is valid for a script, though.

  9. #9
    Quote Originally Posted by ;
    quote Thanks, I'm doing something similar in my EA, I'm not sure OnTick is legitimate for a script, however.
    Use OnStart() instead OnTick(), all functions
    instead

  10. #10
    Inserted Code #property stringent #property show_inputs enter int ExtraTrades=10; static datetime T0; // ------------------------------------------------------------------ void OnStart() int counter=0; T0=0; while(! IsStopped() counterlt;ExtraTrades) if(T0!) =Time#91;0#93;-RRB- T0=Time#91;0#93;; RefreshRates(); OpenIt(); offset ; Sleep(1000); // ------------------------------------------------------------------

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
This website uses cookies
We use cookies to store session information to facilitate remembering your login information, to allow you to save website preferences, to personalise content and ads, to provide social media features and to analyse our traffic. We also share information about your use of our site with our social media, advertising and analytics partners.