Coding Stumped - off by 1 bar...

Questions about MultiCharts and user contributed studies.
NothingDone
Posts: 7
Joined: 24 Aug 2010
Has thanked: 14 times

Coding Stumped - off by 1 bar...

Postby NothingDone » 25 Aug 2010

I'm having trouble trapping the correct high and low in this indicator, my index value seems to start at 1 even though I initialize it at 0, therefore I get the high or low of 1 bar prior to the event to carry forward, rather than the high or low of the bar I want to follow, which is when the event occurred.

Any insight into how to resolve this would be greatly appreciated.

Code: Select all

variables:
MomentumLength ( 1 ),
FlagVar ( 0 ),
MomentumVar ( 0 ),
HighVal ( 0 ),
LowVal ( 0 ),
IndexVar ( 0 );

// Take the one day momentum of price
MomentumVar = Momentum ( C , MomentumLength );

// Check if momentum (MomentumVar) is falling
if MomentumVar< MomentumVar[1] and MomentumVar[1] < MomentumVar[2] then begin
// Set FlagVar flag to -1 for declining momentum
FlagVar = -1;
// end loop
end;

// Check if momentum (MomentumVar) is rising
if MomentumVar> MomentumVar[1] and MomentumVar[1] > MomentumVar[2] then begin
// Set FlagVar flag to +1 for rising momentum
FlagVar = 1;
// end loop
end;

// Check if momentum is flagged rising
if FlagVar = 1 then begin
// Initilize index to 0
IndexVar = 0;
// set HighVal to the high indexed to the flagged bar
HighVal = High[IndexVar];
// if HighVal has a value then hold that value each bar forward using the index counter IndexVar incremented by 1
if HighVal <> 0 then IndexVar = IndexVar + 1;
// end loop
end;

// Check if momentum is flagged falling
if FlagVar = -1 then begin
// Initilize index to 0
IndexVar = 0;
// set LowVal to the Low indexed to the flagged bar
LowVal = Low[IndexVar];
// if LowVal has a value then hold that value for each bar forward using the index counter IndexVar incremented by 1
if LowVal <> 0 then IndexVar = IndexVar + 1;
// end loop
end;

// plot current value of HighVal
plot1 (HighVal,"High");
// plot current value of LowVal
plot2 (LowVal,"Low");

User avatar
TJ
Posts: 7740
Joined: 29 Aug 2006
Location: Global Citizen
Has thanked: 1033 times
Been thanked: 2221 times

Re: Coding Stumped - off by 1 bar...

Postby TJ » 25 Aug 2010

for end of bar variable assignments, add the following condition:

Code: Select all

if barstatus = 2 then ....

NothingDone
Posts: 7
Joined: 24 Aug 2010
Has thanked: 14 times

Re: Coding Stumped - off by 1 bar...

Postby NothingDone » 25 Aug 2010

I think I'm following you but now I'm picking up the high to the right of the signal bar it seems to have shifted.

Code: Select all

...// set HighVal to the high indexed to the flagged bar
if barstatus = 2 then HighVal = High[IndexVar];...
What I am trying to do is freeze the value on the signal bar, what this has done *gratefully* is updated the value on each subsequent signal (and stopped me getting a -1 attempt to look into the future error with my ham handed try at grokking this out :)) as sometimes I get doubles and triples - which is what I want - now I just need to have it stop on the next bar when there is no signal condition so I can carry those values forward.

(Ultimately to use as an entry limit order and then difference the range to calculate profit targets and stop loss values. Once I get the numbers to hold still then I can start in on the juggling to make them useful.)

Thanks again in advance for your weighing in on this one.

User avatar
TJ
Posts: 7740
Joined: 29 Aug 2006
Location: Global Citizen
Has thanked: 1033 times
Been thanked: 2221 times

Re: Coding Stumped - off by 1 bar...

Postby TJ » 25 Aug 2010

you write very nice code... each step is well documented.

2 more tips:

1. draw a flow chart. It can help you visualize the logic.
http://www.gliffy.com/

2. add PRINT statements along the way to track the variables.

NothingDone
Posts: 7
Joined: 24 Aug 2010
Has thanked: 14 times

Re: Coding Stumped - off by 1 bar...

Postby NothingDone » 25 Aug 2010

"...now I just need to have it stop on the next bar when there is no signal condition so I can carry those values forward."

any thoughts?

I'm assuming that I placed the function in the right location in the code, but while it solved one side of the problem I'm having, it created a new one by shifting/picking up a new tracked price over to the bar after the condition has passed.

I'm off to flowchart this thing on Open Office as we speak... :)

And digging in the crates for how to place print statements to track the variable calculations....

Both excellent ideas.

Addendum: Just a thought - while I have no idea how I would go about coding it - wouldn't a 1 dimensional array work to store the value in to be recalled on each subsequent bar and if so you wouldn't happen to know of a code snippet I could stare at to work out how to put that in this puppy?

User avatar
TJ
Posts: 7740
Joined: 29 Aug 2006
Location: Global Citizen
Has thanked: 1033 times
Been thanked: 2221 times

Re: Coding Stumped - off by 1 bar...

Postby TJ » 30 Aug 2010

...
Addendum: Just a thought - while I have no idea how I would go about coding it - wouldn't a 1 dimensional array work to store the value in to be recalled on each subsequent bar and if so you wouldn't happen to know of a code snippet I could stare at to work out how to put that in this puppy?
a diagram can help others see what you want to do,
a flow chart can help you to see your own logic flow.

NothingDone
Posts: 7
Joined: 24 Aug 2010
Has thanked: 14 times

Re: Coding Stumped - off by 1 bar...

Postby NothingDone » 30 Aug 2010

Problem solved - just needed to freeze those numbers - this is a stripped down code sinppet which you can easily convert into an indicator to check the calculations but it does exactly what i need it to do in holding the breakout levels going forward for each pulse of a signal flag.

Thanks again for all your help on this.

The key was resetting the signalvar at the top of the code which then prevents the numbers from updating on each subsequent bar.

Code: Select all

//1-Bar Breakout
//Date: 2010-08-30
//Program takes any signal bar that has been
//flagged as significant and stores values for
//High & low for Breakout Entries and Breakout
//Bar Based Risk stops

Inputs:
PriceVal (C),
LenVal (5);

Variables:
curHigh (0),
curLow (0),
SignalVal (0),
AverageVal (0),
entryHigh (0),
entryLow (0),
exitHigh (0),
exitLow (0);

// Reset signal value thus locking the variables until the condition is met again

SignalVal = 0;

// Set average value

AverageVal = Average(Close,LenVal);

// Long and short signal conditions here to be flagged as SignalValue +/- 1 respectively

if AverageVal > AverageVal[1] then SignalVal = 1;
if AverageVal < AverageVal[1] then SignalVal = -1;

// Begin Breakout Entry & Risk Stop Exit Calculations

if SignalVal <> 0 then begin
curHigh = high;
curLow = low;
end;

// Long Entry

if SignalVal = 1 and marketposition = 0 then begin
entryHigh = curHigh;
exitLow = curLow;
buy ("Long") next bar at entryHigh Stop;
end;

// Long Exit

if marketposition = 1 then begin
sell ("Long Stop Loss") next bar at exitLow stop;
end;

// Short Entry

if SignalVal = -1 and marketposition = 0 then begin
entryLow = curLow;
exitHigh = curHigh;
sellshort ("Short") next bar at entryLow Stop;
end;

// Short Exit

if marketposition = -1 then begin
buytocover ("Short Stop Loss") next bar at exitHigh stop;
end;


Return to “MultiCharts”