In Pine-Script, how do I code two conditions for my entry? - conditional-statements

Could someone please help as I need entry conditions to include the BraidFilter indicator into my strategy. I only need a trade entry long when the green bars and short entry when the red bars cross over the filter line. This is what I did but it's not working and I'm getting a ton of error messages:
Please help. Thank you.
//-- Braid Filter
ma01 = ma(maType, close, Period1)
ma02 = ma(maType, open, Period2)
ma03 = ma(maType, close, Period3)
max = max(max(ma01, ma02), ma03)
min = min(min(ma01, ma02), ma03)
dif = max - min
filter = atr(14) * PipsMinSepPercent / 100
//-- Plots/=
//BraidColor = ma01 > ma02 and dif > filter ? color.green : ma02 > ma01 and dif > filter ? color.red : color.gray
//plot(dif, "Braid", BraidColor, 5, plot.style_columns)
//plot(filter, "Filter", color.blue, 2, plot.style_line)
//bgcolor(BraidColor)
// Condition
C1Price1 = 0.0
C1Price2 = 0.0
C1Price1 := ? ma01 > ma02 and dif > filter
C1Price2 := ? ma02 > ma01 and dif > filter
c1Cross_Long = crossover (C1Price1,C1Price2) and C1Price1 > filter
c1Cross_Short = crossover (C1Price1,C1Price2) and C1Price2 > filter

There are things missing in that code, first you have to define the script with a strategy, then define the maType variable, in addition to that:
C1Price1: =**?** ma01> ma02 and dif> filter
C1Price2: =**?** ma02> ma01 and dif> filter
prior to "?" There must be a condition to meet, I think something was deleted there, after correcting that you will have to write the entries.

Related

Error in if (all(x > 0)) { : missing value where TRUE/FALSE needed in glMDPlot

I am analyzing RNA sequencing data with 4 treatments. During the process in r, I got an error below with the code. It is to make a glimma MDplot. It would be appreciated that anyone solve this problem.
All treatments include 4 replicates, but one of them has only 3. I am unsure that this exerts the error, but just in case, I informed.
glMDPlot(fit.trend2, counts = logCPM.trend, status = trend_global_0.1, coef = 1, groups = d.filt$samples$group, samples = d.filt$samples$Label, sample.cols = d.filt$samples$col, folder = "glimma-plots", html = "HvsC_trend_globalFDR", main = "H. vs. C.", launch = TRUE)
Error in if (all(x > 0)) { : missing value where TRUE/FALSE needed

Why does pinescript place orders two candles after a trigger condition

I'm having some issues where my pinescript algo is placing orders two candles after my trigger conditions. My friends are also having the same issue. Does anyone know why this is the case and what I can do to solve it?
My code (I'm using HullMA as the trigger)
/////////////////Trend Indicator/////////////////
malength = input(defval=26, title="Moving Average Length", minval=1)
tsrc = security(heikinashi(syminfo.tickerid),"15", close)
hullma(tsrc, len) =>
hma = wma(2 * wma(tsrc, len / 2) - wma(tsrc, len), round(sqrt(len)))
zlema =hullma(tsrc, malength)
col = zlema > zlema[1] ? color.green : color.red
plot(zlema,color=col, linewidth=4)
/////////////////Buy and Sell Orders/////////////////
//Order conditions
if col == color.green
strategy.entry(id="long", long=true)
if col == color.red
strategy.entry(id="short", long=false)
Problem:
Found the solution, pinescript waits for candle closes and doesn't immediately initialise solutions, hence it takes two candles after the initial trigger before there is a signal

How to replace query conditions in a loop in Laravel?

I have a user table with name, status and distance. I want to get the users under a particular distance and if the result is empty I want to check it again with an incremented value.
$user = User::where('status',1);
$i = 10;
while($i < 50)
{
$user->having('distance', '<=', $i);
if($user->get()->count())
break;
$i = $i+5;
};
In the first loop I got the query correctly as SELECT * from users where status = 1 and having distance <= 10. If the result is empty in the second loop I got the query as SELECT * from users where status = 1 and having distance <= 10 and having distance <= 15. I want to get the query as SELECT * from users where status = 1 and having distance <= 15. What change should I do for this?
You are working here on objects and you should use clone:
$user = User::where('status',1);
$i = 10;
while($i < 50)
{
$query = (clone $user)->having('distance', '<=', $i);
if($query->get()->count())
break;
$i = $i+5;
}
In addition you are using:
$query->get()->count()
what causes that you get all records matching query from database and calculate their amount. If you just need to have their count, it's much better to use just:
$query->count()

Squeryl Where clause with Nullable Column has no effect

I have a where clause that basically splits my table into two lists. One list where all the fields are "complete"(Not -1) and then one where any field is incomplete. The problem is that one of these fields is not only nullable, but I also only want to count it as incomplete if another field is true.
Where clause:
//l and p are my two tables(This is in a join)
//passed in an option incompletes. If incompletes is true, give back incomplete values
where(
//SHOW INCOMPLETES
(
p.attr1 === -1 or
p.attr2 === -1 or
p.attr3 === -1 or
//only need to check attr4 if this var is true
(p.attr4 === -1).inhibitWhen(!l.needToCheckAttr4) //I've also tried === Some(-1)
).inhibitWhen(!incompletes.isDefined) and
// SHOW COMPLETES
(
p.price <> -1 and
p.serverCost <> -1 and
p.depreciation <> -1 and
(p.attr4 <> -1).inhibitWhen(!l.needToCheckAttr4)
).inhibitWhen(incompletes.isDefined)
)
The attr4 lines seem to have no effect while the rest of the conditions all work correctly. That is as long as the other conditions are true it renders as complete.
EDIT : So it's definitely something to do with calling the inhibitWhen. Is there something wrong with using two different tables in the one line(l and p)?
Okay so I just gave up on the inhibitWhen and used
(p.attr4 === -1 and l.needToCheckAttr4 === true)
and...
((p.attr4 <> -1 and l.needToCheckAttr4 === true) or (l.needToCheckAttr4 === false))
which I probably should have done in the first place.

Matlab: Optimize this (pt 2)

Here's another one:
ValidFirings = ((DwellTimes > 30/(24*60*60)) | (GroupCount > 1));
for i = length(ValidFirings):-1:2
if(~ValidFirings(i))
DwellTimes(i-1) = DwellTimes(i)+DwellTimes(i-1);
GroupCount(i-1) = GroupCount(i)+GroupCount(i-1);
DwellTimes(i) = [];
GroupCount(i) = [];
ReducedWallTime(i) = [];
ReducedWallId(i) = [];
end
end
It appears that the intent is to sum up 'dwelltimes' based on whether or not the sensor firing is considered valid. So I have a vector of sensor firings that Im walking through backwards and summing into the previous row if the current row is not marked as valid.
I can visualize this in C/C++ but I don't know how to translate it into better Matlab vector notation. As it stands now, this loop is v slow.
EDIT:
Could I use some form of DwellTimes = DwellTimes( cumsum( ValidFirings ))?
As with your previous question, replacing the for loop should improve the performance.
%# Find the indices for invalid firings
idx = find(~(DwellTimes > 30/(24*60*60)) | (GroupCount > 1));
%# Index the appropriate elements and add them (start the addition
%# from the second element)
%# This eliminates the for loop
DwellTimes(idx(2:end)-1) = DwellTimes(idx(2:end)-1)+DwellTimes(idx(2:end));
GroupCount(idx(2:end)-1) = GroupCount(idx(2:end)-1)+GroupCount(idx(2:end));
%# Now remove all the unwanted elements (this removes the
%# first element if it was a bad firing. Modify as necessary)
GroupCount(idx)=[];
DwellTimes(idx)=[];
I would consolidate first as shown, then eliminate the invalid data. This avoids the constant resizing of the data. Note that you can't reverse the order of the FOR loop due to the way that the values propagate.
ValidFirings = ((DwellTimes > 30/(24*60*60)) | (GroupCount > 1));
for i = length(ValidFirings):-1:2
if (~ValidFirings(i))
DwellTimes(i-1) = DwellTimes(i) + DwellTimes(i-1);
GroupCount(i-1) = GroupCount(i) + GroupCount(i-1);
end
end
DwellTimes = DwellTimes(ValidFirings);
GroupCount = GroupCount(ValidFirings);
ReducedWallTime = ReducedWallTime(ValidFirings);
ReducedWallId = ReducedWallId(ValidFirings);