I want to enter positions either long or short manually. Then I want that position to close based on the below script.
Code:
# Start Least squares future productor
#LeastSquares created by tradescripter
#Dec. 24, 2010 Merry Christmas
#It uses the Least-Squares Method to forecast a new price
#For an explanation of Least-Squares method see:
#http://en.wikiversity.org/wiki/Least-Squares_Method
#Hint PriceColorOn: Affect the color of the Price bars according to the signals of this indicator\n<b>Default is Yes
#Hint ArrowsOn: Plot Arrows at signal changes \n<b>Default is Yes
#Hint PredictionLineOn: Plot the indicator \n<b>Default is Yes
#Hint ShowTodayOnly: Plot only the most recent arrows in order not to clutter the Price history\nMakes it easier to see prior prices \n<b>Default is no
#Hint ShowExtraDays: Further adjustment to Show Only Today \n<b>Default is 0
#Hint space: Allows you to move the arrows, and avoid stepping on other arrows when multiple studies are shown \n<b>Default is 0.333
#Hint length: Touch this only if you are an expert! Just kidding. \n<b>Default is 9
#Hint price: For the experimenter \n<b>Default is HL2
#FuturePredictor
input price = close;
input length = 9;
input PredictionLineOn = Yes;
# Input BubblesOn = No;
input ShowTodayOnly = no;
input ShowExtraDays = 0;
input space = 0.333;
def Today = if !ShowTodayOnly then 1 else if GetDay() + ShowExtraDays >= GetLastDay() then 1 else 0;
def AvgPrice = Average(price, length);
def SumTime = fold i = 1 to length + 1 with x = 0 do x + i;
def AvgTime = SumTime / length;
#sx is the sum of all the deviations from time for the last x bars
def sx = fold j = 1 to length + 1 with y = 0 do y + ((j - AvgTime) * (GetValue(price, length - j, length + 1) - AvgPrice));
#sy is the sum of all the deviations from price for the last x bars
def sy = fold k = 1 to length + 1 with z = 0 do z + (Power(k - AvgTime, 2));
#m is the slope of the line, b is the slope intercept of the line in the equation y = mx + b
def m = sx / sy;
def b = AvgPrice - m * AvgTime;
def Prediction = (m * (length + 1)) + b;
plot FuturePrediction = if !PredictionLineOn then Double.NaN else Prediction;
FuturePrediction.AssignValueColor(if FuturePrediction > FuturePrediction[1] then Color.GREEN else Color.RED);
FuturePrediction.SetLineWeight(3);
FuturePrediction.HideBubble();
# End Least Squares Future Predictor section
# Start Order section
# def buy = Bullish and FuturePrediction > FuturePrediction[1] and Uptrend and atr > .20 ;
def buy = FuturePrediction > FuturePrediction[1] ;
def sell = FuturePrediction < FuturePrediction[1] ;
AddOrder(OrderType.BUY_TO_CLOSE, buy);
AddOrder(OrderType.SELL_TO_CLOSE, sell);
Last edited by a moderator: