Coding help..How do I get indicator to filter instead of alert? - page 9

 
Maji:
A suggestion to all members, do not use "count up" routines to close trades. For example, do not use something like this:

for (i = 0; i<t; i++)

{

if(OrderSelect(tik,SELECT_BY_TICKET)){

double prc = Bid;

if (op == OP_SELL) prc = Ask;

CloseOrder(tik, OrderLots(), prc);

}

If you are using multiple orders, it will not close the last order. Use a "count down" routine. Here is my discussion with the developers of Metaquotes when I first stumbled on this irritating "bug".

http://www.metaquotes.net/forum/2018/

ok so how SHOULD this be? and will this line do what I want it to do?

if (prc == Bid && currentlong minorts):

 
Maji:
A suggestion to all members, do not use "count up" routines to close trades. For example, do not use something like this:

for (i = 0; i<t; i++)

{

if(OrderSelect(tik,SELECT_BY_TICKET)){

double prc = Bid;

if (op == OP_SELL) prc = Ask;

CloseOrder(tik, OrderLots(), prc);

}

If you are using multiple orders, it will not close the last order. Use a "count down" routine. Here is my discussion with the developers of Metaquotes when I first stumbled on this irritating "bug".

http://www.metaquotes.net/forum/2018/

ok so how SHOULD this be? and will this line do what I want it to do?

if (prc == Bid && currentlong minorts);

for (i = 0; i<t; i++)

{

if(OrderSelect(tik,SELECT_BY_TICKET)){

double prc = Bid;

if (op == OP_SELL) prc = Ask;

if (prc == Bid && currentlong minorts);

CloseOrder(tik, OrderLots(), prc);
 

Please refer to the link posted in my previous posting. There is a sample code in that link.

 
Maji:
Please refer to the link posted in my previous posting. There is a sample code in that link.

I see this line from the sample..

for(trade=OrdersTotal()-1;trade>=0;trade--){

while I get the idea of counting back instead of counting forward

this is counting it two places correct?

void CloseOrders(int op)

{

int tik[30], t = 0;

for(int i =0;i<OrdersTotal();i++){//------counting forward error here

if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)){

if(OrderSymbol()==Symbol() && MagicNum==OrderMagicNumber() && OrderType() == op){

tik[t] = OrderTicket(); t++;

}

}

}

for (i = 0; i<t; i++)//------counting forward error here

{

if(OrderSelect(tik,SELECT_BY_TICKET)){

double prc = Bid;

if (op == OP_SELL) prc = Ask;

CloseOrder(tik, OrderLots(), prc);

}

}

}

I just confused on how to change them...would it be...

for(int i =0;i>OrdersTotal();i--){

and

for (i = 0; i>t; i--)

I'm still trying to get my brain around the crossback thing.

 

The following is an example of a plain vanilla closing routine without any bells and whistles. I think the code is fairly straightforward to get your feet wet.

void CloseAll()

{

int trade;

for(trade=OrdersTotal()-1;trade>=0;trade--)

{

OrderSelect(trade,SELECT_BY_POS,MODE_TRADES);

if(OrderSymbol()!=Symbol())

continue;

if(OrderSymbol()==Symbol() && OrderMagicNumber()== MagicNum)

{

if(OrderType()==OP_BUY)

OrderClose(OrderTicket(),OrderLots(),Bid,Slippage,Blue);

if(OrderType()==OP_SELL)

OrderClose(OrderTicket(),OrderLots(),Ask,Slippage,Red);

}

}

}

 
Maji:
The following is an example of a plain vanilla closing routine without any bells and whistles. I think the code is fairly straightforward to get your feet wet.

void CloseAll()

{

int trade;

for(trade=OrdersTotal()-1;trade>=0;trade--)

{

OrderSelect(trade,SELECT_BY_POS,MODE_TRADES);

if(OrderSymbol()!=Symbol())

continue;

if(OrderSymbol()==Symbol() && OrderMagicNumber()== MagicNum)

{

if(OrderType()==OP_BUY && currentlong > minorts)//---I want to close long positions when the ema's crossback down

OrderClose(OrderTicket(),OrderLots(),Bid,Slippage,Blue);

if(OrderType()==OP_SELL) && currentlong < minorts)//---I want to close short positions when the ema's crossback up

OrderClose(OrderTicket(),OrderLots(),Ask,Slippage,Red);

}

}

}

}

Ok plain vanilla, that's more like it...

[/PHP]

I get these errors when I paste this in...

Compiling 'whatever.mq4'...

'(' - function definition unexpected C:\Program Files\Interbank FX Trader 4-live mini\experts\whatever.mq4 (85, 14)

'trade' - variable not defined C:\Program Files\Interbank FX Trader 4-live mini\experts\whatever.mq4 (88, 6)

'trade' - variable not defined C:\Program Files\Interbank FX Trader 4-live mini\experts\whatever.mq4 (88, 28)

'trade' - variable not defined C:\Program Files\Interbank FX Trader 4-live mini\experts\whatever.mq4 (88, 37)

'trade' - variable not defined C:\Program Files\Interbank FX Trader 4-live mini\experts\whatever.mq4 (90, 15)

'MagicNum' - variable not defined C:\Program Files\Interbank FX Trader 4-live mini\experts\whatever.mq4 (94, 54)

'Slippage' - variable not defined C:\Program Files\Interbank FX Trader 4-live mini\experts\whatever.mq4 (97, 46)

')' - unbalanced right parenthesis C:\Program Files\Interbank FX Trader 4-live mini\experts\whatever.mq4 (99, 53)

'Slippage' - variable not defined C:\Program Files\Interbank FX Trader 4-live mini\experts\whatever.mq4 (100, 46)

')' - unbalanced right parenthesis C:\Program Files\Interbank FX Trader 4-live mini\experts\whatever.mq4 (100, 58)

'}' - unbalanced parentheses C:\Program Files\Interbank FX Trader 4-live mini\experts\whatever.mq4 (104, 1)

11 error(s), 0 warning(s)

[/PHP]

I don't know why it says 'trade' is an error it looks like trade is defined as an 'int'

ok whatever...so...let's see...

#property copyright "Copyright 2006, Aaragorn"

//+--------- settings may vary use at your own risk-----------------+

#include

//+--------------user inputs--------------------+

extern double Trendsetter = 250;

extern double Minortrendsetter = 150;

extern double LongEMA = 20;

extern double ShortEMA = 5;

extern double TrailingStop = 15;

extern double TrailingStopTrigger = 1;

extern double StopLoss = 186;

extern double TakeProfit = 250;

extern double Lots = 0.1;

extern double EquityStop = 9;

extern int Slippage = 3;

#define trade 0

#define MagicNum 0

//---- Custom "Channel-1" Indicator and Filter Parameters

extern int Hours=36;

extern color col=SkyBlue;

extern double TF = 60; //--which bar period for the custom indicator to use

extern double upperproximity = 30; //---disallows long orders within this proximity to resistance line

extern double lowerproximity = 30; //---disallows short orders within this proximity to the support line

//+-----------close based on not triggering trailing stop in allotted time----------------+

extern int MonitorInMinutes = 60; // minutes after open to check state of trade

extern int ThresholdMove = 11; // if after that time we don't have +'x' pips we will exit

extern int MinsMultiplier = 600; // multiplies the MonitorInMinutes to make minutes (if 'x'=60) into hours

//+----------------------end of allotted time user inputs-----------------------------+

//+-----------------------------end of user inputs----------------------------------+

//+------------------------------------------------------------------+

//| expert start function

//+------------------------------------------------------------------+

int start(){

CloseOrder();

int cnt, ticket;

if(Bars<100){

Print("bars less than 100");

return(0);

}

//+----------------------Get Moving Average(s) Data----------------------------------------+

double currentlong=iMA(NULL,0,LongEMA,0,MODE_EMA,PRICE_CLOSE,0);//--current period longEMA

double currentshort=iMA(NULL,0,ShortEMA,0,MODE_EMA,PRICE_CLOSE,0);//--current period shortEMA

double trendsetter=iMA(NULL,0,Trendsetter,0,MODE_EMA,PRICE_CLOSE,0);//--current period TrendsetterEMA

double minorts=iMA(NULL,0,Minortrendsetter,0,MODE_EMA,PRICE_CLOSE,0);//--current period MinortrendsetterEMA

double prevlong=iMA(NULL,0,LongEMA,0,MODE_EMA,PRICE_CLOSE,1);//--previous period longEMA

double prevshort=iMA(NULL,0,ShortEMA,0,MODE_EMA,PRICE_CLOSE,1);//--previous period shortEMA

double prevtrendsetter=iMA(NULL,0,Trendsetter,0,MODE_EMA,PRICE_CLOSE,1);//--previous period TrendsetterEMA

double prevminorts=iMA(NULL,0,Minortrendsetter,0,MODE_EMA,PRICE_CLOSE,1);//--previous period MinortrendsetterEMA

//+----------------------------end of Get Moving Average(s) Data-----------------------------+

//+--------------------channel filter---------------------------+

double resistance = iCustom(NULL,TF,"Channel-1",Hours,col,0,0);

double support = iCustom(NULL,TF,"Channel-1",Hours,col,2,0);

//+------------------- end channel filter------------------------+

//+---------Obnoxious money management code needs revision-----------------+

int total=OrdersTotal();

if(total<1){

if(AccountFreeMargin()<(1000*Lots)){

Print("We have no money. Free Margin = ", AccountFreeMargin());

return(0);

}

//+---------end of Obnoxious money management code-----------------+

//+---------------------------------------Order Entry--------------------------------------------+

//+---------enter long positions----------+

if (prevshortcurrentlong && currentshort>currentlong>Trendsetter && Ask > resistance - upperproximity*Point){ //---conditions to open long positions change as desired

ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,3,Ask-StopLoss*Point,Ask+TakeProfit*Point, NULL,16384,0,Green);

if(ticket>0){

if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES )) Print("BUY order opened : ",OrderOpenPrice());

}

else Print("Error opening BUY order : ",GetLastError());

return(0);

}

//+---------enter short positions----------+

if (prevshort>prevlong && currentshort<currentlong && currentshort<currentlong<Trendsetter && Ask < support + lowerproximity*Point){ //---conditions to open short positions change as desired

ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,3,Bid+StopLoss*Point,Bid-TakeProfit*Point, NULL,16384,0,Red);

if(ticket>0) {

if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES )) Print("SELL order opened : ",OrderOpenPrice());

}

else Print("Error opening SELL order : ",GetLastError());

return(0);

}

}

//+---------end of order entry-------------------------+

//+------close on moving average cross-----------------+

void CloseAll()

{

int trade;

for(trade=OrdersTotal()-1;trade>=0;trade--)

{

OrderSelect(trade,SELECT_BY_POS,MODE_TRADES);

if(OrderSymbol()!=Symbol())

continue;

if(OrderSymbol()==Symbol() && OrderMagicNumber()== MagicNum)

{

if(OrderType()==OP_BUY && currentlong > minorts)//---I want to close long positions when the ema's crossback down

OrderClose(OrderTicket(),OrderLots(),Bid,Slippage,Blue);

if(OrderType()==OP_SELL && currentlong < minorts)//---I want to close short positions when the ema's crossback up

OrderClose(OrderTicket(),OrderLots(),Ask,Slippage,Red);

}

}

}

}

//+--------end of close on moving average cross--------+

now by adding this

extern int Slippage = 3;

#define trade 0

#define MagicNum 0

I'm down to...

[PHP]Compiling 'whatever.mq4'...

'(' - function definition unexpected C:\Program Files\Interbank FX Trader 4-live mini\experts\whatever.mq4 (87, 14)

'}' - unbalanced parentheses C:\Program Files\Interbank FX Trader 4-live mini\experts\whatever.mq4 (106, 1)

2 error(s), 0 warning(s)

I don't know what to do about these two.

[PHP]//+------close on moving average cross-----------------+

void CloseAll()//function definition unexpected error occurs here

{

int trade;

for(trade=OrdersTotal()-1;trade>=0;trade--)

{

OrderSelect(trade,SELECT_BY_POS,MODE_TRADES);

if(OrderSymbol()!=Symbol())

continue;

if(OrderSymbol()==Symbol() && OrderMagicNumber()== MagicNum)

{

if(OrderType()==OP_BUY && currentlong > minorts)//---I want to close long positions when the ema's crossback down

OrderClose(OrderTicket(),OrderLots(),Bid,Slippage,Blue);

if(OrderType()==OP_SELL && currentlong < minorts)//---I want to close short positions when the ema's crossback up

OrderClose(OrderTicket(),OrderLots(),Ask,Slippage,Red);

}

}

}

}//--------unblanced parentheses error occurs here

 

Where did you define the "CloseOrder(); " function? The name of the closing function I posted was different... CloseAll()

See if that takes care of the problems.

 

ok that makes sense...so I guess there are competing closing strategies going here...the CloseOrder() was relating to the close on time in trade function which I never got worked out to do what I intended. So I guess for the time being and for the present purpose I just delete that eh?..someone showed me a cool trick to turn code off just add the "//" to the front of the line...

here's the whole thing....as is..

#property copyright "Copyright 2006, Aaragorn"

//+--------- settings may vary use at your own risk-----------------+

#include

//+--------------user inputs--------------------+

extern double Trendsetter = 250;

extern double Minortrendsetter = 150;

extern double LongEMA = 20;

extern double ShortEMA = 5;

extern double TrailingStop = 15;

extern double TrailingStopTrigger = 1;

extern double StopLoss = 186;

extern double TakeProfit = 250;

extern double Lots = 0.1;

extern double EquityStop = 9;

extern int Slippage = 3;

#define trade 0

#define MagicNum 0

//---- Custom "Channel-1" Indicator and Filter Parameters

extern int Hours=36;

extern color col=SkyBlue;

extern double TF = 60; //--which bar period for the custom indicator to use

extern double upperproximity = 30; //---disallows long orders within this proximity to resistance line

extern double lowerproximity = 30; //---disallows short orders within this proximity to the support line

//+-----------close based on not triggering trailing stop in allotted time----------------+

extern int MonitorInMinutes = 60; // minutes after open to check state of trade

extern int ThresholdMove = 11; // if after that time we don't have +'x' pips we will exit

extern int MinsMultiplier = 600; // multiplies the MonitorInMinutes to make minutes (if 'x'=60) into hours

//+----------------------end of allotted time user inputs-----------------------------+

//+-----------------------------end of user inputs----------------------------------+

//+------------------------------------------------------------------+

//| expert start function

//+------------------------------------------------------------------+

int start(){

//+--turned off--+CloseOrder();

int cnt, ticket;

if(Bars<100){

Print("bars less than 100");

return(0);

}

//+----------------------Get Moving Average(s) Data----------------------------------------+

double currentlong=iMA(NULL,0,LongEMA,0,MODE_EMA,PRICE_CLOSE,0);//--current period longEMA

double currentshort=iMA(NULL,0,ShortEMA,0,MODE_EMA,PRICE_CLOSE,0);//--current period shortEMA

double trendsetter=iMA(NULL,0,Trendsetter,0,MODE_EMA,PRICE_CLOSE,0);//--current period TrendsetterEMA

double minorts=iMA(NULL,0,Minortrendsetter,0,MODE_EMA,PRICE_CLOSE,0);//--current period MinortrendsetterEMA

double prevlong=iMA(NULL,0,LongEMA,0,MODE_EMA,PRICE_CLOSE,1);//--previous period longEMA

double prevshort=iMA(NULL,0,ShortEMA,0,MODE_EMA,PRICE_CLOSE,1);//--previous period shortEMA

double prevtrendsetter=iMA(NULL,0,Trendsetter,0,MODE_EMA,PRICE_CLOSE,1);//--previous period TrendsetterEMA

double prevminorts=iMA(NULL,0,Minortrendsetter,0,MODE_EMA,PRICE_CLOSE,1);//--previous period MinortrendsetterEMA

//+----------------------------end of Get Moving Average(s) Data-----------------------------+

//+--------------------channel filter---------------------------+

double resistance = iCustom(NULL,TF,"Channel-1",Hours,col,0,0);

double support = iCustom(NULL,TF,"Channel-1",Hours,col,2,0);

//+------------------- end channel filter------------------------+

//+---------Obnoxious money management code needs revision-----------------+

int total=OrdersTotal();

if(total<1){

if(AccountFreeMargin()<(1000*Lots)){

Print("We have no money. Free Margin = ", AccountFreeMargin());

return(0);

}

//+---------end of Obnoxious money management code-----------------+

//+---------------------------------------Order Entry--------------------------------------------+

//+---------enter long positions----------+

if (prevshortcurrentlong && currentshort>currentlong>Trendsetter && Ask > resistance - upperproximity*Point){ //---conditions to open long positions change as desired

ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,3,Ask-StopLoss*Point,Ask+TakeProfit*Point, NULL,16384,0,Green);

if(ticket>0){

if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES )) Print("BUY order opened : ",OrderOpenPrice());

}

else Print("Error opening BUY order : ",GetLastError());

return(0);

}

//+---------enter short positions----------+

if (prevshort>prevlong && currentshort<currentlong && currentshort<currentlong<Trendsetter && Ask < support + lowerproximity*Point){ //---conditions to open short positions change as desired

ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,3,Bid+StopLoss*Point,Bid-TakeProfit*Point, NULL,16384,0,Red);

if(ticket>0) {

if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES )) Print("SELL order opened : ",OrderOpenPrice());

}

else Print("Error opening SELL order : ",GetLastError());

return(0);

}

}

//+---------end of order entry-------------------------+

//+------close on moving average cross-----------------+

void CloseAll()//function definition unexpected error occurs here

{

int trade;

for(trade=OrdersTotal()-1;trade>=0;trade--)

{

OrderSelect(trade,SELECT_BY_POS,MODE_TRADES);

if(OrderSymbol()!=Symbol())

continue;

if(OrderSymbol()==Symbol() && OrderMagicNumber()== MagicNum)

{

if(OrderType()==OP_BUY && currentlong > minorts)//---I want to close long positions when the ema's crossback down

OrderClose(OrderTicket(),OrderLots(),Bid,Slippage,Blue);

if(OrderType()==OP_SELL && currentlong < minorts)//---I want to close short positions when the ema's crossback up

OrderClose(OrderTicket(),OrderLots(),Ask,Slippage,Red);

}

}

}

}//--------unblanced parentheses error occurs here

//+--------end of close on moving average cross--------+

//+-------------------------Trailing Stop Code------------------------------------+

for(cnt=0;cnt<total;cnt++) {

OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);

if(OrderType()<=OP_SELL && OrderSymbol()==Symbol()) {

if(OrderType()==OP_BUY){

if(TrailingStop>0) {

if(Bid-OrderOpenPrice()>Point*TrailingStopTrigger) {

if(OrderStopLoss()<Bid-Point*TrailingStop) {

OrderModify(OrderTicket(),OrderOpenPrice(),Bid-Point*TrailingStop,OrderTakeProfit(),0,Green);

return(0);

}

}

}

}else{

if(TrailingStop>0) {

if((OrderOpenPrice()-Ask)>(Point*TrailingStopTrigger)) {

if((OrderStopLoss()>(Ask+Point*TrailingStop)) || (OrderStopLoss()==0)) {

OrderModify(OrderTicket(),OrderOpenPrice(),Ask+Point*TrailingStop,OrderTakeProfit(),0,Red);

return(0);

}

}

}

}

//+-------------------------End of Trailing Stop Code----------------------------+

//+---------------------Equity Stop Code---------------------------+

if((AccountEquity()+ EquityStop)<AccountBalance()) {

{

int ttotal = OrdersTotal();

for(int i=ttotal-1;i>=0;i--)

{

OrderSelect(i, SELECT_BY_POS);

int type = OrderType();

bool result = false;

switch(type)

{

//Close opened long positions

case OP_BUY : result = OrderClose( OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_BID), 5, Red );

break;

//Close opened short positions

case OP_SELL : result = OrderClose( OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_ASK), 5, Red );

}

if(result == false)

{

Alert("Order " , OrderTicket() , " failed to close. Error:" , GetLastError() );

Sleep(3000);

}

}

return(0);

}

}

}

}

}

//+---------------------End of Equity Stop Code---------------------------+

//|

//+---------------------Close Based on Time-------------------------------+

//+--------------needs revision, not working as desired---------------------+

//+------------I want it to close IF and ONLY IF trailing stop is NOT triggered-------------+

//+--turned off--+void CloseOrder()

//+--turned off--+{

//+--turned off--+ double Profit=ThresholdMove*Point;

//+--turned off--+ int total = OrdersTotal();

//+--turned off--+ for (int cnt = 0 ; cnt < total ; cnt++)

//+--turned off--+ {

//+--turned off--+ OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES);

//+--turned off--+ if ((CurTime()-OrderOpenTime())>MonitorInMinutes*60*MinsMultiplier)

//+--turned off--+ {

//+--turned off--+ if(OrderSymbol()==Symbol() && OrderType()==OP_BUY && Bid-Profit<OrderOpenPrice() )

//+--turned off--+ {

//+--turned off--+ OrderClose(OrderTicket(),OrderLots(),Bid,3,Violet);

//+--turned off--+ }

//+--turned off--+ if(OrderSymbol()==Symbol() && OrderType()==OP_SELL && Bid+Profit>OrderOpenPrice())

//+--turned off--+ {

//+--turned off--+ OrderClose(OrderTicket(),OrderLots(),Ask,3,Violet);

//+--turned off--+ }

//+--turned off--+ }

//+--turned off--+ }

//+--turned off--+}

//+---------------------------end of close on time code---------------+

the same two error still happen on compiling.

 

Okay, I am still waiting for an email to arrive. So... few more minutes to kill.

You have to cut the closeall function and paste it outside the start loop. It is a separate function. Then you have to call the closeall function from the start loop where you want it to close the trades. I think you have to study this concept of calling functions before you go any further.

 
Maji:
Okay, I am still waiting for an email to arrive. So... few more minutes to kill. You have to cut the closeall function and paste it outside the start loop. It is a separate function. Then you have to call the closeall function from the start loop where you want it to close the trades. I think you have to study this concept of calling functions before you go any further.

so far from what I can see in the editor about functions it is looking for parameters and I don't see any specified in the ()??

..you say I need to 'cut' and 'paste outside the start loop'

//+------------------------------------------------------------------+

//| expert start function

//+------------------------------------------------------------------+

CloseAll();

int start(){

//+--turned off--+CloseOrder(); [/PHP]

this gives the same two errors plus this error

'CloseAll' - expression on global scope not allowed C:\Program Files\Interbank FX Trader 4-live mini\experts\whatever.mq4 (33, 1)

when I put it next to where the other one was like this...

[PHP]int start(){

//+--turned off--+CloseOrder();

CloseAll();

that still leaves the two errors down the page like it makes no difference at all. You're right I don't get this function call thing at all.

do you know how to make this work?

I'm lookin here http://www.metatrader.info/node/53 offhand I don't see what's wrong.

Reason: