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.
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 Alain 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); } } //+------------------------------------------------------------------+
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 Alain says you want to use positions not orders, but a better way to deal with it is by using CPositionInfo::SelectByMagic method.
Perhaps it is better to use
double tradeVolume = SymbolInfoDouble(_Symbol,SYMBOL_TRADE_TICK_SIZE);
Perhaps it is better to use
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); } } //+------------------------------------------------------------------+
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.
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
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.