jeudi 12 février 2026

prix = [100,98,105,102,110,107,115,112,120,117].

 un trader effectue des operations d'achat et vendre d'une seule action sur une periode de 10 jours . Chque jour, le prix de action est donnee dans un tableaux. Contraintes: le trader ne peut effectuer qu'une seule operation par jour: soit acheter une action, soit en vendre une. Il ne peut pas achter et vendre le meme jour, le trader ne peut detenir qu'une seule action a la fois, le trader peut commencer par vendre une action (ce qui signifie qu'il commence par une position courte), chaque fois qu'il change de position (c'est a dire qu'apres avoir achete il vend, ou apres avoir vendu il achete) il doit payer une commission fixe de 2 unites monetaires, l'objectif est de maximiser le gain total net (c'est a dire la somme des profits et pertes realises sur les operations, apres deduction des commissions. Donnees: un tableau prix de taille 10 contenant les prix de l'action pour chauqe jour: prix = [100,98,105,102,110,107,115,112,120,117]. Objectif: en c#, ecrire un algorithme qui, a partir du tableau des prix, determiner la strategie d'achat/vente (une seule operation par jour) permettant de maximiser le gain total net sur 10 jours)


===============


Ý tưởng (rất gọn)

Mỗi ngày chỉ cần giữ 3 giá trị tốt nhất:

  • flat: đang không giữ vị thế (lời/lỗ đã “chốt” hết)

  • longPos: đang giữ LONG (đã mua 1 cổ)

  • shortPos: đang giữ SHORT (đã bán khống 1 cổ)

Mỗi ngày ta có thể:

  • mở long từ flat: flat - price

  • đóng long về flat: longPos + price - fee

  • mở short từ flat: flat + price

  • đóng short về flat: shortPos - price - fee

Phí fee=2 được trừ khi đóng vị thế (tức là khi đổi trạng thái từ long/short về flat).
Kết quả cần “lợi nhuận thực hiện” ⇒ lấy flat ở ngày cuối.


=======================

using System;


class Program

{

    static void Main()

    {

        int[] prices = { 100, 98, 105, 102, 110, 107, 115, 112, 120, 117 };

        int fee = 2;


        int flat = 0;

        int longPos = -prices[0];   // mua ngày 1

        int shortPos = prices[0];   // bán khống ngày 1


        for (int i = 1; i < prices.Length; i++)

        {

            int p = prices[i];


            int newFlat = flat;

            newFlat = Math.Max(newFlat, longPos + p - fee);   // đóng long

            newFlat = Math.Max(newFlat, shortPos - p - fee);  // đóng short


            int newLong = Math.Max(longPos, flat - p);        // mở long

            int newShort = Math.Max(shortPos, flat + p);      // mở short


            flat = newFlat;

            longPos = newLong;

            shortPos = newShort;

        }


        Console.WriteLine(flat); // max profit net, kết thúc flat

    }

}



====================================

using System;

class Program
{
    static void Main()
    {
        int[] prix = { 100, 98, 105, 102, 110, 107, 115, 112, 120, 117 };
        int commission = 2;

        int libre = 0;              // aucune position
        int achat = int.MinValue/2; // position achetée
        int vente = int.MinValue/2; // position vendue à découvert

        foreach (int p in prix)
        {
            int nouveauLibre = Math.Max(
                libre,
                Math.Max(achat + p - commission, vente - p - commission)
            );

            int nouvelAchat = Math.Max(achat, libre - p);
            int nouvelleVente = Math.Max(vente, libre + p);

            libre = nouveauLibre;
            achat = nouvelAchat;
            vente = nouvelleVente;
        }

        Console.WriteLine("Gain maximal net = " + libre);
    }
}



libre  = aucun stock détenu
achat  = on possède une action achetée
vente  = on est en position courte


Ta sẽ mô phỏng từng ngày với 3 trạng thái:

  • libre = không giữ vị thế nào
  • achat = đang giữ vị thế mua (long)
  • vente = đang giữ vị thế bán khống (short)

Ban đầu:

libre = 0
achat = -∞
vente = -∞

Giá:

[100, 98, 105, 102, 110, 107, 115, 112, 120, 117]

Commission = 2


Ngày 1 - Giá 100

Mua

achat = max(-∞, libre - 100)
= max(-∞, 0 - 100)
= -100

Bán khống

vente = max(-∞, libre + 100)
= 100

Đứng ngoài

libre = 0

Kết quả:

libre = 0
achat = -100
vente = 100

Ngày 2 - Giá 98

Đóng vị thế

Nếu hôm qua đang short:

100 - 98 - 2 = 0

Nếu hôm qua đang long:

-100 + 98 - 2 = -4
nouveauLibre = max(0, -4, 0)
= 0

Mở long

achat = max(-100, 0 - 98)
= -98

Mở short

vente = max(100, 0 + 98)
= 100

Kết quả:

libre = 0
achat = -98
vente = 100

Ngày 3 - Giá 105

Đóng long:

-98 + 105 - 2 = 5

Đóng short:

100 - 105 - 2 = -7
libre = max(0,5,-7)
= 5

Mở long:

achat = max(-98,0-105)
= -98

Mở short:

vente = max(100,0+105)
= 105

Kết quả:

libre = 5
achat = -98
vente = 105

Ngày 4 - Giá 102

Đóng long:

-98 + 102 - 2 = 2

Đóng short:

105 - 102 - 2 = 1
libre = max(5,2,1)
= 5

Mở long:

achat = max(-98,5-102)
= -97

Mở short:

vente = max(105,5+102)
= 107

Kết quả:

libre = 5
achat = -97
vente = 107

Ngày 5 - Giá 110

Đóng long:

-97 + 110 - 2 = 11

Đóng short:

107 - 110 - 2 = -5
libre = 11

Mở long:

achat = max(-97,5-110)
= -97

Mở short:

vente = max(107,5+110)
= 115

Kết quả:

libre = 11
achat = -97
vente = 115

Ngày 6 - Giá 107

Đóng long:

-97 + 107 - 2 = 8

Đóng short:

115 - 107 - 2 = 6
libre = 11

Mở long:

achat = max(-97,11-107)
= -96

Mở short:

vente = max(115,11+107)
= 118

Kết quả:

libre = 11
achat = -96
vente = 118

Ngày 7 - Giá 115

libre = max(
11,
-96+115-2 = 17,
118-115-2 = 1
)
= 17
achat = -96
vente = 126

Ngày 8 - Giá 112

libre = max(
17,
-96+112-2 = 14,
126-112-2 = 12
)
= 17
achat = max(-96,17-112)
= -95

vente = max(126,17+112)
= 129

Ngày 9 - Giá 120

libre = max(
17,
-95+120-2 = 23,
129-120-2 = 7
)
= 23
achat = -95
vente = 137

Ngày 10 - Giá 117

libre = max(
23,
-95+117-2 = 20,
137-117-2 = 18
)
= 23

Kết quả cuối

Gain maximal net = 23

Nếu muốn biết chuỗi giao dịch thực tế nào tạo ra 23, ta phải lưu lại "parent state" trong DP rồi backtracking.

Chuỗi tối ưu là:

Ngày 2 : Buy 98
Ngày 3 : Sell 105 => +5

Ngày 4 : Buy 102
Ngày 5 : Sell 110 => +6

Ngày 6 : Buy 107
Ngày 7 : Sell 115 => +6

Ngày 8 : Buy 112
Ngày 9 : Sell 120 => +6

Tổng:

(105-98-2)
+(110-102-2)
+(115-107-2)
+(120-112-2)

= 5+6+6+6
= 23

Aucun commentaire:

Enregistrer un commentaire