View Full Version : Code for linking multiple check boxes with If then statements
John2411
11-20-2014, 07:11 PM
I dimed checkbox 1,2,and 3 as Boolean. How do I link the follow coed.
 
ElseIf spreadPrice < doubleLongLevel And netPosition > 0 Then        
           If Sheets("INPUTS").OLEObjects("CheckBox1").Object.Value = True Then
               If ROCofPoly > PolyROC1 Then
                  
                    
             If Sheets("INPUTS").OLEObjects("CheckBox2").Object.Value = True Then
               If SMEMA > EMA Then
                   
                     
             If Sheets("INPUTS").OLEObjects("CheckBox3").Object.Value = True Then
               If TrueStrengthIndicator > TSIBuy1 Then
                    
                    'double a long position
                    tradeSignalMessage = "DOUBLE LONG"
                    orderSide = "BUY"
                    orderPrice = doubleLongLevel
                End If
raj85
11-21-2014, 12:36 AM
I can see various IF statement without end if. It's not much clear that what is your query but I have corrected your code as below:
ElseIf spreadPrice < doubleLongLevel And netPosition > 0 Then
    If Sheets("INPUTS").OLEObjects("CheckBox1").Object.Value = True And ROCofPoly > PolyROC1 And _
        Sheets("INPUTS").OLEObjects("CheckBox2").Object.Value = True And SMEMA > EMA And _
        Sheets("INPUTS").OLEObjects("CheckBox3").Object.Value = True And _
        TrueStrengthIndicator > TSIBuy1 Then
        'double a long position
        tradeSignalMessage = "DOUBLE LONG"
        orderSide = "BUY"
        orderPrice = doubleLongLevel
    End If
End If
IF this is not your are expecting then please provide more details and if possible your excel file
John2411
11-21-2014, 03:32 PM
Send me your email address and I will send you the excel file. It is running but it is now not looking at the other parts of the program. I will paste. Running but not sure if the coed is set properly???
 
If (spreadPrice < pukeLongLevel Or positionAge > maxPositionAge) And  netPosition > 0 Then
            'puke a long position - ie. spread price has moved against position and is beyong st. dev. bands
            tradeSignalMessage = "PUKE LONG"
            orderSide = "SELL"
            orderQty = netPosition * -1
            orderPrice = pukeLongLevel
            
        ElseIf spreadPrice < doubleLongLevel And netPosition > 0 Then
        
            If Sheets("INPUTS").OLEObjects("CheckBox1").Object.Value = True And _
                ROCofPoly > PolyROC1 Or _
        Sheets("INPUTS").OLEObjects("CheckBox2").Object.Value = True And _
                SMEMA > EMA Or _
        Sheets("INPUTS").OLEObjects("CheckBox3").Object.Value = True And _
                 TrueStrengthIndicator > TSIBuy1 Then
                    'double a long position
                    tradeSignalMessage = "DOUBLE LONG"
                    orderSide = "BUY"
                    orderPrice = doubleLongLevel
                End If
                 
             Else
                   tradeSignalMessage = "DOUBLE LONG"
                    orderSide = "BUY"
                    orderPrice = doubleLongLevel
            End If
Aussiebear
11-21-2014, 06:19 PM
First impression here is that the logic of your statements is changing significantly between the Posts #1 and 3. Try writing out the concept in english and then we'll have a crack at the code concept.
John2411
11-21-2014, 07:19 PM
the logic of the code below works. I want to be able to use three different check boxes to have none or 1, 2 3, running. I did not know how to add the additional ck box logic to make run correctly. See below.
If (spreadPrice < pukeLongLevel Or positionAge > maxPositionAge) And netPosition > 0 Then
            'puke a long position - ie. spread price has moved against position and is beyong st. dev. bands
            tradeSignalMessage = "PUKE LONG"
            orderSide = "SELL"
            orderQty = netPosition * -1
            orderPrice = pukeLongLevel
        ElseIf spreadPrice < doubleLongLevel And netPosition > 0 Then
            If Sheets("INPUTS").OLEObjects("CheckBox1").Object.Value = True Then
                If ROCofPoly > PolyROC1 And SMEMA > EMA And TrueStrengthIndicator > TSIBuy1 Then
                    'double a long position
                    tradeSignalMessage = "DOUBLE LONG"
                    orderSide = "BUY"
                    orderPrice = doubleLongLevel
                End If
             Else
                    tradeSignalMessage = "DOUBLE LONG"
                    orderSide = "BUY"
                    orderPrice = doubleLongLevel
Aussiebear
11-21-2014, 10:52 PM
Just to show you that the logic of your current code is not quite right
Primary Check
If (spreadPrice < pukeLongLevel Or positionAge > maxPositionAge) And netPosition > 0 Then
'puke a long position - ie. spread price has moved against position and is beyong st. dev. bands
tradeSignalMessage = "PUKE LONG"
orderSide = "SELL"
orderQty = netPosition * -1
orderPrice = pukeLongLevel
End If
If two of the above conditions fail then carry out secondary check
ElseIf spreadPrice < doubleLongLevel And netPosition > 0 AndIf Sheets("INPUTS").OLEObjects("CheckBox1").Object.Value = True And _
If ROCofPoly > PolyROC1 And SMEMA > EMA And TrueStrengthIndicator > TSIBuy1 Then
Provided all 5 conditions are true then 
'double a long position
tradeSignalMessage = "DOUBLE LONG"
orderSide = "BUY"
orderPrice = doubleLongLevel
End If
Else
Same option as above??
'double a long position
tradeSignalMessage = "DOUBLE LONG"
orderSide = "BUY"
orderPrice = doubleLongLevel
End If
Else
John2411
11-22-2014, 10:52 AM
Same option as  above??
this loop is if no check box is checked. Basically I am using this coed loops as filters vs my over all strategy. How would you coed three check boxes vs the one check box so that I can check any combination or none which the program would go to the option as above loop. Thanks for helping.
westconn1
11-22-2014, 02:34 PM
I dimed checkbox 1,2,and 3 as Boolean.checkboxes are objects on a form or worksheet, they should not be dimensioned, if they are dimensioned as variables, the variable will override the object locally (within the scope of the variable)
John2411
11-22-2014, 08:04 PM
I took the Dim checkbox as boolean our of the coed
John2411
11-23-2014, 05:34 PM
I did rewrite the coed but it still is not reading properly. I am getting values that I know are incorrect even when there are no check boxes checked.  See below
Dim PolyROC1, TSIBuy1, TSISell1 As Integer
        Dim TSIBuy2, TSISell2 As Integer
        Dim CheckBox1, CheckBox2, CheckBox3 As String
               
            PolyROC1 = Sheet2.Cells(31, "C")
            TSIBuy1 = Sheet2.Cells(47, "C")
            TSISell1 = Sheet2.Cells(48, "C")
            SMEMA = Sheet2.Cells(39, "C")
              
       
        positionAge = positionAge + 1
        netPosition = Sheet4.Cells(i - 1, "S")
        polySlope = Sheet4.Cells(i, "N") - Sheet4.Cells(i - 1, "N")
        orderQty = 0
        orderPrice = 0
 
If (spreadPrice < pukeLongLevel Or positionAge > maxPositionAge) And netPosition > 0 Then
            'puke a long position - ie. spread price has moved against position and is beyong st. dev. bands
            tradeSignalMessage = "PUKE LONG"
            orderSide = "SELL"
            orderQty = netPosition * -1
            orderPrice = pukeLongLevel
ElseIf spreadPrice < doubleLongLevel And netPosition > 0 Then     
            If Sheets("INPUTS").OLEObjects("CheckBox1").Object.Value = True Then
                CheckBox1 = ROCofPoly > PolyROC1
                Else
        If Sheets("INPUTS").OLEObjects("CheckBox2").Object.Value = True Then
            CheckBox2 = SMEMA > EMA
            Else
         If Sheets("INPUTS").OLEObjects("CheckBox3").Object.Value = True Then
             If CheckBox3 = TrueStrengthIndicator > TSIBuy1 Then
                    'double a long position
                    tradeSignalMessage = "DOUBLE LONG"
                    orderSide = "BUY"
                    orderPrice = doubleLongLevel
                End If
                 End If
                  End If
                   End If
             Else
                   tradeSignalMessage = "DOUBLE LONG"
                    orderSide = "BUY"
                    orderPrice = doubleLongLevel
            End If
Powered by vBulletin® Version 4.2.5 Copyright © 2025 vBulletin Solutions Inc. All rights reserved.