Welcome to Forex-TSD!, one of the largest Forex forums worldwide, where you will be able to find the most complete and reliable Forex information imaginable.
From the list below, select the forum that you want to visit and register to post, as many times you want. It’s absolutely free. Click here for registering on Forex-TSD.
Exclusive Forum
The Exclusive Forum is the only paid section. Once you subscribe, you will get free access to real cutting-edge Trading Systems (automated and not), Indicators, Signals, Articles, etc., that will help and guide you, in ways that you could only imagine, with your Forex trading.
Elite Section
Get access to private discussions, specialized support, indicators and trading systems reported every week.
Advanced Elite Section
For professional traders, trading system developers and any other member who may need to use and/or convert, the most cutting-edge exclusive indicators and trading systems for MT4 and MT5.
Could someone tell me the code for stop loss and tp. I need to add it to this EA.
this hidden SL and TP
PHP Code:
int TakeProfit=20; // 20 pips take profit int StopLoss =40; // 40 pips stoploss int Slippage = 3; int MagicNumber=1; int i;
int start(){ //----- exit @ TP if((ScanTrades()>=1)&& (ProfitInPips()>=TakeProfit)){ //----- This part will close all open orders and delete pending trades for(i = OrdersTotal()-1; i >=0; i--) { OrderSelect(i,SELECT_BY_POS,MODE_TRADES); if (OrderSymbol() == Symbol()&& OrderMagicNumber()==MagicNumber ) { if (OrderType()==OP_BUY)OrderClose(OrderTicket(),OrderLots(),Ask,Slippage,Plum); if (OrderType()==OP_SELL) OrderClose(OrderTicket(),OrderLots(),Bid,Slippage,Plum); if (OrderType() == OP_SELLLIMIT) OrderDelete(OrderTicket()); if (OrderType() == OP_BUYLIMIT) OrderDelete(OrderTicket()); if (OrderType() == OP_BUYSTOP) OrderDelete(OrderTicket()); if (OrderType() == OP_SELLSTOP) OrderDelete(OrderTicket()); } } }
//------exit @ SL if((ScanTrades()>=1)&& (ProfitInPips()<= -StopLoss)){ //----- This part will close all open orders and delete pending trades for(i = OrdersTotal()-1; i >=0; i--) { OrderSelect(i,SELECT_BY_POS,MODE_TRADES); if (OrderSymbol() == Symbol()&& OrderMagicNumber()==MagicNumber ) { if (OrderType()==OP_BUY)OrderClose(OrderTicket(),OrderLots(),Ask,Slippage,Plum); if (OrderType()==OP_SELL) OrderClose(OrderTicket(),OrderLots(),Bid,Slippage,Plum); if (OrderType() == OP_SELLLIMIT) OrderDelete(OrderTicket()); if (OrderType() == OP_BUYLIMIT) OrderDelete(OrderTicket()); if (OrderType() == OP_BUYSTOP) OrderDelete(OrderTicket()); if (OrderType() == OP_SELLSTOP) OrderDelete(OrderTicket()); } } }
return (0); } //----------- Call functions int ScanTrades() { int Tot = OrdersTotal(); int Numb = 0;
What I want do is :
- Exit all the trades " Basket" at 5 pips more than the breakeven price of the open trades
What I am trying to do in the first for loop is find the value of open trades + swap and convert it to pips , and this is the part where I am getting stuck
I have tried few ideas but I have reached a dead end, I know the problem is in the first for statement but I can't solve it, any help is highly appropriated
//--- Count the open trades int i; int count=0; for(i=0;i<OrdersTotal();i++) { if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) break; if(OrderMagicNumber()==MagicNumber && OrderType()<2) { count++; } }
//--- find the profit in pips of the open trades RefreshRates(); double profits=0,openPrice=0,points=0; string sym=""; int i; int totalOrders=OrdersTotal(); for(i=0;i<totalOrders;i++) { if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) break; if(OrderMagicNumber()==MagicNumber) { sym=OrderSymbol(); openPrice=OrderOpenPrice(); if(OrderType()==0) { profits+=(MarketInfo(sym,MODE_BID)-openPrice)/MarketInfo(sym,MODE_POINT); } if(OrderType()==1) { profits+=(OrderOpenPrice()-MarketInfo(sym,MODE_ASK))/MarketInfo(sym,MODE_POINT); } } }
//--- Close when the open trades are 5 pips more than the breakeven price
if (count>1 && profits>MinPro ) { for(i = OrdersTotal()-1; i >=0; i--) { OrderSelect(i,SELECT_BY_POS,MODE_TRADES); if (OrderSymbol() == Symbol()&& OrderMagicNumber()==MagicNumber ) { OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),Slippage,Plum);
Thanks Ralph but I would be missing the swap , and I want to include the swap profit/loss in the close of the orders decision
But the first loop accumulates both profit and swap, doesn't it?
I was thinking that the Equity figure is the one to compare against what 5 pips would mean for the open lots.... and you then don't need to actually compute the break-even price.
Last edited by ralph.ronnquist; 03-07-2009 at 10:11 PM.
Reason: added sentence
Hi Ralph
I have 3 variables
OrdLots ; in lots and represent the open lots but this will skip the swap
Equity ; this is the total profit loss and this one will include the swap
MinPro ; this is my take profit which is 5 pips + the breakeven point
I can't use OrdLots to find the profit/loss in pips as this will skip the swap
I can't use Equity to find the profit/loss in pips because for that I need to know how much lots,are used but my calculation for the lots skip the swap
Somehow I need to use a function of both OrdLots and Equity to know how many pips are opened
There might be something ready in MT4 that relate the account balance or account equity but I didn't find any
The solution you proposed with thanks will find the profit/loss in pips without considering swap, but I have already put a function for that ... I want to exit at 5 pips above the breakeven with the swap
I`m very inexperienced with the Mql4 language. I want to compare the openordertime with a Int var and if it meats a certain amount of time and the orders are still open I want to increase the amount of open orders allowed so that I can try to reach a Breakeven situation or Maybe a profit situation and then close all open orders at the same time and set the max orders back to its originally allowed max. Can some one help me or show me code on another post that already does something like this.
I`m using this code but it seems as if it does not work.
// If orders is open for a long time close the orders and try to break even
void TimeProtection()
{
int totalorders = OrdersTotal();
for(int i=totalorders-1;i>=0;i--)
{
OrderSelect(i, SELECT_BY_POS);
if ( OrderSymbol()==Symbol() )
{
prTime = OrderOpenTime();
prTime = prTime * CallTime;
if (prTime >= TimeCurrent())
{
MaxAllowable_Trades = MaxAllowable_Trades + 3;
flag = 1;
Hi Ralph
I have 3 variables
OrdLots ; in lots and represent the open lots but this will skip the swap
Equity ; this is the total profit loss and this one will include the swap
MinPro ; this is my take profit which is 5 pips + the breakeven point
I can't use OrdLots to find the profit/loss in pips as this will skip the swap
I can't use Equity to find the profit/loss in pips because for that I need to know how much lots,are used but my calculation for the lots skip the swap
Somehow I need to use a function of both OrdLots and Equity to know how many pips are opened
There might be something ready in MT4 that relate the account balance or account equity but I didn't find any
The solution you proposed with thanks will find the profit/loss in pips without considering swap, but I have already put a function for that ... I want to exit at 5 pips above the breakeven with the swap
Ah... maybe I understand... So if you also accumulate profit without swap into EquityNoSwap in the first loop, then you could use that rather than Equity in my "if" statement. (?) Because then the test would be "is there 5 pips profit relative the breakeven+swap price?" which actually is the same as "is there 5 pips profit ignoring the swap?" (or maybe I still don't understand)
Basically "profits+swap" is relative "breakeven", and thus "profits" is relative "breakeven+swap", and "profits" is distributed over "OrdLots". All in all you still don't need to determine an actual pips price for "breakeven".