How to code? - page 267

 
christinaLi:
I would like to help with this function you are talking about but it seems I can't simply figure out your logic. This really has nothing to do with other logic of the strategy .

I would simply like to know this.

If you have 1 lot buy at 1.4110 and 2 lot sell at 1.4200. At what price you think you have 6 pips gain?

Hi Christina,

in my position management never, the 1 buy lot closes with profit, before 2 lot sell are opened.(sells never because 1st buy lot closed in profit, wait for next signal)

back to your example:

but there is a line at approx 1.4175, where 2 sell lots (profit up, market down) and 1 buy lot (profit down, market down) are price equal (breakeven 0), so at 1.4169 6 pips profit are ready - then i would close all orders

IX

 
ixbone:
Hi Christina,

in my position management never, the 1 buy lot closes with profit, before 2 lot sell are opened.(sells never because 1st buy lot closed in profit, wait for next signal)

back to your example:

but there is a line at approx 1.4175, where 2 sell lots (profit up, market down) and 1 buy lot (profit down, market down) are price equal (breakeven 0), so at 1.4169 6 pips profit are ready - then i would close all orders

IX

Because my example is 1.4110 and 1.4200. Here's my calculation:

First of all, at 1.4170(not considering spreads) we are break even. Now we have 2 lots of sell at gain at 30 pips each and 1 lot of buy at loss of 60 pips, so break even in dollar terms.

If exactly with my example, you should have 6 pips of gain overall at 1.4164.

At at 1.4164(not considering spreads) we have 2 lots of sell at gain at 36 pips each and 1 lot of buy at loss of 54 pips, so gain of 72 pips from sells and loss of 54 pips from buys. Overall for the group of trades, you have 3 lots with a gain of 18 pips. So it's more reasonable to consider now you have 6 pips gain.

However things will be complicated again because of spreads. Because if bid is 1.4164, ask maybe 1.4166, so your sells only have 34 pips gain each. Overall for the group of trades, you have 3 lots with a gain of 14 pips. So now in fact you have 4.7 pips gain.

If I take this example one step further, if we have 2 buy and 3 sell. Then at 1.4164 you break even.

If we don't have to consider spreads, then we need 30 pips total gain to have 5 pips of gain on each lot. So The 6 pips gain would be at the price of 1.4134, is this correct?

You system probably won't have this kind of situation, it's the principle I want to get. Do you agree with the above calculation?

 
christinaLi:
Because my example is 1.4110 and 1.4200. Here's my calculation:

First of all, at 1.4170(not considering spreads) we are break even. Now we have 2 lots of sell at gain at 30 pips each and 1 lot of buy at loss of 60 pips, so break even in dollar terms.

If exactly with my example, you should have 6 pips of gain overall at 1.4164.

At at 1.4164(not considering spreads) we have 2 lots of sell at gain at 36 pips each and 1 lot of buy at loss of 54 pips, so gain of 72 pips from sells and loss of 54 pips from buys. Overall for the group of trades, you have 3 lots with a gain of 18 pips. So it's more reasonable to consider now you have 6 pips gain.

However things will be complicated again because of spreads. Because if bid is 1.4164, ask maybe 1.4166, so your sells only have 34 pips gain each. Overall for the group of trades, you have 3 lots with a gain of 14 pips. So now in fact you have 4.7 pips gain.

If I take this example one step further, if we have 2 buy and 3 sell. Then at 1.4164 you break even.

If we don't have to consider spreads, then we need 30 pips total gain to have 5 pips of gain on each lot. So The 6 pips gain would be at the price of 1.4134, is this correct?

You system probably won't have this kind of situation, it's the principle I want to get. Do you agree with the above calculation?

Christina,

i agree, you are 100% right with both calculations!

 

Hi, Michael,

I'm way too busy for the last couple days, I've already done this but needs more time to put together. Will post it here once I find some time to do so in the next few days. Chrisitna

 
 
christinaLi:
Hi, Michael, I'm way too busy for the last couple days, I've already done this but needs more time to put together. Will post it here once I find some time to do so in the next few days. Chrisitna

Hi Christina,

no problem, take your time, i always appreciate your suggestions!

Michael

 
 

i need help

Hello, everyone !

This is my idea:i need 2 indicator buffers in separate_window,1 is devupBuffer[];2 is devdwBuffer[]. For example if have five digital 1,2,3,4,5 they average is3.Then the data is greater than the average of 3 4 and 5 devupBuffer={【(4-average)*(4-average)】+【(5-average)*(5-average)】}/Standard deviation(1,2,3,4,5)*Standard deviation(1,2,3,4,5).Data is less than the average of 3 1 and 2 devdwBuffer={【(1-average)*(1-average)】+【(2-average)*(2-average)】}/Standard deviation(1,2,3,4,5)*Standard deviation(1,2,3,4,5).

 

why can't this EA buy or sell?

hi, i'm really new to this programming with Meta... please help...

I'm working with a system on Stochastics with two levels, 25 and 75...

ONE trade only...

if the MAIN and SIGNAL are both below 25 and crosses, BUY.

if the MAIN and SIGNAL crosses above 25, CLOSE BUY.

if the MAIN and SIGNAL are both above 75 and crosses, SELL.

if the MAIN and SIGNAL crosses (no need to be below 75), CLOSE SELL.

that's it...

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

//| StochCross.mq4 |

//| RobertEli |

//| http://www.metaquotes.net |

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

#property copyright "RobertEli"

#property link "http://www.metaquotes.net"

//--- input parameters

extern double TakeProfit=250.0;

extern double Lots=0.01;

extern double TrailingStop=35.0;

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

//| expert initialization function |

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

int init()

{

//----

//----

return(0);

}

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

//| expert deinitialization function |

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

int deinit()

{

//----

//----

return(0);

}

int Confirm (double line1, double line2, int level_buy, int level_sell)

{

static int confirmation = 0;

if ((line1 < level_buy) && (line2 < level_buy)) confirmation = 1; //confirm BUY, when two lines are below level_buy

if ((line1 > level_sell) && (line2 > level_sell)) confirmation = 2; //confirm SELL, when two lines are above level_sell

if ((line1 > level_buy) && (line2 > level_buy)) confirmation = 3; //confirm CLOSE BUY, when two lines are above level_buy

if ((line1 < level_sell) && (line2 < level_sell)) confirmation = 4; //confirm CLOSE SELL, when two lines are below level_sell

//if ((line1 > level_buy_close) && (line2 > level_buy_close)) confirmation = 3; //confirm CLOSE BUY

//if ((line1 < level_sell_close) && (line2 < level_sell_close)) confirmation = 4; //confirm CLOSE SELL

//else return (0);

return (confirmation);

}

int Crossed (double line1, double line2)

{

static int last_direction = 0;

static int current_direction = 0;

if (line1 > line2) current_direction = 1; //UP

if (line1 < line2) current_direction = 2; //DOWN

if (current_direction != last_direction) //changed direction

{

last_direction = current_direction;

return (last_direction);

}

else

{

return (0);

}

}

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

//| expert start function |

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

int start()

{

//----

int cnt, ticket;

double Stoch_main, Stoch_sign;

if (Bars < 100)

{

Print("BARS less than 100");

return (0);

}

if (TakeProfit < 10)

{

Print("TakeProfit less than 10");

return (0);

}

/*

Stoch_main = iStochastic (NULL,15,21,8,13,MODE_SMA,1,MODE_MAIN,0);

Stoch_sign = iStochastic (NULL,15,21,8,13,MODE_SMA,1,MODE_SIGNAL,0);

*/

Stoch_main = iStochastic (NULL,0,30,20,12,MODE_SMA,1,MODE_MAIN,0);

Stoch_sign = iStochastic (NULL,0,30,20,12,MODE_SMA,1,MODE_SIGNAL,0);

/*

Stoch_main = iMA(NULL,0,8,0,MODE_EMA,PRICE_CLOSE,0);

Stoch_sign = iMA(NULL,0,13,0,MODE_EMA,PRICE_CLOSE,0);

*/

int isCrossed = Crossed(Stoch_main,Stoch_sign);

int isConfirmed = Confirm(Stoch_main,Stoch_sign,25,75);

int total = OrdersTotal();

if (total == 0)

{

if ((isCrossed == 1) && (isConfirmed == 1))

{

ticket = OrderSend(Symbol(),OP_BUY,Lots,Ask,3,0,Ask+TakeProfit*Point,"Buy at " + Ask,24848,0,Green);

if (ticket > 0)

{

if (OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) Print("BUY Order opened: " +Ask ,OrderOpenPrice());

else Print ("Error Opening BUY Order: ", GetLastError());

return (0);

}

}

if ((isCrossed == 2) && (isConfirmed == 2))

{

ticket = OrderSend(Symbol(),OP_SELL,Lots,Bid,3,0,Bid-TakeProfit*Point,"Sell at " + Bid,24848,0,Red);

if (ticket > 0)

{

if (OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) Print("SELL Order opened: " +Bid ,OrderOpenPrice());

else Print ("Error Opening SELL Order: ", GetLastError());

return (0);

}

}

return (0);

}

else

{

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

{

OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES);

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

{

if (OrderType() == OP_BUY) //the trade was a Buy position

{ //check for closing)

if ((isCrossed == 2) && (isConfirmed == 3))

//if (isCrossed == 2)

{ //change of direction

OrderClose(OrderTicket(),OrderLots(),Bid,3,Violet); //close Buy position

return (0);

}

}

else // the trade was a SELL position

{

//should it be closed?

//if ((isCrossed == 1) && (isConfirmed == 4))

if (isCrossed == 1) //Closing the SELL need not to be below the level_buy... it's very dangerous to wait for it...

{

OrderClose(OrderTicket(),OrderLots(),Ask,3,Violet); //Sell position is now closed

return (0);

}

}

}

}

}

//----

return(0);

}

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

EA can't do what it's supposed to do, HELP!

hello, I'm really new to this mq4 programming but I've read codersgurus tutorials (thank you so much!) so I think I can understand something... However, I've made this EA that's based on MOVING AVERAGES... BUT IT'S TERRIBLY NOT WORKING THE WAY IT SHOULD BE!

the rules here are very simple...

1. MA_04_1H (means moving average period 4 for 1 hour timeframe) crosses above MA_14_1H,

confirm it with MA_04_4H being above MA_14_4H (the trend is upwards) ------> BUY!

2. MA_04_1H crosses below MA_14_1H,

confirm it with MA_04_4H being below MA_14_4H (the trend is downwards)------> SELL!

3. when MA_04_1H crosses with the MA_14_1H, close that order (it can start a new order given rules #1 and #2)

4. one trade only... (if BUY, close that BUY. after that, SELL if necessary)

I've made 2 functions: Crossed, and Confirmed.

Crossed is for the 1 hour timeframe.

Confirmed is for the 4 hour timeframe.

can anyone help me debug this thing?

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

//| MACross1TradeOnly.mq4 |

//| RobertEli |

//| |

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

// this will work on MA_04, and MA__14

#property copyright "RobertEli"

#property link ""

extern double Lots = 0.01;

extern int stoploss_value = 10; // $10

bool up = 0, //UP direction, when MA_04 is above MA_14

down = 0, //DOWN direction, when MA_14 is above MA_04

change = 0, //when the MA_04 and MA_14 cross

firsttime = true; //first time for direction_prev

int direction_curr = 0; // 1 if UP, 2 if down...

int direction_prev = 0; // this will hold previous value of direction_curr

int direction_trend= 0; // trend direction for confirmation

int cross = 0; // if direction_curr and direction_prev changes course

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

//| expert initialization function |

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

int init()

{

//----

//----

return(0);

}

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

//| expert deinitialization function |

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

int deinit()

{

//----

//----

return(0);

}

int Crossed (double line1, double line2)

{

static int last_direction = 0;

static int current_direction = 0;

if (line1 - line2 > 0)//(line1 > line2)

{

current_direction = 1; //UP

if (firsttime == true)

{

current_direction = 0;

last_direction = 1;

firsttime = false;

return (0);

}

}

if (line1 - line2 < 0)//(line1 < line2)

{

current_direction = 2; //DOWN

if (firsttime == true)

{

current_direction = 0;

last_direction = 2;

firsttime = false;

return (0);

}

}

if ((current_direction != last_direction) && (firsttime == false)) //changed direction

{

last_direction = current_direction;

return (last_direction);

}

else

{

return (last_direction);

}

}

int Confirmed (double line1, double line2)

{

int trend;

if (line1 > line2) trend = 1; // trend direction is UP

if (line1 < line2) trend = 2; // trend direction is DOWN

return (trend);

}

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

//| expert start function |

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

int start()

{

//----

int cnt,ticket, total;

//double MA_04, MA_07 , MA_14 , MA_21 , MA_28; //MOVING AVERAGES with their corresponding period

if (Bars < 100) { Print ("Bars less than 100."); return (0); }

double MA_04_1H = iMA(NULL,60,04,0,MODE_EMA,PRICE_CLOSE,0);

double MA_14_1H = iMA(NULL,60,14,0,MODE_EMA,PRICE_CLOSE,0);

double MA_04_4H = iMA(NULL,240,04,0,MODE_EMA,PRICE_CLOSE,0);

double MA_14_4H = iMA(NULL,240,14,0,MODE_EMA,PRICE_CLOSE,0);

//double stoploss = stoploss_value / (Lots * 100000);

//new code from v_1

cross = Crossed (MA_04_1H,MA_14_1H); // =1 for UP, =2 for DOWN

direction_trend = Confirmed (MA_04_4H, MA_14_4H); // =1 for UP, =2 for DOWN

total = OrdersTotal();

if (total == 0)

{

if((cross == 1) && (direction_trend == 1))

{

ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,3,/*Ask-stoploss*/0,0,"Buy at: "+ Ask,12345,0,Green);

if(ticket>0)

{

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

}

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

cross = 0;

return(0);

}

if((cross == 2) && (direction_trend == 2))

{

ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,3,/*Bid+stoploss*/0,0,"Sell at: "+ Bid,12345,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());

cross = 0;

return(0);

}

}

if (total == 1)

{

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

{

OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);

// bool no_buy_sell = ((OrderTakeProfit() 0));

if ((OrderType()==OP_BUY) && (cross == 2) )// && (no_buy_sell))

{

OrderClose(OrderTicket(),OrderLots(),Bid,3,Violet); // close position

//OrderSend(Symbol(),OP_SELL,Lots,Bid,3,Bid+stoploss,0,"Sell at: "+ Bid,12345,0,Red);

return (0);

}

else if ((OrderType()==OP_SELL) && (cross == 1) )// && (no_buy_sell))

{

OrderClose(OrderTicket(),OrderLots(),Ask,3,Violet); // close position

//OrderSend(Symbol(),OP_BUY,Lots,Ask,3,Ask-stoploss,0,"Buy at: "+ Ask,12345,0,Green);

return (0);

}

}

}

else return (0);

//----

return(0);

}

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

please ignore that previous post of mine... i trashed that idea... i'm working on this now... so please help...