how to code to calculate an average of two inputs

Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • #194948 quote
    josef1604
    Participant
    Senior

    hello, I ask you for help please

    How can the bot recalculate the profit when I do two inputs at differentt price?

     

    that is, after putting a second entry, how can I code so that the bot can calculate the average of both entries leaving the initial stoploss of the first entry and have a positive exit?

    #194952 quote
    Nicolas
    Keymaster
    Master

    The average price of orders open price is POSITIONPRICE

    So for 2 buy orders, the profit is positive above POSITIONPRICE.

    josef1604 thanked this post
    #194954 quote
    josef1604
    Participant
    Senior

    I explain myself better. The code makes an entry (it is executing a stoploss) if it is placed in the opposite direction to the entry, how can I make it make a second entry and do the average between the first and the second and the operation comes out positive? that without modifying the initial stoploss

    #194958 quote
    Nicolas
    Keymaster
    Master

    Sorry I dont get it 🙂

    1. Do you want to cover your floating loss with an opposite order?
    2. The second order is of the same type of the first one?
    3. When the second order is opened, the first one is still at market or triggered its stoploss already?
    josef1604 thanked this post
    #194969 quote
    josef1604
    Participant
    Senior

    1. Do you want to cover your floating loss with an opposite order? NO

    2. Is the second order of the same type as the first? YES

    3. When the second order is opened, is the first one still in the market or has it already triggered its stoploss? Stay in the market and follow the stop of the initially open position

    I give an example: I buy at 12100 (stop at 12090) and it falls to 12000, increase the position bot (buy again and the stop remains at 12090). Once you have opened the second position take an average of both positions and recalculate a positive exit again.

    #195011 quote
    Nicolas
    Keymaster
    Master
    Ok here is a dummy strategy that simulate that behavior (averaging down the first order only 1 time, and exit if the profit is positive)
    defparam cumulateorders=true 
    defparam preloadbars=0
    
    buyc = rsi[14]crosses over 50
    
    //first order
    if not longonmarket and buyc then 
     buy 1 contract at market 
     set stop ploss 100
    endif
    
    //averaging down the first order at 20 points in loss
    if longonmarket and tradeprice-close>=20 and countofposition<2 then 
     buy 1 contract at market 
    endif 
    
    //put the exit in positive area 
    if longonmarket and countofposition=2 then 
     set target price positionprice+1
    endif 
    
    
    josef1604 thanked this post
    averaging-down-buy-orders.png averaging-down-buy-orders.png
Viewing 6 posts - 1 through 6 (of 6 total)
  • You must be logged in to reply to this topic.

how to code to calculate an average of two inputs


ProOrder: Automated Strategies & Backtesting

New Reply
Author
author-avatar
josef1604 @josef1604 Participant
Summary

This topic contains 5 replies,
has 2 voices, and was last updated by Nicolas
3 years, 8 months ago.

Topic Details
Forum: ProOrder: Automated Strategies & Backtesting
Language: English
Started: 06/08/2022
Status: Active
Attachments: 1 files
Logo Logo
Loading...