On two different EAs I can go long and short but if I try to get one ea to do both it instantly closes the position upon entering it. Any ideas?

 

Hey Guys,


I am struggling with getting my ea to trade both directions without immediately closing them.

Any suggestions? I am relatively new to coding.

Files:
 
VINCENT PAUL CAPIZZO:

Hey Guys,


I am struggling with getting my ea to trade both directions without immediately closing them.

Any suggestions? I am relatively new to coding.

Fix the compiler warnings for lines 78 and 119, and you'll see improvement.

 

Hey,

Are you using different magic numbers?

 
Michail Manelidis:

Hey,

Are you using different magic numbers?

I am not, I need different magic numbers within the same ea?

 
Seng Joo Thio:

Fix the compiler warnings for lines 78 and 119, and you'll see improvement.

I'll give that a shot, thank you

 
VINCENT PAUL CAPIZZO:

Hey Guys,


I am struggling with getting my ea to trade both directions without immediately closing them.

Any suggestions? I am relatively new to coding.

Won't solve your immediate problem, but you might need to look at the structure of your coding.

A quick look told me you have just the standard functions: OnInit, OnTick, OnDeinit..I'm no expert, but if you separate out some of the code actions into functions, it makes it a lot easier to read and debug.

Take this segment, in OnTick(), which I'm guessing is to identify a sell order?

if(Bid < ThirtyMinuteEMA)
{
if(ThirtyMinuteCCI>-100)
if(CurrentMACDSignal<PreviousMACDSignal)
if((TimeCurrent()>= StrToTime(StartTrading)) && (TimeCurrent() <= StrToTime(StopTrading)))
if(AccountBalance() > StopTradingBalance)
if(Bid < FiveMinute200PeriodEMA)
if((FirstClose>CurrentSlow) && (SecondClose<CurrentSlow))

if(OrdersTotal()==0)
// etc

That code is a prime candidate to put into a separate function like this, away from OnTick()

void OnTick(){
 // other code
 if(IsSellOrder())
  ticket=OrderSend(..);

}

bool IsSellOrder(){
 if(!cond0)return false;
 if(!cond1)return false;
 return true;
}

For me, this makes it easier to read, and debug. 

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...
Reason: