# Regarding "Optimize" Function

I have a query related to "Optimize" function. In below code, I want to get the best combination values for "Change in ADX", "Change in Open Price" & "Change in Close Price" for Yesterday & a Day Before to Yesterday.

I gone through all the Topics related to "Optimize" function but still need guidance.

Optimized values successfully for "Long & Short EMAs". But couldn't optimize variable PB1, PB2 & PB3 as mentioned in below code ...

_N(Title = StrFormat("{{NAME}} - {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo %g, Close %g (%.1f%%) {{VALUES}}", O, H, L, C, SelectedValue( ROC( C, 1 ) ) ));
SetChartOptions(0,chartShowArrows|chartShowDates); //Enable X-Axis (Date/Time Axis)

Plot(Close,"Candles",colorDefault, styleCandle | styleThick); //Plot Candles

OptimizerSetEngine("spso");
OptimizerSetOption("Runs", 1 );
OptimizerSetOption("MaxEval", 1000 );

d1 = DateNum() > Ref(DateNum(),-1);
to = ValueWhen(d1,O,1) ; //Today's Open Price
YC = abs(Ref(Close,-1)); // Yesterday's Close Price

PB1 = abs(YC - (Ref(YC,-1)));
PB1 = Optimize("PB1", 0.02, 0.01, 10, 0.01);

PB2 = Abs((Ref(to,-1))- (Ref(to,-2)));
PB2 = Optimize("PB2", 0.02, 0.01, 10, 0.01);

PB3 = Optimize("PB3", 0.02, 0.01, 10, 0.01);

par1 = Param("Short EMA",76,2,100,1);
par2 = Param("Long EMA",81,2,100,1);
Plot(EMA(Close,par1),"EMA-10",colorGreen , styleLine | styleThick); //Plot Short EMA lines
Plot(EMA(Close,par2),"EMA-20",colorBlue , styleLine | styleThick); //Plot Long EMA Lines

Buy = Cross(EMA(Close,par1),EMA(Close,par2)) AND PB1 > 0.02 AND PB2 > 0.02 AND PB3 > 0.02;

Sell = Cross(EMA(Close,par2),EMA(Close,par1));

SetPositionSize(30000,spsValue);

/* Plot Buy and Sell Signal Arrows */
PlotShapes(IIf(Buy, shapeSquare, shapeNone),colorGreen, 0, L, Offset=-40);
PlotShapes(IIf(Sell, shapeSquare, shapeNone),colorRed, 0, H, Offset=40);
PlotShapes(IIf(Sell, shapeSquare, shapeNone),colorOrange, 0,H, Offset=50);
PlotShapes(IIf(Sell, shapeDownArrow, shapeNone),colorWhite, 0,H, Offset=-45);

_SECTION_END();

Tushar Gawankar

``````_SECTION_BEGIN("Buy Only Strategy");

_N(Title = StrFormat("{{NAME}} - {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo %g, Close %g (%.1f%%) {{VALUES}}", O, H, L, C, SelectedValue( ROC( C, 1 ) ) ));
SetChartOptions(0,chartShowArrows|chartShowDates); //Enable X-Axis (Date/Time Axis)

Plot(Close,"Candles",colorDefault, styleCandle | styleThick); //Plot Candles

OptimizerSetEngine("spso");
OptimizerSetOption("Runs", 1 );
OptimizerSetOption("MaxEval", 1000 );

d1 = DateNum() > Ref(DateNum(),-1);
to = ValueWhen(d1,O,1) ; //Today's Open Price
YC = abs(Ref(Close,-1)); // Yesterday's Close Price

PB1 = abs(YC - (Ref(YC,-1)));
PB1 = Optimize("PB1", 0.02, 0.01, 10, 0.01);

PB2 = Abs((Ref(to,-1))- (Ref(to,-2)));
PB2 = Optimize("PB2", 0.02, 0.01, 10, 0.01);

PB3 = Optimize("PB3", 0.02, 0.01, 10, 0.01);

par1 = Param("Short EMA",76,2,100,1);
par2 = Param("Long EMA",81,2,100,1);
Plot(EMA(Close,par1),"EMA-10",colorGreen , styleLine | styleThick); //Plot Short EMA lines
Plot(EMA(Close,par2),"EMA-20",colorBlue , styleLine | styleThick); //Plot Long EMA Lines

Buy = Cross(EMA(Close,par1),EMA(Close,par2)) AND PB1 > 0.02 AND PB2 > 0.02 AND PB3 > 0.02;

Sell = Cross(EMA(Close,par2),EMA(Close,par1));

SetPositionSize(30000,spsValue);

/* Plot Buy and Sell Signal Arrows */
PlotShapes(IIf(Buy, shapeSquare, shapeNone),colorGreen, 0, L, Offset=-40);
PlotShapes(IIf(Sell, shapeSquare, shapeNone),colorRed, 0, H, Offset=40);
PlotShapes(IIf(Sell, shapeSquare, shapeNone),colorOrange, 0,H, Offset=50);
PlotShapes(IIf(Sell, shapeDownArrow, shapeNone),colorWhite, 0,H, Offset=-45);

_SECTION_END();
``````

try this one

``````_SECTION_BEGIN("Buy Only Strategy");

_N(Title = StrFormat("{{NAME}} - {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo %g, Close %g (%.1f%%) {{VALUES}}", O, H, L, C, SelectedValue( ROC( C, 1 ) ) ));
SetChartOptions(0,chartShowArrows|chartShowDates); //Enable X-Axis (Date/Time Axis)

Plot(Close,"Candles",colorDefault, styleCandle | styleThick); //Plot Candles

OptimizerSetEngine("spso");
OptimizerSetOption("Runs", 1 );
OptimizerSetOption("MaxEval", 1000 );

PB1 = abs(Ref(Close,-1) - Ref(Close,-2));
//PB1 = Optimize("PB1", 0.02, 0.01, 10, 0.01);

PB2 = Abs(Ref(Open,-1)- Ref(Open,-2));
//PB2 = Optimize("PB2", 0.02, 0.01, 10, 0.01);
ADX_period = Optimize("PB3", 14, 2, 30, 2);
//PB3 = Optimize("PB3", 0.02, 0.01, 10, 0.01);

par1 = Optimize("Short EMA",76,2,100,1);
par2 = Optimize("Long EMA",81,2,100,1);
Plot(EMA(Close,par1),"EMA-10",colorGreen , styleLine | styleThick); //Plot Short EMA lines
Plot(EMA(Close,par2),"EMA-20",colorBlue , styleLine | styleThick); //Plot Long EMA Lines

Buy = Cross(EMA(Close,par1),EMA(Close,par2)) AND PB1 > 0.02 AND PB2 > 0.02 AND PB3 > 0.02;

Sell = Cross(EMA(Close,par2),EMA(Close,par1));

SetPositionSize(30000,spsValue);

/* Plot Buy and Sell Signal Arrows */
PlotShapes(IIf(Buy, shapeSquare, shapeNone),colorGreen, 0, L, Offset=-40);