How to add all or some conditions to alert ? - Pine script tradingview - charts

I have a list of conditions for a candle. This is just an example:
aa = (close > open) and (low > low[1])
bb = (close[1] > open[1]) and (close[1] > 5)
cc = (close > ((high[1] - low[1])*23.6/100 + low[1]))
dd = (close > EMA34) and (close > ema(close, 10))
I set alert using the following code:
if aa
alert("A- " + tostring(close, "#.######"), alert.freq_once_per_bar)
else if bb
alert("B- " + tostring(close, "#.######"), alert.freq_once_per_bar)
else if cc
alert("C- " + tostring(close, "#.######"), alert.freq_once_per_bar)
else if dd
alert("D- " + tostring(close, "#.######"), alert.freq_once_per_bar)
With each condition, I will get an alert with a letter at the beginning of the message so I can know the priority of the conditions, ie A is the best, D is the last one.
I would like to know if there is any way to check all conditions at the same time, so I can set the priority like:
if the alert has all conditions fulfilled, so it's the best A
if the alert has at least 3 conditions fulfilled, then it's B
if the alert has at least 2 conditions, then it's C
and if there is only 1 condition fulfilled, then it will be D
The real list has more than 10 conditions so I cannot check them manually. Please give me some code to do it programmatically.
I think, it's something related to array but I don't know how to do it.
Thank you for helping me.

aa = (close > open) and (low > low[1]) ?1:0
bb = (close[1] > open[1]) and (close[1] > 5) ?1:0
cc = (close > ((high[1] - low[1])*23.6/100 + low[1])) ?1:0
dd = (close > EMA34) and (close > ema(close, 10)) ?1:0
number_of_condition_true=aa+bb+cc+dd
bool all_condition_true=na
bool three_condition_true=na
bool two_condition_true=na
bool one_condition_true=na
bool none_condition_true=na
if number_of_condition_true>=4
all_condition_true:=true
else if number_of_condition_true>=3
three_condition_true:=true
else if number_of_condition_true>=2
two_condition_true:=true
else if number_of_condition_true>=1
one_condition_true:=true
else if number_of_condition_true==0
none_condition_true:=true
this is one of the ways ,this code will help you in programming your logic. in the above code i have replaced it into numerical value if true , than i have added all , this will give you the overall how may are true at a time.

Related

Keeping orders from closing if conditions are met & multiple orders

First time coding - trying to set up a bot that creates multiple buy orders every time the buying signal is met and closed once the short signal is triggered - and vice versa. It seems to be working in TradingView when I add the strategy to the chart however when I connect it to 3 commas it only takes in one buy order (paper trading account) - and it closes it specifically when the condition to keep the position from closing is triggered.
I think I've set up the strategy correctly to take up to six positions with pyramiding.
strategy("DEMA CROSS STRATEGY", shorttitle = "DEMAC-S", overlay = true, initial_capital = 4000, default_qty_type = strategy.percent_of_equity, default_qty_value = 20, commission_type = strategy.commission.percent, commission_value = 0.03, pyramiding = 6)
The conditions to keep a position from closing if longAction or shortAction are true, then keep the opened positions from closing, otherwise close all positions and open a long/short.
positionState = volume < valueThreshold and ADXsignal < adxThreshold
//logic
longCondition = ta.crossover(DEMA1, DEMA2) and supertrend_X_min < close and mfi+rsi/1.8 > 40 and close > WMA
shortCondition = ta.crossunder(DEMA1, DEMA2) and supertrend_X_min > close and mfi+rsi/1.8 < 60 and close < WMA and atr < .05
longAction = (longCondition and positionState)
shortAction = (shortCondition and positionState)
if (shortAction or longAction)
false
else
if (longCondition)
strategy.entry("Enter Long", strategy.long)
strategy.exit("Exit Short", stop=na)
if (shortCondition)
strategy.entry("Enter Short", strategy.short)
strategy.exit("Exit Long", stop=na)
//
//Buy and sell alerts
buyAlert = XXXXXXXXXXXXXXXXXXXXXXXXXXX
exitAlert = XXXXXXXXXXXXXXXXXXXXXXXXXXXX
if longAction ? na : longCondition
alert(buyAlert, alert.freq_once_per_bar)
if shortAction ? na : shortCondition
alert(exitAlert, alert.freq_once_per_bar)
I've tried multiple different if then statements but honestly I'm not sure if I'm just doing it plain wrong at this stage.
Like I've stated, the code works in TradingView - not in 3Commas

pinescript newbie: problem to implement RR as TP! can you check my script? :)

var float loLows = na
is_new_position = (strategy.position_size[1] != strategy.position_size) // This will only be true on the bar of the entry
loLows := is_new_position // Only update the value if it is a new position. Keep the old value otherwise
TPL=(strategy.position_avg_price/RR)-(loLows/RR)+strategy.position_avg_price
TPS=(strategy.position_avg_price/RR)-(hiHighs/RR)-strategy.position_avg_price
if inDateRange and (close > (ema200) and close > (ema992) and ta.crossover(ema13,ema62) and ta.crossover(ema21,ema62))
strategy.entry("entry long", strategy.long)
strategy.exit("long exit", profit=TPL, stop=loLows)
if inDateRange and(close < (ema200) and close < (ema992) and ta.crossunder(ema13,ema62) and ta.crossunder(ema21,ema62))
strategy.entry("entry short", strategy.short)
strategy.exit("short exit", profit=TPS , stop=hiHighs)

Liberty Basic Payroll Calculator IF ELSE

I'm just getting started with learning basic programming with Sams and already am having issues with the simple payroll calculator I've been trying to write. Could someone explain how I could better rewrite this? And can you only follow IF THEN statements with a print command? I think I'll have to invest in a newer book but I was hoping I could get this to run at least in the meantime.
Input "Please input payrate: "; Ans$
Print
Input "Please input hours worked: "; Hrs$
If (Hrs$ >= "40") Then
payRate = (Hrs$ * Ans$)
Else If (Hrs$ <= "41") Then
payRate = Hrs$ * (1.5 * Ans$)
End If
taxRate = payRate * .15
grossPay = payRate * Hrs$
netPay = payRate - taxRate
Print
Print: "Your net pay is: "; netPay
Also, I see you have an else if statement. One of the perplexing design decisions in Liberty Basic is that it doesn't support the else if/elseif statement that you might find in most BASIC language dialects.
From the online manual:
The elseif keyword is not supported. Here is an >example using elseif (QBasic):
'QBasic only
if sis boom bah then
print "Yippie!"
elseif la dee da then
print "So what!"
end if
Instead in Liberty BASIC, it looks like this:
'Liberty BASIC - no elseif
if sis boom bah then
print "Yippie!"
else
if la dee da then
print "So what!"
end if
end if
And to extend the above official documentation a little further with my own code sample in LB (Liberty Basic), you might just appreciate how deeply nested the if, else, end if can become.
'Liberty BASIC
grade = 55
if grade > 90 then
print "Excellent!"
else
if grade > 80 and grade < 90 then
print "Good"
else
if grade > 70 and grade < 80 then
print "We need to try a little harder."
else
if grade > 60 and grade < 70 then
print "We need to try a lot harder!"
else
print "Let's have a talk."
end if
end if
end if
end if
end
The equivalent in another dialect of BASIC, in this case FreeBasic, might look something like this which of course appears much tidier with only one end if vs a trail of end if's:
'FreeBasic
Dim grade as integer = 55
if grade > 90 then
print "Excellent!"
elseif grade > 80 and grade < 90 then
print "Good"
elseif grade > 70 and grade < 80 then
print "We need to try a little harder."
elseif grade > 60 and grade < 70 then
print "We need to try a lot harder!"
else
print "Let's have a talk."
end if
sleep
end
But all is not lost with LB because the language actually implements a fairly powerful select case statement which can include conditional statements which is not possible in certain dialects of BASIC (i.e. PureBasic)
'LB - Liberty Basic
grade = 55
select case grade
case grade > 90
print "Excellent!"
case (grade > 80) and (grade < 90)
print "Good"
case (grade > 70) and (grade < 80)
print "We need to try a little harder."
case (grade > 60) and (grade < 70)
print "We need to try a lot harder!"
case else
print "Let's have a talk."
end select
I don't use Liberty, but I imagine it's complaining when you're trying to do math on strings. Additionally, I find your misuse of the "payrate" name to be confusing. The logic of the overtime calc is also wrong. Here's my first attempt at a rewrite:
Input "Please input payrate: "; Rate$
Rate = val(Rate$)
Print
Input "Please input hours worked: "; Hrs$
Hrs = val(Hrs$)
Gross = Hrs * Rate
If Hrs > 40 Then Gross = Gross + ((Hrs - 40) * Rate / 2))
Taxes = Gross * .15
Net = Gross - Taxes
Print
Print: "Your gross pay is: "; Gross
Print: "Your net pay (after $";Taxes;" in taxes) is: "; Net
This code will work in Liberty BASIC:
INPUT "Please enter hours worked: "; hours 'you don't need a dollar sign at the end of the variable because it's a value not a string
INPUT "Please enter your hourly rate: "; hourlyRate
gross = hours * hourlyRate 'works out the gross amount
IF (hours > 40) THEN 'if the person worked more than 40 hours, then the following command is applicable...
gross = 40 * hourlyRate + ((hours - 40) * (hourlyRate * 1.5)) 'this line calculates the money earned for 40 hours, and adds the remaining hours at 1.5 times the hourly rate
END IF 'end the IF condition
tax = gross * 0.15 'calculates the tax at 15%
net = gross - tax 'calculates the net pay (i.e., gross pay after tax)
PRINT "-------------------------------"
PRINT "Your gross pay is: "; gross
PRINT "Your net pay is: "; net
As the previous poster suggested, you should use values instead of strings for maths (values don't have a dollar sign at the end), and when you label your variables they should have a meaningful name in relation to what you're trying to achieve.
Liberty BASIC has a tutorial under Help that you can use to teach yourself how to code. Otherwise Beginning Programming for Dummies is another useful guide - that's what I'm using :)

Email attachments recorded in Excel

The macro below is designed to take x amount of emails, count how many attachments there are in each mail and then locate certain file formats. It will then record what it has found in a certain excel spreadsheet.
The macro works perfectly, but I am now wanting to add in another scenario. The scenario I want to add in is that of if an email has more than 1 .csv file, it shall be recorded as "Multiple" rather than "YES".
Has anyone got any ideas to implement this scenario?
If .Attachments.Count = 0 Then
csv_report = "NO"
pdf_report = "NO"
xls_report = "NO"
End If
If .Attachments.Count > 0 Then
For i2 = 1 To .Attachments.Count
If LCase(Right(.Attachments(i2).Filename, 4)) = ".csv" Then
csv_report = "YES"
GoTo CSVyes 'if a .csv file is found, it skips to the PDF attachment checker
Else
csv_report = "NO"
End If
Next
CSVyes:
For i2 = 1 To .Attachments.Count
If LCase(Right(.Attachments(i2).Filename, 4)) = ".pdf" Then
pdf_report = "YES"
GoTo PDFyes 'if a .pdf file is found, it skips to the XLS attachment checker
Else
pdf_report = "NO"
End If
Next
PDFyes:
For i2 = 1 To .Attachments.Count
If LCase(Right(.Attachments(i2).Filename, 4)) = ".xls" Or LCase(Right(.Attachments(i2).Filename, 5)) = ".xlsx" Or UCase(Right(.Attachments(i2).Filename, 4)) = ".XLS" Then
xls_report = "YES"
GoTo XLSyes 'if a .xls file is found, it skips to the end of the checks
Else
xls_report = "NO"
End If
Next
XLSyes:
End If
Sheets("Mail Report").Activate
Range("C65000").End(xlUp).Offset(1).Value = csv_report
Range("D65000").End(xlUp).Offset(1).Value = pdf_report
Range("E65000").End(xlUp).Offset(1).Value = xls_report
subject_line = mail.Subject
Range("A65000").End(xlUp).Offset(1).Value = subject_line
Check this below code. I have just added If Else block to check if Attachment.count > 1. THat's It.
If .Attachments.Count > 0 Then
For i2 = 1 To .Attachments.Count
If LCase(Right(.Attachments(i2).Filename, 4)) = ".csv" Then
If .Attachments.Count > 1
csv_report = "MULTIPLE"
Else
csv_report = "YES"
End If
GoTo CSVyes 'if a .csv file is found, it skips to the PDF attachment checker
Else
csv_report = "NO"
End If
Next
After many attempts to implement this scenario, I was unsuccessful. So I figured to put an alternative method after this macro.
This macro is just a minor part of a much larger scale macro, but basically when there was blanks left in Column B I would replace a "YES" with "Multiple", which was sufficient to the outcome.
Dim kk As Long
Sheets("Mail Report").Activate
LastRow = Cells(Rows.Count, 1).End(xlUp).Row
For kk = 1 To LastRow
If IsEmpty(Cells(kk, 2)) Then
Cells(kk, 2) = MAIN_PATH & "For Resolution\" & "multiple_csv\"
Cells(kk, 3) = "MULTIPLE"
End If
Next kk

VBA Excel make array of sequential numbers

I am working in VBA for Excel at the moment but am really only versed in Matlab. It's important for my work to stay in the memory of vba (not on the worksheets of excel) for time purposes.
What I need to do is make an array of sequential integers, say 4000 through 5000.
In matlab this is really easy, I would just do... i = 4000:5000, or i=4000:1:5000. With the 1 in the second case being my 'step.'
I was wondering what is the best way to achieve this result in vba?
Thanks
Without looping - Just seen Rory's same answer above after posting
Sub MakeArray()
Dim x As Long, y As Long, arr As Variant
x = 4000: y = 5000
arr = Evaluate("Row(" & x & ":" & y & ")")
'Show result
Sheets(1).Range("A1").Resize(y - x + 1) = arr
End Sub
The following is an example of creating and then displaying a set of sequential numbers:
Sub seqnum()
Dim firstnum As Long, secnum As Long
firstnum = 7
secnum = 23
ReDim ary(1 To secnum - firstnum + 1) As Long
For i = 1 To UBound(ary)
ary(i) = firstnum + (i - 1)
Next i
msg = ""
For i = 1 To UBound(ary)
msg = msg & i & vbTab & ary(i) & vbCrLf
Next i
MsgBox msg
End Sub
I Us "Fill" - "Series":
Write in first cell number ex. 400 and in the "Series" window insert increment step and in "Stop Value" last value. ex. 420
Or with Macro
Range("I1").Select
ActiveCell.FormulaR1C1 = "4000"
Range("I1").Select
Selection.DataSeries Rowcol:=xlColumns, Type:=xlLinear, Date:=xlDay, _
Step:=1, Stop:=4020, Trend:=False