One order at a time

 


Hi I am a very beginning stage of coding i managed to do this via you tube, i need help on open only one trade at any time. after closing the next trade has to open. please help me on this. I am using mql5


#include <Trade/Trade.mqh>

input group "Trading Inputs"
input double Lots = 0.1;
input double TpDist = 0.00010;
input double SlDist = 0.0750;

input ENUM_TIMEFRAMES StochTimeframe = PERIOD_M1;
input int StochK = 5;
input int StochD = 3;
input int Stochslowing = 3;
input double StochUpperLevel = 80;
input double StochLowerLevel = 20;

int handleStoch;
int totalBars;

CTrade trade;

int OnInit()
  {
   handleStoch = iStochastic(_Symbol,StochTimeframe,StochK,StochD,Stochslowing,MODE_SMA,STO_LOWHIGH);

   return(INIT_SUCCEEDED);
  }

void OneDeinit(const int reason)
  {

  }

void OnTick()
  {
   int bars = iBars(_Symbol,StochTimeframe);
   if(totalBars !=bars)
   
     {
      totalBars = bars;

      double stoch[];
      CopyBuffer(handleStoch,MAIN_LINE,1,2,stoch);

      
      double bid = SymbolInfoDouble(_Symbol,SYMBOL_BID);
      double ask = SymbolInfoDouble(_Symbol,SYMBOL_ASK);
      if(stoch[1] > StochUpperLevel && stoch[0] > StochUpperLevel && stoch[1] < stoch[0] && OrdersTotal() <= 2)
        {
           {
            trade.Sell(Lots,_Symbol,bid,bid+SlDist,bid-TpDist);
           }
        }
      else
         if(stoch[1] < StochLowerLevel && stoch[0] < StochLowerLevel && stoch[1] > stoch[0] && OrdersTotal() <= 2)
           {
              {
               trade.Buy(Lots,_Symbol,ask,ask-SlDist,ask+TpDist);
              }
           }
     }
  }
Documentation on MQL5: Constants, Enumerations and Structures / Environment State / Symbol Properties
Documentation on MQL5: Constants, Enumerations and Structures / Environment State / Symbol Properties
  • www.mql5.com
Symbol Properties - Environment State - Constants, Enumerations and Structures - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 
When you post code please use the Code button (Alt+S) !

Improperly formatted code removed by moderator. Please EDIT your post and use the CODE button (Alt-S) when inserting code.

Code button in editor

Hover your mouse over your post and select "edit" ... 

MQL5.community - User Memo
MQL5.community - User Memo
  • www.mql5.com
You have just registered and most likely you have questions such as, "How do I insert a picture to my a message?" "How do I format my MQL5 source code?" "Where are my personal messages kept?" You may have many other questions. In this article, we have prepared some hands-on tips that will help you get accustomed in MQL5.community and take full advantage of its available features.
 
Sergey Golubev #:
When you post code please use the Code button (Alt+S) !

Improperly formatted code removed by moderator. Please EDIT your post and use the CODE button (Alt-S) when inserting code.

Hover your mouse over your post and select "edit" ... 

Thanks, i have done it as you guided.
 
8939506789:


Hi I am a very beginning stage of coding i managed to do this via you tube, i need help on open only one trade at any time. after closing the next trade has to open. please help me on this. I am using mql5


You can use PositionsTotal(). As long as this is greater zero don't trade.

BTW here is a list of all function with a short eyplanation so that you can search for keywords.

Bear in mind there's virtually nothing that hasn't already been programmed for MT4/MT5 and is ready for you - so searching gives better results than AI or ChatGPT!

Documentation on MQL5: Trade Functions / PositionsTotal
Documentation on MQL5: Trade Functions / PositionsTotal
  • www.mql5.com
PositionsTotal - Trade Functions - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 

OrdersTotal counts the number of orders and not positions. 

if(stoch[1] > StochUpperLevel && stoch[0] > StochUpperLevel && stoch[1] < stoch[0] && PositionsTotal()==0 )

You enter new trade if number of open positions are zero.

 
  1.    int bars = iBars(_Symbol,StochTimeframe);
       if(totalBars !=bars)

    For a new bar test, Bars is unreliable (a refresh/reconnect can change number of bars on chart), volume is unreliable (miss ticks), Price is unreliable (duplicate prices and The == operand. - MQL4 programming forum.) Always use time.
              MT4: New candle - MQL4 programming forum #3 (2014)
              MT5: Accessing variables - MQL4 programming forum #3 (2022)

    I disagree with making a new bar function, because it can only be called once per tick (second call returns false). A variable can be tested multiple times.
              Running EA once at the start of each bar - MQL4 programming forum (2011)

  2.                trade.Buy(Lots,_Symbol,ask,ask-SlDist,ask+TpDist);

    You buy at the Ask and sell at the Bid. Pending Buy Stop orders become market orders when hit by the Ask.

    1. Your buy order's TP/SL (or Sell Stop's/Sell Limit's entry) are triggered when the Bid / OrderClosePrice reaches it. Using Ask±n, makes your SL shorter and your TP longer, by the spread. Don't you want the specified amount used in either direction?

    2. Your sell order's TP/SL (or Buy Stop's/Buy Limit's entry) will be triggered when the Ask / OrderClosePrice reaches it. To trigger close at a specific Bid price, add the average spread.
                MODE_SPREAD (Paul) - MQL4 programming forum - Page 3 #25

    3. The charts show Bid prices only. Turn on the Ask line to see how big the spread is (Tools → Options (control+O) → charts → Show ask line.)

      Most brokers with variable spreads widen considerably at end of day (5 PM ET) ± 30 minutes.
      My GBPJPY shows average spread = 26 points, average maximum spread = 134.
      My EURCHF shows average spread = 18 points, average maximum spread = 106.
      (your broker will be similar).
                Is it reasonable to have such a huge spreads (20 PIP spreads) in EURCHF? - General - MQL5 programming forum (2022)

 
Carl Schreiber #:

You can use PositionsTotal(). As long as this is greater zero don't trade.

BTW here is a list of all function with a short eyplanation so that you can search for keywords.

Bear in mind there's virtually nothing that hasn't already been programmed for MT4/MT5 and is ready for you - so searching gives better results than AI or ChatGPT!

Thanks it had worked.
 
Yashar Seyyedin #:

OrdersTotal counts the number of orders and not positions. 

You enter new trade if number of open positions are zero.

Thanks, for telling me where to insert the code exactly