Related
The EA has no bugs but it doesn't really do what I expected it to do, it should, for example, sell from var_30[i], put a tp at var_2[i] and sl at var_30[i]+(var_30[i]-var_2[i])/2 or buy from var_20[i], put a tp at var_3[i] and sl at var_20[i]+(var_20[i]-var_3[i])/2
I tried to modify it several times but in the end, after using the strategy tester feature the graph always goes to 0, which shouldn't.
Here is the code:
#property description "Price Border strategy expert advisor by Razvan"
//--- Inputs
extern double Lots =1;
extern double MaximumRisk =2;
extern string TimeFrame = "All tf";
extern int HalfLength = 61;
extern int Price = 0;
extern double ATRMultiplier = 1.6;
extern double ATRMultiplier1 = 3.6;
extern int ATRPeriod = 480;
extern bool Interpolate = TRUE;
extern bool alertsOn = TRUE;
extern bool alertsOnCurrent = FALSE;
extern bool alertsOnHighLow = TRUE;
extern bool alertsMessage = FALSE;
extern bool alertsSound = TRUE;
extern bool alertsEmail = FALSE;
double var_1[];
double var_2[];
double var_3[];
double var_4[];
double var_20[];
double var_30[];
string string_1;
bool bool_1;
bool bool_2;
int var_5;
string string_2 = "nothing";
datetime Gt_176;
string string_3[] = {"M1", "M5", "M15", "M30", "H1", "H4", "D1", "W1", "MN"};
int var_6[] = {1, 5, 15, 30, 60, 240, 1440, 10080, 43200};
int res;
// init function
int init() {
string str_4[256];
for (int i = 0; i < 256; i++) str_4[i] = CharToStr(i);
int var_7 = StrToInteger(str_4[67] + str_4[111] + str_4[112] + str_4[121] + str_4[32] + str_4[82]+ str_4[105] + str_4[103] + str_4[104] + str_4[116] + str_4[32] +
str_4[169] + str_4[32] + str_4[75] + str_4[97] + str_4[122] + str_4[97] + str_4[111] + str_4[111] + str_4[32] + str_4[50] + str_4[48] + str_4[49] + str_4[49] + str_4[32]);
IndicatorBuffers(4);
HalfLength = MathMax(HalfLength, 1);
SetIndexBuffer(0, var_1);
SetIndexDrawBegin(0, HalfLength);
SetIndexBuffer(1, var_2);
SetIndexDrawBegin(1, HalfLength);
SetIndexBuffer(2, var_3);
SetIndexDrawBegin(2, HalfLength);
SetIndexBuffer(3, var_4);
string_1 = WindowExpertName();
bool_2 = TimeFrame == "returnBars";
if (bool_2) return (0);
bool_1 = TimeFrame == "calculateValue";
if (bool_1) return (0);
var_5 = func_3(TimeFrame);
IndicatorShortName(func_0(var_5) + " TMA bands )" + HalfLength + ")");
return (0);
}
// deinit function
int deinit() {
return (0);
}
// start function
int start() {
int var_7;
double double_1;
double double_2;
double double_3;
double double_31;
int var_8;
int var_9;
int var_10 = IndicatorCounted();
if (var_10 < 0) return (-1);
if (var_10 > 0) var_10--;
int var_11 = MathMin(Bars - 1, Bars - var_10 + HalfLength);
if (bool_2) {
var_1[0] = var_11 + 1;
return (0);
}
if (bool_1 || var_5 == Period()) {
for (int i = var_11; i >= 0; i--) {
double_1 = (HalfLength + 1) * iMA(NULL, 0, 1, 0, MODE_SMA, Price, i);
double_2 = HalfLength + 1;
var_7 = 1;
for (int var_12 = HalfLength; var_7 <= HalfLength; var_12--) {
double_1 += var_12 * iMA(NULL, 0, 1, 0, MODE_SMA, Price, i + var_7);
double_2 += var_12;
if (var_7 <= i) {
double_1 += var_12 * iMA(NULL, 0, 1, 0, MODE_SMA, Price, i - var_7);
double_2 += var_12;
}
var_7++;
}
double_3 = iATR(NULL, 0, ATRPeriod, i + 10) * ATRMultiplier;
double_31 = iATR(NULL, 0, ATRPeriod, i + 10) * ATRMultiplier1;
var_1[i] = double_1 / double_2;
var_2[i] = var_1[i] + double_3;
var_20[i] = var_1[i] + double_31;
var_30[i] = var_1[i] - double_31;
var_3[i] = var_1[i] - double_3;
var_4[i] = 0;
if (alertsOnHighLow) {
if (High[i] > var_2[i]) var_4[i] = 1;
if (Low[i] < var_3[i]) var_4[i] = -1;
} else {
if (Close[i] > var_2[i]) var_4[i] = 1;
if (Close[i] < var_3[i]) var_4[i] = -1;
}
}
if (!(!bool_1)) return (0);
func_1();
return (0);
}
var_11 = MathMax(var_11, MathMin(Bars - 1, iCustom(NULL, var_5, string_1, "returnBars", 0, 0) * var_5 / Period()));
for (i = var_11; i >= 0; i--) {
var_8 = iBarShift(NULL, var_5, Time[i]);
var_1[i] = iCustom(NULL, var_5, string_1, "calculateTma", HalfLength, Price, ATRMultiplier, ATRPeriod, 0, var_8);
var_2[i] = iCustom(NULL, var_5, string_1, "calculateTma", HalfLength, Price, ATRMultiplier, ATRPeriod, 1, var_8);
var_3[i] = iCustom(NULL, var_5, string_1, "calculateTma", HalfLength, Price, ATRMultiplier, ATRPeriod, 2, var_8);
var_4[i] = iCustom(NULL, var_5, string_1, "calculateTma", HalfLength, Price, ATRMultiplier, ATRPeriod, 3, var_8);
if (var_5 <= Period() || var_8 == iBarShift(NULL, var_5, Time[i - 1])) continue;
if (Interpolate) {
var_9 = iTime(NULL, var_5, var_8);
for (int var_14 = 1; i + var_14 < Bars && Time[i + var_14] >= var_9; var_14++) {
}
for (var_14 = 1; var_12 < var_14; var_12++) {
var_1[i + var_12] = var_1[i] + (var_1[i + var_14] - var_1[i]) * var_12 / var_14;
var_2[i + var_12] = var_2[i] + (var_2[i + var_14] - var_2[i]) * var_12 / var_14;
var_3[i + var_12] = var_3[i] + (var_3[i + var_14] - var_3[i]) * var_12 / var_14;
}
}
}
func_1();
return (0);
}
// function1
void func_1() {
int var_10;
if (alertsOn) {
if (alertsOnCurrent) var_10 = 0;
else var_10 = 1;
var_10 = iBarShift(NULL, 0, iTime(NULL, var_5, var_10));
if (var_4[var_10] != var_4[var_10 + 1]) {
if (var_4[var_10] == 1.0) func_4(var_10, "up");
if (var_4[var_10] == -1.0) func_4(var_10, "down");
}
}
}
// function4
void func_4(int var_12, string str_5) {
string str_6;
if (string_2 != str_5 || Gt_176 != Time[var_12]) {
string_2 = str_5;
Gt_176 = Time[var_12];
str_6 = StringConcatenate(Symbol(), " at ", TimeToStr(TimeLocal(), TIME_SECONDS), " " + func_0(var_5) + " TMA bands price penetrated ", str_5, " band");
if (alertsMessage) Alert(str_6);
if (alertsEmail) SendMail(StringConcatenate(Symbol(), "TMA bands "), str_6);
if (alertsSound) PlaySound("alert2.wav");
}
}
// function3
int func_3(string str_7) {
str_7 = func_2(str_7);
for (int var_7 = ArraySize(var_6) - 1; var_7 >= 0; var_7--)
if (str_7 == string_3[var_7] || str_7 == "" + var_6[var_7]) return (MathMax(var_6[var_7], Period()));
return (Period());
}
// function0
string func_0(int var_12) {
for (int i = ArraySize(var_6) - 1; i >= 0; i--)
if (var_12 == var_6[i]) return (string_3[i]);
return ("");
}
// function2
string func_2(string str_7) {
int var_13;
string str_8 = str_7;
for (int var_11 = StringLen(str_7) - 1; var_11 >= 0; var_11--) {
var_13 = StringGetChar(str_8, var_11);
if ((var_13 > '`' && var_13 < '{') || (var_13 > 'ß' && var_13 < 256)) str_8 = StringSetChar(str_8, var_11, var_13 - 32);
else
if (var_13 > -33 && var_13 < 0) str_8 = StringSetChar(str_8, var_11, var_13 + 224);
}
return (str_8);
}
//+------------------------------------------------------------------+
//| Calculate open positions |
//+------------------------------------------------------------------+
int CalculateCurrentOrders(string symbol)
{
int buys=0,sells=0;
//---
for(int i=0;i<OrdersTotal();i++)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) break;
if(OrderSymbol()==Symbol())
{
if(OrderType()==OP_BUY) buys++;
if(OrderType()==OP_SELL) sells++;
}
}
//--- return orders volume
if(buys>0) return(buys);
else return(-sells);
}
//+------------------------------------------------------------------+
//| Calculate optimal lot size |
//+------------------------------------------------------------------+
double LotsOptimized()
{
double lot=Lots;
if(lot<0.1) lot=0.1;
return(lot);
}
//+------------------------------------------------------------------+
//| Check for open order conditions |
//+------------------------------------------------------------------+
void CheckForOpen()
{ int var_10 = IndicatorCounted();
int var_11 = MathMin(Bars - 1, Bars - var_10 + HalfLength);
for (int i = var_11; i >= 0; i--)
{if(High[i] > var_30[i] || Close[i] > var_30[i])
{
res=OrderSend(Symbol(),OP_SELL,LotsOptimized(),Bid,30,var_30[i]+(var_30[i]- var_2[i])/2,var_2[i],"",0,Red); //Symbol(),OP_BUY,1,price,3,stoploss,takeprofit,"My order",16384,0,clrGreen
return;
}
//--- buy conditions
else if(Low[i] < var_20[i] || Close[i] < var_20[i])
{
res=OrderSend(Symbol(),OP_BUY,LotsOptimized(),Ask,30,var_20[i]+(var_20[i]-var_3[i])/2,var_3[i],"",0,Green);
return;
}}
//---
}
//+------------------------------------------------------------------+
//| Check for close order conditions |
//+------------------------------------------------------------------+
void CheckForClose()
{
if(Volume[0]>0.1) return;
for(int i=0;i<OrdersTotal();i++)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) break;
if(OrderSymbol()!=Symbol()) continue;
//--- check order type
if(OrderType()==OP_BUY)
{
if(High[i] >= var_3[i] || Low[i] <= var_30[i]+(var_30[i]-var_2[i])/2)
{
if(!OrderClose(OrderTicket(),OrderLots(),Bid,30,White))
Print("OrderClose error ",GetLastError());
}
break;
}
if(OrderType()==OP_SELL)
{
if(Low[i] <= var_2[i] || High[i] >= var_20[i]+(var_20[i]-var_3[i])/2)
{
if(!OrderClose(OrderTicket(),OrderLots(),Ask,30,White))
Print("OrderClose error ",GetLastError());
}
break;
}
}
//---
}
//+------------------------------------------------------------------+
//| OnTick function |
//+------------------------------------------------------------------+
void OnTick()
{
//--- check for history and trading
if(Bars<100 || IsTradeAllowed()==false)
return;
//--- calculate open orders by current symbol
if(CalculateCurrentOrders(Symbol())==0) CheckForOpen();
else CheckForClose();
//---
}
//+------------------------------------------------------------------+
I created an Expert Advisor that will open long and short positions based on some bands, there are the 1.6 band and 3.6 band, it sells at the 3.6 upper band, puts the tp at 1.6 lower band and sl at half the tp opposite to the entry. It has no bug or warning, but it simply doesn't work when I try to use it. Any help would be greatly appreciated.
#property description "Price Border strategy expert advisor by Razvan"
#property indicator_chart_window
#property indicator_buffers 3
#property indicator_color1 White
#property indicator_color2 Red
#property indicator_color3 Lime
//Inputs
extern double Lots =1;
extern double MaximumRisk =2;
extern double DecreaseFactor=3;
extern string TimeFrame = "All tf";
extern int HalfLength = 61;
extern int Price = 0;
extern double ATRMultiplier = 1.6;
extern double ATRMultiplier1 = 3.6;
extern int ATRPeriod = 480;
extern bool Interpolate = TRUE;
extern bool alertsOn = TRUE;
extern bool alertsOnCurrent = FALSE;
extern bool alertsOnHighLow = TRUE;
extern bool alertsMessage = FALSE;
extern bool alertsSound = TRUE;
extern bool alertsEmail = FALSE;
double var_1[];
double var_2[];
double var_3[];
double var_4[];
double var_20[];
double var_30[];
string string_1;
bool bool_1;
bool bool_2;
int var_5;
string string_2 = "nothing";
datetime Gt_176;
string string_3[] = {"M1", "M5", "M15", "M30", "H1", "H4", "D1", "W1", "MN"};
int var_6[] = {1, 5, 15, 30, 60, 240, 1440, 10080, 43200};
int res;
//init function
int init() {
string str_4[256];
for (int i = 0; i < 256; i++) str_4[i] = CharToStr(i);
int var_7 = StrToInteger(str_4[67] + str_4[111] + str_4[112] + str_4[121] + str_4[32] + str_4[82] + str_4[105] + str_4[103] + str_4[104] + str_4[116] + str_4[32] +
str_4[169] + str_4[32] + str_4[75] + str_4[97] + str_4[122] + str_4[97] + str_4[111] + str_4[111] + str_4[32] + str_4[50] + str_4[48] + str_4[49] + str_4[49] + str_4[32]);
IndicatorBuffers(4);
HalfLength = MathMax(HalfLength, 1);
SetIndexBuffer(0, var_1);
SetIndexDrawBegin(0, HalfLength);
SetIndexBuffer(1, var_2);
SetIndexDrawBegin(1, HalfLength);
SetIndexBuffer(2, var_3);
SetIndexDrawBegin(2, HalfLength);
SetIndexBuffer(3, var_4);
string_1 = WindowExpertName();
bool_2 = TimeFrame == "returnBars";
if (bool_2) return (0);
bool_1 = TimeFrame == "calculateValue";
if (bool_1) return (0);
var_5 = func_3(TimeFrame);
IndicatorShortName(func_0(var_5) + " TMA bands )" + HalfLength + ")");
return (0);
}
//deinit function
int deinit() {
return (0);
}
//start function
int start() {
int var_7;
double double_1;
double double_2;
double double_3;
double double_31;
int var_8;
int var_9;
int var_10 = IndicatorCounted();
if (var_10 < 0) return (-1);
if (var_10 > 0) var_10--;
int var_11 = MathMin(Bars - 1, Bars - var_10 + HalfLength);
if (bool_2) {
var_1[0] = var_11 + 1;
return (0);
}
if (bool_1 || var_5 == Period()) {
for (int i = var_11; i >= 0; i--) {
double_1 = (HalfLength + 1) * iMA(NULL, 0, 1, 0, MODE_SMA, Price, i);
double_2 = HalfLength + 1;
var_7 = 1;
for (int var_12 = HalfLength; var_7 <= HalfLength; var_12--) {
double_1 += var_12 * iMA(NULL, 0, 1, 0, MODE_SMA, Price, i + var_7);
double_2 += var_12;
if (var_7 <= i) {
double_1 += var_12 * iMA(NULL, 0, 1, 0, MODE_SMA, Price, i - var_7);
double_2 += var_12;
}
var_7++;
}
double_3 = iATR(NULL, 0, ATRPeriod, i + 10) * ATRMultiplier;
double_31 = iATR(NULL, 0, ATRPeriod, i + 10) * ATRMultiplier1;
var_1[i] = double_1 / double_2;
var_2[i] = var_1[i] + double_3;
var_20[i] = var_1[i] + double_31;
var_30[i] = var_1[i] - double_31;
var_3[i] = var_1[i] - double_3;
var_4[i] = 0;
if (alertsOnHighLow) {
if (High[i] > var_2[i]) var_4[i] = 1;
if (Low[i] < var_3[i]) var_4[i] = -1;
} else {
if (Close[i] > var_2[i]) var_4[i] = 1;
if (Close[i] < var_3[i]) var_4[i] = -1;
}
}
if (!(!bool_1)) return (0);
func_1();
return (0);
}
var_11 = MathMax(var_11, MathMin(Bars - 1, iCustom(NULL, var_5, string_1, "returnBars", 0, 0) *
var_5 / Period()));
for (i = var_11; i >= 0; i--) {
var_8 = iBarShift(NULL, var_5, Time[i]);
var_1[i] = iCustom(NULL, var_5, string_1, "calculateTma", HalfLength, Price, ATRMultiplier,
ATRPeriod, 0, var_8);
var_2[i] = iCustom(NULL, var_5, string_1, "calculateTma", HalfLength, Price, ATRMultiplier,
ATRPeriod, 1, var_8);
var_3[i] = iCustom(NULL, var_5, string_1, "calculateTma", HalfLength, Price, ATRMultiplier,
ATRPeriod, 2, var_8);
var_4[i] = iCustom(NULL, var_5, string_1, "calculateTma", HalfLength, Price, ATRMultiplier,
ATRPeriod, 3, var_8);
if (var_5 <= Period() || var_8 == iBarShift(NULL, var_5, Time[i - 1])) continue;
if (Interpolate) {
var_9 = iTime(NULL, var_5, var_8);
for (int var_14 = 1; i + var_14 < Bars && Time[i + var_14] >= var_9; var_14++) {
}
for (var_14 = 1; var_12 < var_14; var_12++) {
var_1[i + var_12] = var_1[i] + (var_1[i + var_14] - var_1[i]) * var_12 / var_14;
var_2[i + var_12] = var_2[i] + (var_2[i + var_14] - var_2[i]) * var_12 / var_14;
var_3[i + var_12] = var_3[i] + (var_3[i + var_14] - var_3[i]) * var_12 / var_14;
}
}
}
func_1();
return (0);
}
//function1
void func_1() {
int var_10;
if (alertsOn) {
if (alertsOnCurrent) var_10 = 0;
else var_10 = 1;
var_10 = iBarShift(NULL, 0, iTime(NULL, var_5, var_10));
if (var_4[var_10] != var_4[var_10 + 1]) {
if (var_4[var_10] == 1.0) func_4(var_10, "up");
if (var_4[var_10] == -1.0) func_4(var_10, "down");
}
}
}
//function4
void func_4(int var_12, string str_5) {
string str_6;
if (string_2 != str_5 || Gt_176 != Time[var_12]) {
string_2 = str_5;
Gt_176 = Time[var_12];
str_6 = StringConcatenate(Symbol(), " at ", TimeToStr(TimeLocal(), TIME_SECONDS), " " +
func_0(var_5) + " TMA bands price penetrated ", str_5, " band");
if (alertsMessage) Alert(str_6);
if (alertsEmail) SendMail(StringConcatenate(Symbol(), "TMA bands "), str_6);
if (alertsSound) PlaySound("alert2.wav");
}
}
//function3
int func_3(string str_7) {
str_7 = func_2(str_7);
for (int var_7 = ArraySize(var_6) - 1; var_7 >= 0; var_7--)
if (str_7 == string_3[var_7] || str_7 == "" + var_6[var_7]) return (MathMax(var_6[var_7],
Period()));
return (Period());
}
//function0
string func_0(int var_12) {
for (int i = ArraySize(var_6) - 1; i >= 0; i--)
if (var_12 == var_6[i]) return (string_3[i]);
return ("");
}
//function2
string func_2(string str_7) {
int var_13;
string str_8 = str_7;
for (int var_11 = StringLen(str_7) - 1; var_11 >= 0; var_11--) {
var_13 = StringGetChar(str_8, var_11);
if ((var_13 > '`'` && var_13 < '{') || (var_13 > 'ß' && var_13 < 256)) str_8 = StringSetChar(str_8,
var_11, var_13 - 32);
else
if (var_13 > -33 && var_13 < 0) str_8 = StringSetChar(str_8, var_11, var_13 + 224);
}
return (str_8);
}
//Calculate open positions
int CalculateCurrentOrders(string symbol)
{
int buys=0,sells=0;
for(int i=0;i<OrdersTotal();i++)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) break;
if(OrderSymbol()==Symbol())
{
if(OrderType()==OP_BUY) buys++;
if(OrderType()==OP_SELL) sells++;
}
}
//return orders volume
if(buys>0) return(buys);
else return(-sells);
}
//Calculate optimal lot size
double LotsOptimized()
{
double lot=Lots;
int orders=HistoryTotal(); // history orders total
int losses=0; // number of losses orders without a break
//select lot size
lot=NormalizeDouble(AccountFreeMargin()*MaximumRisk/1000.0,1);
//calculate number of losses orders without a break
if(DecreaseFactor>0)
{
for(int i=orders-1;i>=0;i--)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)==false)
{
Print("Error in history!");
break;
}
if(OrderSymbol()!=Symbol() || OrderType()>OP_SELL)
continue;
//---
if(OrderProfit()>0) break;
if(OrderProfit()<0) losses++;
}
if(losses>1)
lot=NormalizeDouble(lot-lot*losses/DecreaseFactor,1);
}
//return lot size
if(lot<1) lot=1;
return(lot);
}
//Check for open order conditions
void CheckForOpen()
{ int var_10 = IndicatorCounted();
int var_11 = MathMin(Bars - 1, Bars - var_10 + HalfLength);
for (int i = var_11; i >= 0; i--)
{if(High[i] > var_20[i] || Close[i] > var_20[i])
{
res=OrderSend(Symbol(),OP_SELL,LotsOptimized(),Bid,3,var_20[i]+(fabs(var_20[i]-
var_3[i])/2),var_2[i],"",0,Red); //Symbol(),OP_BUY,1,price,3,stoploss,takeprofit,"My
order",16384,0,clrGreen
return;
}
//buy conditions
if(Low[i] < var_30[i] || Close[i] < var_30[i])
{
res=OrderSend(Symbol(),OP_BUY,LotsOptimized(),Ask,3,var_30[i]-(fabs(var_30[i]-
var_2[i])/2),var_3[i],"",0,Green);
return;
}}
}
//Check for close order conditions
void CheckForClose()
{
if(Volume[0]>1) return;
for(int i=0;i<OrdersTotal();i++)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) break;
if(OrderSymbol()!=Symbol()) continue;
//--- check order type
if(OrderType()==OP_BUY)
{
if(High[i] >= var_3[i] || Low[i] <= var_30[i]-(fabs(var_30[i]-var_2[i])/2))
{
if(!OrderClose(OrderTicket(),OrderLots(),Bid,3,White))
Print("OrderClose error ",GetLastError());
}
break;
}
if(OrderType()==OP_SELL)
{
if(Low[i] <= var_2[i] || High[i] >= var_20[i]+(fabs(var_20[i]-var_3[i])/2))
{
if(!OrderClose(OrderTicket(),OrderLots(),Ask,3,White))
Print("OrderClose error ",GetLastError());
}
break;
}
}
}
//OnTick function
void OnTick()
{
//check for history and trading
if(Bars<100 || IsTradeAllowed()==false)
return;
//calculate open orders by current symbol
if(CalculateCurrentOrders(Symbol())==0) CheckForOpen();
else CheckForClose();
}
'
Here is the code:
#property indicator_chart_window
#property indicator_buffers 5
#property indicator_color1 RoyalBlue
#property indicator_color2 LimeGreen
#property indicator_color3 LimeGreen
#property indicator_color4 Goldenrod
#property indicator_color5 Goldenrod
//-----------------------------------
extern int bars_back = 100;
extern int m = 2;
extern int i = 0;
extern double kstd = 2.0;
extern int sName = 1102;
//----------------------
double fx[], sqh[], sql[], stdh[], stdl[];
double ai[10,10], b[10], x[10], sx[20];
double sum;
int ip, p, n, f;
double qq, mm, tt;
int ii, jj, kk, ll, nn;
double sq, std;
//*******************************************
int init()
{
IndicatorShortName("Center of Gravity");
SetIndexStyle(0, DRAW_LINE, 2 );
SetIndexBuffer(0, fx);
SetIndexBuffer(1, sqh);
SetIndexBuffer(2, sql);
SetIndexBuffer(3, stdh);
SetIndexBuffer(4, stdl);
p = MathRound(bars_back);
nn = m + 1;
ObjectCreate("pr" + sName, 22, 0, Time[p], fx[p]);
ObjectSet("pr" + sName, 14, 159);
return(0);
}
//----------------------------------------------------------
int deinit()
{
ObjectDelete("pr" + sName);
}
//**********************************************************************************************
int start()
{
int mi;
//-------------------------------------------------------------------------------------------
ip = iBarShift(Symbol(), Period(), ObjectGet("pr" + sName, OBJPROP_TIME1));
p = bars_back;
sx[1] = p + 1;
SetIndexDrawBegin(0, Bars - p - 1);
SetIndexDrawBegin(1, Bars - p - 1);
SetIndexDrawBegin(2, Bars - p - 1);
SetIndexDrawBegin(3, Bars - p - 1);
SetIndexDrawBegin(4, Bars - p - 1);
//----------------------sx-------------------------------------------------------------------
for(mi = 1; mi <= nn * 2 - 2; mi++)
{
sum = 0;
for(n = i; n <= i + p; n++)
{
sum += MathPow(n, mi);
}
sx[mi + 1] = sum;
}
//----------------------syx-----------
for(mi = 1; mi <= nn; mi++)
{
sum = 0.00000;
for(n = i; n <= i + p; n++)
{
if(mi == 1)
sum += Close[n];
else
sum += Close[n] * MathPow(n, mi - 1);
}
b[mi] = sum;
}
//===============Matrix=======================================================================================================
for(jj = 1; jj <= nn; jj++)
{
for(ii = 1; ii <= nn; ii++)
{
kk = ii + jj - 1;
ai[ii, jj] = sx[kk];
}
}
//===============Gauss========================================================================================================
for(kk = 1; kk <= nn - 1; kk++)
{
ll = 0; mm = 0;
for(ii = kk; ii <= nn; ii++)
{
if(MathAbs(ai[ii, kk]) > mm)
{
mm = MathAbs(ai[ii, kk]);
ll = ii;
}
}
if(ll == 0)
return(0);
if(ll != kk)
{
for(jj = 1; jj <= nn; jj++)
{
tt = ai[kk, jj];
ai[kk, jj] = ai[ll, jj];
ai[ll, jj] = tt;
}
tt = b[kk]; b[kk] = b[ll]; b[ll] = tt;
}
for(ii = kk + 1; ii <= nn; ii++)
{
qq = ai[ii, kk] / ai[kk, kk];
for(jj = 1; jj <= nn; jj++)
{
if(jj == kk)
ai[ii, jj] = 0;
else
ai[ii, jj] = ai[ii, jj] - qq * ai[kk, jj];
}
b[ii] = b[ii] - qq * b[kk];
}
}
x[nn] = b[nn] / ai[nn, nn];
for(ii = nn - 1; ii >= 1; ii--)
{
tt = 0;
for(jj = 1; jj <= nn - ii; jj++)
{
tt = tt + ai[ii, ii + jj] * x[ii + jj];
x[ii] = (1 / ai[ii, ii]) * (b[ii] - tt);
}
}
//===========================================================================================================================
for(n = i; n <= i + p; n++)
{
sum = 0;
for(kk = 1; kk <= m; kk++)
{
sum += x[kk + 1] * MathPow(n, kk);
}
fx[n] = x[1] + sum;
}
//-----------------------------------Std-----------------------------------------------------------------------------------
sq = 0.0;
for(n = i; n <= i + p; n++)
{
sq += MathPow(Close[n] - fx[n], 2);
}
sq = MathSqrt(sq / (p + 1)) * kstd;
std = iStdDev(NULL, 0, p, MODE_SMA, 0, PRICE_CLOSE, i) * kstd;
for(n = i; n <= i + p; n++)
{
sqh[n] = fx[n] + sq;
sql[n] = fx[n] - sq;
stdh[n] = fx[n] + std;
stdl[n] = fx[n] - std;
}
//-------------------------------------------------------------------------------
ObjectMove("pr" + sName, 0, Time[p], fx[p]);
//----------------------------------------------------------------------------------------------------------------------------
return(0);
}
//==========================================================================================================================
I would like the increase the lines width to 2.
I solved with
SetIndexStyle(0, DRAW_LINE, EMPTY,2, indicator_color1);
SetIndexStyle(1, DRAW_LINE, EMPTY,2, indicator_color2);
SetIndexStyle(2, DRAW_LINE, EMPTY,2, indicator_color3);
SetIndexStyle(3, DRAW_LINE, EMPTY,2, indicator_color4);
SetIndexStyle(4, DRAW_LINE, EMPTY,2, indicator_color5);
Hello I am trying to convert an ARGB 8888 image into yuv 420 sp in android and I am getting a totally greenish and compressed image.Please help me in code if I am doing it the correct way.
The code seems something as below.
Image(Context context) {
// This Constructor is used to initialize height and width of screen
screenHeight = 800;//m1.heightPixels;
screenWidth = 480;//m1.widthPixels;
bufferSize = 4 * screenHeight * screenWidth;
buffer = new byte[bufferSize];
newarrs =new byte[bufferSize];
log("constructor width:- " + screenWidth + " height:- " + screenHeight);
}
public void capture() {
// Take the Data from frame buffer and store in buffer
log("capture Screen");
BufferedInputStream bis = null;
try {
// log("in try");
bis = new BufferedInputStream(new FileInputStream("/data/fb0.raw"));
readSize = bis.read(buffer, 0, bufferSize);
bis.close();
}
catch (Exception e) {
// log("in catch");
e.printStackTrace();
}
encodeYUV420(buffer);
byte[] arr = resize1(buffer);
FileOutputStream fos;
try {
File f = Files.getImageFile();
fos = new FileOutputStream(f);
fos.write(arr);
fos.close();
} catch (Exception e) {
}
private byte[] resize1(byte[] buffer) {
final int RATIO = 4;
byte[][][] newBuff = new byte[screenWidth][screenHeight][4];
int pos1 = 0;
for (int i = 0; i < screenWidth; i++) {
for (int j = 0; j < screenHeight; j++) {
newBuff[i][j][0] = buffer[pos1++];
newBuff[i][j][1] = buffer[pos1++];
newBuff[i][j][2] = buffer[pos1++];
newBuff[i][j][3] = buffer[pos1++];
}
}
byte[] buffer1 = new byte[buffer.length*3 / (RATIO * RATIO)];
int pos2 = 0;
int i = 0, j = 0;
for (i = 0; i < screenWidth; i++) {
for (j = 0; j < screenHeight; j++) {
try {
if (i % RATIO == 0 && j % RATIO == 0) {
buffer1[pos2++] = newBuff[i][j][0];
buffer1[pos2++] = newBuff[i][j][1];
buffer1[pos2++] = newBuff[i][j][2];
buffer1[pos2++] = newBuff[i][j][3];
}
} catch (Exception e) {
log(" i " + i + " j " + j);
}
}
}
log(" valuesof i " + i + " j " + j);
if (pos2 == buffer.length / (RATIO * RATIO))
log("S size:- " + pos2);
else
log("F size:- " + pos2);
return buffer1;
}
private byte[] encodeYUV420(byte[] argb) {
byte[] yuv420sp = new byte[(screenHeight * screenWidth * 3) / 2];
final int frameSize = screenWidth * screenHeight;
int yIndex = 0;
int uIndex = frameSize;
int vIndex = frameSize + (frameSize / 4);
int R, G, B;
int Y, U, V;
int index = 0;
for (int j = 0; j < screenHeight; j++) {
for (int i = 0; i < screenWidth; i++) {
int pp = (j * screenWidth + i) * 4;
//a = (argb[index] & 0xff000000) >> 24; // a is not used obviously
R = (argb[index] & 0xff0000) >> 16;
G = (argb[index] & 0xff00) >> 8;
B = (argb[index] & 0xff) >> 0;
Y = ((66 * R + 129 * G + 25 * B + 128) >> 8) + 16;
U = ((-38 * R - 74 * G + 112 * B + 128) >> 8) + 128;
V = ((112 * R - 94 * G - 18 * B + 128) >> 8) + 128;
yuv420sp[yIndex++] = (byte) ((Y < 0) ? 0 : ((Y > 255) ? 255 : Y));
if (j % 2 == 0 && i % 2 == 0) {
yuv420sp[uIndex++] = (byte) ((U<0) ? 0 : ((U > 255) ? 255 : U));
yuv420sp[vIndex++] = (byte) ((V<0) ? 0 : ((V > 255) ? 255 : V));
}
}
return yuv420sp;
}
Update:
Screenshot illustrating the problem:
I think I have made some changes and it looks some what as the original image but it is not clear or legible.Can I get some ideas on how to make it almost as the original image
Image(Context context) {
// This Constructor is used to initialize height and width of screen
screenHeight = 800;//m1.heightPixels;
screenWidth = 480;//m1.widthPixels;
bufferSize = 4 * screenHeight * screenWidth;
buffer = new byte[bufferSize];
newarrs =new byte[bufferSize];
log("constructor width:- " + screenWidth + " height:- " + screenHeight);
}
public void capture() {
// Take the Data from frame buffer and store in buffer
log("capture Screen");
BufferedInputStream bis = null;
try {
// log("in try");
bis = new BufferedInputStream(new FileInputStream("/data/fb0.raw"));
readSize = bis.read(buffer, 0, bufferSize);
bis.close();
}
catch (Exception e) {
// log("in catch");
e.printStackTrace();
}
encodeYUV420(buffer);
byte[] arr = resize1(buffer);
FileOutputStream fos;
try {
File f = Files.getImageFile();
fos = new FileOutputStream(f);
fos.write(arr);
fos.close();
} catch (Exception e) {
}
private byte[] resize1(byte[] buffer) {
final int RATIO = 4;
byte[][][] newBuff = new byte[screenWidth][screenHeight][4];
int pos1 = 0;
for (int i = 0; i < screenWidth; i++) {
for (int j = 0; j < screenHeight; j++) {
newBuff[i][j][0] = buffer[pos1++];
newBuff[i][j][1] = buffer[pos1++];
newBuff[i][j][2] = buffer[pos1++];
newBuff[i][j][3] = buffer[pos1++];
}
}
byte[] buffer1 = new byte[buffer.length*3 / (RATIO * RATIO)];
int pos2 = 0;
int i = 0, j = 0;
for (i = 0; i < screenWidth; i++) {
for (j = 0; j < screenHeight; j++) {
try {
if (i % RATIO == 0 && j % RATIO == 0) {
buffer1[pos2++] = newBuff[i][j][0];
buffer1[pos2++] = newBuff[i][j][1];
buffer1[pos2++] = newBuff[i][j][2];
buffer1[pos2++] = newBuff[i][j][3];
}
} catch (Exception e) {
log(" i " + i + " j " + j);
}
}
}
log(" valuesof i " + i + " j " + j);
if (pos2 == buffer.length / (RATIO * RATIO))
log("S size:- " + pos2);
else
log("F size:- " + pos2);
return buffer1;
}
private byte[] encodeYUV420(byte[] argb) {
byte[] yuv420sp = new byte[(screenHeight * screenWidth * 3) / 2];
final int frameSize = screenWidth * screenHeight;
int yIndex = 0;
int uvIndex=frameSize;
int a, R, G, B, Y, U, V;
int index = 0;
for (int j = 0; j < height; j++) {
for (int i = 0; i < width; i++) {
int pp = (j * width + i) * 4;
R = argb[pp+ 0];
G = argb[pp + 1];
B = argb[pp + 2];
a = argb[pp + 3];
Y = ( ( 66 * R + 129 * G + 25 * B + 128) >> 8) + 16;
U = ( ( -38 * R - 74 * G + 112 * B + 128) >> 8) + 128;
V = ( ( 112 * R - 94 * G - 18 * B + 128) >> 8) + 128;
yuv420sp[yIndex++] = (byte) ((Y < 0) ? 0 : ((Y > 255) ? 255 : Y));
if (j % 2 == 0 && i % 2 == 0) {
yuv420sp[uvIndex++] = (byte)((U<0) ? 0 : ((U > 255) ? 255 : U));
yuv420sp[uvIndex++] = (byte)((V<0) ? 0 : ((V > 255) ? 255 : V));
}
}
return yuv420sp;
}
You're not storing the YUV data correctly. According to this document, YUV420SP data is stored in two planes, one containing the Y data, and another containing the interleaved U and V data:
| Y_0 | Y_1 | Y_2 | Y_3 | Y_4 | ... | Y_w-2 | Y_w-1 | /* h rows */
| Y_w | Y_w+1 | Y_w+2 | ...
:
:
| U_0 | V_0 | U_2 | V_2 | U_4 | ... | U_w-2 | V_w-2 | /* h/2 rows */
| U_2w | V_2w | U_2w+2| ...
:
:
Your code seems to be storing the U and V data in separate planes:
int uIndex = frameSize;
int vIndex = frameSize + (frameSize / 4);
I want to print all the possible solutions to LCS problem.
The two strings abcbdab and bdcaba should print following 3 strings:
bdab,bcba,bcab.
C is the global matrix table which takes values according to algorithm and m, n are the length of the sequences a, b.
But The output is something unexpected.
#include<stdio.h>
#include<conio.h>
int co=0,m=0,n=0,c[10][10];
char a[10],b[10];
void main()
{
int i,j;
clrscr();
printf("Enter Two strings: ");
scanf("%s",a);
scanf("%s",b);
m=strlen(a);
n=strlen(b);
for(i=0;i<=m;i++)
{
for(j=0;j<=n;j++)
{ if(i==0 || j==0)
{
c[i][j]=0;
}
else if(a[i-1]==b[j-1])
{
c[i][j]=c[i-1][j-1]+1;
}
else if(c[i-1][j]>=c[i][j-1])
{
c[i][j]=c[i-1][j];
}
else
{
c[i][j]=c[i][j-1];
}
}
}
for(i=0;i<=m;i++)
{
for(j=0;j<=n;j++)
{
printf("%d\t",c[i][j]);
}
printf("\n");
}
print(m,n);
getch();
}
print(int i,int j)
{
if(i==0 || j==0)
return 0;
else if(a[i-1]==b[j-1])
{
print(i-1,j-1);
if(co==c[m][n])
{
co=0;
printf("\n");
}
printf("%c",a[i-1]);
co++;
}
else if(c[i-1][j]==c[i][j-1])
{
print(i-1,j);
print(i,j-1);
}
else if(c[i][j-1]>=c[i-1][j])
print(i,j-1);
else
print(i-1,j);
return;
}
Here you can find a recursive approach of how to do this: Reading out all LCSs
Here is my code for this approach in Java:
private Set<String> lcs(int[][] dp, String fst, String snd, int i, int j) {
Set<String> lcss = new HashSet<>();
if (i == 0 || j == 0) {
lcss.add("");
} else if (fst.charAt(i - 1) == snd.charAt(j - 1)) {
for (String lcs : lcs(dp, fst, snd, i - 1, j - 1)) {
lcss.add(lcs + fst.charAt(i - 1));
}
} else {
if (dp[i - 1][j] >= dp[i][j - 1]) {
lcss.addAll(lcs(dp, fst, snd, i - 1, j));
}
if (dp[i][j - 1] >= dp[i - 1][j]) {
lcss.addAll(lcs(dp, fst, snd, i, j - 1));
}
}
return lcss;
}
Here is the Java code with comments explaining how to print all possible lcs.
import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;
public class LongestCommonSubsequence {
public static int[][] LCSmatrix(String X, String Y) {
//we ignore the top most row and left most column in this matrix
//so we add 1 and create a matrix with appropriate row and column size
int m = X.length() + 1, n = Y.length() + 1;
int[][] c = new int[m][n];
for (int i = 1; i < m; i++) {
for (int j = 1; j < n; j++) {
//since we added 1 to row size and column size,
// we substract 1 from i,j to find the char at that index
if (X.charAt(i - 1) == Y.charAt(j - 1)) {
c[i][j] = c[i - 1][j - 1] + 1;
} else if (c[i - 1][j] >= c[i][j - 1]) {
c[i][j] = c[i - 1][j];
} else {
c[i][j] = c[i][j - 1];
}
}
}
printMatrix(c);
return c;
}
public static void printMatrix(int[][] grid) {
for (int r = 0; r < grid.length; r++) {
for (int c = 0; c < grid[r].length; c++) {
System.out.print(grid[r][c] + " ");
}
System.out.println();
}
}
public static void allLCS(int[][] c, String X, String Y, int i, int j, Set<String> setLCS, String s) {
//return when either of the string length is 0
if (i == 0 || j == 0) {
setLCS.add(s);
return;
}
//if last characters are equal, they belong in lcs
if (X.charAt(i - 1) == Y.charAt(j - 1)) {
//prepend the char to lcs since, we are going backwards
s = X.charAt(i - 1) + s;
//continue finding lcs in substrings X.substring(0,i-1) and Y.substring(0,j-1)
allLCS(c, X, Y, i - 1, j - 1, setLCS, s);
} // if there is a tie in matrix cells, we backtrack in both ways,
// else one way, which ever is greater
else if (c[i - 1][j] == c[i][j - 1]) {
//continue finding lcs in substring X.substring(0,i-1)
allLCS(c, X, Y, i - 1, j, setLCS, s);
//continue finding lcs in substring Y.substring(0,j-1)
allLCS(c, X, Y, i, j - 1, setLCS, s);
} else if (c[i - 1][j] > c[i][j - 1]) {
allLCS(c, X, Y, i - 1, j, setLCS, s);
} else {
allLCS(c, X, Y, i, j - 1, setLCS, s);
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println(" Enter String X and Y : ");
String X = sc.next();
String Y = sc.next();
sc.close();
Set<String> set = new HashSet<String>();
allLCS(LCSmatrix(X, Y), X, Y, X.length(), Y.length(), set, "");
System.out.println(set.toString());
}
}
class Solution
{
public int function1(String s,String t,int n,int m,int dp[][]){
if(n==0 || m==0){
return 0;
}
if(dp[n][m]!=-1){
return dp[n][m];
}
if(s.charAt(n-1)==t.charAt(m-1)){
return dp[n][m]=1+function1(s,t,n-1,m-1,dp);
}
return dp[n][m]=Math.max(function1(s,t,n-1,m,dp),function1(s,t,n,m-1,dp));
}
public HashSet<String> function2(String s,String t,int n,int m,int dp[][],HashMap<String,HashSet<String>> map){
HashSet<String> temp=new HashSet<String>();
String key=n+"-"+m;
if(n==0 || m==0){
temp.add("");
return temp;
}
if(map.containsKey(key)){
return map.get(key);
}
if(s.charAt(n-1)==t.charAt(m-1)){
for(String tempstr:function2(s,t,n-1,m-1,dp,map)){
temp.add(tempstr+s.substring(n-1,n));
}
}
else{
if(dp[n-1][m]>=dp[n][m-1]){
temp.addAll(function2(s,t,n-1,m,dp,map));
}
if(dp[n-1][m]<=dp[n][m-1]){
temp.addAll(function2(s,t,n,m-1,dp,map));
}
}
map.put(key,temp);
return temp;
}
public List<String> all_longest_common_subsequences(String s, String t)
{
int n=s.length();
int m=t.length();
int dp[][]=new int[n+1][m+1];
for(int i=0;i<=n;i++){
for(int j=0;j<=m;j++){
dp[i][j]=-1;
}
}
function1(s,t,n,m,dp);
HashMap<String,HashSet<String>> map=new HashMap<String,HashSet<String>>();
ArrayList<String> ans=new ArrayList<String>(function2(s,t,n,m,dp,map));
Collections.sort(ans);
return ans;
}
}
Your source code is not printing the lcs. It is actually calculating the length of lcs. Source code given by you is totally wrong. First try to print one lcs. Then extend that solution to print all the lcs. For your help given below is working java solution.
static int arr[][];
static void lcs(String s1, String s2) {
for (int i = 1; i <= s1.length(); i++) {
for (int j = 1; j <= s2.length(); j++) {
if (s1.charAt(i - 1) == s2.charAt(j - 1))
arr[i][j] = arr[i - 1][j - 1] + 1;
else
arr[i][j] = Math.max(arr[i - 1][j], arr[i][j - 1]);
}
}
}
static Set<String> lcs(String s1, String s2, int len1, int len2) {
if (len1 == 0 || len2 == 0) {
Set<String> set = new HashSet<String>();
set.add("");
return set;
}
if (s1.charAt(len1 - 1) == s2.charAt(len2 - 1)) {
Set<String> set = lcs(s1, s2, len1 - 1, len2 - 1);
Set<String> set1 = new HashSet<>();
for (String temp : set) {
temp = temp + s1.charAt(len1 - 1);
set1.add(temp);
}
return set1;
} else {
Set<String> set = new HashSet<>();
Set<String> set1 = new HashSet<>();
if (arr[len1 - 1][len2] >= arr[len1][len2 - 1]) {
set = lcs(s1, s2, len1 - 1, len2);
}
if (arr[len1][len2 - 1] >= arr[len1 - 1][len2]) {
set1 = lcs(s1, s2, len1, len2 - 1);
}
for (String temp : set) {
set1.add(temp);
}
//System.out.println("In lcs" + set1);
return set1;
}
}
public static void main(String[] args) {
String s1 = "abcbdab";
String s2 = "bdcaba ";
arr = new int[s1.length() + 1][s2.length() + 1];
lcs(s1, s2);
System.out.println(lcs(s1, s2, s1.length(), s2.length()));
}
If last character of strings are equal then they must be in lcs. If they are not equal lcs will be either constructed from upper side of matrix or left side of matrix depending upon which value is greater. If both the value is equal then lcs will be constructed from both the side. So keep constructing the lcs until you have constructed all the lcs and store them in a set.