That's great. There's a lot to learn, and this is the place to do it.
Glad you found it.
Ron(ForexMT4)
bluey39to wrote:
Hi Ron,
Yes, I think it was not having Max as a Global variable. I'm still new to programming (1 year on this), so some things still to learn. Obvious now I see it.
Thanks for help.
--- In MetaTrader_Experts_and_Indicators@ , Ron Thompson <ron@...> wrote:yahoogroups. com
>
>
> Yes, the trailing stop code does work.
>
> The use of an array (and all the pieces of it) allow you to manage
> several different orders.
>
> If the account is ever only going to have one order open at a time, then
> single variables will work ok.
>
> Max will always be == Bid because you did:
>
> double Max = Bid;
>
> which sets it the same. You'll need to declare MAX as a global so
> you're not declaring it every tick, and reset it to Bid when the order
> closes.
>
> ... does that make sense?
>
> Ron(ForexMT4)
>
>
> bluey39to wrote:
> >
> > Thanks Ron,
> > Does your hidden trailing stop part work?
> > It's not far from my code, just that you're using an array.
> >
> > At its simplest, the code is trying to do this:
> >
> > double Max = Bid;
> >
> > if (Bid > Max) // if Bid goes up
> > {
> > Max = Bid; // we want to store it as Max, and use it later
> > }
> >
> > However, Max appears to always be the same as Bid, even if Bid goes
> > below the Max, Max copies it. Do you know if an array the only way
> > around this?
> >
> > --- In MetaTrader_Experts_and_Indicators@ yahoogroups. com
> > <mailto:MetaTrader_Experts_and_ Indicators% 40yahoogroups. com>, Ron
> > Thompson <ron@> wrote:
> > >
> > >
> > > Here's a copy of code I'm currently working on that hides almost all of
> > > the order process.
> > >
> > > It has a structure for order management, and procedures you can call to
> > > replace the standard order functions.
> > >
> > > It is _*NOT*_ complete, as I still have to write the load, save and
> > sync
> > > functions. I really hate not having structures, which would make the
> > > process so simple.
> > >
> > > Ron(ForexMT4)
> > >
> > >
> > > bluey39to wrote:
> > > >
> > > > Thanks for reply Robert.
> > > > I'm talking about a hidden trailing stop that doesn't use the
> > > > "ModifyOrder" function. I'm not worried about a disconnect, as I can
> > > > easily place a firm stop when I open the trade.
> > > > This is a hidden stop, that if price moves below (on a buy), it will
> > > > trigger a sell. Obviously that part is easy to be hidden, but to have
> > > > it trail up, I can't seem to solve.
> > > >
> > > > Here's a snippit of code:
> > > >
> > > > //---set up------------------- --
> > > > int HiddenTrailingStopPips = 5;
> > > > double HiddenTrailingStopPrice = 0;
> > > >
> > > > //--after finding any open Buy orders
> > > >
> > > > if ((Bid-OrderOpenPrice() > 0) && // If price has moved up from when
> > > > order was opened, and
> > > > (HiddenTrailingStopPrice < Bid - Point*HiddenTrailin gStopPips) ) // If
> > > > our Hidden Stop Value is lower than it should be?
> > > > {
> > > > HiddenTrailingStopPrice = Bid - Point*HiddenTrailin gStopPips; // Set
> > > > new value for Hidden Stop Value
> > > > }
> > > >
> > > > // closing if stop hit------------------ --
> > > >
> > > > if (Bid <= HiddenTrailingStopPrice) // Price has moved below our
> > > > HiddenStopLine
> > > > {
> > > > OrderClose(OrderTicket(),OrderLots( ),Bid,9,Violet) ; // Close BUY order
> > > > }
> > > >
> > > >
> > > > The above doesn't work. The HiddenTrailingStopPrice, moves up and
> > > > down, and not just up as it should. Any ideas appreciated.
> > > >
> > > > --- In MetaTrader_Experts_and_Indicators@ yahoogroups. com
> > <mailto:MetaTrader_Experts_and_ Indicators% 40yahoogroups. com>
> > > > <mailto:MetaTrader_Experts_and_ Indicators% 40yahoogroups. com>, Robert
> > > > Hill <robydoby314@> wrote:
> > > > >
> > > > > bluey39to,
> > > > > What type of trailing stop are you using?
> > > > >
> > > > > It would be easy to modify the existing TraingStop EA to use hidden
> > > > trailing but if there is a disconnect then the broker might not have
> > > > any stoploss set.
> > > > >
> > > > > An alternative is to have the broker sent the hidden value plus or
> > > > minus a set number of pips for protection.
> > > > >
> > > > > For example if the stop should be 1.4780 on a buy the broker might
> > > > get 1.4760.
> > > > > The EA would close the trade at 1.4780 if no further movement
> > > > positive movement.
> > > > >
> > > > > Otherwise you could send me your code privately and I will see what
> > > > I can do.
> > > > >
> > > > >
> > > > > Robert
> > > > >
> > > > > --- On Sat, 3/14/09, bluey39to <dbyrt@> wrote:
> > > > >
> > > > > From: bluey39to <dbyrt@>
> > > > > Subject: [MT_E and I] Hidden Trailing Stop
> > > > > To: MetaTrader_Experts_and_Indicators@ yahoogroups. com
> > <mailto:MetaTrader_Experts_and_ Indicators% 40yahoogroups. com>
> > > > <mailto:MetaTrader_Experts_and_ Indicators% 40yahoogroups. com>
> > > > > Date: Saturday, March 14, 2009, 12:45 PM
> > > > >
> > > > >
> > > > >
> > > > >
> > > > >
> > > > > After many searches and trying to build this myself, I've not
> > > > managed to find a hidden trailing stop. Plenty of hidden TP and
> > hidden
> > > > Stops, but the hidden trailing seems a bit elusive. Has anyone
> > created
> > > > a bit of code to do this?
> > > > > My attempts; the hidden stop value moves up with each new high, but
> > > > when the price moves back down, so does my stop. All kinds of 'if'
> > > > statements tried, but can't get it to work.
> > > > >
> > > > > Any thoughts?
> > > > >
> > > > > Regards
> > > > > bluey39to
> > > > >
> > > >
> > > >
> > >
> > > //+--------------+
> > > //|VirtualShell |
> > > //+--------------+
> > > #property copyright "Ron Thompson"
> > > #property link "http://www.ForexMT4.com/ <http://www.ForexMT4.com/ >"
> > >
> > > // This EA is NEVER to be SOLD individually
> > > // This EA is NEVER to be INCLUDED as part of a collection that is SOLD
> > >
> > >
> > > // EA SPECIFIC
> > >
> > >
> > > // user input
> > > extern double Lots = 0.5 ;
> > > extern double ProfitMade = 0 ;
> > > extern double LossLimit = 0 ;
> > > extern double BreakEven = 7 ;
> > > extern double TrailStop = 15 ;
> > >
> > > double myPoint; // support for 3/5 decimal places
> > > bool TradeAllowed=false; // used to manage trades
> > > int loopcount; // count of order attempts
> > > int maxloop=5; // maximum number of attempts to handle errors
> > > int LL2SL=10; // LossLimit to StopLoss server spread
> > > int MagicNumber = 200903141510; // allows multiple experts to trade
> > on same account
> > > string TradeComment = "VS.txt"; // where to log information
> > >
> > > // Bar handling
> > > datetime bartime=0; // used to determine when a bar has moved
> > > datetime vopsavetime=0; // used to determine when a bar has moved
> > > datetime vopsynctime=0; // used to determine when a bar has moved
> > >
> > >
> > > // Virtual Order Processor
> > >
> > > #define vopct 100 // maximum numbers of orders to handle
> > >
> > > int vopPTR; // pointer for ticket
> > > int vopOrderTicket[vopct]; // ticket number
> > > string vopOrderSymbol[vopct]; // which symbol
> > > string vopOrderType[vopct]; // buy or sell order
> > > double vopOrderLots[vopct]; // lot size
> > > double vopOrderOpenPrice[vopct]; // Order Open Price
> > > double vopOrderTakeProfit[vopct]; // take profit
> > > double vopOrderStopLoss[vopct]; // stop loss
> > > int vopOrderMagicNumber[vopct]; // MagicNumber
> > >
> > >
> > > // used for verbose error logging
> > > #include <stdlib.mqh>
> > >
> > >
> > > //+-------------+
> > > //| Custom init |
> > > //|-------------+
> > > // Called ONCE when EA is added to chart or recompiled
> > >
> > > int init()
> > > {
> > > // get normalized Point based on Broker decimal places
> > > myPoint = SetPoint();
> > >
> > > //re-load the array, and sync it to real orders
> > > vopLoad();
> > > vopSync();
> > >
> > > logwrite(TradeComment,"Init Complete");
> > > Comment(" ");
> > >
> > >
> > >
> > > }
> > >
> > > //+----------------+
> > > //| Custom DE-init |
> > > //+----------------+
> > > // Called ONCE when EA is removed from chart
> > >
> > > int deinit()
> > > {
> > > // program is closing, save the array
> > > vopSave();
> > >
> > > logwrite(TradeComment,"DE-Init Complete");
> > > Comment(" ");
> > > }
> > >
> > >
> > > //+-----------+
> > > //| Main |
> > > //+-----------+
> > > // Called EACH TICK and each Bar[]
> > >
> > > int start()
> > > {
> > >
> > > int cnt=0;
> > > int gle=0;
> > > int ticket=0;
> > > int OrdersPerSymbol=0;
> > >
> > > // stoploss and takeprofit and close control
> > > double SL=0;
> > > double TP=0;
> > >
> > > double CurrentProfit=0;
> > > double CurrentBasket=0;
> > >
> > > // direction control
> > > bool BUYme=false;
> > > bool SELLme=false;
> > >
> > > //safety counter
> > > int loopcount=0;
> > >
> > > //volume
> > > double v1, v2, v3;
> > > double v12p, v23p;
> > >
> > >
> > > //Save the array once a minute in case of crash
> > > if(vopsavetime!=iTime(Symbol( ), 1, 0) )
> > > {
> > > vopsavetime=iTime(Symbol( ), 1, 0) ;
> > > vopSave();
> > > }
> > >
> > > //It is *possible* (but unlikely) for the array to
> > > //get out of sync, so re-sync once every 15 minutes
> > > if(vopsynctime!=iTime(Symbol( ), 15, 0) )
> > > {
> > > vopsynctime=iTime(Symbol( ), 15, 0) ;
> > > vopSync();
> > > }
> > >
> > >
> > > // bar counting
> > > if(bartime!=iTime(Symbol( ), 0, 0) )
> > > {
> > > bartime=iTime(Symbol(), 0, 0) ;
> > >
> > >
> > > //+------------------ --------- --+
> > > //| Code here will execute once |
> > > //| at the OPEN of a NEW BAR |
> > > //+------------------ --------- --+
> > >
> > > v1=iVolume(Symbol(),0,1);
> > > v2=iVolume(Symbol(),0,2);
> > > v3=iVolume(Symbol(),0,3);
> > >
> > > logwrite(TradeComment,"New Bar v1="+v1+" v2="+v2+" v3="+v3);
> > >
> > > if(TradeAllowed) CloseEverything();
> > > if(TradeAllowed && v1>v2 && v2>v3) { OpenBuy(); OpenSell(); }
> > >
> > > // prevents 1st order when EA put on chart
> > > TradeAllowed=true;
> > >
> > > //+------------+
> > > //| End Insert |
> > > //+------------+
> > >
> > > }
> > >
> > >
> > >
> > >
> > > //+------------------ --------- --+
> > > //| Insert your indicator here |
> > > //| And set either BUYme or |
> > > //| SELLme true to place orders |
> > > //+------------------ --------- --+
> > >
> > >
> > > //+------------+
> > > //| End Insert |
> > > //+------------+
> > >
> > >
> > >
> > > //
> > > // Order Management
> > > //
> > >
> > > for(cnt=OrdersTotal();cnt>=0; cnt--)
> > > {
> > > OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES) ;
> > > if( OrderSymbol()==Symbol( ) && OrderMagicNumber( )==MagicNumber )
> > > {
> > >
> > > vopPTR=vopSelect(OrderTicket( ));
> > >
> > > if(vopOrderType[vopPTR]== "BUY")
> > > {
> > > CurrentProfit=(Bid-vopOrderOpe nPrice[vopPTR] ) ;
> > >
> > > // Modify for break even
> > > //=================== ====
> > > if(BreakEven>0 && vopOrderStopLoss[ vopPTR]== 0 && CurrentProfit >=
> > (BreakEven*myPoint) )
> > > {
> > > vopOrderStopLoss[vopPTR]=vopOrder OpenPrice[ vopPTR]+( Ask-Bid);
> > > logwrite(TradeComment,"BREAKEVEN BUY
> > Ticket="+vopOrderTicket[vopPTR] +" SL="+vopOrderStopLo ss[vopPTR] +"
> > CurrentProfit="+CurrentProfit+ " Bid="+Bid+" Ask="+Ask);
> > > }
> > >
> > > // check for trailing stop
> > > //=================== ======
> > > if( TrailStop>0)
> > > {
> > > // This starts trailing after 'TrailStop' pips of profit
> > > if(Close[0]>=vopOrderOpenPri ce[vopPTR] +(TrailStop* myPoint) )
> > > {
> > > // if you're here, then you're at least TrailStop pips in profit
> > > if( Bid-(TrailStop*myPoint) > vopOrderStopLoss[ vopPTR] )
> > > {
> > > vopOrderStopLoss[vopPTR]=Bid- (TrailStop* myPoint);
> > > logwrite(TradeComment,"TRAILSTOP BUY
> > Ticket="+vopOrderTicket[vopPTR] +" SL="+vopOrderStopLo ss[vopPTR] +"
> > Bid="+Bid);
> > > }
> > > }
> > > }
> > >
> > > if(ProfitMade>0 && CurrentProfit> =(ProfitMade* myPoint))
> > CloseBuy("PROFIT");
> > > if(LossLimit>0 && CurrentProfit< =(LossLimit* (-1)*myPoint) )
> > CloseBuy("LOSS");
> > > if(vopOrderStopLoss[vopPTR]> 0 && Close[0]<=vopOrderS topLoss[vopPTR]
> > ) CloseBuy("STOPLOSS");
> > >
> > > } //BUY
> > >
> > >
> > >
> > >
> > > if(vopOrderType[vopPTR]== "SELL")
> > > {
> > > CurrentProfit=(vopOrderOpenPri ce[vopPTR] -Ask);
> > >
> > > // Modify for break even
> > > //=================== ====
> > > if(BreakEven>0 && vopOrderStopLoss[ vopPTR]== 0 && CurrentProfit >=
> > (BreakEven*myPoint) )
> > > {
> > > vopOrderStopLoss[vopPTR]=vopOrder OpenPrice[ vopPTR]-( Ask-Bid);
> > > logwrite(TradeComment,"BREAKEVEN SELL
> > Ticket="+vopOrderTicket[vopPTR] +" SL="+vopOrderStopLo ss[vopPTR] +"
> > CurrentProfit="+CurrentProfit+ " Ask="+Ask+" Bid="+Bid);
> > > }
> > >
> > > // check for trailing stop
> > > //=================== ======
> > > if( TrailStop>0)
> > > {
> > > // This starts trailing after 'TrailStop' pips of profit
> > > if(Close[0]<=vopOrderOpenPri ce[vopPTR] -(TrailStop* myPoint) )
> > > {
> > > if( Ask+(TrailStop*myPoint) < vopOrderStopLoss[ vopPTR] )
> > > {
> > > vopOrderStopLoss[vopPTR]=Ask+ (TrailStop* myPoint);
> > > logwrite(TradeComment,"TRAILSTOP SELL
> > Ticket="+vopOrderTicket[vopPTR] +" SL="+vopOrderStopLo ss[vopPTR] +"
> > Ask="+Ask);
> > > }
> > > }
> > > }
> > >
> > > if(ProfitMade>0 && CurrentProfit> =(ProfitMade* myPoint) )
> > CloseSell("PROFIT");
> > > if(LossLimit>0 && CurrentProfit< =(LossLimit* (-1)*myPoint) )
> > CloseSell("LOSS");
> > > if(vopOrderStopLoss[vopPTR]> 0 && Close[0]>=vopOrderS topLoss[vopPTR]
> > ) CloseSell("STOPLOSS");
> > >
> > >
> > > } //sell
> > >
> > > } //OrderSymbol
> > >
> > > } // for
> > >
> > > } // start()
> > >
> > >
> > > //+-----------------+
> > > //| CloseEverything |
> > > //+-----------------+
> > > // Closes all OPEN and PENDING orders
> > >
> > > int CloseEverything()
> > > {
> > > int i;
> > >
> > > for(i=OrdersTotal();i>=0;i- -)
> > > {
> > >
> > > OrderSelect(i, SELECT_BY_POS) ;
> > > if(OrderSymbol()==Symbol( ) && OrderMagicNumber( )==MagicNumber)
> > > {
> > > vopPTR=vopSelect(OrderTicket( ));
> > >
> > > if(OrderType()==OP_BUY) CloseBuy ("CLOSEEVERYTHING" );
> > > if(OrderType()==OP_SELL) CloseSell("CLOSEEVE RYTHING") ;
> > > if(OrderType()==OP_BUYLIMIT) OrderDelete( vopOrderTicket[ vopPTR] );
> > > if(OrderType()==OP_SELLLIMIT) OrderDelete( vopOrderTicket[ vopPTR] );
> > > if(OrderType()==OP_BUYSTOP) OrderDelete( vopOrderTicket[ vopPTR] );
> > > if(OrderType()==OP_SELLSTOP) OrderDelete( vopOrderTicket[ vopPTR] );
> > > }
> > >
> > > Sleep(1000);
> > >
> > > } //for
> > >
> > > } // closeeverything
> > >
> > >
> > >
> > > // log data to a file name passed in
> > > // print everything regardless of log setting
> > > void logwrite (string filename, string mydata)
> > > {
> > > int myhandle;
> > > string gregorian=TimeToStr(CurTime( ),TIME_DATE| TIME_SECONDS) ;
> > >
> > > Print(mydata+" "+gregorian)