I want to make an EA that closes positions at a particular point and opens new trades.

 

Hello, everyone. My name is David, and although I am still learning MQL5. I am having trouble getting my bot to close out positions. I want the ask line to cancel out any buy positions on that

currency pair and switch to selling when it drops below the 50 MA (Mavalue), and the bid line to do the opposite when it rises above the 50 MA (MaValue) and switches to buying. This is my code.

I kindly ask for your assistance with this.

if(PositionSelect(_Symbol) == false){
 
   if(StdDevVal>StdDevAVGVal && Ask > MaValine && MaValine > MAValue && MAValue > MAAvalue)        // condition
     {
      double price=SymbolInfoDouble(_Symbol,SYMBOL_ASK);
      trade.Buy(0.1, _Symbol, price, price-50*_Point, price+150*_Point);                   //Buy trade
      posTicket = trade.ResultOrder();
      if(StdDevVal > StdDevAVGVal && Ask < MAValue)
      {
         if(PositionGetInteger(POSITION_TYPE)== POSITION_TYPE_BUY)
         {
            trade.PositionClose(_Symbol);
            trade.Sell(0.3, _Symbol, price, price+100*_Point, price-50*_Point);     //If buy position is open and ask is below the MaValue close buy position and open sell position
	    posTicket = trade.ResultOrder();			
            Comment("Trade Modified");                                                           
         }
      }
      Comment("Buy signal","\n");
      /*"Current Std Dev value is ",StdDevVal,"\n",
      "Std Dev Avg value is ",StdDevAVGVal,"\n",
      "Ask value is ",Ask,"\n",
      "MA value is ",MAValue);*/
     }

    if (StdDevVal > StdDevAVGVal && Bid < MaValine && MaValine < MAValue && MAValue < MAAvalue)   //condition 
     {
     double price=SymbolInfoDouble(_Symbol,SYMBOL_BID);
      trade.Sell(0.2, _Symbol, price, price+50*_Point, price-150*_Point);                                               //Sell trade
      posTicket= trade.ResultOrder();                                                                                                   
      
        if(StdDevVal > StdDevAVGVal && Bid > MAValue)
      {
         if(PositionGetInteger(POSITION_TYPE)== POSITION_TYPE_SELL)
         {
            trade.PositionClose(_Symbol);
            trade.Buy(0.3, _Symbol, price, price-100*_Point, price+50*_Point);
	    posTicket = trade.ResultOrder();
            Comment("Trade modified");                                                                      //If sell position is open and bid above MaValue close sell position and open buy position

         }
      }
      Comment("Sell signal","\n");
      //"Current Std Dev value is ",StdDevVal,"\n",
      //"Std Dev Avg value is ",StdDevAVGVal,"\n",
      //"Bid value is ",Bid,"\n",
      //"MA value is ",MAValue);
     }
   
 
David Udoh: . I want the ask line to cancel out any buy positions

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 to 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)

 
William Roeder #:

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 to 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)

Hi there William, could please assist me with my code too. I want to cancel the TP and SL so that my buy position will close when the fast MA crosses the slow Ma and via versa  for Sell position.
 
David Udoh:

Hello, everyone. My name is David, and although I am still learning MQL5. I am having trouble getting my bot to close out positions. I want the ask line to cancel out any buy positions on that

currency pair and switch to selling when it drops below the 50 MA (Mavalue), and the bid line to do the opposite when it rises above the 50 MA (MaValue) and switches to buying. This is my code.

I kindly ask for your assistance with this.

I want to know how is working 
 
TSHIMENA Gauthier #:
Hi there William, could please assist me with my code too. I want to cancel the TP and SL so that my buy position will close when the fast MA crosses the slow Ma and via versa  for Sell position.

Do not double post!

I have deleted your duplicated post.

 
  1. TSHIMENA Gauthier #: Hi there William, could please assist me with my code too. I want to cancel the TP and SL so that my buy position will close when the fast MA crosses the slow Ma and via versa  for Sell position.

    Help you with what? You haven't stated a problem, you stated a want. Show us your attempt (using the CODE button) and state the nature of your difficulty.
              No free help (2017)

    Or pay someone. Top of every page is the link Freelance.
              Hiring to write script - General - MQL5 programming forum (2018)

    We're not going to code it for you (although it could happen if you are lucky or the issue is interesting).
              No free help (2017)


  2. Don't Hijack other threads for your off-topic post. Next time, make your own, new, thread.

    Don't double post!
              General rules and best pratices of the Forum. - General - MQL5 programming forum (2017)

 
Feisal Ahmed Mohamed #:
I want to know how is working 

When the BID is below the 10 M.A and 200 M.A is greater than the 50 M.A which is greater than 10 M.A it sells and when the ASK is above 10 M.A is greater than the 50 M.A which is greater than the 200 M.A it buys. I want it to close an open buy position if the ASK goes below the 50 M.A at the same time open a sell trade and should do same when the bid goes above the 50 M.A it should close the sell trade and at the same time open a buy trade.

 
David Udoh #:

When the BID is below the 10 M.A and 200 M.A is greater than the 50 M.A which is greater than 10 M.A it sells and when the ASK is above 10 M.A is greater than the 50 M.A which is greater than the 200 M.A it buys. I want it to close an open buy position if the ASK goes below the 50 M.A at the same time open a sell trade and should do same when the bid goes above the 50 M.A it should close the sell trade and at the same time open a buy trade.

It makes absolutely no sense to use the Ask's position relative to MAs that are calculated using the Bid.

 
Keith Watford #:

It makes absolutely no sense to use the Ask's position relative to MAs that are calculated using the Bid.

So what should I do instead?

 
David Udoh #:

So what should I do instead?

Use the Bid's position relative to MAs because they are calculated using the Bid.
 
David Udoh #: So what should I do instead?

Duh! Use the Bid.