Trouble making an expert advisor

 

Hello everyone,

I need a help about coding. Actually, I am learning about coding and decided to make an expert advisor, but along the way I got stuck because it doesn't work well in MT4.

I want to make an EA with following specifications:

- To Buy or Sell when MA crosses.

- When the opened Buy order is in a certain amount of loss, the EA should close the Buy order immediately and open a Sell order and Vice Versa. (Close and reverse trade)

- When the opened Buy order is in a certain amount of profit, the EA should close the Buy order immediately and Buy again which applies to the Sell order as well. (Close and open the same trade again)

This would continue like this until the total profit from all the orders is equal to a certain amount and close all the orders.

And wait for MA to cross again and start a new cycle.

I attached the expert I was working on in case someone can help.

Files:
V3.mq4  7 kb
 
  1. like_a_tuff: I want … someone can help.

    Help you with what? You haven't stated a problem, you stated a want.
         How To Ask Questions The Smart Way. 2004
              Prune pointless queries.

    You have only four choices:

    1. Search for it. Do you expect us to do your research for you?

    2. Beg at:

    3. MT4: Learn to code it.
      MT5: Begin learning to code it.

      If you don't learn MQL4/5, there is no common language for us to communicate. If we tell you what you need, you can't code it. If we give you the code, you don't know how to integrate it into your code.

    4. or pay (Freelance) someone to code it. Top of every page is the link Code Base.
                Hiring to write script - General - MQL5 programming forum 2019.08.21

    We're not going to code it for you (although it could happen if you are lucky or the problem is interesting.) We are willing to help you when you post your attempt (using CODE button) and state the nature of your problem.
              No free help 2017.04.21

  2. like_a_tuff: - When the opened Buy order is in a certain amount of loss, the EA should close the Buy order immediately and open a Sell order and Vice Versa. 
    Code your lotsize to your loss.

    Risk depends on your initial stop loss, lot size, and the value of the symbol. It does not depend on margin and leverage. No SL means you have infinite risk. Never risk more than a small percentage of your trading funds, certainly less than 2% per trade, 6% total.

    1. You place the stop where it needs to be — where the reason for the trade is no longer valid. E.g. trading a support bounce the stop goes below the support.

    2. AccountBalance * percent/100 = RISK = OrderLots * (|OrderOpenPrice - OrderStopLoss| * DeltaPerLot + CommissionPerLot) (Note OOP-OSL includes the spread, and DeltaPerLot is usually around $10/pip but it takes account of the exchange rates of the pair vs. your account currency.)

    3. Do NOT use TickValue by itself - DeltaPerLot and verify that MODE_TICKVALUE is returning a value in your deposit currency, as promised by the documentation, or whether it is returning a value in the instrument's base currency.
                MODE_TICKVALUE is not reliable on non-fx instruments with many brokers - MQL4 programming forum 2017.10.10
                Is there an universal solution for Tick value? - Currency Pairs - General - MQL5 programming forum 2018.02.11
                Lot value calculation off by a factor of 100 - MQL5 programming forum 2019.07.19

    4. You must normalize lots properly and check against min and max.

    5. You must also check FreeMargin to avoid stop out

    Most pairs are worth about $10 per PIP. A $5 risk with a (very small) 5 PIP SL is $5/$10/5 or 0.1 Lots maximum.

 

Thanks William,

I'm sorry for not being specific or clear in asking.

Actually, I got stuck on two things.

1. Which function is run to open an order that reverses when it lost a certain amount and close and reopen when it gains a certain amount?

2. Is there anyway to select certain closed orders from a particular time?

 
  1. There is no such function - write one.
  2. Yes.
         How To Ask Questions The Smart Way. 2004
              Only ask questions with yes/no answers if you want “yes” or “no” as the answer.

 

1. Thanks for the answer, I made that function as I presented it before, but it only works in the first order only.

2. Thanks.

Reason: