Coding buy/stop orders above or below the price but only for a limited time

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #97064 quote
    domf
    Participant
    New
     
    
    
    
    // Conditions to enter long positions
    indicator1 = Average[12](close)
    indicator2 = Average[22](close)
    level1 = open + (10*pipsize)
    c1 = (indicator1 CROSSES OVER indicator2)
    IF c1 THEN
    BUY 1 CONTRACT AT level1 LIMIT
    ENDIF
    
    // Conditions to exit long positions
    indicator3 = Average[9](close)
    indicator4 = Average[20](close)
    c2 = (indicator3 CROSSES UNDER indicator4)
    IF c2 THEN
    SELL AT MARKET
    ENDIF
    
    // Conditions to enter short positions
    indicator5 = Average[12](close)
    indicator6 = Average[22](close)
    level2 = open - (10*pipsize)
    c3 = (indicator5 CROSSES UNDER indicator6)
    IF c3 THEN
    SELLSHORT 1 CONTRACT AT level2 LIMIT
    ENDIF
    
    // Conditions to exit short positions
    indicator7 = Average[9](close)
    indicator8 = Average[20](close)
    c4 = (indicator7 CROSSES OVER indicator8)
    
    IF c4 THEN
    EXITSHORT AT MARKET
    ENDIF
    

    I am trying to create some coding for what I believe should be a simple task but I just can not get my coding head around it. Would really appreciate some help.

    This is based on 4hr charts. I have a simple moving average crossover which I wish to trigger a buy order at a price 10 PIPS BELOW the trade price at the point of the crossover. If the price does not drop to that price within the next 2 bars/candles, then I wish to cancel the order.

    Likewise in reverse for a crossover triggering a sell order at a price 10 PIPS ABOVE the trade price. Also to be cancelled if price does not rise to that level within next 2 bars/candles.

    I have read the programming guide and done many forum searches on this, but still can’t find the solution. The coding shown illustrates how far I have got so far.

    Many thanks

    #98017 quote
    Nicolas
    Keymaster
    Master
    You can use BARINDEX and count of bars to put the limit orders for a limited time: Save the BARINDEX in a variable when the MA cross occurs and compare it with the actual bar:
    // Conditions to enter long positions
    indicator1 = Average[12](close)
    indicator2 = Average[22](close)
    level1 = open + (10*pipsize)
    c1 = (indicator1 CROSSES OVER indicator2)
    IF c1 THEN
     buybar = barindex
    ENDIF
    if barindex-buybar<=2 then 
     BUY 1 CONTRACT AT level1 LIMIT
    endif
    Just do the same for your sell limit orders code.
Viewing 2 posts - 1 through 2 (of 2 total)
  • You must be logged in to reply to this topic.

Coding buy/stop orders above or below the price but only for a limited time


ProOrder: Automated Strategies & Backtesting

New Reply
Author
author-avatar
domf @domf Participant
Summary

This topic contains 1 reply,
has 2 voices, and was last updated by Nicolas
6 years, 9 months ago.

Topic Details
Forum: ProOrder: Automated Strategies & Backtesting
Language: English
Started: 04/24/2019
Status: Active
Attachments: No files
Logo Logo
Loading...