# buy or sell, higher or lower, than last signal

Questions about MultiCharts and user contributed studies.
virginiatrader
Posts: 79
Joined: 05 May 2007
Location: Virginia
Has thanked: 5 times
Been thanked: 5 times

### buy or sell, higher or lower, than last signal

I have coded a very simple trend-following system:

"If the long term moving average is greater than the same moving average N-1, then buy, at the close, if the current bar high is greater than the highest high from N days ago, or if the long term moving average is less than the same moving average N-1, then sell, at the close, if the current bar low is less than the lowest low from N days ago."

I would like to code it to buy or sell if the new signal "is greater than or less than" the price of the last signal in the same direction, but cannot figure out how to do so.

Any help is appreciated in advance.

virginiatrader

TJ
Posts: 6638
Joined: 29 Aug 2006
Location: Global Citizen
Has thanked: 971 times
Been thanked: 1919 times

### Re: buy or sell, higher or lower, than last signal

virginiatrader wrote:I have coded a very simple trend-following system:

"If the long term moving average is greater than the same moving average N-1, then buy, at the close, if the current bar high is greater than the highest high from N days ago, or if the long term moving average is less than the same moving average N-1, then sell, at the close, if the current bar low is less than the lowest low from N days ago."

I would like to code it to buy or sell if the new signal "is greater than or less than" the price of the last signal in the same direction, but cannot figure out how to do so.

Any help is appreciated in advance.

virginiatrader

first, set up a storage variable.
when the condition is met, put the value in the storage variable.
when the condition is met again, you can evaluate the values.

Code: Select all

`variable:buy.price(0);if condition1 = true thenbuy.price = close;if buy.price > buy.price[1] thenbuy....`

virginiatrader
Posts: 79
Joined: 05 May 2007
Location: Virginia
Has thanked: 5 times
Been thanked: 5 times

### Re: buy or sell, higher or lower, than last signal

TJ:

Thanks for the help. I added the variable as recommended, but am mis-directed as to how and where to set the condition. Here is the current system code:

Inputs: lengthTrendAvg(260), lengthBreakOut(20);
Variable: buy.price (0);
Variable: sell.price (0);
******************************************************************************
If xaverage(close, lengthTrendAvg) > xaverage(close, lengthTrendAvg) [1]
Then begin
If close > highest(high, lengthBreakOut) [1] and close > open
Then buy 1 contract next bar at open;
End;
******************************************************************************
If xaverage(close, lengthTrendAvg) < xaverage(close, lengthTrendAvg) [1]
Then begin
If close < lowest(low, lengthBreakOut) [1] and close < open
Then sellshort 1 contract next bar at open;
End;

Thanks again for any help offered.

virginiatrader

TJ
Posts: 6638
Joined: 29 Aug 2006
Location: Global Citizen
Has thanked: 971 times
Been thanked: 1919 times

### Re: buy or sell, higher or lower, than last signal

virginiatrader wrote:TJ:

Thanks for the help. I added the variable as recommended, but am mis-directed as to how and where to set the condition...

I would suggest you to start with post #1 here:
viewtopic.php?f=1&t=6929

Return to “MultiCharts”