Fibonacci math in an EA

 

Hi there I am having hassles with implementing Fib math in my EA...


let me explain :


I want the EA to enter a buy order for example, and then it must make the stoploss the lowest low of the last 2 weeks candles...

then it must place a take profit of a 1.1618 fib extension of the difference between the stop loss and order entry price...


This is what i did :


total =OrdersTotal();
////////////////////
double sll = Low[iLowest(NULL,PERIOD_W1,MODE_LOW,2,1)]; // Stop loss for long trade is lowest low of last 2 weeks
double sls = High[iHighest(NULL,PERIOD_W1,MODE_HIGH,2,1)]; //Stop loss for short trade is highest high of last 2 weeks
double slla = sll-10*Point; // make Stoploss for long trade 10pips lower than lowest low of last 2 weeks
double fsll = NormalizeDouble(slla,Digits); // Normalize the number to use in OrderSend function -- Use fsll in Ordersend
double slsa = sls+10*Point; // make the stop loss for sell trade 10 pips higher than the last 2 weeks high
double fsls = NormalizeDouble(slsa,Digits); // Normalize the number to use in OrderSend -- Use fsls in ordersend
//----TP Settings
double entry = OrderOpenPrice(); // Start point to work out the Fib math for take Profit
////////////////////
double tpl = (entry-sll); // 1st determine the difference between the Stoploss and the Order entry price
double tpl2 = tpl*0.618; // now multiply the difference by 0.618
double tpl3 = entry+tpl2; // now add it to the order entry price to get the 1.618 Fib extension
double ftpl = NormalizeDouble(tpl3,Digits); // Normalize the number to use in Ordersend -- Use ftpl in Ordersend
//------------------------------------->
double tps = (sls-entry); // 1st determine the difference between sl and entry price
double tps2 = tps*0.618; // multiply the value to get fib value
double tps3 = entry-tps2; // add to entry price to get 1.618 Fib extension value
double ftps = NormalizeDouble(tps3,Digits); // Normalize the number to use in Ordersend -- Use ftpl in Ordersend


I did the math of this on a calculator and it is accurate...you can test the math by drawing a fib on a chart a follow this math and you will get the correct value for the 1.618 Fib extension.


The problem I have though is when i use the Ea it gives me a error 130...this is a invalid stops...but I dont know why because of these factors :

1: the stops are in excess of 100 pips moslty and so can not be too small or too big

2: I have normalized the values and so they cant be wrong in this way


any one see a blatent problem with this code? I appreciate the help.


I have attached the EA and the indy it uses

Files:
 
heres the indy
Files:
mpiptl.mq4  3 kb
 
anybody got an idea?
 

Hi,

- You're not testing if ftpl was > OrderOpenPrice() + the StopLevel ( MarketInfo(symbol, MODE_STOPLEVEL) ) that could be the cause in certain cases where your highest reference point is near the current price.

-where is your MODIFY? if you want to set up the StopLoss TakeProfit while creating the order then your double entry must = Ask or Bid and not OrderOpenPrice() : you don't know what is OrderOpenPrice() until you've read the order do you?

 
Jacques366:

Hi,

- You're not testing if ftpl was > OrderOpenPrice() + the StopLevel ( MarketInfo(symbol, MODE_STOPLEVEL) ) that could be the cause in certain cases where your highest reference point is near the current price.

-where is your MODIFY? if you want to set up the StopLoss TakeProfit while creating the order then your double entry must = Ask or Bid and not OrderOpenPrice() : you don't know what is OrderOpenPrice() until you've read the order do you?

Thank you for pointing out my mistakes...I have a fair few things to fix here it seems...thanx for the help I will be trying to apply what you have pointed out .


thanks again!

Reason: