New to programming, need to know what’s wrong with my code

Viewing 7 posts - 1 through 7 (of 7 total)
  • Author
    Posts
  • #245769 quote
    Uniclan
    Participant
    New

    Hi! I’m new to trading and new to programming as well. I’m trying to create a simple stop losses system depending on how many point the stock moves.

    The issue is that the stop losses don’t trigger as I wish. Can someone see what’s wrong with the code?

    I wish:

    Initial stop loss at 100

    When the stock moves 100+, the stop loss moves to 50.

    A fuurther movement of 50 point, the stop loss moves another 50 point

    After 200 points of market move, the trailingstop of 100 point kicks in.

     

    // **Lång strategi för Nasdaq**
    DEFPARAM PRELOADBARS = 1000
    
    ONCE StopAdjustedNASDAQLong = 0
    ONCE StopAdjustedNASDAQLong150 = 0
    ONCE TrailingNASDAQLong = 0
    
    IF NOT LongOnMarket AND NOT ShortOnMarket THEN
    IF Time = 083000 OR Time = 153000 THEN
    BUY 0.5 CONTRACTS AT MARKET
    SET STOP LOSS 100
    StopAdjustedNASDAQLong = 0
    StopAdjustedNASDAQLong150 = 0
    TrailingNASDAQLong = 0
    ENDIF
    ENDIF
    
    // Justera stop-loss om priset har gått +100 punkter från TradePrice
    IF LongOnMarket AND Close >= TradePrice + 100 AND StopAdjustedNASDAQLong = 0 THEN
    SET STOP LOSS TradePrice + 50
    StopAdjustedNASDAQLong = 1
    ENDIF
    
    // Justera stop-loss om priset har gått +150 punkter från TradePrice
    IF LongOnMarket AND Close >= TradePrice + 150 AND StopAdjustedNASDAQLong150 = 0 THEN
    SET STOP LOSS TradePrice + 100
    StopAdjustedNASDAQLong150 = 1
    ENDIF
    
    // Aktivera trailing stop-loss om priset har gått +200 punkter
    IF LongOnMarket AND Close >= TradePrice + 200 AND TrailingNASDAQLong = 0 THEN
    SET STOP TRAILING 100
    TrailingNASDAQLong = 1
    ENDIF
    
    // Stäng alla långa positioner innan stängning
    IF LongOnMarket AND Time = 215700 THEN
    SELL AT MARKET
    ENDIF
    #245770 quote
    Iván González
    Moderator
    Master

    Hi.

    I see you’re using incorrect expressions when defining the stop loss. If you want to set the stop at X points, you should use the expression SET STOP pLOSS X.

    Here are some examples:

    https://www.prorealcode.com/documentation/ploss-2/

    Uniclan and druby thanked this post
    #245786 quote
    Uniclan
    Participant
    New

    Thanks! Is that all that is needed to get the code working? Appreciate all help!

    #245790 quote
    Iván González
    Moderator
    Master

    In principle, yes. Perhaps I would change the last expression to this one:

    IF LongOnMarket AND openTime >= 215700 THEN
    SELL AT MARKET
    ENDIF
    

    Or you could place this at the beginning of the code:

    defparam flatafter = 215700
    
    Uniclan thanked this post
    #245791 quote
    JC_Bywan
    Moderator
    Master

    Hello,

    as you mention you’re new to this, it’s probably worth mentionning what might seem obvious for those familiar with the platform:

    1) the instructions “time” and “opentime” are respectively the closing time and the opening time of a candle

    2) a proorder code is only read at candle close, and if orders are sent they are executed immediatly after, namely at the open of the following candle

     

    These 2 points together in mind, when you say in the code:

    IF Time = 083000 OR Time = 153000 THEN
    BUY 0.5 CONTRACTS AT MARKET
    if you run your code say on a one hour  timeframe rather than a smaller timeframe, you wouldn’t have any candle closing at 8:30 or 15:30 so the orders would never be sent.
    .
    Similarly, when you say :
    IF LongOnMarket AND Time = 215700 THEN
    SELL AT MARKET

    if you are on a 5mn timeframe, or even just a 2mn timeframe, there is no candle closing at 21:57, so you would need to run such code on a 1mn timeframe or x seconds providing it divides 1mn (30s,15s,…)

    .
    Maybe you were already doing that, but I don’t see it in your post, so that’s why I specify it, just in case… because, if you’re running on a timeframe not compatible with those times and you see no orders sent, you might be looking for an error in the code when in fact, it’s an error in timeframe choice (or you might choose to keep a certain timeframe but then need to amend time values in the code to make them compatible with chosen timeframe).
    Iván González, Uniclan and druby thanked this post
    #245848 quote
    robertogozzi
    Moderator
    Master

    I want to delve deeper into setting a TARGET and a STOP LOSS.

    SET STOP LOSS and SET TARGET PROFIT both require a value, be it a constant or a variable or an expression, expressed as a price, thus that value must be converted from a number into a price by multiplying it by PIPSIZE (or its equivalent POINTSIZE). Should you use a difference between prices, such as the expression highest[10](high) – lowest[10](low), then NO conversion should be performed as they ARE prices, so this is correct:

    SET STOP LOSS highest[10](high) - lowest[10](low)
    SET TARGET PROFIT highest[10](high) - lowest[10](low)

    pSTOP and pPROFIT both require a value that is the number of PIPS, be it a constant or a variable or an expression,  thus that value must be a number which doesn’t require any conversion. A conversion would be needed if you would like to use a value expressed as a price, such as the expression highest[10](high) – lowest[10](low). These statements are both correct:

    SET STOP pLOSS 100                                             //Pips are assumed by default
    SET STOP pLOSS (highest[10](high) - lowest[10](low) / PipSize  //any price expression must be converted into Pips

    any price, be it a constant or a variable or a price expression, must be converted into Pips by DIVIDING that price by PIPSIZE (or its equivalent POINTSIZE).

    What has been said about LOSS/TARGET and pLOSS/pTARGET is also true for TRAILING/pTRAILING.

    A different case are SET STOP price and SET TARGET price, as they only require a price, so the statements:

    SET STOP PRICE TradePrice
    SET STOP PRICE TradePrice + 50*PipSize

    are both correct and only the latter one requires the numeric constant to be converted into a price, while TRADEPRICE is a price by itself.

    druby, Uniclan and Iván González thanked this post
    #245851 quote
    robertogozzi
    Moderator
    Master

    My bad, the line:

    What has been said about LOSS/TARGET and pLOSS/pTARGET is also true for TRAILING/pTRAILING

    must be replaced by:

    What has been said about LOSS/PROFIT and pLOSS/pPROFIT is also true for TRAILING/pTRAILING.

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

New to programming, need to know what’s wrong with my code


ProOrder: Automated Strategies & Backtesting

New Reply
Author
author-avatar
Uniclan @uniclan Participant
Summary

This topic contains 6 replies,
has 4 voices, and was last updated by robertogozzi
10 months, 1 week ago.

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