Coding Question

 
void OnTick()
  {
   // We create a variable for the signal
   string signal ="";
   
   // We define the EA
   double K0=iStochastic(_Symbol,_Period,5,3,3,MODE_SMA,0,MODE_MAIN,0);
   double D0=iStochastic(_Symbol,_Period,5,3,3,MODE_SMA,0,MODE_SIGNAL,0);
   double K1=iStochastic(_Symbol,_Period,5,3,3,MODE_SMA,0,MODE_MAIN,1);
   double D1=iStochastic(_Symbol,_Period,5,3,3,MODE_SMA,0,MODE_SIGNAL,1);
   
   // sell signal
      if ((K0 > 80)&&(D0 > 80))
      if ((D0 > K0)&&(D1 < K1))
     {
         signal="sell";
     }
      
   // buy signal
      if ((K0 < 20)&&(D0 < 20))
      if ((D0 < K0)&&(D1 > K1))
     {
         signal="buy";
     }
     
   // Buy 01 Microlot
   if (signal=="buy" && OrdersTotal()==0)
   OrderSend (_Symbol,OP_BUY,0.05,Ask,3,0,Ask+150*_Point,NULL,0,0,Green);
   
   // Sell 01 Microlot
   if (signal=="sell" && OrdersTotal()==0)
   OrderSend (_Symbol,OP_SELL,0.05,Bid,3,0,Bid-150*_Point,NULL,0,0,Red);
   
   // Chart output for the signal
   Comment ("The current signal is: ",signal);
                  

I made a Simple Stochastic EA, I want the buy and sell orders to close when they hit the 50% line in Stochastics. So how can I code that? Here's what I have thanks for your help.




   

Documentation on MQL5: Constants, Enumerations and Structures / Trade Constants / Trade Orders in DOM
Documentation on MQL5: Constants, Enumerations and Structures / Trade Constants / Trade Orders in DOM
  • www.mql5.com
For equity securities, the Depth of Market window is available, where you can see the current Buy and Sell orders. Desired direction of a trade operation, required amount and requested price are specified for each order. To obtain...
 

When you post code please use the CODE button (Alt-S)!

Use the CODE button

 
Ok bear with me I'm new to this coding stuff!
 
  1. Don't double post! You already had this thread open.
              General rules and best pratices of the Forum. - General - MQL5 programming forum

  2. Please edit your (original) post and use the CODE button (Alt-S)! (For large amounts of code, attach it.)
              General rules and best pratices of the Forum. - General - MQL5 programming forum
              Messages Editor

  3. zunifx79: to close when they hit the 50% line in Stochastics. So how can I code that?
    Previous candle Stochastic was above 50% and current is below, or visa versa, then it hit.
    bool hit50 = (ps - 50)*(cs - 50) <= 0;
    
    //or
    
    if (     ps > 50 && cs <=50) … // Close sell.
    else if (ps < 50 && cs >=50) … // Close buy.

  4. OrderSend (_Symbol,OP_BUY,0.05,Ask,3,0,Ask+150*_Point,NULL,0,0,Green);
    Check your return codes for errors, and report them including GLE/LE, your variable values, and the market. That way we would know that at least you are calling your code.
              Common Errors in MQL4 Programs and How to Avoid Them - MQL4 Articles
  5. You buy at the Ask and sell at the Bid. So for buy orders you pay the spread on open. For sell orders you pay the spread on close.
    1. Your buy order's TP/SL (or Sell Stop's/Sell Limit's entry) are triggered when the Bid reaches it. Not the Ask. Your SL is shorter by the spread and your TP would be longer. Don't you want the same/specified amount for either direction?
    2. Your sell order's TP/SL (or Buy Stop's/Buy Limit's entry) will be triggered when the Ask reaches it. To trigger 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.)
  6. Never risk more than a small percentage of your account, certainly less than 2% per trade, 6% total to the account.

    Risk depends on your initial stop loss, lot size, and the value of the pair. It does not depend on margin and leverage. No SL means you have infinite risk.

    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.

  7.  if (signal=="buy" && OrdersTotal()==0)
    Magic number only allows an EA to identify its trades from all others. Using OrdersTotal/OrdersHistoryTotal (MT4) or PositionsTotal (MT5), directly and/or no Magic number filtering on your OrderSelect / Position select loop means your code is incompatible with every EA (including itself on other charts and manual trading.)
              Symbol Doesn't equal Ordersymbol when another currency is added to another seperate chart . - MQL4 programming forum
              PositionClose is not working - MQL5 programming forum
              MagicNumber: "Magic" Identifier of the Order - MQL4 Articles

 
Thank You Bill you're awesome man for taking the time to do that for me I greatly appreciate it.
Reason: