Anyone want to write a simple EA?

 

I have taken a few stabs at this EA but I guess I am not a programmer. What I am trying to write is a simple EA using just the stoch indicator with a 5,3,3 setting on close/close.

A buy is generated when the stoch value hits 0% and a sell is generated when the stoch value hits 100% on the daily chart. There is no stop loss instead a hedged position is taken when price goes 50 pips against the trade. Profit is taken at the other extreme. Only 1 buy and 1 sell can be open at the same time.

For example, a buy is generated on Oct 15th when stoch value hits 0% at 1.2550. On Oct 16th the stoch again hits 0% at 1.2531 but does not take a buy because there is already one buy postion. A few days later the stoch value hits 100% and the buy is closed and a sell position opened.

Lets say that in the above example that the price goes down to 1.2485 then a hedged sell position would have been opened at 1.25 (50 pips). These 2 positions will remain until the stoch value hits 100% at say 1.2710 and the buy is closed and only the sell remains which will be closed out when the stoch value hits 0% again. If the price would have continued up to say 1.2775 then a hedged buy would have been opened at 1.2760 (50 pips)and so on and so on.

I would like to be able to choose the number of lots and mini lots as I have mini account. I would also like to be able to close a trade manually without messing up the EA. Any other additions or enhancements feel free to add.

If anyone would like to tackle this I would appreciate it. Thanks.

Kevin

 

there is an online ea builder, here's the link;

http://sufx.core.t3-ism.net/ExpertAdvisorBuilder

be sure to leave the "variables" section alone, and push complete when you're done to download it to mql4 when you're done, NOT SAVE, this just saves an html file.

one last note, the perfect thing about this builder for this particular idea is, the writer of this builder automatically put in, only one trade allowed at a time.

 

Thanks Eaglehawk for the link.

I was able to write most of the EA but still missing a few pieces of the puzzle. I still need it to open a buy or sell position as a hedge if the price goes 50 pips against the trade and close the trade at the 100 or 0 stoch value leaving the one trade open and not opening another. Below is the code so far. If anyone can help me with this I would appreciate it. Thanks.

#property copyright "Expert Advisor Builder"

#property link "http://sufx.core.t3-ism.net/ExpertAdvisorBuilder/"

extern int MagicNumber = 0;

extern bool SignalMail = False;

extern bool EachTickMode = True;

extern double Lots = 1;

extern int Slippage = 3;

extern bool StopLossMode = False;

extern int StopLoss = 30;

extern bool TakeProfitMode = False;

extern int TakeProfit = 60;

extern bool TrailingStopMode = False;

extern int TrailingStop = 30;

#define SIGNAL_NONE 0

#define SIGNAL_BUY 1

#define SIGNAL_SELL 2

#define SIGNAL_CLOSEBUY 3

#define SIGNAL_CLOSESELL 4

int BarCount;

int Current;

bool TickCheck = False;

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

//| expert initialization function |

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

int init() {

BarCount = Bars;

if (EachTickMode) Current = 0; else Current = 1;

return(0);

}

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

//| expert deinitialization function |

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

int deinit() {

return(0);

}

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

//| expert start function |

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

int start() {

int Order = SIGNAL_NONE;

int Total, Ticket;

double StopLossLevel, TakeProfitLevel;

if (EachTickMode && Bars != BarCount) TickCheck = False;

Total = OrdersTotal();

Order = SIGNAL_NONE;

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

//| Variable Begin |

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

double Buy1_1 = iStochastic(NULL, 0, 5, 3, 3, MODE_SMA, 1, MODE_MAIN, Current + 0);

double Buy1_2 = 0;

double Sell1_1 = iStochastic(NULL, 0, 5, 3, 3, MODE_SMA, 1, MODE_MAIN, Current + 0);

double Sell1_2 = 100;

double CloseBuy1_1 = iStochastic(NULL, 0, 5, 3, 3, MODE_SMA, 1, MODE_MAIN, Current + 0);

double CloseBuy1_2 = 100;

double CloseSell1_1 = iStochastic(NULL, 0, 5, 3, 3, MODE_SMA, 1, MODE_MAIN, Current + 0);

double CloseSell1_2 = 0;

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

//| Variable End |

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

//Check position

bool IsTrade = False;

for (int i = 0; i < Total; i ++) {

OrderSelect(i, SELECT_BY_POS, MODE_TRADES);

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

IsTrade = True;

if(OrderType() == OP_BUY) {

//Close

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

//| Signal Begin(Exit Buy) |

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

if (CloseBuy1_1 == CloseBuy1_2) Order = SIGNAL_CLOSEBUY;

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

//| Signal End(Exit Buy) |

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

if (Order == SIGNAL_CLOSEBUY && ((EachTickMode && !TickCheck) || (!EachTickMode && (Bars != BarCount)))) {

OrderClose(OrderTicket(), OrderLots(), Bid, Slippage, MediumSeaGreen);

if (SignalMail) SendMail("[Signal Alert]", "[" + Symbol() + "] " + DoubleToStr(Bid, Digits) + " Close Buy");

if (!EachTickMode) BarCount = Bars;

IsTrade = False;

continue;

}

//Trailing stop

if(TrailingStopMode && TrailingStop > 0) {

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

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

OrderModify(OrderTicket(), OrderOpenPrice(), Bid - Point * TrailingStop, OrderTakeProfit(), 0, MediumSeaGreen);

if (!EachTickMode) BarCount = Bars;

continue;

}

}

}

} else {

//Close

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

//| Signal Begin(Exit Sell) |

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

if (CloseSell1_1 == CloseSell1_2) Order = SIGNAL_CLOSESELL;

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

//| Signal End(Exit Sell) |

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

if (Order == SIGNAL_CLOSESELL && ((EachTickMode && !TickCheck) || (!EachTickMode && (Bars != BarCount)))) {

OrderClose(OrderTicket(), OrderLots(), Ask, Slippage, DarkOrange);

if (SignalMail) SendMail("[Signal Alert]", "[" + Symbol() + "] " + DoubleToStr(Ask, Digits) + " Close Sell");

if (!EachTickMode) BarCount = Bars;

IsTrade = False;

continue;

}

//Trailing stop

if(TrailingStopMode && TrailingStop > 0) {

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

if((OrderStopLoss() > (Ask + Point * TrailingStop)) || (OrderStopLoss() == 0)) {

OrderModify(OrderTicket(), OrderOpenPrice(), Ask + Point * TrailingStop, OrderTakeProfit(), 0, DarkOrange);

if (!EachTickMode) BarCount = Bars;

continue;

}

}

}

}

}

}

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

//| Signal Begin(Entry) |

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

if (Buy1_1 == Buy1_2) Order = SIGNAL_BUY;

if (Sell1_1 == Sell1_2) Order = SIGNAL_SELL;

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

//| Signal End |

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

//Buy

if (Order == SIGNAL_BUY && ((EachTickMode && !TickCheck) || (!EachTickMode && (Bars != BarCount)))) {

if(!IsTrade) {

//Check free margin

if (AccountFreeMargin() < (1000 * Lots)) {

Print("We have no money. Free Margin = ", AccountFreeMargin());

return(0);

}

if (StopLossMode) StopLossLevel = Ask - StopLoss * Point; else StopLossLevel = 0.0;

if (TakeProfitMode) TakeProfitLevel = Ask + TakeProfit * Point; else TakeProfitLevel = 0.0;

Ticket = OrderSend(Symbol(), OP_BUY, Lots, Ask, Slippage, StopLossLevel, TakeProfitLevel, "Buy(#" + MagicNumber + ")", MagicNumber, 0, DodgerBlue);

if(Ticket > 0) {

if (OrderSelect(Ticket, SELECT_BY_TICKET, MODE_TRADES)) {

Print("BUY order opened : ", OrderOpenPrice());

if (SignalMail) SendMail("[Signal Alert]", "[" + Symbol() + "] " + DoubleToStr(Ask, Digits) + " Open Buy");

} else {

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

}

}

if (EachTickMode) TickCheck = True;

if (!EachTickMode) BarCount = Bars;

return(0);

}

}

//Sell

if (Order == SIGNAL_SELL && ((EachTickMode && !TickCheck) || (!EachTickMode && (Bars != BarCount)))) {

if(!IsTrade) {

//Check free margin

if (AccountFreeMargin() < (1000 * Lots)) {

Print("We have no money. Free Margin = ", AccountFreeMargin());

return(0);

}

if (StopLossMode) StopLossLevel = Bid + StopLoss * Point; else StopLossLevel = 0.0;

if (TakeProfitMode) TakeProfitLevel = Bid - TakeProfit * Point; else TakeProfitLevel = 0.0;

Ticket = OrderSend(Symbol(), OP_SELL, Lots, Bid, Slippage, StopLossLevel, TakeProfitLevel, "Sell(#" + MagicNumber + ")", MagicNumber, 0, DeepPink);

if(Ticket > 0) {

if (OrderSelect(Ticket, SELECT_BY_TICKET, MODE_TRADES)) {

Print("SELL order opened : ", OrderOpenPrice());

if (SignalMail) SendMail("[Signal Alert]", "[" + Symbol() + "] " + DoubleToStr(Bid, Digits) + " Open Sell");

} else {

Print("Error opening SELL order : ", GetLastError());

}

}

if (EachTickMode) TickCheck = True;

if (!EachTickMode) BarCount = Bars;

return(0);

}

}

if (!EachTickMode) BarCount = Bars;

return(0);

}

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

 
Kaper:
Thanks Eaglehawk for the link.

I was able to write most of the EA but still missing a few pieces of the puzzle. I still need it to open a buy or sell position as a hedge if the price goes 50 pips against the trade and close the trade at the 100 or 0 stoch value leaving the one trade open and not opening another. Below is the code so far. If anyone can help me with this I would appreciate it. Thanks.

#property copyright "Expert Advisor Builder"

#property link "http://sufx.core.t3-ism.net/ExpertAdvisorBuilder/"

extern int MagicNumber = 0;

extern bool SignalMail = False;

extern bool EachTickMode = True;

extern double Lots = 1;

extern int Slippage = 3;

extern bool StopLossMode = False;

extern int StopLoss = 30;

extern bool TakeProfitMode = False;

extern int TakeProfit = 60;

extern bool TrailingStopMode = False;

extern int TrailingStop = 30;

#define SIGNAL_NONE 0

#define SIGNAL_BUY 1

#define SIGNAL_SELL 2

#define SIGNAL_CLOSEBUY 3

#define SIGNAL_CLOSESELL 4

int BarCount;

int Current;

bool TickCheck = False;

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

//| expert initialization function |

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

int init() {

BarCount = Bars;

if (EachTickMode) Current = 0; else Current = 1;

return(0);

}

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

//| expert deinitialization function |

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

int deinit() {

return(0);

}

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

//| expert start function |

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

int start() {

int Order = SIGNAL_NONE;

int Total, Ticket;

double StopLossLevel, TakeProfitLevel;

if (EachTickMode && Bars != BarCount) TickCheck = False;

Total = OrdersTotal();

Order = SIGNAL_NONE;

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

//| Variable Begin |

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

double Buy1_1 = iStochastic(NULL, 0, 5, 3, 3, MODE_SMA, 1, MODE_MAIN, Current + 0);

double Buy1_2 = 0;

double Sell1_1 = iStochastic(NULL, 0, 5, 3, 3, MODE_SMA, 1, MODE_MAIN, Current + 0);

double Sell1_2 = 100;

double CloseBuy1_1 = iStochastic(NULL, 0, 5, 3, 3, MODE_SMA, 1, MODE_MAIN, Current + 0);

double CloseBuy1_2 = 100;

double CloseSell1_1 = iStochastic(NULL, 0, 5, 3, 3, MODE_SMA, 1, MODE_MAIN, Current + 0);

double CloseSell1_2 = 0;

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

//| Variable End |

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

//Check position

bool IsTrade = False;

for (int i = 0; i < Total; i ++) {

OrderSelect(i, SELECT_BY_POS, MODE_TRADES);

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

IsTrade = True;

if(OrderType() == OP_BUY) {

//Close

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

//| Signal Begin(Exit Buy) |

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

if (CloseBuy1_1 == CloseBuy1_2) Order = SIGNAL_CLOSEBUY;

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

//| Signal End(Exit Buy) |

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

if (Order == SIGNAL_CLOSEBUY && ((EachTickMode && !TickCheck) || (!EachTickMode && (Bars != BarCount)))) {

OrderClose(OrderTicket(), OrderLots(), Bid, Slippage, MediumSeaGreen);

if (SignalMail) SendMail("[Signal Alert]", "[" + Symbol() + "] " + DoubleToStr(Bid, Digits) + " Close Buy");

if (!EachTickMode) BarCount = Bars;

IsTrade = False;

continue;

}

//Trailing stop

if(TrailingStopMode && TrailingStop > 0) {

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

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

OrderModify(OrderTicket(), OrderOpenPrice(), Bid - Point * TrailingStop, OrderTakeProfit(), 0, MediumSeaGreen);

if (!EachTickMode) BarCount = Bars;

continue;

}

}

}

} else {

//Close

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

//| Signal Begin(Exit Sell) |

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

if (CloseSell1_1 == CloseSell1_2) Order = SIGNAL_CLOSESELL;

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

//| Signal End(Exit Sell) |

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

if (Order == SIGNAL_CLOSESELL && ((EachTickMode && !TickCheck) || (!EachTickMode && (Bars != BarCount)))) {

OrderClose(OrderTicket(), OrderLots(), Ask, Slippage, DarkOrange);

if (SignalMail) SendMail("[Signal Alert]", "[" + Symbol() + "] " + DoubleToStr(Ask, Digits) + " Close Sell");

if (!EachTickMode) BarCount = Bars;

IsTrade = False;

continue;

}

//Trailing stop

if(TrailingStopMode && TrailingStop > 0) {

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

if((OrderStopLoss() > (Ask + Point * TrailingStop)) || (OrderStopLoss() == 0)) {

OrderModify(OrderTicket(), OrderOpenPrice(), Ask + Point * TrailingStop, OrderTakeProfit(), 0, DarkOrange);

if (!EachTickMode) BarCount = Bars;

continue;

}

}

}

}

}

}

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

//| Signal Begin(Entry) |

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

if (Buy1_1 == Buy1_2) Order = SIGNAL_BUY;

if (Sell1_1 == Sell1_2) Order = SIGNAL_SELL;

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

//| Signal End |

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

//Buy

if (Order == SIGNAL_BUY && ((EachTickMode && !TickCheck) || (!EachTickMode && (Bars != BarCount)))) {

if(!IsTrade) {

//Check free margin

if (AccountFreeMargin() < (1000 * Lots)) {

Print("We have no money. Free Margin = ", AccountFreeMargin());

return(0);

}

if (StopLossMode) StopLossLevel = Ask - StopLoss * Point; else StopLossLevel = 0.0;

if (TakeProfitMode) TakeProfitLevel = Ask + TakeProfit * Point; else TakeProfitLevel = 0.0;

Ticket = OrderSend(Symbol(), OP_BUY, Lots, Ask, Slippage, StopLossLevel, TakeProfitLevel, "Buy(#" + MagicNumber + ")", MagicNumber, 0, DodgerBlue);

if(Ticket > 0) {

if (OrderSelect(Ticket, SELECT_BY_TICKET, MODE_TRADES)) {

Print("BUY order opened : ", OrderOpenPrice());

if (SignalMail) SendMail("[Signal Alert]", "[" + Symbol() + "] " + DoubleToStr(Ask, Digits) + " Open Buy");

} else {

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

}

}

if (EachTickMode) TickCheck = True;

if (!EachTickMode) BarCount = Bars;

return(0);

}

}

//Sell

if (Order == SIGNAL_SELL && ((EachTickMode && !TickCheck) || (!EachTickMode && (Bars != BarCount)))) {

if(!IsTrade) {

//Check free margin

if (AccountFreeMargin() < (1000 * Lots)) {

Print("We have no money. Free Margin = ", AccountFreeMargin());

return(0);

}

if (StopLossMode) StopLossLevel = Bid + StopLoss * Point; else StopLossLevel = 0.0;

if (TakeProfitMode) TakeProfitLevel = Bid - TakeProfit * Point; else TakeProfitLevel = 0.0;

Ticket = OrderSend(Symbol(), OP_SELL, Lots, Bid, Slippage, StopLossLevel, TakeProfitLevel, "Sell(#" + MagicNumber + ")", MagicNumber, 0, DeepPink);

if(Ticket > 0) {

if (OrderSelect(Ticket, SELECT_BY_TICKET, MODE_TRADES)) {

Print("SELL order opened : ", OrderOpenPrice());

if (SignalMail) SendMail("[Signal Alert]", "[" + Symbol() + "] " + DoubleToStr(Bid, Digits) + " Open Sell");

} else {

Print("Error opening SELL order : ", GetLastError());

}

}

if (EachTickMode) TickCheck = True;

if (!EachTickMode) BarCount = Bars;

return(0);

}

}

if (!EachTickMode) BarCount = Bars;

return(0);

}

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

i'm not sure what you mean exactly by "hedge" position, but from your description, it sounds like a trailing stop. could you please be a little more specific?

maybe include a guide picture

 

Eaglehawk I want to be able to take a trade in the opposite direction of an open trade instead of using a stoploss. If a buy goes south by 50 pips, I want to take a sell in addition to the buy 50 pips lower. Only 1 or 2 trades can be active at once. A buy or a sell or a buy and a sell. They say a picture is worth a 1000 words so if you look at the attached chart I will explain what I want more clearly. $Right now I want to get this working with 0,100 but will probably tweak abit and take profit at 5 and 95. I just want to get this working for now.

Blue Box - You will notice that on 7/31 stoch on USDJPY daily chart reached 0% at 114.26 signaling a buy. On 08/31 stoch also reached 0% once again but no buy signal because already a buy open. The low on 8/4 reached 113.96 so a sell in the would not be open as it wasn't 50 pips lower then 114.26. So we only have 1 position open - a buy at 114.26.

Yellow Box - On 8/9 stoch hits 100% at 115.51 closing the buy position and opening a sell postion at 115.51. Afterwards price continues rising. A buy position is also now open at 116.01. We now have 2 orders open, a sell at 115.51 and a buy at 116.01.

Price continues to go in an uptrend. On 8/28 stoch almost hits 100% but comes up short at 98.5. On 9/1 stoch goes down to 14.24. Buy an sell positions are still open.

Green Box - On 9/13 stoch hits 100% again at 117.94. No sell position because sell postion already open. However, we can take profit on our buy position from 116.01. So now we only have the 115.51 sell postion open.

By 9/22 price falls to 116.08 but stoch only hits 4.24 not 0% so sell postion can not be closed.

Magenta Box - Stoch once again hits 100% but we already have a sell position so no new sell position is open.

On 10/6 price goes beyond 119.09 generating a buy a 118.44. So now we agian have 2 postions open. 115.51 sell and 118.44 buy.

White Box - On 10/10 stoch once again hits 100% at 119.70. Buy is closed and only sell position at 115.51 remains open.

On 10/19 stoch goes down to 4.2 at 118.10, not enough to close sell. Postion currently remains open with a current price of 117.57. Once stoch hits 0% this postion will be closed.

Files:
usdjpydaily.gif  21 kb
 

here's what i would do about the one sell and one buy allowed at once

extern int MagicNumber = 0;

extern bool SignalMail = False;

extern bool EachTickMode = True;

extern double Lots = 1;

extern int Slippage = 3;

extern bool StopLossMode = False;

extern int StopLoss = 30;

extern bool TakeProfitMode = False;

extern int TakeProfit = 60;

extern bool TrailingStopMode = False;

extern int TrailingStop = 30;

#define SIGNAL_NONE 0

#define SIGNAL_BUY 1

#define SIGNAL_SELL 2

#define SIGNAL_CLOSEBUY 3

#define SIGNAL_CLOSESELL 4

int BarCount;

int Current;

bool TickCheck = False;

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

//| expert initialization function |

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

int init() {

BarCount = Bars;

if (EachTickMode) Current = 0; else Current = 1;

return(0);

}

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

//| expert deinitialization function |

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

int deinit() {

return(0);

}

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

//| expert start function |

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

int start() {

int Order = SIGNAL_NONE;

int Total, Ticket;

double StopLossLevel, TakeProfitLevel;

if (EachTickMode && Bars != BarCount) TickCheck = False;

Total = OrdersTotal();

Order = SIGNAL_NONE;

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

//| Variable Begin |

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

double Buy1_1 = iStochastic(NULL, 0, 5, 3, 3, MODE_SMA, 1, MODE_MAIN, Current + 0);

double Buy1_2 = 0;

double Sell1_1 = iStochastic(NULL, 0, 5, 3, 3, MODE_SMA, 1, MODE_MAIN, Current + 0);

double Sell1_2 = 100;

double CloseBuy1_1 = iStochastic(NULL, 0, 5, 3, 3, MODE_SMA, 1, MODE_MAIN, Current + 0);

double CloseBuy1_2 = 100;

double CloseSell1_1 = iStochastic(NULL, 0, 5, 3, 3, MODE_SMA, 1, MODE_MAIN, Current + 0);

double CloseSell1_2 = 0;

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

//| Variable End |

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

//Check position

bool IsTrade = False;

for (int i = 0; i < Total; i ++) {

OrderSelect(i, SELECT_BY_POS, MODE_TRADES);

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

IsTrade = True;

if(OrderType() == OP_BUY) {

//Close

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

//| Signal Begin(Exit Buy) |

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

if (CloseBuy1_1 == CloseBuy1_2) Order = SIGNAL_CLOSEBUY;

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

//| Signal End(Exit Buy) |

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

if (Order == SIGNAL_CLOSEBUY && ((EachTickMode && !TickCheck) || (!EachTickMode && (Bars != BarCount)))) {

OrderClose(OrderTicket(), OrderLots(), Bid, Slippage, MediumSeaGreen);

if (SignalMail) SendMail("[Signal Alert]", "[" + Symbol() + "] " + DoubleToStr(Bid, Digits) + " Close Buy");

if (!EachTickMode) BarCount = Bars;

IsTrade = False;

continue;

}

//Trailing stop

if(TrailingStopMode && TrailingStop > 0) {

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

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

OrderModify(OrderTicket(), OrderOpenPrice(), Bid - Point * TrailingStop, OrderTakeProfit(), 0, MediumSeaGreen);

if (!EachTickMode) BarCount = Bars;

continue;

}

}

}

} else {

//Close

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

//| Signal Begin(Exit Sell) |

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

if (CloseSell1_1 == CloseSell1_2) Order = SIGNAL_CLOSESELL;

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

//| Signal End(Exit Sell) |

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

if (Order == SIGNAL_CLOSESELL && ((EachTickMode && !TickCheck) || (!EachTickMode && (Bars != BarCount)))) {

OrderClose(OrderTicket(), OrderLots(), Ask, Slippage, DarkOrange);

if (SignalMail) SendMail("[Signal Alert]", "[" + Symbol() + "] " + DoubleToStr(Ask, Digits) + " Close Sell");

if (!EachTickMode) BarCount = Bars;

IsTrade = False;

continue;

}

//Trailing stop

if(TrailingStopMode && TrailingStop > 0) {

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

if((OrderStopLoss() > (Ask + Point * TrailingStop)) || (OrderStopLoss() == 0)) {

OrderModify(OrderTicket(), OrderOpenPrice(), Ask + Point * TrailingStop, OrderTakeProfit(), 0, DarkOrange);

if (!EachTickMode) BarCount = Bars;

continue;

}

}

}

}

}

}

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

//| Signal Begin(Entry) |

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

if (Buy1_1 == Buy1_2) Order = SIGNAL_BUY;

if (Sell1_1 == Sell1_2) Order = SIGNAL_SELL;

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

//| Signal End |

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

//Buy

if (Order == SIGNAL_BUY && (OrdersTotal() < 2 && OrderType==OP_SELL) && ((EachTickMode && !TickCheck) || (!EachTickMode && (Bars != BarCount)))) {

if(!IsTrade) {

//Check free margin

if (AccountFreeMargin() < (1000 * Lots)) {

Print("We have no money. Free Margin = ", AccountFreeMargin());

return(0);

}

if (StopLossMode) StopLossLevel = Ask - StopLoss * Point; else StopLossLevel = 0.0;

if (TakeProfitMode) TakeProfitLevel = Ask + TakeProfit * Point; else TakeProfitLevel = 0.0;

Ticket = OrderSend(Symbol(), OP_BUY, Lots, Ask, Slippage, StopLossLevel, TakeProfitLevel, "Buy(#" + MagicNumber + ")", MagicNumber, 0, DodgerBlue);

if(Ticket > 0) {

if (OrderSelect(Ticket, SELECT_BY_TICKET, MODE_TRADES)) {

Print("BUY order opened : ", OrderOpenPrice());

if (SignalMail) SendMail("[Signal Alert]", "[" + Symbol() + "] " + DoubleToStr(Ask, Digits) + " Open Buy");

} else {

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

}

}

if (EachTickMode) TickCheck = True;

if (!EachTickMode) BarCount = Bars;

return(0);

}

}

//Sell

if (Order == SIGNAL_SELL && (OrdersTotal() < 2 && OrderType==OP_SELL) && ((EachTickMode && !TickCheck) || (!EachTickMode && (Bars != BarCount)))) {

if(!IsTrade) {

//Check free margin

if (AccountFreeMargin() < (1000 * Lots)) {

Print("We have no money. Free Margin = ", AccountFreeMargin());

return(0);

}

if (StopLossMode) StopLossLevel = Bid + StopLoss * Point; else StopLossLevel = 0.0;

if (TakeProfitMode) TakeProfitLevel = Bid - TakeProfit * Point; else TakeProfitLevel = 0.0;

Ticket = OrderSend(Symbol(), OP_SELL, Lots, Bid, Slippage, StopLossLevel, TakeProfitLevel, "Sell(#" + MagicNumber + ")", MagicNumber, 0, DeepPink);

if(Ticket > 0) {

if (OrderSelect(Ticket, SELECT_BY_TICKET, MODE_TRADES)) {

Print("SELL order opened : ", OrderOpenPrice());

if (SignalMail) SendMail("[Signal Alert]", "[" + Symbol() + "] " + DoubleToStr(Bid, Digits) + " Open Sell");

} else {

Print("Error opening SELL order : ", GetLastError());

}

}

if (EachTickMode) TickCheck = True;

if (!EachTickMode) BarCount = Bars;

return(0);

}

}

if (!EachTickMode) BarCount = Bars;

return(0);

}

//+------------------------------------------------------------------+[/php]

theres a little bit of a trick towards the trailing stop, what you have to do is use the logics you already have,

double Buy1_1 = iStochastic(NULL, 0, 5, 3, 3, MODE_SMA, 1, MODE_MAIN, Current + 0);

double Buy1_2 = 0;

double Sell1_1 = iStochastic(NULL, 0, 5, 3, 3, MODE_SMA, 1, MODE_MAIN, Current + 0);

double Sell1_2 = 100;

double CloseBuy1_1 = iStochastic(NULL, 0, 5, 3, 3, MODE_SMA, 1, MODE_MAIN, Current + 0);

double CloseBuy1_2 = 100;

double CloseSell1_1 = iStochastic(NULL, 0, 5, 3, 3, MODE_SMA, 1, MODE_MAIN, Current + 0);

double CloseSell1_2 = 0;

and copy their names to how you want them arrange and add at the end

[php]

{

TrailingStopMode = True;

}
 

scan for open orders and check OrderOpenPrice-Ask or Bid 50 depending on if its a buy or sell?

void hedgetest() {

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

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

if (OrderSymbol()==Symbol() && OrderMagicNumber()==MAGIC) {

if (OrderType()==OP_SELL) {

if(Hedgeactivated==false && ExteralEnablehedge && OrderOpenPrice()-Ask>Point*50){

Buy;

Hedgeactivated=true;

}}}}}}

and then turn your flag back off when it closes?

just guessing and this is probably wrong

Reason: