How to code? - page 123

 
InTrance:
Don't try it, do it

i'm at work... and the metatrader port is blocked at here

 

Hey, I'm still new to programming, and I am having trouble adding the following features to this simple EA:

1. Take Profit

2. Stop Loss

3. Trailing Stop

4. Number of pips to activate trailing stop

//---- input parameters

extern double Lots=0.1;

extern int Slippage=5;

int MagicNum = 98760;

bool longPosOpened, shortPosOpened;

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

//| expert initialization function |

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

int init()

{

//----

//----

return(0);

}

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

//| expert deinitialization function |

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

int deinit()

{

//----

//----

return(0);

}

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

//| expert start function |

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

int start()

{

double crossup = iCustom(NULL , 0, "18_28Cross", 1,18,1,28,1,0, 1);

double crossdown = iCustom(NULL , 0, "18_28Cross", 1,18,1,28,1,1,1);

if (crossup != EMPTY_VALUE)

{

if (!PosOpened(OP_BUY))

{

CloseAllOpenAndPendingTrades();

OrderSend(Symbol(),OP_BUY,Lots,Ask,Slippage,0,0,Symbol(),MagicNum,0);

}

}

else if(crossdown != EMPTY_VALUE)

{

if (!PosOpened(OP_SELL))

{

CloseAllOpenAndPendingTrades();

OrderSend(Symbol(),OP_SELL,Lots,Bid,Slippage,0,0,Symbol(),MagicNum,0);

}

}

return(0);

}

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

bool PosOpened(int orderType)

{

int total=OrdersTotal();

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

{

OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);

if(OrderSymbol() == Symbol() && OrderType() == orderType)

{

return(true);

}

}

return(false);

}

void CloseAllOpenAndPendingTrades()

{

int total = OrdersTotal();

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

{

OrderSelect(i, SELECT_BY_POS);

if(Symbol() != OrderSymbol()) continue; // important! only close positions of current currency pair

int type = OrderType();

bool result = false;

switch(type)

{

//Close opened long positions

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

break;

//Close opened short positions

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

break;

//Close pending orders

case OP_BUYLIMIT :

case OP_BUYSTOP :

case OP_SELLLIMIT :

case OP_SELLSTOP : result = OrderDelete( OrderTicket() );

}

}

return(0);

}

 

How to detect a jump from parabolic sar?

I'm currently using this code for detecting a jump:

if(iSAR(NULL,0,0.02,0.2,0)-iSAR(NULL,0,0.02,0.2,1)>1.0){ // if there is a jump up

But this doesn't seem to work right.

How can i detect if the psar is above or below my bars?

TIA

 
:: use your proxy in metatrader...

IN10TION

ssvl:
i'm at work... and the metatrader port is blocked at here
 
:: compare your sar result with a current close[0]
ssvl:
I'm currently using this code for detecting a jump:

if(iSAR(NULL,0,0.02,0.2,0)-iSAR(NULL,0,0.02,0.2,1)>1.0){ // if there is a jump up

But this doesn't seem to work right.

How can i detect if the psar is above or below my bars?

TIA
 

Hey IN10TION, do you know of a good way to check if a trade just closed so that the EA won't place another trade (re-entry) until next signal comes? My EA checks trade condition upto lets say 10 bars but if a Sell trade condition was already met in the first few bars and closed I want the EA to stop checking upto the 10 bar Offset and wait until the next actual signal?

Thansk

 

Use the OrderHistory() function.

Lux

 
luxinterior:
Use the OrderHistory() function. Lux

Thanks but not sure how that works. I don't really want to know if an order was just closed but rather if an order was just Opened and Closed within so many bars. Then only place another trade on next main signal entry. I'm still trying to eliminate quick trade re-entries when an order was just opened and closed but trade conditions are still met. I want it to somehow only do One trade per main signal, so even though trade conditions are still valid don't trade again if already traded on that signal (including offset).

Let me see if I can explain with an example. It might be something that can't be helped.

EG: Let's say I'm using QQE and VQI as a signal trade entry point (QQE cross and VQI changes from buy to Sell or visa-versa)

Now I'm adding a SignalOffset option of lets say 6 bars because the QQE cross and VQI signal might not happen exactly on the same bar.

Now lets say there is a valid trade within 2 bars of each other when QQE and VQI agree, so now a trade is placed but I have a TP of 10 pips and the bar moves 15 pips my TP is hit and the trade is closed. Trade conditions are still met because of my 6 bar Offset now another trade is placed (re-entry) bad if that one or two bars moves 100 pips and my TP is 10, I've just had 10 trades, bad I only want One trade per Signal (Offset included)

Does that make sense? i would still want it to trade within the 6 bars though if and opposite signal happens and the other trade was closed still.

Thanks

 
:: make a [switch] variable, if there is already 1 sell order made it stops doing other sells (switch value is -1) & will only readjust when there is a buy signal & buy order made (switch value is 1)... something extra (surplus) you can think about is a timer reset, in case if there is a big trend up or down, you can have 2 or 3 sells with a kind of interval in between same orders (timing).

IN10TION

matrixebiz:
Thanks but not sure how that works. I don't really want to know if an order was just closed but rather if an order was just Opened and Closed within so many bars. Then only place another trade on next main signal entry. I'm still trying to eliminate quick trade re-entries when an order was just opened and closed but trade conditions are still met. I want it to somehow only do One trade per main signal, so even though trade conditions are still valid don't trade again if already traded on that signal (including offset).

Let me see if I can explain with an example. It might be something that can't be helped.

EG: Let's say I'm using QQE and VQI as a signal trade entry point (QQE cross and VQI changes from buy to Sell or visa-versa)

Now I'm adding a SignalOffset option of lets say 6 bars because the QQE cross and VQI signal might not happen exactly on the same bar.

Now lets say there is a valid trade within 2 bars of each other when QQE and VQI agree, so now a trade is placed but I have a TP of 10 pips and the bar moves 15 pips my TP is hit and the trade is closed. Trade conditions are still met because of my 6 bar Offset now another trade is placed (re-entry) bad if that one or two bars moves 100 pips and my TP is 10, I've just had 10 trades, bad I only want One trade per Signal (Offset included)

Does that make sense? i would still want it to trade within the 6 bars though if and opposite signal happens and the other trade was closed still.

Thanks
 

Sharing data between 2 metatrader applications

I'm trying to write data to a file from one metatrader in order to read it from a second metatrader.

The problem is that when I'm trying to write to c:\ I'm getting error message "absolute file path "C:\EURUSD.txt" is not allowed"

Any Idea or other way to share data between two metatrader applications ?

Reason: