Buy/sell code isn’t working

Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #222516 quote
    ioannispapadakis
    Participant
    New

    Hey everyone,

    I have this code:

    if Time>163000 and Time<230000 and signal=1 and low<=BuyLine and not longonmarket then
    buy 10 contract at market
    endif
    
    set target profit 10
    set stop loss SellLine
    
    if Time>163000 and Time<230000 and signal=-1 and high>=SellLine and not shortonmarket then
    sell 10 contracts at market
    endif
    
    set target profit 5
    set stop loss BuyLine

    Basically what I want to do is if the open is above the BuyLine (signal=1) to buy a hit of that line with stop at the SellLine and vice versa, if the open is below the SellLine (signal=-1) to sell a hit of the SellLine with stop at the BuyLine.

    The problem is that the backtest gives trades at wrong places. Can you help me spot the problem in the code please?

     

    John

    #222522 quote
    GraHal
    Participant
    Master

    Try below … does it give you what you want?

    if Time>163000 and Time<230000 and signal=1 and low<=BuyLine and not longonmarket then
    buy 10 contract at market
    set target profit 10
    set stop loss SellLine
    endif
     
    
     
    if Time>163000 and Time<230000 and signal=-1 and high>=SellLine and not shortonmarket then
    sellShort 10 contracts at market
    set target profit 5
    set stop loss BuyLine
    endif
     
    
    ioannispapadakis thanked this post
    #222534 quote
    robertogozzi
    Moderator
    Master

    In line 1 you only check the LOW price, not the OPEN, which could also be below BuyLine.

    Add

    AND open > BuyLine

    to that line.

    You may want to add a check for OPEN also in line 8.

    ioannispapadakis thanked this post
Viewing 3 posts - 1 through 3 (of 3 total)
  • You must be logged in to reply to this topic.

Buy/sell code isn’t working


ProOrder: Automated Strategies & Backtesting

New Reply
Author
Summary

This topic contains 2 replies,
has 3 voices, and was last updated by robertogozzi
2 years, 4 months ago.

Topic Details
Forum: ProOrder: Automated Strategies & Backtesting
Language: English
Started: 10/16/2023
Status: Active
Attachments: No files
Logo Logo
Loading...