Saturday, 17 October 2009

Best Forex Web Sites - 2009.10.17



I decided to re-publish this one (an updated version) because this is the part of my blog I am extensively using myself, so I don't want to have it somewhere deep in archives... (OK, with so 'many' blog entries there is 'no deep in archives' ... anyways...).


Economic calendars, commentaries, news feeds:
General stuff:
MT4 platform, expert advisors, indicators:
There are nice video-howtos on www.fxdd.com:
And of course
Neural Networks for trading:
to be continued...

Thursday, 15 October 2009

TradeProtector 1.2

An updated version of the EA (expert advisor) for MT4 platform, meant as an assistant - to put SL if you wouldn't, and to make sure the winning trade will not turn into loosing when you fall asleep ;-)

Here is the code:

// trade assistant - to watch after SL's for your trades
// some ideas and code fragments: "S.Projects" - "cortex.snowcron.com"


extern int logging=1;
//logging=1  - if you want logs in Experts\Files directory
extern int nInitialSL=15;
// inital SL
extern int nTrailingStop=35;
//nTrailingStop [pips] - initial trailing stop. It will be used until your trade will reach profit = nPropSLThreshold
extern int nPropSLThreshold=12;
//nPropSLThreshold [pips] - after reaching this profit proportional trailing stop will be used
extern double dPropSLRatio=0.35;
//dPropSLRatio [decimal] - multiplying factor ( PropSL = Profit * dPropSLRatio  - Spred )
extern int nUseEscape=0;
//nUseEscape [ 1 or 0 ] - escape misplaced trades as soon as they reach some minimal profit
extern int nEscapeLevel=0;
//nEscapeLevel [pips] - lose size after which we want our trade to terminate
//as soon as it will reach next high
extern int nEscapeTP=35;
//nEscapeTP [pips] - take profit level in pips (you can set to negative value
//- then it will be a lose that you would be happy to get,
//in the case your trade reached some impressive negative pips value)
extern int nSleep=0;
//delay after new bar
extern int nSlip = 2;
//maximum price slip allowed

double dEscapeLevel;
double dInitialSL;
double dTrailingStop;
double dEscapeTP;
double dPropSLThreshold;
double dTakeProfit;
double dTakeProfitMin;
double dTakeProfitMax;
double dTakeProfitT;
int nBars, nSpread, nDigits, nBarsSameTrend, nCloseErr, nOpenErr, i;

double dDeltaPrice, dnBid, dnAsk, dSpread, dStopLevel, dMax, dMin, dMacdDelta, dMacd1, dMacd2;

// double dOldBalance, dNewBalance;

int nTakeProfitMax=100;

int nBarsSinceTrade=0;

string strExpert;


// ------

int init ()
{
   nBars = Bars;
   nSpread = MarketInfo(Symbol(), MODE_SPREAD);
   dSpread = NormalizeDouble(nSpread * Point,4);
   nDigits = MarketInfo(Symbol(), MODE_DIGITS);
   dEscapeTP = NormalizeDouble(nEscapeTP * Point,4); 

   dEscapeLevel = nEscapeLevel * Point;
  
   strExpert = "tp-1.2.0-"+Symbol()+"-"+Period();

   return(0);
}

// ------
int deinit()
{
   return(0);
}

// ------

int start()
{

   // ------
   // to let MT rest a bit after new bar:
   Sleep(nSleep*1000);
   if(nSleep > 0)
      RefreshRates();
  
  
   dnBid=NormalizeDouble(Bid,nDigits);
   dnAsk=NormalizeDouble(Ask,nDigits);

   ModifyOrders();

   // ------

   return(0);
}


// ------

void ModifyOrders()
{
   double dSl;
   double arrSL[4];
   double arrTP[4];

   dTrailingStop = NormalizeDouble(nTrailingStop * Point,4);
   dEscapeTP = NormalizeDouble(nEscapeTP * Point,4);
   dPropSLThreshold = nPropSLThreshold * Point;
   dSpread = MarketInfo(Symbol(),MODE_SPREAD) * Point;
   dStopLevel = MarketInfo(Symbol(),MODE_STOPLEVEL) * Point;
   dInitialSL = NormalizeDouble(nInitialSL * Point,4);
 
   for(int nCnt = 0; nCnt < OrdersTotal(); nCnt++)
   {
      OrderSelect(nCnt, SELECT_BY_POS, MODE_TRADES);
      if(OrderSymbol() == Symbol() ) //(OrderMagicNumber() == nMagic)
      {
         if(OrderType() == OP_BUY)
         {
            dSl=OrderStopLoss();
            if( dSl == 0 )
               if( dInitialSL != 0)
                  dSl = dnAsk - dInitialSL;
           
            ArrayResize(arrSL,5);
            ArrayInitialize(arrSL,dSl);

            LogSL("OP_BUY-check",dSl,arrSL[0],arrSL[1],arrSL[2],arrSL[3]);


            if( dPropSLRatio > 0 )
            {
               if( Bid >= (OrderOpenPrice() + dPropSLThreshold) )
               {
                  dSl = NormalizeDouble( OrderOpenPrice() + dPropSLRatio*(Bid - OrderOpenPrice()) - dSpread,4 );
                  if(OrderStopLoss() < dSl)
                  arrSL[1]=dSl;
               }
               else
               {
                  if(dTrailingStop != 0)
                     arrSL[2]=dnBid - dTrailingStop;
               }
            }              
           

            dSl=arrSL[ArrayMaximum(arrSL)];

            LogSL("OP_BUY - max",dSl,arrSL[0],arrSL[1],arrSL[2],arrSL[3]);

            if( dSl > OrderStopLoss() || OrderStopLoss() == 0 )
               {
                  OrderModify(OrderTicket(), OrderOpenPrice(),
                     dSl, OrderTakeProfit(), 0, Yellow);
                  Log("Buy - modify", OrderOpenPrice(), dSl, OrderTakeProfit());
               }

            // Escape buy
            //if( dEscape != 0 && dnBid < OrderOpenPrice() - dEscape - 5 * Point )
            if( nUseEscape == 1 && dnBid < OrderOpenPrice() - dEscapeLevel - 5 * Point )
            {
               OrderModify(OrderTicket(), OrderOpenPrice(),
               OrderStopLoss(), OrderOpenPrice() + dEscapeTP, 0, Aqua);
               Log("Buy - EscapeLevel", OrderOpenPrice(), dSl, OrderTakeProfit());
            }

         } // end OP_BUY

//////////////////////////////////////////////////////////////////////////////////////////////////

         if(OrderType() == OP_SELL)
         {
            dSl=OrderStopLoss();
            if( dSl == 0 )
               if( dInitialSL != 0)
                  dSl = dnBid + dInitialSL;
           
            ArrayResize(arrSL,5);
            ArrayInitialize(arrSL,dSl);
 
            LogSL("OP_SELL-check",dSl,arrSL[0],arrSL[1],arrSL[2],arrSL[3]);

            if( dPropSLRatio > 0 )
            {
               if( Ask <= (OrderOpenPrice() - dPropSLThreshold) )
               {
                  dSl = NormalizeDouble(OrderOpenPrice() - dPropSLRatio*(OrderOpenPrice() - Ask) + dSpread,4);
                  if(OrderStopLoss() > dSl)
                  arrSL[1]=dSl;
               }
               else
               {
                  if(dTrailingStop != 0)
                     arrSL[2]=dnBid + dTrailingStop;
               }
            }

            dSl=arrSL[ArrayMinimum(arrSL)];

            LogSL("OP_SELL - min",dSl,arrSL[0],arrSL[1],arrSL[2],arrSL[3]);

            if( dSl < OrderStopLoss() || OrderStopLoss() == 0 )
            {
               OrderModify(OrderTicket(), OrderOpenPrice(),
                  dSl, OrderTakeProfit(), 0, Yellow);
               Log("Sell - modify", OrderOpenPrice(), dSl, OrderTakeProfit());
            }

            // Escape sell
            //if( dEscape != 0 && dnAsk > OrderOpenPrice() + dEscape + 5 * Point )
            if( nUseEscape == 1 && dnAsk > OrderOpenPrice() + dEscapeLevel + 5 * Point )
            {
               OrderModify(OrderTicket(), OrderOpenPrice(),
               OrderStopLoss(), OrderOpenPrice() - dEscapeTP, 0, Aqua);
              
               Log("Buy - EscapeLevel", OrderOpenPrice(), dSl, OrderTakeProfit());
            }

         } // End OP_SELL
      } //end if(OrderMagicNumber() == nMagic)

   } //end for(int nCnt = 0; nCnt < OrdersTotal(); nCnt++)
} // end ModifyOrders()


//////////////////////////////////////////////////////////////////////////////////////////////////


void Log(string msg, double val1, double val2, double val3)
{
   if(logging > 0 )
   {
      int handle;
      handle=FileOpen(strExpert+".log",FILE_CSV|FILE_READ|FILE_WRITE,';');
      if(handle<1)
      {
         //Print("File "+strExpert+"-log.txt not found, the last error is ", GetLastError());
         Print("File "+strExpert+"-log.txt not found, the last error is ", GetLastError());
         return(false);
      }

      FileSeek(handle, 0, SEEK_END);
      //---- add data to the end of file
      //FileWrite(handle, Year(), Month(), Day(), Hour(), Minute(), "Bid, Ask ", msg, Bid, Ask, "___", val1, val2, val3, val4, val5, val6);
      FileWrite(handle, Year(), Month(), Day(), Hour(), Minute(), msg, Bid, Ask, "___", val1, "___", val2, val3);
      FileClose(handle);
   }
}


void LogSL(string msg, double val1, double val2, double val3, double val4, double val5)
{
   if(logging > 1 )
   {
      int handle;
      handle=FileOpen(strExpert+"-sl_log.txt",FILE_CSV|FILE_READ|FILE_WRITE,';');
      if(handle<1)
      {
         Print("File "+strExpert+"-sl_log.txt not found, the last error is ", GetLastError());
         return(false);
      }
      FileSeek(handle, 0, SEEK_END);
      //---- add data to the end of file
      FileWrite(handle, Year(), Month(), Day(), Hour(), Minute(), msg, Bid, Ask, val1, val2, val3, val4, val5);
      FileClose(handle);
   }
}





Wednesday, 14 October 2009

Falling Dollar

From   http://www.time.com/time/magazine/article/0,9171,1666266,00.html
"  It's not that we Americans have gotten a lot poorer. It's that we might be about to. "

What is average share of the greenback in big economies' (EU, Russia, Australia, India, China) treasury reserves? 60%? 70%? 80%?
Isn't it the cheapest option for the world - to bailout America, rather then to let it declare bankruptcy?

And some links to other articles on the topic:

An article from March 2009:
http://www.reuters.com/article/reutersEdge/idUSTRE52M5UZ20090323

http://blogs.reuters.com/great-debate/2009/10/13/dollar-faces-long-journey-downward/

http://www.reuters.com/article/usDollarRpt/idUSN1318753620091013

http://yglesias.thinkprogress.org/archives/2009/10/the-falling-dollar.php

http://buzz.yahoo.com/article/1:financial_tim933:fa2384c7b4f2bf161cdd1bacc9caf09e/Obama-under-fire-over-falling-dollar 
and 
http://www.ft.com/cms/s/0/08ca4832-b36a-11de-ae8d-00144feab49a.html?ftcamp=rss&nclick_check=1

http://www.kippreport.com/2009/10/is-the-dollar-dying/






Saturday, 3 October 2009

Non-Farm Payrols - Q3 2009 - My BIG mistake :(

Last time I have done big FA (fundamental announcement) I have easily got 100 pips (Jan 2009 - the USD was falling, the unemployment rate was setting new records), so I thought this one will be piece of cake.

It wasn't. I totally screwed it up. I think its' a good lesson though - that's why I am sharing it with you.

First: how to do it properly:
Until further notice - I believe in two ways of trading FA, both of them share one rule:
no orders from 10 min before till 10 min after FA, including pending orders (no pending orders to be simple).

Method 1.
Open an order in a direction, that everyone is expecting  (I assume that you don't have open order yet).

Of course one can ask: how can I know what "everyone is expecting?". - Read analysis, commentaries, look at the history (H4, D1 charts). A good place to go is: http://www.forexpros.com/technical/
So once you have an idea what to expect, watch the market from something like 4 hrs before FA. If there is no clear channel visible around 2-1 hrs before FA - than I have no idea what's going on. But if there is - just buy/sell in that channel direction (of course wait for the channel high/low respectively). At about 15 min before FA, set SL (stop loss); for a downtrend - at channel  high + 10 pips, and for uptrend - at channel low - 10 pips. Then wait.
After FA time - in most cases - market will start to move in the expected direction. After first long bar on M1 move your SL to about 30-40% of the distance of the last swing(that is assuming that the move was in expected direction ;-). You can use an EA for automatic SL handling - http://codebase.mql4.com/6043
Then wait again. When people will actually get the FA figures - then either the price move will continue, or - it will reverse (like happened with me). If it will reverse, wait until it will cross the previous swing starting point. After that  - if the trend continues, wait until it will stop, retrace, and then set your pending order slightly above/below last top/bottom.

Method 2.
This is actually a second part of the Method 1. - don't open any orders before FA. When price will start to move, wait till it stops and retraces a little bit, then set pending order in the direction of the first move after announcement, with price = last maximum plus some safety margin (3-5 pips).

In both cases keep SL (StopLoss values) tight, but only at the beginning. And IMHO - use M1 chart around the fundamental announcement. The price will move so fast, so even M5 will not display the market's "inner beat".
Also in both cases, if there is high amplitude, crazy jumps - just wait. You most likely have no idea what is happening. Maybe it's the great opportunity - but there will be a next one!  So don't lose your pips in vain!

In any case - practice it several times on a demo account, before going live! This is very important.

Why not to use pending orders?
The answer is very simple: the price can move so fast, so your pending order will be executed with up to 15 pips re-quote (sic!). Or maybe even bigger.
Let's see an example:
At 15:30 my time (GMT +3) or  8:30 EST the Non-farm Payrolls change is due to be released. At about 12:00 (05:00 EST) the price reached 1.4563 - morning high, then started relatively slow, channeled decline to the pre-FA range (1.4531 - 1.4544).





Knowing the market is going to go down, I have opened pending order at about yellow dotted line.
Just before announcement time I tried to move it up, but the "trade context was busy" and after few moments of  swearing I finally managed to move the trigger to a solid yellow line. When the price went down, my order was re-quoted and finally opened at ... even below yellow dotted line (1.4505). I have got a profit of about 17 pips and then I should quickly set SL between  order open and current price. I didn't. The price went up, now I had between  -5  and -10 pips. It was a time to close and wait but I didn't. The market finally got the figures ( -263k versus -190k expected) and decided - "we cannot continue down, no matter how much we'd like to". So the price started to climb and hit my initial SL at 1.4552.

With the first method I would get mere few pips of the downward movement, then all the climb between 1.4567 - 1.4635.
With the second method I would get "only" the main dish, but of course - if the FA stats would be in the favor of the market sentiment - with the first method I would earn additional 40+ pips versus method 2.

So long, and may the force be with you!
Or wisdom, patience and peace even better...