How to tell script to move on from the purchase time and take new current bar price

hi, new to CQ, looking for advice please. I have a script which just purchased shares at current price “ md.L1.last” .
Then I need to sell them when price will reach or > first_target. Here is the code:

if ((float(EMAlist[-1]) > md.L1.daily_low) and (float(EMAlist[-1]) > ydLow)):

            lod = md.L1.daily_low
            print ("lod is " + str(round(lod,2)))
            ## Define shares to buy at level risking previous low.
            risk = int(round((200/ (level - lod)),0))
            order.send(self.symbol, 'buy', risk, type='MKT')
            self.status = 1

            ##Set Stop
            order.send(self.symbol, 'sell', risk, type='STP', stop=lod)
            print("EMA > daily low, SKYNET just purchased " + str(risk) + " shares of " + self.symbol + " risking current low at " + str(md.L1.daily_low))
            print (service.time_to_string(service.system_time, '%Y-%m-%d'))
            print (service.time_to_string(service.system_time, '%H:%M:%S'))

            purchase_bar = round(md.L1.last,2)
            print ("purchase_price is " + str(purchase_bar))

            first_target = round(purchase_bar + ATR,2)
            print ("first_target is " + str(first_target))
            print ("ATR " + str(ATR))
            print ( "md.L1.last " + str(md.L1.last))
            print( "round(md.L1.last,2) " + str(round(md.L1.last,2)))

            #if ((round(md.L1.last,2)) > first_target):
            #if ((round(md.L1.last,2)) == first_target)
            #if ( == first_target):
            #if ( > first_target):

            if ( > purchase_bar + ATR):
                print ("bar_1m" + str(
                new_stop = first_target - ATR/2
                order.send(self.symbol, 'sell', int(risk/2), type='MKT', stop=new_stop)
                print ("sold 50% shares, " + int(risk/2) + ", at " + str(first_target))
                print("didn't reach the target")

Seems like last if statement is executed (coz it printed else condition “didn’t reach the target” line), but the if condition is not met, I don’t know why… tried:
if ((round(md.L1.last,2)) > first_target)
if ((round(md.L1.last,2)) == first_target)
if ( == first_target):

if statement should be right after the purchase, so its right..
Seems like it treats last bar “” as my purchase bar “md.L1.last” coz this if statement happens right after the purchase, at the same time current price cannot immediately be > first_target … that’s why its never executed…what am I missing please? How to tell script to move on from the purchase time and take new current bar price and then use it for my if statement? thanks a lot in advance.


  • ptunneyptunney Posts: 246

    Would need to see more of your code to help. You can PM me if you want or email at

    In any situation like this, where you feel you are running blind, find a symbol that you think should meet your criteria and run just the backtest on that symbol alone (you can modify your is_symbol_qualified code or just limit it when you press NEW TEST (SYMBOLS>USE SPECIFIC SYMBOLS).
    Then add a print statement before your IF statement to see what you are comparing.
    This will normally let you see and understand what is blocking you quite quickly.

Sign In or Register to comment.