• Get access

How to code? - page 207

ljubas
111
ljubas  

Problem with closing position

Hi,

I just started with my new EA and opening positions is OK, I setup tp and sl. EA open and close 1 by 1 position and never there are more than 1 order. But I would like to close position if some of indicators alert me before sl or tp limit.

If I try something like this:

for (int i=1; i<=OrdersTotal(); i++) {

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

if (OrderSymbol()==Symbol()) {

Ans=OrderClose(OrderTicket(),alLots,Bid,2);// Order closing

}

}

}[/CODE]

Nothing happened. Doesn't find any opened positions and I know there is 1.

If I try something like this:

for (int i=1; i<=OrdersTotal(); i++) {

CloseOrder(OrderValue(i,VAL_TICKET),OrderValue(i,VAL_LOTS),Bid,3);

}

Compiler report error message that VAL_TICKER and VAL_LOTS are not defined and in examples I found they are nowhere declared.

If I try direcly by number to close:

[CODE]

Ans=OrderClose(0,alLots,Bid,2);// Order closing

I get error message 4107 or 4105 that ticker is invalid

Can somebody check this and my ea in attachment and help please?

Thanks a lot

Files:
rsiproba.mq4 6 kb
username1
42
username1  

iHighest iLowest Clarification

Assume 3 bars: Bar [2], Bar [1] and Bar [0] (Current).

Is the following "stop_low()" function correct to find the lowest low of bar [2] and [3] ?

double stop_low = iLowest(Symbol(),0,1,2,1);

------------------------------------------------------------

Reference:

int iLowest( string symbol, int timeframe, int type, int count=WHOLE_ARRAY, int start=0)

Returns the shift of the least value over a specific number of periods depending on type.

Parameters:

symbol - Symbol the data of which should be used to calculate indicator. NULL means the current symbol.

timeframe - Timeframe. It can be any of Timeframe enumeration values. 0 means the current chart timeframe.

type - Series array identifier. It can be any of Series array identifier enumeration values.

count - Number of periods (in direction from the start bar to the back one) on which the calculation is carried out.

start - Shift showing the bar, relative to the current bar, that the data should be taken from.

Sample:

// calculating the lowest value on the 10 consequtive bars in the range

// from the 10th to the 19th index inclusive on the current chart

double val=Low;

username1
42
username1  

Reduce Open Position

Does anyone know how to close / reduce half a position. What function do we call ?

Example: Reduce an open position from 10 Lots to 5 Lots without opening another trade in the opposite direction ?

Ovidiu Marius
603
Ovidiu Marius  

Is called 'partial close';

bool OrderClose( int ticket, double lots, double price, int slippage, color Color=CLR_NONE)

Just specify how many lots to close.

Roger09
167
Roger09  
ljuba973:
Hi,

Try this way

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

if (OrderSelect(i, SELECT_BY_POS)) {

if (OrderSymbol()==Symbol()&&OrderType()==0) {

Ans=OrderClose(OrderTicket(),alLots,Bid,2);// Order closing

}

if (OrderSymbol()==Symbol()&&OrderType()==1) {

Ans=OrderClose(OrderTicket(),alLots,Ask,2);// Order closing

}

}

Roger09
167
Roger09  
username1:
Does anyone know how to close / reduce half a position. What function do we call ? Example: Reduce an open position from 10 Lots to 5 Lots without opening another trade in the opposite direction ?

Just close order for 5 lots, like this:

OrderClose(OrderTicket(),5.0,.......

ljubas
111
ljubas  

Hi Roger,

Thanks a lot for help. In meantime I managed to fix it to work:

OrderSend(Symbol(),OP_BUY,alLots,Ask,3,0,0,EA_Tester,Magic);

if(OrderSelect(OrdersTotal()-1, SELECT_BY_POS)==true) {

alTicker = OrderTicket();

Alert("Bought! ", alTicker);

} else Print("OrderSelect failed error code is ",GetLastError());

[/CODE]

Like that I found alTicker (after opening position) which I close later on.

But your code I will use to optimize my Closing function. Sorry for maybe "beginner's code", I am into mq4 just 2 days ... will improve - I promise

Thanks again

Roger09:
Try this way

[CODE]

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

if (OrderSelect(i, SELECT_BY_POS)) {

if (OrderSymbol()==Symbol()&&OrderType()==0) {

Ans=OrderClose(OrderTicket(),alLots,Bid,2);// Order closing

}

if (OrderSymbol()==Symbol()&&OrderType()==1) {

Ans=OrderClose(OrderTicket(),alLots,Ask,2);// Order closing

}

}

fxbg
4
fxbg  

How I can place a fixed StopLoss ?

Hi,

Could someone please let me know how I can place a fixed StopLoss in ths code?

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

//| Daydream by Cothool |

//| Recommended: USD/JPY 1H |

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

#define MAGIC_NUM 48213657

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

extern double Lots = 0.1;

extern int ChannelPeriod = 25;

extern int Slippage = 3;

extern int TakeProfit = 15;

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

double LastOrderTime = 0;

double CurrentDirection = 0;

double CurrentTakeProfitPrice = 0;

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

void OpenLong()

{

if (Time[0] == LastOrderTime)

return;

if (CurrentDirection != 0)

return;

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

"Daydream", MAGIC_NUM, 0, Blue);

LastOrderTime = Time[0];

CurrentDirection = 1;

}

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

void OpenShort()

{

if (Time[0] == LastOrderTime)

return;

if (CurrentDirection != 0)

return;

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

"Daydream", MAGIC_NUM, 0, Red);

LastOrderTime = Time[0];

CurrentDirection = -1;

}

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

void CloseLong()

{

int i;

if (Time[0] == LastOrderTime)

return;

if (CurrentDirection != 1)

return;

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

{

if (OrderSelect(i, SELECT_BY_POS) && OrderSymbol() == Symbol() &&

OrderMagicNumber() == MAGIC_NUM && OrderType() == OP_BUY)

{

OrderClose(OrderTicket(), OrderLots(), Bid, 3, White);

LastOrderTime = Time[0];

CurrentDirection = 0;

}

}

}

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

void CloseShort()

{

int i;

if (Time[0] == LastOrderTime)

return;

if (CurrentDirection != -1)

return;

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

{

if (OrderSelect(i, SELECT_BY_POS) && OrderSymbol() == Symbol() &&

OrderMagicNumber() == MAGIC_NUM && OrderType() == OP_SELL)

{

OrderClose(OrderTicket(), OrderLots(), Ask, 3, White);

LastOrderTime = Time[0];

CurrentDirection = 0;

}

}

}

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

void start()

{

double HighestValue;

double LowestValue;

HighestValue = High;

LowestValue = Low[Lowest(NULL, 0, MODE_LOW, ChannelPeriod, 1)];

// BUY

if (Close[0] < LowestValue)

{

CloseShort();

OpenLong();

CurrentTakeProfitPrice = Bid + TakeProfit * Point;

}

// SELL

if (Close[0] > HighestValue)

{

CloseLong();

OpenShort();

CurrentTakeProfitPrice = Ask - TakeProfit * Point;

}

// Trailing Profit Taking for Long Position

if (CurrentDirection == 1)

{

if (CurrentTakeProfitPrice > Bid + TakeProfit * Point)

CurrentTakeProfitPrice = Bid + TakeProfit * Point;

if (Bid >= CurrentTakeProfitPrice)

CloseLong();

}

// Trailing Profit Taking for Short Position

if (CurrentDirection == -1)

{

if (CurrentTakeProfitPrice < Ask - TakeProfit * Point)

CurrentTakeProfitPrice = Ask - TakeProfit * Point;

if (Ask <= CurrentTakeProfitPrice)

CloseShort();

}

}

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

Regards!

Roger09
167
Roger09  
fxbg:
Hi,

Could someone please let me know how I can place a fixed StopLoss in ths code?

Replace

extern double Lots = 0.1;

extern int ChannelPeriod = 25;

extern int Slippage = 3;

extern int TakeProfit = 15;

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

double LastOrderTime = 0;

double CurrentDirection = 0;

double CurrentTakeProfitPrice = 0;

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

void OpenLong()

{

if (Time[0] == LastOrderTime)

return;

if (CurrentDirection != 0)

return;

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

"Daydream", MAGIC_NUM, 0, Blue);

LastOrderTime = Time[0];

CurrentDirection = 1;

}

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

void OpenShort()

{

if (Time[0] == LastOrderTime)

return;

if (CurrentDirection != 0)

return;

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

"Daydream", MAGIC_NUM, 0, Red);

LastOrderTime = Time[0];

CurrentDirection = -1;

} [/CODE]

to

[CODE]extern double Lots = 0.1;

extern int ChannelPeriod = 25;

extern int Slippage = 3;

extern int TakeProfit = 15;

extern int StopLoss = 15;

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

double LastOrderTime = 0;

double CurrentDirection = 0;

double CurrentTakeProfitPrice = 0;

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

void OpenLong()

{

if (Time[0] == LastOrderTime)

return;

if (CurrentDirection != 0)

return;

OrderSend(Symbol(), OP_BUY, Lots, Ask, Slippage, Ask-StopLoss*Point, 0,

"Daydream", MAGIC_NUM, 0, Blue);

LastOrderTime = Time[0];

CurrentDirection = 1;

}

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

void OpenShort()

{

if (Time[0] == LastOrderTime)

return;

if (CurrentDirection != 0)

return;

OrderSend(Symbol(), OP_SELL, Lots, Bid, Slippage, Bid+StopLoss*Point, 0,

"Daydream", MAGIC_NUM, 0, Red);

LastOrderTime = Time[0];

CurrentDirection = -1;

}
asam9392
68
asam9392  

Need help modify the indicator

Hi all programmer,

I found an indicator (currency positions) in the forum which shows the current positions I am trading. Now I want to find someone out there help me make this indicator to use external window at the bottom of the chart, also the fonts and color can be changed. I am not good at program. Thanks a lot.

asam

Files: