How to code? - page 186

 

Okay, on the original question:

#property indicator_chart_window

#property indicator_buffers 1

#property indicator_color1 Magenta

extern int MAPeriod=200;

extern double iController = 1.386;

//---- buffers

//double Memory[200];

double ExtMapBuffer1[];

int limit = 200;

//---- variables

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

//| Custom indicator initialization function |

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

int init()

{

IndicatorBuffers(1);

//---- drawing settings

SetIndexBuffer(0,ExtMapBuffer1);

SetIndexStyle(0,DRAW_LINE,STYLE_SOLID,2);

IndicatorShortName( "Antaria_Rising");

//---- initialization done

return(0);

}

int start()

{

int limit;

//ExtMapBuffer1[1] = ExtMapBuffer1[0];

//ExtMapBuffer1[0] = iController;

for(int i=(0); i<=(limit-1); i++)

{

ExtMapBuffer1 = ExtMapBuffer1;

}

ExtMapBuffer1[0] = iController;

return(0);

}[/CODE]

Above is the code for the indicator. Simply, it draws a line that should be updatable via the EA using the intiially mentioned code:

[CODE]temp = iCustom(NULL, 0, "AntariaRising", 200, 1.2, 0, 0); //temp not used. Function returns a double

I guess I want to replace the final piece of code that actually allows me to update into the incicator object the variable that its tracking on, so that I can later use it as a stop-out line. The remaining code is irrelevant however. When I run the above function i expect antaria to move position from 1.39 or whatever it starts on, to 1.2. I can't remember if I use the 200 variable. I think I've chopped out my header and footer comments, but the entire slave indicator is up there, which is AntariaRising meantioned in immediately above code

this was the only function I could find that talked to indicator objects, and I use it to read off values in other areas.

 

About your indicator code. It's wrong.

Var. limit equal zero ever.

Take off the string:

int limit;

from the start().

 

Help me with this code pls

i want someone to help me with this code it open trade when i want it but close it automatically so i want it to trade with stop loss not closing when not crossing again this is the code

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

//| JR300.mq4 |

//| onelove |

//| Forex Forum | Forex Tsd | Metatrader Forum |

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

#property copyright "oneLove"

#property link "https://www.forex-tsd.com"

//---- input parameters

extern double TakeProfit=250.0;

extern double Lots=0.1;

extern double TrailingStop=35.0;

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

//| 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)current_direction = 1; //up

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

if(current_direction != last_direction) //changed

{

last_direction = current_direction;

return (last_direction);

}

else

{

return (0);

}

}

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

//| expert start function |

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

int start()

{

//----

int cnt, ticket, total;

double shortEma, longEma;

if(Bars<100)

{

Print("bars less than 100");

return(0);

}

if(TakeProfit<10)

{

Print("TakeProfit less than 10");

return(0); // check TakeProfit

}

shortEma = iMA(NULL,0,1,0,MODE_EMA,PRICE_CLOSE,0);

longEma = iMA(NULL,0,200,0,MODE_SMA,PRICE_CLOSE,0);

int isCrossed = Crossed (shortEma,longEma);

total = OrdersTotal();

if(total < 1)

{

if(isCrossed == 1)

{

ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,3,0,Ask+TakeProfit*Point,

"My EA",12345,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);

}

if(isCrossed == 2)

{

ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,3,0,

Bid-TakeProfit*Point,"My EA",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());

return(0);

}

return(0);

}

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

{

OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);

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

{

if(OrderType()==OP_BUY) // long position is opened

{

// should it be closed?

if(isCrossed == 2)

{

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

// close position

return(0); // exit

}

// check for trailing stop

if(TrailingStop>0)

{

if(Bid-OrderOpenPrice()>Point*TrailingStop)

{

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

{

OrderModify(OrderTicket(),OrderOpenPrice(),Bid-

Point*TrailingStop,OrderTakeProfit(),0,Green);

return(0);

}

}

}

}

else // go to short position

{

// should it be closed?

if(isCrossed == 1)

{

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

// close position

return(0); // exit

}

// check for trailing stop

if(TrailingStop>0)

{

if((OrderOpenPrice()-Ask)>(Point*TrailingStop))

{

if((OrderStopLoss()>(Ask+Point*TrailingStop)) ||

(OrderStopLoss()==0))

{

OrderModify(OrderTicket(),OrderOpenPrice(),Ask+Point*TrailingStop,

OrderTakeProfit(),0,Red);

return(0);

}

}

}

}

}

}

return(0);

}

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

 

Try this:

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

//| JR300.mq4 |

//| onelove |

//| Forex Forum | Forex Tsd | Metatrader Forum |

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

#property copyright "oneLove"

#property link "https://www.forex-tsd.com"

//---- input parameters

extern double TakeProfit=250.0;

extern double StopLoss=100.0;

extern double Lots=0.1;

extern double TrailingStop=35.0;

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

//| 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)current_direction = 1; //up

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

if(current_direction != last_direction) //changed

{

last_direction = current_direction;

return (last_direction);

}

else

{

return (0);

}

}

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

//| expert start function |

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

int start()

{

//----

int cnt, ticket, total;

double shortEma, longEma;

if(Bars<100)

{

Print("bars less than 100");

return(0);

}

if(TakeProfit<10)

{

Print("TakeProfit less than 10");

return(0); // check TakeProfit

}

shortEma = iMA(NULL,0,1,0,MODE_EMA,PRICE_CLOSE,0);

longEma = iMA(NULL,0,200,0,MODE_SMA,PRICE_CLOSE,0);

int isCrossed = Crossed (shortEma,longEma);

total = OrdersTotal();

if(total < 1)

{

if(isCrossed == 1)

{

ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,3,Ask-StopLoss*Point,Ask+ TakeProfit*Point,

"My EA",12345,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);

}

if(isCrossed == 2)

{

ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,3,Bid+StopLoss*Point,

Bid-TakeProfit*Point,"My EA",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());

return(0);

}

return(0);

}

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

{

OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);

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

{

if(OrderType()==OP_BUY) // long position is opened

{

// should it be closed?

//if(isCrossed == 2)

//{

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

// close position

//return(0); // exit

//}

// check for trailing stop

if(TrailingStop>0)

{

if(Bid-OrderOpenPrice()>Point*TrailingStop)

{

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

{

OrderModify(OrderTicket(),OrderOpenPrice(),Bid-

Point*TrailingStop,OrderTakeProfit(),0,Green);

return(0);

}

}

}

}

else // go to short position

{

// should it be closed?

//if(isCrossed == 1)

//{

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

// close position

//return(0); // exit

//}

// check for trailing stop

if(TrailingStop>0)

{

if((OrderOpenPrice()-Ask)>(Point*TrailingStop))

{

if((OrderStopLoss()>(Ask+Point*TrailingStop)) ||

(OrderStopLoss()==0))

{

OrderModify(OrderTicket(),OrderOpenPrice(),Ask+Point*TrailingStop,

OrderTakeProfit(),0,Red);

return(0);

}

}

}

}

}

}

return(0);

}

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

Thanks

Roger09:
Try this:
//+------------------------------------------------------------------+

//| JR300.mq4 |

//| onelove |

//| Forex Forum | Forex Tsd | Metatrader Forum |

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

#property copyright "oneLove"

#property link "https://www.forex-tsd.com"

//---- input parameters

extern double TakeProfit=250.0;

extern double StopLoss=100.0;

extern double Lots=0.1;

extern double TrailingStop=35.0;

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

//| 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)current_direction = 1; //up

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

if(current_direction != last_direction) //changed

{

last_direction = current_direction;

return (last_direction);

}

else

{

return (0);

}

}

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

//| expert start function |

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

int start()

{

//----

int cnt, ticket, total;

double shortEma, longEma;

if(Bars<100)

{

Print("bars less than 100");

return(0);

}

if(TakeProfit<10)

{

Print("TakeProfit less than 10");

return(0); // check TakeProfit

}

shortEma = iMA(NULL,0,1,0,MODE_EMA,PRICE_CLOSE,0);

longEma = iMA(NULL,0,200,0,MODE_SMA,PRICE_CLOSE,0);

int isCrossed = Crossed (shortEma,longEma);

total = OrdersTotal();

if(total < 1)

{

if(isCrossed == 1)

{

ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,3,Ask-StopLoss*Point,Ask+ TakeProfit*Point,

"My EA",12345,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);

}

if(isCrossed == 2)

{

ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,3,Bid+StopLoss*Point,

Bid-TakeProfit*Point,"My EA",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());

return(0);

}

return(0);

}

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

{

OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);

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

{

if(OrderType()==OP_BUY) // long position is opened

{

// should it be closed?

//if(isCrossed == 2)

//{

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

// close position

//return(0); // exit

//}

// check for trailing stop

if(TrailingStop>0)

{

if(Bid-OrderOpenPrice()>Point*TrailingStop)

{

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

{

OrderModify(OrderTicket(),OrderOpenPrice(),Bid-

Point*TrailingStop,OrderTakeProfit(),0,Green);

return(0);

}

}

}

}

else // go to short position

{

// should it be closed?

//if(isCrossed == 1)

//{

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

// close position

//return(0); // exit

//}

// check for trailing stop

if(TrailingStop>0)

{

if((OrderOpenPrice()-Ask)>(Point*TrailingStop))

{

if((OrderStopLoss()>(Ask+Point*TrailingStop)) ||

(OrderStopLoss()==0))

{

OrderModify(OrderTicket(),OrderOpenPrice(),Ask+Point*TrailingStop,

OrderTakeProfit(),0,Red);

return(0);

}

}

}

}

}

}

return(0);

}

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

thanks roger09 God Bless you

 

Thanks Roger, I've removed that second limit declaration from my start function. I'm surprised the compiler was allowing that one, but it hadn't impacted my program noticably.

Still I am unsure how to alter the running instance of that indcator from my EA. A method would be much appreciated, or at least confirmation that the API does not currently allow me to pass information to running indicators.

Thanks.

 
yingli:
I start this tread for getting some help here.I am learnig to code for the indicator attached .however it did not work out.Could anybody help? It is a very reliable indicator and if somebody could make a EA for it .it is almost a holy grail.tia.

Basic priciple is:

1.open Sell position when red arrow appear,

2open buy position when white arrow appear and automaticly close all th esell position/positions.

3.apply for any timeframe .

4.no stop loss,

5.add order function.

Loking forward to response!

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

//| IINWMARROWS.mq4 |

//| Based on EMA_CROSS.mq4 |

//| Copyright ?2006, MetaQuotes Software Corp. |

//| Forex Trading Software: Forex Trading Platform MetaTrader 4 |

//| Last little modified by Iin Zulkarnain |

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

#property copyright "Copyright ?2006, MetaQuotes Software Corp."

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

//----

#property indicator_chart_window

#property indicator_buffers 2

#property indicator_color1 White

#property indicator_color2 Red

#property indicator_width1 2

#property indicator_width2 2

//----

double CrossUp[];

double CrossDown[];

extern int FasterMode=3; //0=sma, 1=ema, 2=smma, 3=lwma

extern int FasterMA= 3;

extern int SlowerMode=3; //0=sma, 1=ema, 2=smma, 3=lwma

extern int SlowerMA= 3;

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

//| Custom indicator initialization function |

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

int init()

{

//---- indicators

SetIndexStyle(0, DRAW_ARROW, EMPTY);

SetIndexArrow(0, 233);

SetIndexBuffer(0, CrossUp);

SetIndexStyle(1, DRAW_ARROW, EMPTY);

SetIndexArrow(1, 234);

SetIndexBuffer(1, CrossDown);

//----

return(0);

}

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

//| Custom indicator deinitialization function |

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

int deinit()

{

//----

//----

return(0);

}

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

//| Custom indicator iteration function |

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

int start()

{

int limit, i, counter;

double fasterMAnow, slowerMAnow, fasterMAprevious, slowerMAprevious, fasterMAafter, slowerMAafter;

double Range, AvgRange;

int counted_bars=IndicatorCounted();

//---- check for possible errors

if(counted_bars<0) return(-1);

//---- last counted bar will be recounted

if(counted_bars>0) counted_bars--;

//----

limit=Bars-counted_bars;

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

{

counter=i;

Range=0;

AvgRange=0;

for(counter=i ;counter<=i+9;counter++)

{

AvgRange=AvgRange+MathAbs(High[counter]-Low[counter]);

}

Range=AvgRange/10;

fasterMAnow=iMA(NULL, 0, FasterMA, 0, FasterMode, PRICE_CLOSE, i);

fasterMAprevious=iMA(NULL, 0, FasterMA, 0, FasterMode, PRICE_CLOSE, i+1);

fasterMAafter=iMA(NULL, 0, FasterMA, 0, FasterMode, PRICE_CLOSE, i-1);

//----

slowerMAnow=iMA(NULL, 0, SlowerMA, 0, SlowerMode, PRICE_OPEN, i);

slowerMAprevious=iMA(NULL, 0, SlowerMA, 0, SlowerMode, PRICE_OPEN, i+1);

slowerMAafter=iMA(NULL, 0, SlowerMA, 0, SlowerMode, PRICE_OPEN, i-1);

if ((fasterMAnow > slowerMAnow) && (fasterMAprevious slowerMAafter))

{

CrossUp=Low - Range*0.5;

}

else if ((fasterMAnow slowerMAprevious) && (fasterMAafter < slowerMAafter))

{

CrossDown=High + Range*0.5;

}

}

return(0);

}

//+------------------------------------------------------------------+this thread for learning how to code properly,I am learning to code for

Could anyone help code an alert for this indicator, its really cool .

 

George's Oscar Calculator - Can anyone code an indicator for it?

Hi Folks,

I don't know if anyone is familiar with Forex Made-EZ by George Smith.

I'm currently reviewing his work and wondered if anyone has coded an indicator for the Oscar oscillator he uses?

The formula is

let A = the highest high of the last eight bars (including this one)

let B = the lowest low of the past eight bars (including this one)

let C = current bars closing price

let X = the previous oscillator figure (Oscar)

Today's "rough" oscillator equals (C-B) divided by (A-B) times 100.

Next we "smooth" our rough number (let's call it Y) like this:

Final oscillator number = ((X divided by 3) times 2), plus (Y divided by 3).

If anyone has either coded this or can code this it would be highly appreciated.

Regards Steve

 

Inverted Trailing Stop

Hi,

I want some help with an idea.

I'm using trailing stops on my EAs (3 step trailing), but I want to do the inverted thing to protect losses.

I will try to explain:

I open an order, with a initial stop loss.

If the trade go 15 pips in loss direction and come back 10 pips, close the trade (first step).

If the trade go 30 pips in loss direction and come back 15 pips, close the trade (second step).

Something like this.

Thanks for the help

 
dedreko:
Hi,

I want some help with an idea.

I'm using trailing stops on my EAs (3 step trailing), but I want to do the inverted thing to protect losses.

I will try to explain:

I open an order, with a initial stop loss.

If the trade go 15 pips in loss direction and come back 10 pips, close the trade (first step).

If the trade go 30 pips in loss direction and come back 15 pips, close the trade (second step).

Something like this.

Thanks for the help

This is something new to me.

So, the losing trade closing always need to wait until it bounce back right?

How about if it doen't bounceback?

Will we close at the intial SL?

Reason: