CCI Crossover Strategy (Developing)

 

I am developing a new strategy which has done quite well in manual testing so am now in the phase of my trading bot creating.

I have most of it working though the logic and closing of the trades is giving me a little trouble. Maybe I have not been getting enough sleep where the answer is right there, but cannot see it yet. :)

The bot will open the first trade just fine according to where it should while I watch the visual in MT4 though will not perform another of the other functions the logic calls the functions to perform.

The second issue is I am looking to convert the Close All Trade script into a function. It seems I would need to pass the correct parameters to the function first. Next, I would need to properly convert the script to function notation.

Based on the reading I understand the concept though after making it look like a function I received unbalanced ')' errors. After rereading the book (function) section I still am not grasping something here.

I thought I would post the two sections I am having trouble with here...

if(CurrentCCI > -100 && PreviousCCI < -100 && CurrentCCI < 0) { EnterShrt(Sym, Lots, ""); } // Enters a trade otherwise goes to the next condition.

else if(CurrentCCI < -100 && PreviousCCI > -100 && CurrentCCI <0) {EnterShrt(Sym, Lots, ""); } //Enters a trade otherwise goes to the next condition.

else if(CurrentCCI < 100 && PreviousCCI > 100 && CurrentCCI > 0) { EnterLong(Sym, Lots, ""); } //Enters a trade otherwise goes to teh next condition.

else if(CurrentCCI > 100 && PreviousCCI < 100 && CurrentCCI >0) { EnterLong(Sym, Lots, ""); } //Enters a trade otherwise goes to teh next condition.

else if(CurrentCCI < 0 && PreviousCCI > 0) {CloseAll("None", MagicNumber, OP_ALL,MaxRetry, Slippage, Commentary ); EnterShrt(Sym, Lots, "");}

else if(CurrentCCI > 0 && PreviousCCI < 0) {CloseAll("None", MagicNumber, OP_ALL,MaxRetry, Slippage, Commentary ); EnterLong(Sym, Lots, "");}

void CloseAll( string Sym, int MagicNumber, int Type, int MaxTry, int Slippage, string Commentary )

{

bool NullSym = False; if( Sym == "None" ) { NullSym = True; } //Checks if the Symbol value was sent to the function.

bool NullMag = False; if( MagicNumber == 0 ) { NullMag = True; } //Checks if the Magic Number value was sent to the function.

for( int i = OrdersTotal()-1; i >= 0; i-- ) //Performs the closing function while there are open orders.

{

if( OrderSelect(i,SELECT_BY_POS,MODE_TRADES) == false ) { continue; }

if( NullMag ) { MagicNumber = OrderMagicNumber(); }

if( MagicNumber != OrderMagicNumber() ) { continue; }

if( NullSym ) { Sym = OrderSymbol(); }

if( Sym != OrderSymbol() ) { continue; }

SymPoints = MarketInfo( Sym, MODE_POINT );

SymDigits = MarketInfo( Sym, MODE_DIGITS );

if( SymPoints == 0.001 ) { SymPoints = 0.01; SymDigits = 3; }

else if( SymPoints == 0.00001 ) { SymPoints = 0.0001; SymDigits = 5; }

int OrdType = OrderType();

if( OrdType != Type && Type != OP_ALL ) { continue; }

int err = 0; bool OrderLoop = False; int TryCount = MaxTry;

while( !OrderLoop )

{

while( IsTradeContextBusy() ) { Sleep( 10 ); }

RefreshRates();

double SymAsk = NormalizeDouble( MarketInfo( Sym, MODE_ASK ), SymDigits );

double SymBid = NormalizeDouble( MarketInfo( Sym, MODE_BID ), SymDigits );

if( OrdType == OP_BUY ) { OrderClose( OrderTicket(), OrderLots(), SymBid, Slippage*SymPoints, CLR_NONE ); }

else if( OrdType == OP_SELL ) { OrderClose( OrderTicket(), OrderLots(), SymAsk, Slippage*SymPoints, CLR_NONE ); }

int Err=GetLastError();

switch (Err)

{

//---- Success

case ERR_NO_ERROR: OrderLoop = true; break;

//---- Retry Error

case ERR_INVALID_STOPS:

case ERR_SERVER_BUSY:

case ERR_NO_CONNECTION:

case ERR_INVALID_PRICE:

case ERR_OFF_QUOTES:

case ERR_BROKER_BUSY:

case ERR_TRADE_CONTEXT_BUSY: TryCount++; break;

case ERR_PRICE_CHANGED:

case ERR_REQUOTE: continue;

//---- Fatal known Error

case ERR_INVALID_TRADE_VOLUME: OrderLoop = true; Alert( Commentary + " Invalid Lots" ); break;

case ERR_MARKET_CLOSED: OrderLoop = true; Alert( Commentary + " Market Close" ); break;

case ERR_TRADE_DISABLED: OrderLoop = true; Alert( Commentary + " Trades Disabled" ); break;

case ERR_NOT_ENOUGH_MONEY: OrderLoop = true; Alert( Commentary + " Not Enough Money" ); break;

case ERR_TRADE_TOO_MANY_ORDERS: OrderLoop = true; Alert( Commentary + " Too Many Orders" ); break;

case 149: OrderLoop = true; Alert( Commentary + " Hedge is prohibited" ); break;

//---- Fatal Unknown Error

case ERR_NO_RESULT:

default: OrderLoop = true; Print( "Unknown Error - " + Err ); break;

//----

}

// end switch

if( TryCount > 10) { OrderLoop = true; }

}

}

}

 

Just a cursory look reveals all sorts of undefined variables in the function like SymPoints,SymDigits, OP_ALL etc so I don't how you managed to get the bracket problem.

On the logic problem I can't see the point in the "else if" statements I would look for the close all conditions first then check for new entries on the next tick or make some effort to stop the orders bumping into each other on their way to the server. I would simplify the logic using boolean algebra I mean if you have already checked for CCI<0 you can ignore check 2 and check 6.

 
Reason: