Please do a little help

Hey guys,

soo i started coding my first ea but ran into some problems.

It keeps opening more than on position at each tick even though i try to tell it not to do that. Can anyone help this noob out a bit. I know it’s probably something really stupid that i missed but any help is welcome.

//+------------------------------------------------------------------+
//|                                             StochasticCtrade.mq5 |
//|                                                      |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright ""
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict

 #include<Trade\Trade.mqh>
  CTrade trade;

void OnTick()
  {
   double KArray[];
   double DArray[];
   
   ArraySetAsSeries(KArray,true);
   ArraySetAsSeries(DArray,true);
   
   int StochasticDefinition=iStochastic(_Symbol,_Period,5,3,3,MODE_SMA,STO_LOWHIGH);
   
   CopyBuffer(StochasticDefinition,0,0,3,KArray);
   CopyBuffer(StochasticDefinition,1,0,3,DArray);
   
   double Kvalue0=KArray[0];
   double Dvalue0=DArray[0];
   
   double Kvalue1=KArray[1];
   double Dvalue1=DArray[1];

   double Ask=NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_ASK),_Digits);
   double Balance=AccountInfoDouble(ACCOUNT_BALANCE);
   double Equity=AccountInfoDouble(ACCOUNT_EQUITY);
   double TradeVolume=0.01;
   int OrdersTotal;

   
   //------ Buy Order  
   if ((Kvalue0<20&&Dvalue0<20)&& ((Kvalue0>Dvalue0) && (Kvalue1<Dvalue1)) && (OrdersTotal() != 0));
   {
      trade.Buy(TradeVolume,NULL,Ask,0,0,NULL);

   }
   if ((Kvalue0>80&&Dvalue0>80)&& ((Kvalue0<Dvalue0) && (Kvalue1>Dvalue1)))
   {
   trade.PositionClose(Symbol(),ULONG_MAX);
 }
 
  }

OrdersTotal() is for pending orders with mql5. Use PositionTotal() and read the documentation please.

Place handles creation in body head:

int StochasticDefinition = INVALID_HANDLE;

Create inputs at the beginning too:

input int              inp_Kperiod=5;                     // Stochastic Oscillator: K-period (number of bars for calculations) 
input int              inp_Dperiod=3;                     // Stochastic Oscillator: D-period (period of first smoothing) 
input int              inp_slowing=3;                     // Stochastic Oscillator: final smoothing 
input ENUM_MA_METHOD   inp_ma_method=MODE_SMA;            // Stochastic Oscillator: type of smoothing 
input ENUM_STO_PRICE   inp_price_field=STO_LOWHIGH;       // Stochastic Oscillator: stochastic calculation method 

And handle definition

void OnInit()
  {
   StochasticDefinition=iStochastic(_Symbol,_Period,inp_Kperiod,inp_Dperiod,inp_slowing,inp_ma_method,inp_price_field);
  }

If you’re gonna use the library classes you might as well make your life easier and go all the way with it. Also, like bklinck1g says you want to use positions not orders, but a better way to deal with it is by using CPositionInfo::SelectByMagic method.

#define MAGIC 101010
#include <Trade\Trade.mqh>
#include <Indicators\Indicators.mqh>

CTrade         g_trade;
CiStochastic   g_sto;
CPositionInfo  g_pos;
//+------------------------------------------------------------------+
int OnInit()
{
   if(!g_sto.Create(_Symbol, _Period, 5, 3, 3, MODE_SMA, STO_LOWHIGH))
      return INIT_FAILED;
   g_trade.SetExpertMagicNumber(MAGIC);
   g_trade.SetDeviationInPoints(10);
   return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
void OnTick()
{
   g_sto.Refresh();
   double tradeVolume = 0.01;
   if(g_sto.Main(0) < 20
      && g_sto.Signal(0) < 20
      && g_sto.Main(0) > g_sto.Signal(0)
      && g_sto.Main(1) < g_sto.Signal(1)
   ){
      if(g_pos.SelectByMagic(_Symbol, MAGIC)
         && g_pos.PositionType() == POSITION_TYPE_SELL
      ){
         if(!g_trade.PositionClose(_Symbol))
         {
            Print("Position close error: ", _LastError);
            return;
         }
      }
      if(!g_trade.Buy(tradeVolume))
         Print("Trade ERROR: ", _LastError);
   }
}
//+------------------------------------------------------------------+

Perhaps it is better to use

double tradeVolume = SymbolInfoDouble(_Symbol,SYMBOL_TRADE_TICK_SIZE);

I was just filling in a “generic” value into that variable. Obviously OP would have to implement his own MM. And to answer your question, tick-size is the minimal tick value change so you would actually need to use min-lots.

#property copyright “nicholishen”
#property link “interwebs.com
#property version “1.00”
#define MAGIC 101010
#include <Trade\Trade.mqh>
#include <Indicators\Indicators.mqh>

CTrade g_trade;
CiStochastic g_sto;
CPositionInfo g_pos;
CSymbolInfo g_symbol;
//±-----------------------------------------------------------------+
int OnInit()
{
if(!g_sto.Create(_Symbol, _Period, 5, 3, 3, MODE_SMA, STO_LOWHIGH))
return INIT_FAILED;
g_trade.SetExpertMagicNumber(MAGIC);
g_trade.SetDeviationInPoints(10);
g_symbol.Name(_Symbol);
return(INIT_SUCCEEDED);
}
//±-----------------------------------------------------------------+
void OnTick()
{
g_sto.Refresh();
double tradeVolume = g_symbol.LotsMin();
if(g_sto.Main(0) < 20
&& g_sto.Signal(0) < 20
&& g_sto.Main(0) > g_sto.Signal(0)
&& g_sto.Main(1) < g_sto.Signal(1)
){
if(g_pos.SelectByMagic(_Symbol, MAGIC)
&& g_pos.PositionType() == POSITION_TYPE_SELL
){
if(!g_trade.PositionClose(g_pos.Ticket()))
{
Print("Position close error: ", _LastError);
return;
}
}
if(!g_trade.Buy(tradeVolume))
Print("Trade ERROR: ", _LastError);
}
}
//±-----------------------------------------------------------------+

Agreed :slight_smile: :slight_smile: :slight_smile: :slight_smile: :slight_smile: :slight_smile: :slight_smile:

Thank you guys so much, like really appreciate it. i’m just starting to learn all these things from scratch so this helps a ton!