Donchian System with ThinkOrSwim Strategies

i ran across the “4 week rule” system developed by Richard Donchian when i first read John J. Murphy’s book “Technical Analysis of the Financial Markets”. personally, i have a sweet spot for simple rules that produce surprising results and this system is as simple as it gets

the original system had two rules:

  1. enter long positions and cover short positions whenever price exceeds the high of the past 4 weeks
  2. enter short positions and cover long positions whenever price falls below the low of the past 4 weeks

though simple and does work, the system as it is defined above is in the market, be it long or short, at all times. the result of this is that during periods that the market is moving sideways, the system tends to get “whipsawed” pretty badly.

the system below is a modification of the “4 week rule” which i picked up from Ed Seykota’s site.

for the sake of this writing, the system is briefly described here in the following way:

  • The system has two states – it can either be long or short. when it is long, take signals to the upside. when it is short, take signals to the down
  • A long term price channel, the slow channel, is used to determine the overall trend – either up or down. If the price high goes above this channel, the system is long and stays long until price drops below the channel at which time the system is short
  • A short term price channel, the fast channel, is used to generate trading signals – both enter and exit. If the system is long and price goes above the short term upper channel a long trade signal is issued. At the same time a stop is place at the short term lower channel (the stop trails as the lower channel moves up)

as mentioned, this “modified” approach was taken from Ed Seykota – definitely worth a visit to have a look at what rigorous system development looks like

below is the code in thinkScript for the long side (both entry and exit scripts)

Long_Entry

declare LONG_ENTRY;
input SLOW_LENGTH = 126;
input FAST_LENGTH = 30;
# define system as long or short

# N resistance - the highest high over the last N bars
# N support - the lowest low over the last N bars

def longTermResistance = Highest(high, SLOW_LENGTH);
def longTermSupport = Lowest(low, SLOW_LENGTH);

def shortTermResistance = Highest(high, FAST_LENGTH);
def shortTermSupport = Lowest(low, FAST_LENGTH);

# system changes from short to long when price moves above long term resistance
# system changes from long to short when price moves below long term support

rec state = compoundValue(1, if state[1] < 1 and high > longTermResistance[1] then 1
else if state[1] > -1 and low < longTermSupport[1] then -1
else state[1], 1);

def trigger = if state == 1 and high > shortTermResistance[1] then 1 else 0;
def orderprice = open;

addOrder(trigger, orderprice);

SetColor(color.green);

Long Exit

declare LONG_EXIT;
input SLOW_LENGTH=126;
input FAST_LENGTH = 30;

# define system as long or short

# N resistance - the highest high over the last N bars
# N support - the lowest low over the last N bars

def shortTermResistance = Highest(high, FAST_LENGTH);
def shortTermSupport = Lowest(low, FAST_LENGTH);

addorder(low < shortTermSupport[1]);
setcolor(color.gray);

in order to run the code with ToS, do the following:

  • In ToS, create a new strategy – call it myLongEntry
  • Copy/paste the Long_Entry code above and save
  • Create a second strategy – call it myLongExit
  • Copy/paste the Long_Exit code and save
  • Add both these strategies to any chart
  • Play around with the input values (fastlength, slowlength) and open the report to see results

the short side is left as an exercise for the reader 😉
feel free to leave me a message if you have comments or need help with any of this

parting note

as a parting note: there are many voices speaking from inside of us. there is the voice of our parents, the voice of our teachers, the voice of our spouse and the voice of our children. there voices speaking from the past in the shape of our ancestors; they speak through our parents, and through our parents parents and so on it goes forever. there are voices from the media and the government, from doctors and friends and the list goes on and on. but there is one voice that is all so important and that voice is us. it is at times the hardest voice to hear. regardless if due to the noise from these other speakers or the fear of hearing what our own voice is telling us or because we have forgotten what this voice sounds like and we cannot recognize it any more – it is the one true voice that matters in the end…i can only say that after so many years, i am at last not only hearing this voice clearly again, but am back to acting upon it. and it feels great.

till when the road

till we have shed all barriers and have fused completely with ourselves and with each other

till then

Using Invoice4U API with PHP

Invoice4U is an Israeli based service for creating invoices and receipts.
The documentation does not include an example of how to make the call using PHP.

Below is a short example of how to do so for the InvoiceReciept:Create call. I used the ConvertCharSet component to make the conversion from UTF-8 to windows-1255 (which is the encoding expected).
Continue reading

Zend Studio for Eclipse – Hebrew

Trying out the 6.1 version of the Zend Studio for Eclipse on a project for the Haifa University Faclty of Education.
For those of you that are running into problems with saving files in Hebrew, this post describes the issue and what to do about it.

Issue reproduction

  1. You have an out-of-the-box installation of Zend Studio for Eclipse.
  2. A file is opened with Hebrew characters.
  3. You try to save the file and Eclipse raises the following error message:

save could not be completed.

Reason:

some characters cannot be mapped using “cp1255” character encoding.

Either change the encoding or remove the characters which are not supported

by the “cp1255” character encoding.

Continue reading

in a different time, in a different place, i was a…

in a different time, in

a different place, I was a

kite

floating in the wind, stretching in all directions, the wind pushing against me, my colors so bright

in a different time, in

a different place, I was a

dog

running, barking, chewing, playing, biting, snarling – grrrrrrrrrr

in a different time, in

a different place, i

was a monk,

a bird,

a rock,

an ant,

a tree,

a bee,

a lizard,

a dolphin,

a spider,

a king,

a horse,

a pawn,

a-fool-a-wizard-a-warrior-a-doctor-and-priest

now I am me

and tomorrow?

tomorrow what shall i be?

one hour for planet earth

Tel-Aviv took part in turning out the lights for 60 minutes. I was there with my wife and kids. The mayor gave a speech to a few hundred and at the count of three, all lights in the city hall went out. To be honest, I was expecting a bit more. I was naive enough to think that all the lights would go out.

one hour for planet earth

Apart from the city hall, most of the lights in the area did not go off as expected. A friend of mine said to me a few days ago that Israel has not reached the point where global warming and environmentalism can be taken too seriously. According to him, we are still pretty low on the maslow’s pyramid; an emerging economy, most of our budget is directed to defense and our school system is deteriorating at a very fast pace – we are still struggling to survive at a very basic level.

Well, that might be true, and we do have our challenges ahead of us, but for a few minutes on the 27th of March 2008, we joined the rest of the world in celebrating the existence of our lovely planet earth.

Go and find it. Go and look behind the Ranges

Rudyard Kipling“Till a voice, as bad as Conscience, rang interminable changes

On one everlasting Whisper day and night repeated — so:

‘Something hidden. Go and find it. Go and look behind the

Ranges–

‘Something lost behind the Ranges. Lost and waiting for you.

Go!'”

– From Rudyard Kipling’s “The Explorer” – 1898

it is a test of one’s will and conviction to follow one’s inner voice that quests for that which is lost.

we all start out young pioneers in the world, delighted with all that is new, seeking to learn more with each and every day. as the days go by, we become wiser to the subtleties of life, we carry the scars and memories which are the source of fear.

both internal and external change becomes harder as we loose flexibility. at some point we become “comfortably numb“.

Terry Pratchett….

There is a book store near my house that i like to go to. it is owned by a gray haired, middle aged, straight faced Yemen and his wife who looks roughly his age, only with long deep black hair, a thin face, and a wide smile.

A few weeks ago i dropped in on a friday morning and bought a Terry Pratchett book. it was my forth or fifth. Continue reading