Code wanted to capture 00 level

Section for discussions related to indicators, use of indicators, and building of trading stategies using indicators.

Moderator: admin

Code wanted to capture 00 level

Postby LordTwig » Sat Nov 02, 2013 9:39 pm

Hey, how can I write some code to capture/tell when I have gone above or below the price levels of each '00'..... eg; price is 1.0487 then goes to 1.0500

It should be easy but I cant seem to come up with a way to do it.
Has to work on all currencies.

Any help appreciated
Cheers
LordTwig
LordTwig
FXCodeBase: Confirmed User
 
Posts: 157
Joined: Fri Mar 12, 2010 3:15 am

Re: Code wanted to capture 00 level

Postby Apprentice » Sun Nov 03, 2013 5:15 am

One approach might be.
Code: Select all
local Flag1= true;
local Flag2= true;
local Shift=0;
Level = round(source.close[period], 2)

while ( Flag1 or Flag2) do
 
 if core.crosses (source.close , Level+Shift, period) then
 Flag1=false;
 end

 if core.crosses (source.close , Leve-Shift, period) then
 Flag2=false;
 end

Shift= Shift+souce:pipSize()*100;
 
end


function round(num, idp)
  if idp and idp>0 then
    local mult = 10^idp
    return math.floor(num * mult + 0.5) / mult
  end
  return math.floor(num + 0.5)
end ]
User avatar
Apprentice
FXCodeBase: Confirmed User
 
Posts: 36341
Joined: Thu Dec 31, 2009 11:59 am
Location: Zagreb, Croatia

Re: Code wanted to capture 00 level

Postby LordTwig » Mon Nov 04, 2013 10:18 pm

Thanks Apprentice, I will try it out :)
Cheers
LordTwig
LordTwig
FXCodeBase: Confirmed User
 
Posts: 157
Joined: Fri Mar 12, 2010 3:15 am

Re: Code wanted to capture 00 level

Postby LordTwig » Thu Nov 07, 2013 10:21 am

Hi Apprentice,

I Can't seem to know how to implement this into a strategy correctly. :oops:

Can you throw it in a strategy and post it here or in strategy's ? ;)
so I can see how it works.

Thanks
Cheers
LordTwig
LordTwig
FXCodeBase: Confirmed User
 
Posts: 157
Joined: Fri Mar 12, 2010 3:15 am

Re: Code wanted to capture 00 level

Postby Apprentice » Fri Nov 08, 2013 3:58 am

Can you define what you want to achieve within this strategy.
So I have a goal.
User avatar
Apprentice
FXCodeBase: Confirmed User
 
Posts: 36341
Joined: Thu Dec 31, 2009 11:59 am
Location: Zagreb, Croatia

Re: Code wanted to capture 00 level

Postby LordTwig » Fri Nov 08, 2013 7:46 am

Okay,

Simple ideas really.

1) I want to be able to SELL when price touches 00 level ( or for such matter user choice of any other levels between 00 and 99) with stops and limits
...Reverse for BUY,

2) Other Idea is to BUY and SELL at exact same time at 00 level but with the ability to have different stops and limits for each

Thanks for help.
Cheers
LordTwig
LordTwig
FXCodeBase: Confirmed User
 
Posts: 157
Joined: Fri Mar 12, 2010 3:15 am

Re: Code wanted to capture 00 level

Postby LordTwig » Wed Nov 13, 2013 4:01 am

Apprentice wrote:Level = round(source.close[period], 2)

Not sure rounding will work properly as wanted though!

Also for clarity when I say 00 level, that means excluding the fractional number. ie;
EUR/USD = 1.3434(7) - fractional
EUR/USD = 1.3434 - no fractional, so when price changes to this 1.3500 or 1.3400
Cheers
LordTwig
LordTwig
FXCodeBase: Confirmed User
 
Posts: 157
Joined: Fri Mar 12, 2010 3:15 am

Re: Code wanted to capture 00 level

Postby LordTwig » Fri Nov 22, 2013 5:23 am

Hi there.....
Any advances on this yet?
Cheers
LordTwig
LordTwig
FXCodeBase: Confirmed User
 
Posts: 157
Joined: Fri Mar 12, 2010 3:15 am

Re: Code wanted to capture 00 level

Postby LordTwig » Sun May 04, 2014 1:01 am

Hi Apprentice,
Have you made any progress into this strategy yet?
Cheers
LordTwig
LordTwig
FXCodeBase: Confirmed User
 
Posts: 157
Joined: Fri Mar 12, 2010 3:15 am

Re: Code wanted to capture 00 level

Postby moomoofx » Fri May 23, 2014 8:45 pm

Hi LordTwig,

I'm not sure if you want to know if the price goes exactly to the 00 level or beyond. Obviously it is entirely possible the price could skip the 00 level so I assume that isn't what you want.

Either way, this kind of stuff is usually achieved by multiplying the number by it's precision, and rounding or taking the floor of the number, and then doing some kind of comparison. Like this:
Code: Select all
local lastOne = -1;
function ExtUpdate(id, source, period)
    local precision = instance.bid:getPrecision();
    local n = 2;

    local currentPrice = instance.bid[NOW];
    local multiplier = math.pow(10, precision - n);
    local a = currentPrice * multiplier;
    local b = math.floor(a);
    if (lastOne == -1) then
        lastOne = b;
        return;
    end

    if (a == b) then
        core.host:trace("The price is exactly on the 00");
    end

    if (lastOne ~= b) then
        lastOne = b;
        core.host:trace("Signal! " .. currentPrice .. " broke " .. (b / multiplier));       
    else
        core.host:trace(currentPrice);       
    end
end


Gives you the following output. You can see it generates a signal every time we cross the 00 levels.

Code: Select all
1.34804
1.34802
1.34803
1.34805
1.34807
1.34805
Signal! 1.34796 broke 1.347
1.34797
1.34793
1.34794
1.34794
1.34793
Signal! 1.34804 broke 1.348
1.34806
The price is exactly on the 00
1.348
1.34805
1.34806
1.34815


Is that what you were looking for?

I hope you aren't trying to write a Grid strategy ;-)

Cheers,
MooMooFX
User avatar
moomoofx
FXCodeBase: Confirmed User
 
Posts: 193
Joined: Wed Oct 23, 2013 10:26 pm
Location: Okinawa, Japan. http://moomooforex.com


Return to Discussions

Who is online

Users browsing this forum: No registered users and 7 guests

cron